Skip to content

Commit

Permalink
feat: adds getProcessCommandLine
Browse files Browse the repository at this point in the history
  • Loading branch information
jlcarvalho committed Jul 22, 2024
1 parent 0317262 commit 9412389
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
39 changes: 32 additions & 7 deletions src/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Credentials> {
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, '')
Expand Down
9 changes: 9 additions & 0 deletions src/tests/authentication.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})

0 comments on commit 9412389

Please sign in to comment.