Ryozuki 1 year ago
parent
commit
3f4247df1b
1 changed files with 7 additions and 3 deletions
  1. 7 3
      src/index.ts

+ 7 - 3
src/index.ts

@@ -17,9 +17,13 @@ wss.on('listening', () => {
 });
 
 wss.on('connection', (client, req) => {
-    //const ip = req.connection.remoteAddress || '';
-    // if using nginx
-    const ip = (req.headers['x-forwarded-for']! as string).split(/\s*,\s*/)[0];
+    let ip = '';
+
+    try {
+        ip = (req.headers['x-forwarded-for']! as string).split(/\s*,\s*/)[0];
+    } catch (e) {
+        ip = req.connection.remoteAddress || '';
+    }
 
     for (let cli of clients) {
         if (cli.ip === ip) {