@20syldev/api 3.3.0 → 3.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/app.js +14 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
<a href="https://api.sylvain.pro"><img src="https://api.sylvain.pro/favicon.ico" alt="Logo" width="25%" height="auto"/></a>
|
|
3
3
|
|
|
4
4
|
# API Personnelle
|
|
5
|
-
[](https://github.com/20syldev/api/releases/latest)
|
|
6
6
|
</div>
|
|
7
7
|
|
|
8
8
|
---
|
|
@@ -46,7 +46,7 @@ Enfin, **exécutez** le script de build, qui installera les **dépendances** et
|
|
|
46
46
|
$ npm run build
|
|
47
47
|
```
|
|
48
48
|
```console
|
|
49
|
-
> @20syldev/api@3.3.
|
|
49
|
+
> @20syldev/api@3.3.2 build
|
|
50
50
|
> npm install && node app.js
|
|
51
51
|
|
|
52
52
|
[...]
|
package/app.js
CHANGED
|
@@ -85,6 +85,7 @@ function checkGame(moves) {
|
|
|
85
85
|
dotenv.config();
|
|
86
86
|
|
|
87
87
|
// CORS & Express setup
|
|
88
|
+
app.set('trust proxy', 1);
|
|
88
89
|
app.use(cors({ methods: ['GET', 'POST'] }));
|
|
89
90
|
app.use(urlencoded({ extended: true }));
|
|
90
91
|
app.use(json());
|
|
@@ -106,7 +107,8 @@ app.use((req, res, next) => {
|
|
|
106
107
|
|
|
107
108
|
// Too many requests
|
|
108
109
|
app.use((req, res, next) => {
|
|
109
|
-
const ip = req.ip
|
|
110
|
+
const ip = req.headers['cf-connecting-ip'] || req.socket.remoteAddress;
|
|
111
|
+
const now = Date.now();
|
|
110
112
|
const token = req.headers.authorization?.split(' ')[1] || '';
|
|
111
113
|
|
|
112
114
|
const unlimited = process.env.UNLIMITED_TOKEN_LIST?.split(' ') || [];
|
|
@@ -146,19 +148,21 @@ app.use((req, res, next) => {
|
|
|
146
148
|
if (req.method === 'HEAD') return next();
|
|
147
149
|
if (req.originalUrl === '/logs') return next();
|
|
148
150
|
|
|
151
|
+
const ip = req.headers['cf-connecting-ip'] || req.socket.remoteAddress;
|
|
152
|
+
|
|
149
153
|
const startTime = Date.now();
|
|
154
|
+
const timestamp = new Date().toISOString();
|
|
155
|
+
const method = req.method;
|
|
156
|
+
const url = req.originalUrl;
|
|
157
|
+
const platform = req.headers['sec-ch-ua-platform']?.replace(/"/g, '');
|
|
150
158
|
|
|
151
159
|
res.on('finish', () => {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
duration: `${Date.now() - startTime}ms`,
|
|
158
|
-
platform: req.headers['sec-ch-ua-platform']?.replace(/"/g, ''),
|
|
159
|
-
});
|
|
160
|
+
const status = res.statusCode === 304 ? 200 : res.statusCode;
|
|
161
|
+
const duration = `${Date.now() - startTime}ms`;
|
|
162
|
+
|
|
163
|
+
logs.push({ timestamp, method, url, status, duration, platform });
|
|
164
|
+
console.log(`[${new Date().toISOString()}] ${method} ${url} ${res.statusCode} - ${duration} - ${ip}`);
|
|
160
165
|
if (logs.length > 1000) logs.shift();
|
|
161
|
-
console.log(`[${new Date().toISOString()}] ${req.method} ${req.originalUrl} ${res.statusCode} - ${Date.now() - startTime}ms - ${req.ip}`);
|
|
162
166
|
});
|
|
163
167
|
next();
|
|
164
168
|
});
|
package/package.json
CHANGED