Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds option to pass a getProcessCommandLine function #125

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
33 changes: 26 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,39 @@ 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) {
} else 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 getProcessCommandLine option', 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()
})
})