Skip to content

Commit

Permalink
[Dong] support custom auth via command line (#89)
Browse files Browse the repository at this point in the history
* [Dong] support custom auth via command line

* [Dong] fixed code style issue

* [TECH][Dong] use cli parameter instead of question

Co-authored-by: Dong,WANG <dong.wang@anz.com>
  • Loading branch information
wanaright10 and Dong,WANG committed Apr 13, 2020
1 parent 86978ef commit a2f541b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
.idea
build/
build-*/
.vs/
Expand Down
15 changes: 14 additions & 1 deletion README.md
Expand Up @@ -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:

Expand Down
14 changes: 14 additions & 0 deletions 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')
}
}
42 changes: 31 additions & 11 deletions 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))

0 comments on commit a2f541b

Please sign in to comment.