Skip to content

Commit 33175a8

Browse files
committed
add proxy support
1 parent 5d7214d commit 33175a8

File tree

8 files changed

+185
-17
lines changed

8 files changed

+185
-17
lines changed

package-lock.json

Lines changed: 131 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363
"stream-buffers": "^3.0.2",
6464
"tar": "^7.0.0",
6565
"tslib": "^2.4.1",
66-
"ws": "^8.18.0"
66+
"ws": "^8.18.0",
67+
"socks-proxy-agent": "^8.0.4"
6768
},
6869
"devDependencies": {
6970
"@types/byline": "^4.2.31",

src/config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import path = require('path');
77

88
import request = require('request');
99
import WebSocket = require('ws');
10+
import SocksProxyAgent = require('socks-proxy-agent');
1011

1112
import * as api from './api';
1213
import { Authenticator } from './auth';
@@ -170,6 +171,14 @@ export class KubeConfig {
170171
if (cluster && cluster.tlsServerName) {
171172
opts.agentOptions = { servername: cluster.tlsServerName } as https.AgentOptions;
172173
}
174+
175+
if (cluster?.proxyUrl) {
176+
if (cluster.proxyUrl.startsWith('socks')) {
177+
opts.agent = new SocksProxyAgent.SocksProxyAgent(cluster.proxyUrl);
178+
} else {
179+
opts.proxy = cluster.proxyUrl;
180+
}
181+
}
173182
}
174183

175184
public loadFromString(config: string, opts?: Partial<ConfigOptions>): void {

src/config_test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function validateFileLoad(kc: KubeConfig) {
3636
expect(cluster1.name).to.equal('cluster1');
3737
expect(cluster1.caData).to.equal('Q0FEQVRB');
3838
expect(cluster1.server).to.equal('http://example.com');
39+
expect(cluster1.proxyUrl).to.equal('socks5://localhost:1181');
3940
expect(cluster2.name).to.equal('cluster2');
4041
expect(cluster2.caData).to.equal('Q0FEQVRBMg==');
4142
expect(cluster2.server).to.equal('http://example2.com');
@@ -403,6 +404,33 @@ describe('KubeConfig', () => {
403404
rejectUnauthorized: false,
404405
});
405406
});
407+
it('should apply proxy to request.Options', async () => {
408+
const kc = new KubeConfig();
409+
kc.loadFromFile(kc2FileName);
410+
411+
const opts = {} as requestlib.Options;
412+
413+
await kc.applyToRequest(opts);
414+
console.log(opts);
415+
416+
expect(opts).to.deep.equal({
417+
headers: {},
418+
ca: Buffer.from('CADAT@', 'utf-8'),
419+
cert: Buffer.from(']SER_CADATA', 'utf-8'),
420+
key: Buffer.from(']SER_CKDATA', 'utf-8'),
421+
proxy: 'https://localhost:1181',
422+
});
423+
});
424+
it('should apply agent to request.Options', async () => {
425+
const kc = new KubeConfig();
426+
kc.loadFromFile(kcFileName);
427+
428+
const opts = {} as requestlib.Options;
429+
430+
await kc.applyToRequest(opts);
431+
432+
expect(opts.agent).to.exist;
433+
});
406434
});
407435

408436
describe('loadClusterConfigObjects', () => {

src/config_types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface Cluster {
2222
readonly server: string;
2323
readonly skipTLSVerify?: boolean;
2424
readonly tlsServerName?: string;
25+
readonly proxyUrl?: string;
2526
}
2627

2728
export function newClusters(a: any, opts?: Partial<ConfigOptions>): Cluster[] {
@@ -43,6 +44,7 @@ export function exportCluster(cluster: Cluster): any {
4344
'certificate-authority': cluster.caFile,
4445
'insecure-skip-tls-verify': cluster.skipTLSVerify,
4546
'tls-server-name': cluster.tlsServerName,
47+
'proxy-url': cluster.proxyUrl,
4648
},
4749
};
4850
}
@@ -68,6 +70,7 @@ function clusterIterator(
6870
server: elt.cluster.server.replace(/\/$/, ''),
6971
skipTLSVerify: elt.cluster['insecure-skip-tls-verify'] === true,
7072
tlsServerName: elt.cluster['tls-server-name'],
73+
proxyUrl: elt.cluster['proxy-url'],
7174
};
7275
} catch (err) {
7376
switch (onInvalidEntry) {

src/web-socket-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export class WebSocketHandler implements WebSocketInterface {
126126
if (data instanceof Buffer) {
127127
data.copy(buff, 1);
128128
} else {
129-
buff.write(data, 1);
129+
buff.write(data as string, 1);
130130
}
131131

132132
let i = 0;

testdata/kubeconfig-2.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
apiVersion: v1
22
clusters:
33
- cluster:
4-
certificate-authority-data: Q0FEQVRA
4+
certificate-authority-data: Q0FEQVRA
55
server: http://example2.com
6+
proxy-url: https://localhost:1181
67
name: clusterA
78

89
contexts:
@@ -27,4 +28,3 @@ users:
2728
user:
2829
username: foo
2930
password: bar
30-

testdata/kubeconfig.yaml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
apiVersion: v1
22
clusters:
33
- cluster:
4-
certificate-authority-data: Q0FEQVRB
5-
server: http://example.com
6-
name: cluster1
4+
certificate-authority-data: Q0FEQVRB
5+
server: http://example.com
6+
proxy-url: socks5://localhost:1181
7+
name: cluster1
78
- cluster:
89
certificate-authority-data: Q0FEQVRBMg==
910
server: http://example2.com
1011
insecure-skip-tls-verify: true
12+
proxy-url: socks5://example2:8888
1113
name: cluster2
1214

1315
contexts:
1416
- context:
15-
cluster: cluster1
17+
cluster: cluster1
1618
user: user1
17-
name: context1
19+
name: context1
1820
- context:
1921
cluster: cluster2
2022
namespace: namespace2
@@ -25,7 +27,7 @@ contexts:
2527
user: user3
2628
name: passwd
2729

28-
current-context: context2
30+
current-context: context2
2931
kind: Config
3032
preferences: {}
3133
users:
@@ -40,4 +42,4 @@ users:
4042
- name: user3
4143
user:
4244
username: foo
43-
password: bar
45+
password: bar

0 commit comments

Comments
 (0)