-
Notifications
You must be signed in to change notification settings - Fork 68
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: Add bindAddress command line option to allow local-ssl-proxy to play nice with WSL #124
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,10 +30,10 @@ | |
}, | ||
"devDependencies": { | ||
"@tsconfig/node-lts-strictest": "^18.12.1", | ||
"@types/http-proxy": "^1.17.10", | ||
"@types/http-proxy": "1.17.15", | ||
"@types/node": "^18.15.0", | ||
"typescript": "^4.9.5", | ||
"vitest": "^0.29.2" | ||
}, | ||
"packageManager": "[email protected]" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,20 +24,22 @@ const program = createCommand(name) | |
resolve(__dirname, '..', 'resources', 'localhost.pem') | ||
) | ||
.option('-k, --key <key>', 'path to SSL key', exists, resolve(__dirname, '..', 'resources', 'localhost-key.pem')) | ||
.option('-o, --config <config>', 'path to configuration file', (path) => require(absolutePath(path))); | ||
.option('-o, --config <config>', 'path to configuration file', (path) => require(absolutePath(path))) | ||
.option('-b, --bindAddress <bindAddress>', 'bind address for the server'); | ||
|
||
type Proxy = { | ||
export type Proxy = { | ||
hostname: string; | ||
source: number; | ||
target: number; | ||
cert: string; | ||
key: string; | ||
}; | ||
|
||
type Config = { config: Record<string, Proxy> }; | ||
type ParsedArguments = Proxy | Config; | ||
export type Config = { config: Record<string, Proxy> }; | ||
type BindAddress = { bindAddress: string }; | ||
export type ParsedArguments = (Proxy | Config) & BindAddress; | ||
|
||
function isConfig(args: unknown): args is Config { | ||
export function isConfig(args: unknown): args is Config { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exported so it can be used in unit-tests |
||
return Boolean(args && typeof args === 'object' && 'config' in args); | ||
} | ||
|
||
|
@@ -58,9 +60,9 @@ export function isProxy(input: unknown): input is Proxy { | |
); | ||
} | ||
|
||
export function parse(args?: string[]): Proxy | Record<string, Proxy> { | ||
export function parse(args?: string[]): ParsedArguments { | ||
const proxy: ParsedArguments = | ||
args === undefined ? program.parse().opts() : program.parse(args, { from: 'user' }).opts(); | ||
|
||
return isConfig(proxy) ? proxy.config : proxy; | ||
return isConfig(proxy) ? {config: proxy.config, bindAddress: proxy.bindAddress} : {...proxy, bindAddress: proxy.bindAddress}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,95 @@ | ||
import { test, expect } from 'vitest'; | ||
import { parse } from '../src/lib'; | ||
import { isProxy, isConfig, parse, ParsedArguments, Proxy, Config } from '../src/lib'; | ||
|
||
|
||
function expectProxy(result: ParsedArguments): Proxy { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type guard functions to keep TypeScript happy on the tests. |
||
if (isProxy(result)) { | ||
return result; | ||
} | ||
|
||
throw new Error('Returned object is not of type Proxy'); | ||
} | ||
|
||
function expectConfig(result: ParsedArguments): Config { | ||
if (isConfig(result)) { | ||
return result; | ||
} | ||
|
||
throw new Error('Returned object is not of type Proxy'); | ||
} | ||
|
||
test('cert (default)', () => { | ||
const { cert } = parse([]); | ||
const result = parse([]); | ||
const { cert } = expectProxy(result); | ||
expect(cert).toEqual(expect.any(String)); | ||
expect(result.bindAddress).toBeUndefined(); | ||
}); | ||
|
||
test('cert', () => { | ||
const { cert } = parse(['--cert', require.resolve('../resources/localhost.pem')]); | ||
const result = parse(['--cert', require.resolve('../resources/localhost.pem')]); | ||
const { cert } = expectProxy(result); | ||
expect(cert).toEqual(expect.any(String)); | ||
}); | ||
|
||
test('key (default)', () => { | ||
const { key } = parse([]); | ||
const result = parse([]); | ||
const { key } = expectProxy(result); | ||
expect(key).toEqual(expect.any(String)); | ||
}); | ||
|
||
test('key', () => { | ||
const { key } = parse(['--key', require.resolve('../resources/localhost-key.pem')]); | ||
const result = parse(['--key', require.resolve('../resources/localhost-key.pem')]); | ||
const { key } = expectProxy(result); | ||
expect(key).toEqual(expect.any(String)); | ||
}); | ||
|
||
test('hostname (default)', () => { | ||
const { hostname } = parse([]); | ||
const result = parse([]); | ||
const { hostname } = expectProxy(result); | ||
expect(hostname).toBe('localhost'); | ||
}); | ||
|
||
test('hostname', () => { | ||
const { hostname } = parse(['--hostname', '127.0.0.1']); | ||
const result = parse(['--hostname', '127.0.0.1']); | ||
const { hostname } = expectProxy(result); | ||
expect(hostname).toBe('127.0.0.1'); | ||
}); | ||
|
||
test('source (default)', () => { | ||
const { source } = parse([]); | ||
const result = parse([]); | ||
const { source } = expectProxy(result); | ||
expect(source).toBe(9001); | ||
}); | ||
|
||
test('source', () => { | ||
const { source } = parse(['--source', '5001']); | ||
const result = parse(['--source', '5001']); | ||
const { source } = expectProxy(result); | ||
expect(source).toBe(5001); | ||
}); | ||
|
||
test('target (default)', () => { | ||
const { target } = parse([]); | ||
const result = parse([]); | ||
const { target } = expectProxy(result); | ||
expect(target).toBe(9000); | ||
}); | ||
|
||
test('target', () => { | ||
const { target } = parse(['--target', '5000']); | ||
const result = parse(['--target', '5000']); | ||
const { target } = expectProxy(result); | ||
expect(target).toBe(5000); | ||
}); | ||
|
||
test('bindAddress', () => { | ||
const { bindAddress } = parse(['--bindAddress', '0.0.0.0']); | ||
expect(bindAddress).toBe('0.0.0.0'); | ||
}); | ||
|
||
test('config', () => { | ||
const config = parse(['--config', require.resolve('./test-config.json')]); | ||
expect(config).toMatchInlineSnapshot(` | ||
const result = parse(['--config', require.resolve('./test-config.json'),'--bindAddress', '0.0.0.0']); | ||
expect(result.bindAddress).toBe('0.0.0.0'); | ||
|
||
const parsed = expectConfig(result); | ||
expect(parsed.config).toMatchInlineSnapshot(` | ||
{ | ||
"Proxy 1": { | ||
"cert": "/etc/apache2/server.pem", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
v1.17.10 didn't include the
hostname
parameter on the.listen
method, hence the update.