Skip to content

Commit

Permalink
Added rate limiting
Browse files Browse the repository at this point in the history
  • Loading branch information
bostrot committed Jun 30, 2022
1 parent 4682663 commit 8c32d16
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
2 changes: 0 additions & 2 deletions config/config-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ signal_number: '+12345689' # Your signal number / account

web_server: false # Enable/disable web server
web_server_port: 8080 # Port for web server
web_server_ssl_cert: '../src/web/ssl.crt' # Path for SSL cert
web_server_ssl_key: '../src/web/ssl.key' # Path for SSL key

dev_mode: true # Enable/disable dev mode

Expand Down
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"chalk": "^5.0.1",
"debug": "^4.3.4",
"express": "^4.18.1",
"express-rate-limit": "^6.4.0",
"grammy": "^1.9.0",
"node-fetch": "^3.2.6",
"node-gyp": "^9.0.0",
Expand Down
27 changes: 19 additions & 8 deletions src/addons/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@ import fakectx from './fakectx';
import {ticketHandler} from '../text';
import cache from '../cache';
import TelegramAddon from './telegram';
import rateLimit from 'express-rate-limit';

/* include script
<script id="chatScript" src="localhost:8080/chat.js"></script>
*/
const init = function(bot: TelegramAddon) {
// Enable web server with socketio
if (cache.config.web_server) {
// Set up rate limiter
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100,
standardHeaders: true,
legacyHeaders: false,
});

const express = require('express');
const http = require('http');
const app = express();
Expand All @@ -18,17 +27,19 @@ const init = function(bot: TelegramAddon) {
const {Server} = require('socket.io');
const io = new Server(server);
cache.io = io;
app.use(limiter);

// app.get('/', (req, res) => {
// res.writeHead(200, {'Content-Type': 'text/html'});
// });

app.get('/', (_req: any, res: { sendFile: (arg0: string) => void }) => {
app.get('/', (_req: any, res: any) => {
res.sendFile(__dirname + '/web/index.html');
});

app.get(
'/chat.js',
(_req: any, res: { sendFile: (arg0: string) => void }) => {
res.sendFile(__dirname + '/web/chat.js');
},
);
app.get('/chat.js', (_req: any, res: any) => {
res.sendFile(__dirname + '/web/chat.js');
});

io.on(
'connection',
Expand All @@ -48,7 +59,7 @@ const init = function(bot: TelegramAddon) {
},
);

server.listen(port, () => console.log(`Server started on port ${port}`));
server.listen(8080, () => console.log(`Server started on port ${port}`));
}
};

Expand Down

0 comments on commit 8c32d16

Please sign in to comment.