-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #166 from harmony-one/basic-rate-limit
Basic key protection
- Loading branch information
Showing
20 changed files
with
335 additions
and
19 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/sh | ||
sudo setcap CAP_NET_BIND_SERVICE=+eip /usr/bin/node | ||
sudo cp voice-ai-relay.service /etc/systemd/system/voice-ai-relay.service | ||
sudo systemctl start voice-ai-relay | ||
sudo systemctl enable voice-ai-relay | ||
systemctl status voice-ai-relay |
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,2 @@ | ||
#!/bin/sh | ||
journalctl -u voice-ai-relay -n 1000 -f |
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,8 @@ | ||
!#/bin/sh | ||
sudo iptables -A INPUT -i eth0 -p tcp --dport 80 -j ACCEPT | ||
sudo iptables -A INPUT -i eth0 -p tcp --dport 3000 -j ACCEPT | ||
sudo iptables -A INPUT -i eth0 -p tcp --dport 443 -j ACCEPT | ||
sudo iptables -A INPUT -i eth0 -p tcp --dport 8443 -j ACCEPT | ||
sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000 | ||
sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 8443 | ||
sudo iptables --flush |
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,15 @@ | ||
[Unit] | ||
Description=Voice AI Relay Server | ||
Documentation=https://github.com/harmony-one/x/ | ||
After=network.target | ||
|
||
[Service] | ||
Environment=PORT=80 HTTPS_PORT=443 | ||
Type=simple | ||
User=worker | ||
WorkingDirectory=/opt/git/x/voice/relay | ||
ExecStart=/bin/bash -c "source ~/.profile;/usr/bin/node --loader ts-node/esm ./bin/run.ts" | ||
Restart=on-failure | ||
|
||
[Install] | ||
WantedBy=multi-user.target |
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
File renamed without changes.
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,66 @@ | ||
import { Router, type Request, type Response, type NextFunction } from 'express' | ||
import { HttpStatusCode } from 'axios' | ||
import rateLimit, { type Options as RLOptions, type RateLimitRequestHandler } from 'express-rate-limit' | ||
import { BlockedDeviceIds, BlockedIps, OpenAIDistributedKeys } from '../config/index.js' | ||
import { encrypt, hexView, stringToBytes } from '../utils.js' | ||
import { hash as sha256 } from 'fast-sha256' | ||
const router: Router = Router() | ||
|
||
const deviceLimiter = (args?: RLOptions): RateLimitRequestHandler => rateLimit({ | ||
windowMs: 1000 * 60, | ||
limit: 10, | ||
keyGenerator: req => req.header('x-device-token') ?? '', | ||
...args | ||
}) | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
const ipLimiter = (args?: RLOptions): RateLimitRequestHandler => rateLimit({ | ||
windowMs: 1000 * 60, | ||
limit: 10, | ||
keyGenerator: req => req.clientIp ?? 'N/A', | ||
...args | ||
}) | ||
|
||
const parseDeviceToken = (req: Request, res: Response, next: NextFunction): any => { | ||
const deviceToken = req.header('x-device-token') | ||
if (!deviceToken) { | ||
res.status(HttpStatusCode.Forbidden).json({ error: 'device unsupported', code: 100 }) | ||
return | ||
} | ||
const deviceTokenHash = hexView(sha256(stringToBytes(deviceToken))) | ||
if (BlockedDeviceIds.includes(deviceTokenHash)) { | ||
res.status(HttpStatusCode.Forbidden).json({ error: 'device banned', code: 101 }) | ||
return | ||
} | ||
req.deviceToken = deviceToken | ||
req.deviceTokenHash = deviceTokenHash | ||
next() | ||
} | ||
|
||
const checkIpBan = (req: Request, res: Response, next: NextFunction): any => { | ||
if (!req.clientIp) { | ||
console.error('[checkIpBan] Cannot find ip of request', req) | ||
} | ||
if (BlockedIps.includes(req.clientIp ?? 'N/A')) { | ||
res.status(HttpStatusCode.Forbidden).json({ error: 'ip banned', code: 102 }) | ||
} | ||
next() | ||
} | ||
|
||
router.get('/health', (req, res) => { | ||
res.send('OK') | ||
}) | ||
|
||
router.get('/key', parseDeviceToken, checkIpBan, deviceLimiter(), ipLimiter(), (req, res) => { | ||
// TODO: validate the device token, https://developer.apple.com/documentation/devicecheck/accessing_and_modifying_per-device_data | ||
const numKeys = BigInt(OpenAIDistributedKeys.length) | ||
const keyIndex = Number(BigInt('0x' + req.deviceTokenHash) % numKeys) | ||
const key = OpenAIDistributedKeys[keyIndex] | ||
const encryptedKey = encrypt(key) | ||
const encoded = encryptedKey.toString('base64') | ||
|
||
console.log(`[deviceTokenHash=${req.deviceTokenHash}][ip=${req.clientIp}] Provided encryptedKey ${encoded}`) | ||
res.json({ key: encryptedKey.toString('base64') }) | ||
}) | ||
|
||
export default router |
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,10 @@ | ||
declare global { | ||
namespace Express { | ||
interface Request { | ||
deviceToken: string | ||
deviceTokenHash: string | ||
} | ||
} | ||
} | ||
|
||
export {} |
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 |
---|---|---|
|
@@ -243,6 +243,13 @@ | |
"@types/connect" "*" | ||
"@types/node" "*" | ||
|
||
"@types/compression@^1.7.5": | ||
version "1.7.5" | ||
resolved "https://registry.yarnpkg.com/@types/compression/-/compression-1.7.5.tgz#0f80efef6eb031be57b12221c4ba6bc3577808f7" | ||
integrity sha512-AAQvK5pxMpaT+nDvhHrsBhLSYG5yQdtkaJE1WYieSNY2mVFKAgmU4ks65rkZD5oqnGCFLyQpUr1CqI4DmUMyDg== | ||
dependencies: | ||
"@types/express" "*" | ||
|
||
"@types/connect@*": | ||
version "3.4.37" | ||
resolved "https://registry.yarnpkg.com/@types/connect/-/connect-3.4.37.tgz#c66a96689fd3127c8772eb3e9e5c6028ec1a9af5" | ||
|
@@ -371,6 +378,13 @@ | |
resolved "https://registry.yarnpkg.com/@types/range-parser/-/range-parser-1.2.6.tgz#7cb33992049fd7340d5b10c0098e104184dfcd2a" | ||
integrity sha512-+0autS93xyXizIYiyL02FCY8N+KkKPhILhcUSA276HxzreZ16kl+cmwvV2qAM/PuCCwPXzOXOWhiPcw20uSFcA== | ||
|
||
"@types/request-ip@^0.0.41": | ||
version "0.0.41" | ||
resolved "https://registry.yarnpkg.com/@types/request-ip/-/request-ip-0.0.41.tgz#c22a3244df2573402989346062851b06b7a5ac4e" | ||
integrity sha512-Qzz0PM2nSZej4lsLzzNfADIORZhhxO7PED0fXpg4FjXiHuJ/lMyUg+YFF5q8x9HPZH3Gl6N+NOM8QZjItNgGKg== | ||
dependencies: | ||
"@types/node" "*" | ||
|
||
"@types/semver@^7.3.12": | ||
version "7.5.4" | ||
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.5.4.tgz#0a41252ad431c473158b22f9bfb9a63df7541cff" | ||
|
@@ -484,7 +498,7 @@ abort-controller@^3.0.0: | |
dependencies: | ||
event-target-shim "^5.0.0" | ||
|
||
accepts@~1.3.8: | ||
accepts@~1.3.5, accepts@~1.3.8: | ||
version "1.3.8" | ||
resolved "https://registry.yarnpkg.com/accepts/-/accepts-1.3.8.tgz#0bf0be125b67014adcb0b0921e62db7bffe16b2e" | ||
integrity sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw== | ||
|
@@ -709,6 +723,11 @@ builtins@^5.0.1: | |
dependencies: | ||
semver "^7.0.0" | ||
|
||
[email protected]: | ||
version "3.0.0" | ||
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.0.0.tgz#d32815404d689699f85a4ea4fa8755dd13a96048" | ||
integrity sha512-pMhOfFDPiv9t5jjIXkHosWmkSyQbvsgEVNkz0ERHbuLh2T/7j4Mqqpz523Fe8MVY89KC6Sh/QfS2sM+SjgFDcw== | ||
|
||
[email protected]: | ||
version "3.1.2" | ||
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.1.2.tgz#8b0beeb98605adf1b128fa4386403c009e0221a5" | ||
|
@@ -802,6 +821,26 @@ combined-stream@^1.0.8: | |
dependencies: | ||
delayed-stream "~1.0.0" | ||
|
||
compressible@~2.0.16: | ||
version "2.0.18" | ||
resolved "https://registry.yarnpkg.com/compressible/-/compressible-2.0.18.tgz#af53cca6b070d4c3c0750fbd77286a6d7cc46fba" | ||
integrity sha512-AF3r7P5dWxL8MxyITRMlORQNaOA2IkAFaTr4k7BUumjPtRpGDTZpl0Pb1XCO6JeDCBdp126Cgs9sMxqSjgYyRg== | ||
dependencies: | ||
mime-db ">= 1.43.0 < 2" | ||
|
||
compression@^1.7.4: | ||
version "1.7.4" | ||
resolved "https://registry.yarnpkg.com/compression/-/compression-1.7.4.tgz#95523eff170ca57c29a0ca41e6fe131f41e5bb8f" | ||
integrity sha512-jaSIDzP9pZVS4ZfQ+TzvtiWhdpFhE2RDHz8QJkpX9SIpLq88VueF5jJw6t+6CUQcAoA6t+x89MLrWAqpfDE8iQ== | ||
dependencies: | ||
accepts "~1.3.5" | ||
bytes "3.0.0" | ||
compressible "~2.0.16" | ||
debug "2.6.9" | ||
on-headers "~1.0.2" | ||
safe-buffer "5.1.2" | ||
vary "~1.1.2" | ||
|
||
[email protected]: | ||
version "0.0.1" | ||
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" | ||
|
@@ -1329,6 +1368,11 @@ event-target-shim@^5.0.0: | |
resolved "https://registry.yarnpkg.com/event-target-shim/-/event-target-shim-5.0.1.tgz#5d4d3ebdf9583d63a5333ce2deb7480ab2b05789" | ||
integrity sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ== | ||
|
||
express-rate-limit@^7.1.4: | ||
version "7.1.4" | ||
resolved "https://registry.yarnpkg.com/express-rate-limit/-/express-rate-limit-7.1.4.tgz#c321fe186a8366eacdb2c5edf2ad6a2f6d93e576" | ||
integrity sha512-mv/6z+EwnWpr+MjGVavMGvM4Tl8S/tHmpl9ZsDfrQeHpYy4Hfr0UYdKEf9OOTe280oIr70yPxLRmQ6MfINfJDw== | ||
|
||
express@^4.18.2: | ||
version "4.18.2" | ||
resolved "https://registry.yarnpkg.com/express/-/express-4.18.2.tgz#3fabe08296e930c796c19e3c516979386ba9fd59" | ||
|
@@ -1650,7 +1694,7 @@ hasown@^2.0.0: | |
dependencies: | ||
function-bind "^1.1.2" | ||
|
||
[email protected]: | ||
[email protected], http-errors@^2.0.0: | ||
version "2.0.0" | ||
resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-2.0.0.tgz#b7774a1486ef73cf7667ac9ae0858c012c57b9d3" | ||
integrity sha512-FtwrG/euBzaEjYeRqOgly7G0qviiXoJWnvEH2Z1plBdXgbyjv34pHTSb9zoeHMyDy33+DWy5Wt9Wo+TURtOYSQ== | ||
|
@@ -1987,7 +2031,7 @@ micromatch@^4.0.4: | |
braces "^3.0.2" | ||
picomatch "^2.3.1" | ||
|
||
[email protected]: | ||
[email protected], "mime-db@>= 1.43.0 < 2": | ||
version "1.52.0" | ||
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70" | ||
integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg== | ||
|
@@ -2334,6 +2378,11 @@ regexpp@^3.0.0: | |
resolved "https://registry.yarnpkg.com/regexpp/-/regexpp-3.2.0.tgz#0425a2768d8f23bad70ca4b90461fa2f1213e1b2" | ||
integrity sha512-pq2bWo9mVD43nbts2wGv17XLiNLya+GklZ8kaDLV2Z08gDCsGpnKn9BFMepvWuHCbyVvY7J5o5+BVvoQbmlJLg== | ||
|
||
request-ip@^3.3.0: | ||
version "3.3.0" | ||
resolved "https://registry.yarnpkg.com/request-ip/-/request-ip-3.3.0.tgz#863451e8fec03847d44f223e30a5d63e369fa611" | ||
integrity sha512-cA6Xh6e0fDBBBwH77SLJaJPBmD3nWVAcF9/XAcsrIHdjhFzFiB5aNQFytdjCGPezU3ROwrR11IddKAM08vohxA== | ||
|
||
require-directory@^2.1.1: | ||
version "2.1.1" | ||
resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42" | ||
|
Oops, something went wrong.