diff --git a/README.md b/README.md index 6676397..2191137 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,7 @@ LeagueClientUx process. If you wish to await until a client is found, you can us | unsafe | `false` | If you do not wish to authenticate safely using a self-signed certificate you can authorize while ignoring any certificate rejections. To authenticate this way, set unsafe to `true`. The custom certificate option will take precedence over this, meaning this option is meaningless if a custom certificate is provided. | | useDeprecatedWmic | `false` | Use deprecated Windows WMIC command line over Get-CimInstance. Does nothing if the system is not running on Windows. | | windowsShell | `powershell` | Set the Windows shell to use. Either powershell or cmd. | +| getProcessCommandLine | `undefined` | Set custom function to get LeagueClientUx process information. | ```js import { authenticate } from 'league-connect' diff --git a/src/authentication.ts b/src/authentication.ts index ba0c8db..427fe1e 100644 --- a/src/authentication.ts +++ b/src/authentication.ts @@ -78,6 +78,12 @@ export interface AuthenticationOptions { * Default: 'powershell' */ windowsShell?: 'cmd' | 'powershell' + /** + * Set getProcessCommandLine custom function to get LeagueClientUx process information. + * + * Default: undefined + */ + getProcessCommandLine?: (processName: string) => string /** * Debug mode. Prints error information to console. * @internal @@ -128,26 +134,45 @@ export class ClientElevatedPermsError extends Error { * @throws ClientElevatedPermsError If the League Client is running as administrator and the script is not (Windows only) */ export async function authenticate(options?: AuthenticationOptions): Promise { - async function tryAuthenticate() { + async function getProcessCommandLine(isWindows: boolean, executionOptions: { shell?: string; }) { const name = options?.name ?? DEFAULT_NAME - const portRegex = /--app-port=([0-9]+)(?= *"| --)/ - const passwordRegex = /--remoting-auth-token=(.+?)(?= *"| --)/ - const pidRegex = /--app-pid=([0-9]+)(?= *"| --)/ - const isWindows = process.platform === 'win32' + + if (typeof options?.getProcessCommandLine === 'function') { + return options.getProcessCommandLine(isWindows ? `${name}.exe` : name) + } let command: string + if (!isWindows) { command = `ps x -o args | grep '${name}'` - } else if (isWindows && options?.useDeprecatedWmic === true) { + + const { stdout: rawStdout } = await exec(command, {}) + + return rawStdout + } + + if (options?.useDeprecatedWmic) { command = `wmic process where caption='${name}.exe' get commandline` } else { command = `Get-CimInstance -Query "SELECT * from Win32_Process WHERE name LIKE '${name}.exe'" | Select-Object -ExpandProperty CommandLine` } + const { stdout: rawStdout } = await exec(command, executionOptions) + + return rawStdout + } + + async function tryAuthenticate() { + const name = options?.name ?? DEFAULT_NAME + const portRegex = /--app-port=([0-9]+)(?= *"| --)/ + const passwordRegex = /--remoting-auth-token=(.+?)(?= *"| --)/ + const pidRegex = /--app-pid=([0-9]+)(?= *"| --)/ + const isWindows = process.platform === 'win32' + const executionOptions = isWindows ? { shell: options?.windowsShell ?? ('powershell' as string) } : {} try { - const { stdout: rawStdout } = await exec(command, executionOptions) + const rawStdout = await getProcessCommandLine(isWindows, executionOptions) // TODO: investigate regression with calling .replace on rawStdout // Remove newlines from stdout const stdout = rawStdout.replace(/\n|\r/g, '') diff --git a/src/tests/authentication.test.ts b/src/tests/authentication.test.ts index 6dfe230..a579b47 100644 --- a/src/tests/authentication.test.ts +++ b/src/tests/authentication.test.ts @@ -65,4 +65,13 @@ describe('authenticating to the api', () => { expect(credentials?.certificate).toBeUndefined() }) + + test('authentication using get-command-line detection mode', async () => { + const credentials = await authenticate({ + getProcessCommandLine: () => `C:/Riot Games/LeagueClientUx.exe" "--remoting-auth-token=CUSTOM_TOKEN" "--app-port=57730" "--app-pid=28900"` + }) + + expect(credentials).toBeDefined() + expect(credentials?.certificate).toBeDefined() + }) })