-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,230 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
dist | ||
proxy/credential |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const esbuild = require("esbuild"); | ||
|
||
const isDev = process.argv[2] === "-dev"; | ||
|
||
const baseConfig = { | ||
bundle: true, | ||
minify: false, | ||
sourcemap: isDev, | ||
tsconfig: "tsconfig.json", | ||
treeShaking: true, | ||
packages: "external", | ||
}; | ||
|
||
esbuild | ||
.build({ | ||
entryPoints: ["index.ts"], | ||
outfile: "dist/index.js", | ||
target: "node20", | ||
format: "cjs", | ||
platform: "node", | ||
...baseConfig, | ||
}) | ||
.catch(() => process.exit(1)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export async function makeRequest(url: string): Promise<string> { | ||
try { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error( | ||
`Failed to get '${url}' (status code: ${response.status})`, | ||
); | ||
} | ||
return await response.text(); | ||
} catch (error) { | ||
console.error(`Error fetching page: ${error}`); | ||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import admin from "firebase-admin"; | ||
import quota from "./quota"; | ||
const serviceAccount = require("../credential/bonse-001-8a382ce36723.json"); | ||
|
||
admin.initializeApp({ | ||
credential: admin.credential.cert(serviceAccount), | ||
databaseURL: | ||
"https://bonse-001-default-rtdb.asia-southeast1.firebasedatabase.app", | ||
}); | ||
|
||
const database = admin.database(); | ||
|
||
function _createQuota(orginatedFrom: string): Promise<number> { | ||
return new Promise((resolve, reject) => { | ||
const ref = database.ref( | ||
`/rjs-proxy/limits/${orginatedFrom}/per-month`, | ||
); | ||
|
||
ref | ||
.set(quota.perMonth) | ||
.then(() => { | ||
resolve(quota.perMonth); | ||
}) | ||
.catch(reject); | ||
}); | ||
} | ||
|
||
function _checkQuota(orginatedFrom: string): Promise<number> { | ||
return new Promise((resolve, reject) => { | ||
const ref = database.ref( | ||
`/rjs-proxy/limits/${orginatedFrom}/per-month`, | ||
); | ||
|
||
ref | ||
.get() | ||
.then((snapshot) => { | ||
if (snapshot.exists()) { | ||
//existing user | ||
const currentQuota: number = snapshot.val() as number; | ||
resolve(currentQuota); | ||
} else { | ||
//new user | ||
_createQuota(orginatedFrom) | ||
.then((quota: number) => { | ||
resolve(quota); | ||
}) | ||
.catch((err) => { | ||
reject(err); //temp block user forcefully | ||
}); | ||
} | ||
}) | ||
.catch((err) => { | ||
reject(err); //temp block user forcefully | ||
}); | ||
}); | ||
} | ||
|
||
export function decrementQuota( | ||
orginatedFrom: string, | ||
lastLimitCount: number, | ||
): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
const ref = database.ref( | ||
`/rjs-proxy/limits/${orginatedFrom}/per-month`, | ||
); | ||
|
||
ref | ||
.set(lastLimitCount - 1) | ||
.then(resolve) | ||
.catch(reject); | ||
}); | ||
} | ||
|
||
export function firewall( | ||
orginatedFrom: string, | ||
): Promise<number | boolean> { | ||
return new Promise((resolve, reject) => { | ||
if (orginatedFrom === "localhost") { | ||
resolve(true); // No limit for localhost | ||
} | ||
|
||
_checkQuota(orginatedFrom) | ||
.then((currentLimitCount: number) => { | ||
if (currentLimitCount > 0) { | ||
//grant | ||
resolve(currentLimitCount); | ||
} else { | ||
//deny | ||
resolve(false); | ||
} | ||
}) | ||
.catch(reject); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const quota = { | ||
perDay: 20, | ||
perWeek: 80, | ||
perMonth: 100, | ||
}; | ||
export default quota; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.