-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Dong] support custom auth via command line (#89)
* [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 <[email protected]>
- Loading branch information
1 parent
86978ef
commit a2f541b
Showing
4 changed files
with
60 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.idea | ||
build/ | ||
build-*/ | ||
.vs/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |