Skip to content

Commit

Permalink
Added localAddress option
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorMcKay committed Oct 20, 2023
1 parent f7b198a commit 5ae1255
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,15 @@ event is emitted.
- `transport` - An `ITransport` instance, if you need to specify a [custom transport](#custom-transports).
If omitted, defaults to a `WebSocketCMTransport` instance for `SteamClient` platform types, and a
`WebApiTransport` instance for all other platform types. In all likelihood, you don't need to use this.
- `localAddress` - A string containing the local IP address you want to use. For example, `11.22.33.44`
- `httpProxy` - A string containing a URI for an HTTP proxy. For example, `http://user:[email protected]:80`
- `socksProxy` - A string containing a URI for a SOCKS proxy. For example, `socks5://user:[email protected]:1080`
- `agent` - An `https.Agent` instance to use for requests. If omitted, a new `https.Agent` will be created internally.

You can only use one of `httpProxy`, `socksProxy` or `agent` at the same time. If you try to use more than one of them,
an Error will be thrown.
You can only use one of `localAddress`, `httpProxy`, `socksProxy` or `agent` at the same time. If you try to use more
than one of them, an Error will be thrown.

If you specify a custom transport, then you are responsible for handling proxy or agent usage in your transport.

Constructs a new `LoginSession` instance. Example usage:

Expand Down Expand Up @@ -569,12 +572,15 @@ If this is a `string`, it must be either hex- or base64-encoded.
- `options` - An object with zero or more of these properties:
- `transport` - An `ITransport` instance, if you need to specify a [custom transport](#custom-transports).
If omitted, defaults to a `WebApiTransport` instance. In all likelihood, you don't need to use this.
- `localAddress` - A string containing the local IP address you want to use. For example, `11.22.33.44`
- `httpProxy` - A string containing a URI for an HTTP proxy. For example, `http://user:[email protected]:80`
- `socksProxy` A string containing a URI for a SOCKS proxy. For example, `socks5://user:[email protected]:1080`
- `agent` - An `https.Agent` instance to use for requests. If omitted, a new `https.Agent` will be created internally.

You can only use one of `httpProxy`, `socksProxy` or `agent` at the same time. If you try to use more than one of them,
an Error will be thrown.
You can only use one of `localAddress`, `httpProxy`, `socksProxy` or `agent` at the same time. If you try to use more
than one of them, an Error will be thrown.

If you specify a custom transport, then you are responsible for handling proxy or agent usage in your transport.

Constructs a new `LoginApprover` instance. Example usage:

Expand Down
6 changes: 3 additions & 3 deletions src/LoginApprover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export default class LoginApprover {
constructor(accessToken: string, sharedSecret: string|Buffer, options?: ConstructorOptions) {
options = options || {};

let mutuallyExclusiveOptions = ['httpProxy', 'socksProxy', 'agent'];
let mutuallyExclusiveOptions = ['localAddress', 'httpProxy', 'socksProxy', 'agent'];
if (Object.keys(options).filter(k => mutuallyExclusiveOptions.includes(k)).length > 1) {
throw new Error('Cannot specify more than one of httpProxy, socksProxy, or agent at the same time');
throw new Error('Cannot specify more than one of localAddress, httpProxy, socksProxy, or agent at the same time');
}

let agent:HTTPS.Agent = options.agent || new HTTPS.Agent({keepAlive: true});
Expand All @@ -35,7 +35,7 @@ export default class LoginApprover {
agent = new SocksProxyAgent(options.socksProxy);
}

this._webClient = new HttpClient({httpsAgent: agent});
this._webClient = new HttpClient({httpsAgent: agent, localAddress: options.localAddress});

this.accessToken = accessToken;
this.sharedSecret = sharedSecret;
Expand Down
8 changes: 4 additions & 4 deletions src/LoginSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ export default class LoginSession extends TypedEmitter<LoginSessionEvents> {

options = options || {};

let mutuallyExclusiveOptions = ['httpProxy', 'socksProxy', 'agent'];
let mutuallyExclusiveOptions = ['localAddress', 'httpProxy', 'socksProxy', 'agent'];
if (Object.keys(options).filter(k => mutuallyExclusiveOptions.includes(k)).length > 1) {
throw new Error('Cannot specify more than one of httpProxy, socksProxy, or agent at the same time');
throw new Error('Cannot specify more than one of localAddress, httpProxy, socksProxy, or agent at the same time');
}

let agent:HTTPS.Agent = options.agent || new HTTPS.Agent({keepAlive: true});
Expand All @@ -117,15 +117,15 @@ export default class LoginSession extends TypedEmitter<LoginSessionEvents> {
agent = new SocksProxyAgent(options.socksProxy);
}

this._webClient = new HttpClient({httpsAgent: agent});
this._webClient = new HttpClient({httpsAgent: agent, localAddress: options.localAddress});

this._platformType = platformType;

let transport = options.transport;
if (!transport) {
switch (platformType) {
case EAuthTokenPlatformType.SteamClient:
transport = new WebSocketCMTransport(this._webClient, agent);
transport = new WebSocketCMTransport(this._webClient, agent, options.localAddress);
break;

default:
Expand Down
15 changes: 12 additions & 3 deletions src/interfaces-external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,26 @@ export interface ConstructorOptions {
transport?: ITransport,

/**
* A string containing a URI for a SOCKS proxy. For example, `socks5://user:[email protected]:1080`
* A string containing the local IP address you want to use. For example, '11.22.33.44'.
* Cannot be used alongside `socksProxy`, `httpProxy`, or `agent`.
*/
localAddress?: string,

/**
* A string containing a URI for a SOCKS proxy. For example, `socks5://user:[email protected]:1080`.
* Cannot be used alongside `localAddress`, `httpProxy`, or `agent`.
*/
socksProxy?: string,

/**
* A string containing a URI for an HTTP proxy. For example, `http://user:[email protected]:80`
* A string containing a URI for an HTTP proxy. For example, `http://user:[email protected]:80`.
* Cannot be used alongside `localAddress`, `socksProxy`, or `agent`.
*/
httpProxy?: string
httpProxy?: string,

/**
* An `https.Agent` instance to use for requests. If omitted, a new `https.Agent` will be created internally.
* Cannot be used alongside `localAddress`, `socksProxy`, or `httpProxy`.
*/
agent?: HTTPS.Agent
}
Expand Down
7 changes: 5 additions & 2 deletions src/transports/WebSocketCMTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ export default class WebSocketCMTransport implements ITransport {
_connectTimeout = 1000;
_webClient: HttpClient;
_agent: Agent;
_localAddress?: string;
_websocket: any;
_jobs: any;
_clientSessionId = 0;

constructor(webClient: HttpClient, agent: Agent) {
constructor(webClient: HttpClient, agent: Agent, localAddress?: string) {
this._webClient = webClient;
this._agent = agent;
this._localAddress = localAddress;
this._websocket = null;
this._jobs = {};
}
Expand Down Expand Up @@ -108,7 +110,8 @@ export default class WebSocketCMTransport implements ITransport {
let resolved = false;
this._websocket = new WebSocket(`wss://${cm.endpoint}/cmsocket/`, {
connection: {
agent: this._agent
agent: this._agent,
localAddress: this._localAddress
}
});

Expand Down

0 comments on commit 5ae1255

Please sign in to comment.