diff --git a/SettingsHelper.js b/SettingsHelper.js index 17b03fb4..5085bbbe 100644 --- a/SettingsHelper.js +++ b/SettingsHelper.js @@ -71,19 +71,6 @@ const fields = [ default: [], type: 'array', }, - // Server settings - { - local: 'serverroot', - env: 'SERVER_ROOT', - default: '/slserver', - type: 'string', - }, - { - local: 'server_port', - env: 'SERVER_PORT', - default: '8089', - type: 'number', - }, { local: 'autoplay', env: 'AUTOPLAY', diff --git a/example_settings.json b/example_settings.json index b58434e3..de67d9c1 100644 --- a/example_settings.json +++ b/example_settings.json @@ -41,8 +41,6 @@ "defaultPassword": "DefaultPassword123" } ], - "serverroot": "/slserver", - "server_port": 8089, "autoplay": true, "slPlayerQuality": 10000, "slPlayerForceTranscode": false diff --git a/package-lock.json b/package-lock.json index 2b07aee9..167ae179 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1940,7 +1940,8 @@ "after": { "version": "0.8.2", "resolved": "https://registry.npmjs.org/after/-/after-0.8.2.tgz", - "integrity": "sha1-/ts5T58OAqqXaOcCvaI7UF+ufh8=" + "integrity": "sha1-/ts5T58OAqqXaOcCvaI7UF+ufh8=", + "dev": true }, "aggregate-error": { "version": "3.0.1", @@ -2137,7 +2138,8 @@ "arraybuffer.slice": { "version": "0.0.7", "resolved": "https://registry.npmjs.org/arraybuffer.slice/-/arraybuffer.slice-0.0.7.tgz", - "integrity": "sha512-wGUIVQXuehL5TCqQun8OW81jGzAWycqzFF8lFp+GOM5BXLYj3bKNsYC4daB7n6XjCqxQA/qgTJ+8ANR3acjrog==" + "integrity": "sha512-wGUIVQXuehL5TCqQun8OW81jGzAWycqzFF8lFp+GOM5BXLYj3bKNsYC4daB7n6XjCqxQA/qgTJ+8ANR3acjrog==", + "dev": true }, "asn1": { "version": "0.2.4", @@ -2230,7 +2232,8 @@ "async-limiter": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.1.tgz", - "integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==" + "integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==", + "dev": true }, "asynckit": { "version": "0.4.0", @@ -2339,7 +2342,8 @@ "backo2": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/backo2/-/backo2-1.0.2.tgz", - "integrity": "sha1-MasayLEpNjRj41s+u2n038+6eUc=" + "integrity": "sha1-MasayLEpNjRj41s+u2n038+6eUc=", + "dev": true }, "balanced-match": { "version": "1.0.0", @@ -2405,7 +2409,8 @@ "base64-arraybuffer": { "version": "0.1.5", "resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-0.1.5.tgz", - "integrity": "sha1-c5JncZI7Whl0etZmqlzUv5xunOg=" + "integrity": "sha1-c5JncZI7Whl0etZmqlzUv5xunOg=", + "dev": true }, "base64-js": { "version": "1.3.1", @@ -2413,11 +2418,6 @@ "integrity": "sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g==", "dev": true }, - "base64id": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/base64id/-/base64id-2.0.0.tgz", - "integrity": "sha512-lGe34o6EHj9y3Kts9R4ZYs/Gr+6N7MCaMlIFA3F1R2O5/m7K06AxfSeO5530PEERE6/WyEg3lsuyw4GHlPZHog==" - }, "batch": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/batch/-/batch-0.6.1.tgz", @@ -2437,6 +2437,7 @@ "version": "1.0.2", "resolved": "https://registry.npmjs.org/better-assert/-/better-assert-1.0.2.tgz", "integrity": "sha1-QIZrnhueC1W0gYlDEeaPr/rrxSI=", + "dev": true, "requires": { "callsite": "1.0.0" } @@ -2468,7 +2469,8 @@ "blob": { "version": "0.0.5", "resolved": "https://registry.npmjs.org/blob/-/blob-0.0.5.tgz", - "integrity": "sha512-gaqbzQPqOoamawKg0LGVd7SzLgXS+JH61oWprSLH+P+abTczqJbhTR8CmJ2u9/bUYNmHTGJx/UEmn6doAvvuig==" + "integrity": "sha512-gaqbzQPqOoamawKg0LGVd7SzLgXS+JH61oWprSLH+P+abTczqJbhTR8CmJ2u9/bUYNmHTGJx/UEmn6doAvvuig==", + "dev": true }, "bluebird": { "version": "3.7.2", @@ -2896,7 +2898,8 @@ "callsite": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/callsite/-/callsite-1.0.0.tgz", - "integrity": "sha1-KAOY5dZkvXQDi28JBRU+borxvCA=" + "integrity": "sha1-KAOY5dZkvXQDi28JBRU+borxvCA=", + "dev": true }, "callsites": { "version": "2.0.0", @@ -3335,17 +3338,20 @@ "component-bind": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/component-bind/-/component-bind-1.0.0.tgz", - "integrity": "sha1-AMYIq33Nk4l8AAllGx06jh5zu9E=" + "integrity": "sha1-AMYIq33Nk4l8AAllGx06jh5zu9E=", + "dev": true }, "component-emitter": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.2.1.tgz", - "integrity": "sha1-E3kY1teCg/ffemt8WmPhQOaUJeY=" + "integrity": "sha1-E3kY1teCg/ffemt8WmPhQOaUJeY=", + "dev": true }, "component-inherit": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/component-inherit/-/component-inherit-0.0.3.tgz", - "integrity": "sha1-ZF/ErfWLcrZJ1crmUTVhnbJv8UM=" + "integrity": "sha1-ZF/ErfWLcrZJ1crmUTVhnbJv8UM=", + "dev": true }, "compressible": { "version": "2.0.18", @@ -4513,43 +4519,11 @@ "once": "^1.4.0" } }, - "engine.io": { - "version": "3.4.2", - "resolved": "https://registry.npmjs.org/engine.io/-/engine.io-3.4.2.tgz", - "integrity": "sha512-b4Q85dFkGw+TqgytGPrGgACRUhsdKc9S9ErRAXpPGy/CXKs4tYoHDkvIRdsseAF7NjfVwjRFIn6KTnbw7LwJZg==", - "requires": { - "accepts": "~1.3.4", - "base64id": "2.0.0", - "cookie": "0.3.1", - "debug": "~4.1.0", - "engine.io-parser": "~2.2.0", - "ws": "^7.1.2" - }, - "dependencies": { - "cookie": { - "version": "0.3.1", - "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.3.1.tgz", - "integrity": "sha1-5+Ch+e9DtMi6klxcWpboBtFoc7s=" - }, - "debug": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", - "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", - "requires": { - "ms": "^2.1.1" - } - }, - "ms": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" - } - } - }, "engine.io-client": { "version": "3.4.3", "resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-3.4.3.tgz", "integrity": "sha512-0NGY+9hioejTEJCaSJZfWZLk4FPI9dN+1H1C4+wj2iuFba47UgZbJzfWs4aNFajnX/qAaYKbe2lLTfEEWzCmcw==", + "dev": true, "requires": { "component-emitter": "~1.3.0", "component-inherit": "0.0.3", @@ -4567,12 +4541,14 @@ "component-emitter": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.3.0.tgz", - "integrity": "sha512-Rd3se6QB+sO1TwqZjscQrurpEPIfO0/yYnSin6Q/rD3mOutHvUrCAhJub3r90uNb+SESBuE0QYoB90YdfatsRg==" + "integrity": "sha512-Rd3se6QB+sO1TwqZjscQrurpEPIfO0/yYnSin6Q/rD3mOutHvUrCAhJub3r90uNb+SESBuE0QYoB90YdfatsRg==", + "dev": true }, "debug": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", + "dev": true, "requires": { "ms": "^2.1.1" } @@ -4580,12 +4556,14 @@ "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", + "dev": true }, "ws": { "version": "6.1.4", "resolved": "https://registry.npmjs.org/ws/-/ws-6.1.4.tgz", "integrity": "sha512-eqZfL+NE/YQc1/ZynhojeV8q+H050oR8AZ2uIev7RU10svA9ZnJUddHcOUZTJLinZ9yEfdA2kSATS2qZK5fhJA==", + "dev": true, "requires": { "async-limiter": "~1.0.0" } @@ -4596,6 +4574,7 @@ "version": "2.2.0", "resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-2.2.0.tgz", "integrity": "sha512-6I3qD9iUxotsC5HEMuuGsKA0cXerGz+4uGcXQEkfBidgKf0amsjrrtwcbwK/nzpZBxclXlV7gGl9dgWvu4LF6w==", + "dev": true, "requires": { "after": "0.8.2", "arraybuffer.slice": "~0.0.7", @@ -5938,6 +5917,7 @@ "version": "1.0.3", "resolved": "https://registry.npmjs.org/has-binary2/-/has-binary2-1.0.3.tgz", "integrity": "sha512-G1LWKhDSvhGeAQ8mPVQlqNcOB2sJdwATtZKl2pDKKHfpf/rYj24lkinxf69blJbnsvtqqNU+L3SL50vzZhXOnw==", + "dev": true, "requires": { "isarray": "2.0.1" } @@ -5945,7 +5925,8 @@ "has-cors": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/has-cors/-/has-cors-1.1.0.tgz", - "integrity": "sha1-XkdHk/fqmEPRu5nCPu9J/xJv/zk=" + "integrity": "sha1-XkdHk/fqmEPRu5nCPu9J/xJv/zk=", + "dev": true }, "has-flag": { "version": "3.0.0", @@ -6403,7 +6384,8 @@ "indexof": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/indexof/-/indexof-0.0.1.tgz", - "integrity": "sha1-gtwzbSMrkGIXnQWrMpOmYFn9Q10=" + "integrity": "sha1-gtwzbSMrkGIXnQWrMpOmYFn9Q10=", + "dev": true }, "infer-owner": { "version": "1.0.4", @@ -6884,7 +6866,8 @@ "isarray": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/isarray/-/isarray-2.0.1.tgz", - "integrity": "sha1-o32U7ZzaLVmGXJ92/llu4fM4dB4=" + "integrity": "sha1-o32U7ZzaLVmGXJ92/llu4fM4dB4=", + "dev": true }, "isexe": { "version": "2.0.0", @@ -7860,7 +7843,8 @@ "object-component": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/object-component/-/object-component-0.0.3.tgz", - "integrity": "sha1-8MaapQ78lbhmwYb0AKM3acsvEpE=" + "integrity": "sha1-8MaapQ78lbhmwYb0AKM3acsvEpE=", + "dev": true }, "object-copy": { "version": "0.1.0", @@ -8242,6 +8226,7 @@ "version": "0.0.5", "resolved": "https://registry.npmjs.org/parseqs/-/parseqs-0.0.5.tgz", "integrity": "sha1-1SCKNzjkZ2bikbouoXNoSSGouJ0=", + "dev": true, "requires": { "better-assert": "~1.0.0" } @@ -8250,6 +8235,7 @@ "version": "0.0.5", "resolved": "https://registry.npmjs.org/parseuri/-/parseuri-0.0.5.tgz", "integrity": "sha1-gCBKUNTbt3m/3G6+J3jZDkvOMgo=", + "dev": true, "requires": { "better-assert": "~1.0.0" } @@ -10139,43 +10125,11 @@ } } }, - "socket.io": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/socket.io/-/socket.io-2.3.0.tgz", - "integrity": "sha512-2A892lrj0GcgR/9Qk81EaY2gYhCBxurV0PfmmESO6p27QPrUK1J3zdns+5QPqvUYK2q657nSj0guoIil9+7eFg==", - "requires": { - "debug": "~4.1.0", - "engine.io": "~3.4.0", - "has-binary2": "~1.0.2", - "socket.io-adapter": "~1.1.0", - "socket.io-client": "2.3.0", - "socket.io-parser": "~3.4.0" - }, - "dependencies": { - "debug": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", - "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", - "requires": { - "ms": "^2.1.1" - } - }, - "ms": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" - } - } - }, - "socket.io-adapter": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-1.1.2.tgz", - "integrity": "sha512-WzZRUj1kUjrTIrUKpZLEzFZ1OLj5FwLlAFQs9kuZJzJi5DKdU7FsWc36SNmA8iDOtwBQyT8FkrriRM8vXLYz8g==" - }, "socket.io-client": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-2.3.0.tgz", "integrity": "sha512-cEQQf24gET3rfhxZ2jJ5xzAOo/xhZwK+mOqtGRg5IowZsMgwvHwnf/mCRapAAkadhM26y+iydgwsXGObBB5ZdA==", + "dev": true, "requires": { "backo2": "1.0.2", "base64-arraybuffer": "0.1.5", @@ -10197,6 +10151,7 @@ "version": "4.1.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", + "dev": true, "requires": { "ms": "^2.1.1" } @@ -10204,12 +10159,14 @@ "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", + "dev": true }, "socket.io-parser": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-3.3.0.tgz", "integrity": "sha512-hczmV6bDgdaEbVqhAeVMM/jfUfzuEZHsQg6eOmLgJht6G3mPKMxYm75w2+qhAQZ+4X+1+ATZ+QFKeOZD5riHng==", + "dev": true, "requires": { "component-emitter": "1.2.1", "debug": "~3.1.0", @@ -10220,6 +10177,7 @@ "version": "3.1.0", "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "dev": true, "requires": { "ms": "2.0.0" } @@ -10227,37 +10185,13 @@ "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", - "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", + "dev": true } } } } }, - "socket.io-parser": { - "version": "3.4.1", - "resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-3.4.1.tgz", - "integrity": "sha512-11hMgzL+WCLWf1uFtHSNvliI++tcRUWdoeYuwIl+Axvwy9z2gQM+7nJyN3STj1tLj5JyIUH8/gpDGxzAlDdi0A==", - "requires": { - "component-emitter": "1.2.1", - "debug": "~4.1.0", - "isarray": "2.0.1" - }, - "dependencies": { - "debug": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", - "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", - "requires": { - "ms": "^2.1.1" - } - }, - "ms": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" - } - } - }, "sockjs": { "version": "0.3.20", "resolved": "https://registry.npmjs.org/sockjs/-/sockjs-0.3.20.tgz", @@ -10945,7 +10879,8 @@ "to-array": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/to-array/-/to-array-0.1.4.tgz", - "integrity": "sha1-F+bBH3PdTz10zaek/zI46a2b+JA=" + "integrity": "sha1-F+bBH3PdTz10zaek/zI46a2b+JA=", + "dev": true }, "to-arraybuffer": { "version": "1.0.1", @@ -12382,15 +12317,11 @@ "mkdirp": "^0.5.1" } }, - "ws": { - "version": "7.3.0", - "resolved": "https://registry.npmjs.org/ws/-/ws-7.3.0.tgz", - "integrity": "sha512-iFtXzngZVXPGgpTlP1rBqsUK82p9tKqsWRPg5L56egiljujJT3vGAYnHANvFxBieXrTFavhzhxW52jnaWV+w2w==" - }, "xmlhttprequest-ssl": { "version": "1.5.5", "resolved": "https://registry.npmjs.org/xmlhttprequest-ssl/-/xmlhttprequest-ssl-1.5.5.tgz", - "integrity": "sha1-wodrBhaKrcQOV9l+gRkayPQ5iz4=" + "integrity": "sha1-wodrBhaKrcQOV9l+gRkayPQ5iz4=", + "dev": true }, "xtend": { "version": "4.0.2", @@ -12478,7 +12409,8 @@ "yeast": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/yeast/-/yeast-0.1.2.tgz", - "integrity": "sha1-AI4G2AlDIMNy28L47XagymyKxBk=" + "integrity": "sha1-AI4G2AlDIMNy28L47XagymyKxBk=", + "dev": true }, "yorkie": { "version": "2.0.0", diff --git a/package.json b/package.json index 62d1c00a..392c519d 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,6 @@ "cors": "^2.8.5", "express": "^4.17.1", "minimist": "^1.2.5", - "socket.io": "^2.3.0", "vue": "^2.6.11", "vue-router": "^3.3.4", "vuetify": "^2.3.2", diff --git a/server.js b/server.js deleted file mode 100644 index e28463c7..00000000 --- a/server.js +++ /dev/null @@ -1,426 +0,0 @@ -/* eslint-disable no-param-reassign */ -// ABOUT -// Runs the SyncLounge Server software - handles rooms -// Defaults to 8089 - -// TODO: SORT USERS BY HOST - -// V2.0 -const express = require('express'); -const cors = require('cors'); -const http = require('http'); -const io = require('socket.io'); - -const { readSettings } = require('./SettingsHelper'); - -const settings = readSettings(); - -class User { - constructor() { - this.username = null; - this.role = null; - this.room = null; - this.title = null; - this.time = null; - this.avatarUrl = null; - this.uuid = null; - } -} - -const app = express(); -const server = http.Server(app); -const router = express.Router(); - -// CI stuff -const port = process.env.WEBSOCKET_USE_PORT ? process.env.PORT : settings.server_port; - -server.listen(port); - -app.options('*', cors()); // enable pre-flight across-the-board -app.use(cors({ credentials: false })); -app.use((req, res, next) => { - res.setHeader('Access-Control-Allow-Credentials', false); - next(); -}); - -app.use(settings.serverroot, router); -app.get('*', (req, res) => { - res.send('You\'ve connected to the SLServer, you\'re probably looking for the webapp.'); -}); - -const socketServer = io(server, { - path: `${settings.serverroot}/socket.io`, - handlePreflightRequest: (req, res) => { - const headers = { - 'Access-Control-Allow-Headers': 'Content-Type, Authorization', - 'Access-Control-Allow-Origin': req.headers.origin || '*', - 'Access-Control-Allow-Credentials': true, - }; - res.writeHead(200, headers); - res.end(); - }, -}); - -// Setup our router -router.get('/', (req, res) => { - res.send('You\'ve connected to the SLServer, you\'re probably looking for the webapp.'); -}); - -router.get('/health', (req, res) => { - res.setHeader('Content-Type', 'application/json'); - const connectedUsers = Object.keys(socketServer.sockets.connected).length; - let load = 'low'; - if (connectedUsers > 25) { - load = 'medium'; - } - if (connectedUsers > 50) { - load = 'high'; - } - return res.send(JSON.stringify({ load })).end(); -}); - -router.get('/users', (req, res) => { - res.setHeader('Content-Type', 'application/json'); - const users = Object.keys(socketServer.sockets.connected).length; - return res.send(JSON.stringify({ users })).end(); -}); - -const hostComparator = (a, b) => { - if (a.role === 'host') { - return -1; - } - - if (b.role === 'host') { - return 1; - } - - return 0; -}; - -socketServer.on('connection', (socket) => { - function transferHost(user, newHostPredicate) { - if (user.role !== 'host') { - console.log('Not transfering host in room', user.room, 'from', user.username, - 'because its role is', user.role); - return; - } - const room = socketServer.sockets.adapter.rooms[user.room]; - if (!room) { - console.log('Not transfering the host in the room', user.room, - 'because the room was already destroyed'); - return; - } - const newHost = room.users.find(newHostPredicate); - if (!newHost) { - console.log('Not transfering host in room', user.room, 'from', user.username, - 'because suitable user found'); - return; - } - console.log('Transfering host in room', user.room, 'from', user.username, 'to', - newHost.username); - - user.role = 'guest'; - newHost.role = 'host'; - room.hostUser = newHost; - room.hostUsername = newHost.username; - - room.users.sort(hostComparator); - // Resort to make host on top - socket.broadcast.to(user.room).emit('host-swap', newHost); - } - - function removeUser(roomname, username) { - const room = socketServer.sockets.adapter.rooms[roomname]; - if (room === undefined) { - return; - } - - room.users = room.users.filter((user) => user.username !== username); - } - - function handleDisconnect(disconnect) { - if (socket.selfUser === undefined || socket.selfUser === null) { - return; - } - // console.log('User left: ' + socket.selfUser.username) - transferHost(socket.selfUser, (user) => user !== socket.selfUser); - removeUser(socket.selfUser.room, socket.selfUser.username); - if (socketServer.sockets.adapter.rooms[socket.selfUser.room]) { - socket.broadcast.to(socket.selfUser.room).emit('user-left', - socketServer.sockets.adapter.rooms[socket.selfUser.room].users, socket.selfUser); - } - socket.disconnect(disconnect); - console.log(`Total Connected users: ${Object.keys(socketServer.sockets.connected).length}`); - } - - function updateUserData(username, userData, room) { - if (!room) { - console.log('Tried to update a user who isnt in a room', username, userData, room); - return false; - } - - try { - const foundUser = socketServer.sockets.adapter.rooms[room].users - .find((user) => user.username === username); - - if (foundUser !== undefined) { - // This is our user - foundUser.time = userData.time; - foundUser.duration = userData.duration; - foundUser.title = userData.title; - foundUser.lastHeartbeat = (new Date()).getTime(); - foundUser.playerState = userData.playerState; - foundUser.rawTitle = userData.rawTitle; - foundUser.clientResponseTime = userData.clientResponseTime; - foundUser.type = userData.type; - foundUser.parentTitle = userData.parentTitle || ''; - foundUser.grandparentTitle = userData.grandparentTitle || ''; - foundUser.playerProduct = userData.playerProduct || ''; - foundUser.status = userData.status || 'unknown'; - foundUser.machineIdentifier = userData.machineIdentifier || ''; - foundUser.ratingKey = userData.ratingKey; - foundUser.uuid = userData.uuid; - - return true; - } - } catch (e) { - console.log('Failed to update a user', username, userData, room); - } - - return false; - } - - function getNumberFromUsername(username) { - return parseInt(username.match(/\((\d+)\)$/)[1], 10); - } - - function getValidUsername(usersarray, wantedname) { - const userSameName = usersarray.find((user) => user.username === wantedname); - if (userSameName === undefined) { - return wantedname; - } - - // Get users with same username that are numbered like: username(1) - const sameUsersNum = usersarray.filter((user) => user.username.startsWith(`${wantedname}(`)); - if (sameUsersNum.length > 0) { - const userNumbers = sameUsersNum.map((user) => getNumberFromUsername(user.username)); - const nextNumber = Math.max(...userNumbers) + 1; - - return `${wantedname}(${nextNumber})`; - } - - return `${wantedname}(1)`; - } - - console.log('Someone connected to the SyncLounge server socket'); - console.log(`Total Connected users: ${Object.keys(socketServer.sockets.connected).length}`); - - socket.on('join', (data) => { - // A user is attempting to join a room - if (data == null) { - return; - } - - if (!data || !data.username || !data.room) { - console.log('Invalid join attempt', data); - socket.emit('join-result', false, {}, 'Invalid data', []); - return; - } - - const tempUser = new User(); - let result = true; - - let details = `Successfully connected to ${data.room}`; - - if (socket.selfUser != null || socket.selfUser !== undefined) { - // Already in a room! Leave the room we're in - handleDisconnect(false); - } - - let room = socketServer.sockets.adapter.rooms[data.room]; - if (room === undefined || room.users === undefined || room.users === null) { - socket.join(data.room); - room = socketServer.sockets.adapter.rooms[data.room]; - room.users = []; - room.password = data.password; - room.partyPausing = true; - tempUser.role = 'host'; - tempUser.username = getValidUsername([], data.username); - } else { - tempUser.username = getValidUsername(room.users, data.username); - if (room.password === null || room.password === '') { - // Check if we've already got a Host - if (room.hostUsername == null) { - // We dont have a host user yet. - // This should never happen.. - room.hostUsername = tempUser.username; - tempUser.role = 'host'; - socket.join(data.room); - } else { - tempUser.role = 'guest'; - socket.join(data.room); - } - } else if (room.password === data.password) { - // This room has a password - // Good password! - if (room.hostUsername == null) { - // We dont have a host user yet. - room.hostUsername = tempUser.username; - tempUser.role = 'host'; - socket.join(data.room); - } else { - tempUser.role = 'guest'; - socket.join(data.room); - } - } else { - result = false; - details = 'wrong password'; - } - } - - tempUser.avatarUrl = data.avatarUrl; - - // We've sorted out where the user should go, lets send back - let currentUsers = null; - if (result) { - tempUser.room = data.room; - tempUser.uuid = data.uuid; - console.log(`User ${tempUser.username} joined ${tempUser.room}`); - if (tempUser.role === 'host') { - room.hostUsername = tempUser.username; - } - room.users.push(tempUser); - - socket.broadcast.to(data.room).emit('user-joined', room.users, tempUser); - // Set some objects on the socket for ease of use down the road - socket.ourRoom = data.room; - socket.selfUser = tempUser; - currentUsers = room.users; - } else { - console.log('User failed to join a room'); - } - - socket.emit('join-result', result, tempUser, details, currentUsers, room.partyPausing); - }); - - socket.on('poll', (data) => { - if (socket.ourRoom == null) { - // console.log('This user should join a room first') - socket.emit('flowerror', 'You aren\' connected to a room! Use join'); - return; - } - - const room = socketServer.sockets.adapter.rooms[socket.selfUser.room]; - if (!room) { - console.log( - 'Got a poll update from a user in a room that was either destroyed or never existed', - data, - ); - return; - } - // Recieved an update from a user - updateUserData(socket.selfUser.username, data, socket.selfUser.room); - - socket.emit('poll-result', - socketServer.sockets.adapter.rooms[socket.selfUser.room].users, - socket.selfUser, - data.commandId, - room.partyPausing); - - if (socket.selfUser.role === 'host') { - // We're the host, broadcast to all clients our data - const temp = {}; - temp.time = data.time; - temp.duration = data.duration; - temp.title = data.title; - temp.rawTitle = data.rawTitle; - temp.lastHeartbeat = new Date().getTime(); - temp.playerState = data.playerState; - temp.clientResponseTime = data.clientResponseTime; - temp.machineIdentifier = data.machineIdentifier; - temp.type = data.type; - temp.ratingKey = data.ratingKey; - temp.latency = data.latency; - temp.parentTitle = data.parentTitle; - - temp.grandparentTitle = data.grandparentTitle; - temp.status = data.status; - temp.playerProduct = data.playerProduct; - socket.broadcast.to(socket.selfUser.room).emit('host-update', temp); - } - }); - - socket.on('send_message', (msg) => { - // console.log(msg) - if (socket.ourRoom == null) { - // console.log('This user should join a room first') - socket.emit('flowerror', 'You aren\' connected to a room! Use join'); - return; - } - // console.log('New message in channel ' + socket.selfUser.room + ' from ' + socket.selfUser.username + ' saying ' + msg) - socket.broadcast.to(socket.selfUser.room).emit('new_message', { - msg: msg.msg, - user: { - username: socket.selfUser.username, - thumb: socket.selfUser.avatarUrl, - }, - type: msg.type, - }); - }); - - socket.on('party_pausing_change', (value) => { - const user = socket.selfUser; - - if (!user) { - return 'Invalid status'; - } - - const room = socketServer.sockets.adapter.rooms[user.room]; - if (!room) { - return 'Invalid status'; - } - - if (user.role !== 'host') return 'You are not the host'; - room.partyPausing = value; - socket.broadcast.to(socket.selfUser.room).emit('party-pausing-changed', { value, user }); - socket.emit('party-pausing-changed', { value, user }); - return true; - }); - - socket.on('party_pausing_send', (isPause) => { - const user = socket.selfUser; - if (!user) { - return false; - } - - const room = socketServer.sockets.adapter.rooms[user.room]; - - if (!room || !room.partyPausing) { - return false; - } - - socket.broadcast.to(socket.selfUser.room).emit('party-pausing-pause', { isPause, user }); - socket.emit('party-pausing-pause', { isPause, user }); - return true; - }); - - socket.on('transfer_host', (data) => { - if (socket.ourRoom == null) { - socket.emit('flowerror', 'You aren\'t connected to a room! Use join'); - return; - } - transferHost(socket.selfUser, (user) => user.username === data.username); - }); - - socket.on('connect_timeout', () => { - // console.log('timeout') - handleDisconnect(true); - }); - - socket.on('disconnect', () => { - handleDisconnect(true); - }); -}); - -console.log(`SyncLounge Server successfully started on port ${port}`); -console.log(`Running with base URL: ${settings.serverroot}`);