Skip to content

Commit

Permalink
Added version checks
Browse files Browse the repository at this point in the history
  • Loading branch information
FlafyDev committed Dec 19, 2021
1 parent a4c5a1c commit d9daf34
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 45 deletions.
103 changes: 64 additions & 39 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"name": "spotify-listen-together-server",
"version": "0.1.0",
"version": "0.2.0",
"dependencies": {
"@types/semver": "^7.3.9",
"dotenv-defaults": "^3.0.0",
"semver": "^7.3.5",
"socket.io": "^4.4.0"
},
"main": "dist/server.js",
Expand Down
9 changes: 9 additions & 0 deletions src/clientVersionValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import semver from 'semver';

export default class ClientVersionValidator {
requirements = "0.2.x"

validate(version?: string): boolean {
return (!!version) && semver.satisfies(version, this.requirements)
}
}
20 changes: 15 additions & 5 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Server, Socket } from 'socket.io';
import ClientInfo from './clientInfo';
import Player from './player';
import config from './config'

import ClientVersionValidator from './clientVersionValidator';
// track uri example: "spotify:track:5Hyr47BBGpvOfcykSCcaw9"

const io = new Server({
Expand All @@ -13,6 +13,7 @@ const io = new Server({
})
let clientsInfo = new Map<string, ClientInfo>()
const player = new Player(emitToListeners, clientsInfo)
const clientVersionValidator = new ClientVersionValidator()

function emitToListeners(ev: string, ...args: any) {
console.log(`Sending ${ev}: ${args}`)
Expand Down Expand Up @@ -59,10 +60,19 @@ io.on("connection", (socket: Socket) => {
lastPing = Date.now()
});

socket.on("login", (name: string) => {
info.name = name
info.loggedIn = true;
socket.join("listeners")
socket.on("login", (name: string, clientVersion?: string, badVersion?: (requirements: string) => void) => {
if (clientVersionValidator.validate(clientVersion)) {
info.name = name
info.loggedIn = true;
socket.join("listeners")
} else {
if (badVersion != null)
badVersion(clientVersionValidator.requirements)

setTimeout(() => {
socket.disconnect()
}, 3000)
}
})

socket.on("requestHost", (password: string, callback: (permitted: boolean) => void) => {
Expand Down

0 comments on commit d9daf34

Please sign in to comment.