diff --git a/.gitignore b/.gitignore index 5dceccb..cd6e993 100755 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea build/ build-*/ .vs/ diff --git a/README.md b/README.md index eb3a03e..e78b9c8 100644 --- a/README.md +++ b/README.md @@ -154,7 +154,20 @@ npm run build # build ts to js. run it again when code changed. npm start ``` -The server will listen to port `11451/udp`. +Use `--port` pass the port parameter, or else it will use `11451/udp` as default. + +Use `--simpleAuth` pass the auth via username and password, or else there's no authentication. + +Use `--httpAuth` pass the auth via http url, or else there's no authentication. + +Use `--jsonAuth` pass the auth via json file, or else there's no authentication. + +Example: + +```sh +npm run build +npm start -- --port 10086 --simpleAuth username:password +``` Meanwhile the monitor service will be started on port `11451/tcp` by default, you can get online client count via HTTP request: diff --git a/server/src/auth/CustomAuthProvider.ts b/server/src/auth/CustomAuthProvider.ts new file mode 100644 index 0000000..73a6e33 --- /dev/null +++ b/server/src/auth/CustomAuthProvider.ts @@ -0,0 +1,14 @@ +import { BasicAuthProvider, AuthError, AuthErrorType, SHA1 } from './types' + +export class CustomAuthProvider extends BasicAuthProvider { + constructor(private username: string, private password: string) { + super() + } + + async getUserPasswordSHA1(username: string) { + if (username !== this.username) { + throw new AuthError(AuthErrorType.NoSuchUser, 'No such user') + } + return Buffer.from(this.password, 'hex') + } +} diff --git a/server/src/main.ts b/server/src/main.ts index 1a794f6..049d4bc 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -1,25 +1,45 @@ import { SLPServer } from './udpserver' import { ServerMonitor } from './monitor' -import { AuthProvider, JsonAuthProvider, HttpAuthProvider } from './auth' +import { AuthProvider, HttpAuthProvider, JsonAuthProvider } from './auth' +import { CustomAuthProvider } from "./auth/CustomAuthProvider"; -function main (argv: string[]) { - let port = argv[0] - let jsonPath = argv[1] - if (port === undefined) { - port = '11451' +function parseArgs2Obj(args: string[]) { + let argsObj: any = {}; + for (let i = 0; i < args.length; i += 2) { + let key: string = args[i]; + let value: string = args[i + 1]; + if (key.startsWith('--')) { + argsObj[key.slice(2)] = value; + } } + return argsObj; +} + +function main(argv: string[]) { + let argsObj = parseArgs2Obj(argv); + let provider: AuthProvider | undefined - const { USE_HTTP_PROVIDER } = process.env + const {USE_HTTP_PROVIDER} = process.env if (USE_HTTP_PROVIDER) { provider = new HttpAuthProvider(USE_HTTP_PROVIDER) console.log(`using HttpAuthProvider url: ${USE_HTTP_PROVIDER}`) - } else if (jsonPath) { - provider = new JsonAuthProvider(jsonPath) - console.log(`using JsonAuthProvider file: ${jsonPath}`) + } else if (argsObj.httpAuth) { + provider = new HttpAuthProvider(argsObj.httpAuth) + console.log(`using HttpAuthProvider url: ${argsObj.httpAuth}`) + } else if (argsObj.jsonAuth) { + provider = new JsonAuthProvider(argsObj.jsonAuth) + console.log(`using JsonAuthProvider file: ${argsObj.jsonAuth}`) + } else if (argsObj.simpleAuth) { + let username_password = argsObj.simpleAuth.split(':'); + let username = username_password[0]; + let password = username_password[1]; + provider = new CustomAuthProvider(username, password); + console.log(`using simple auth with username=${username} password=${password}`); } - const portNum = parseInt(port) + const portNum = parseInt(argsObj.port || '11451') let s = new SLPServer(portNum, provider) let monitor = new ServerMonitor(s) monitor.start(portNum) } + main(process.argv.slice(2))