@@ -33,6 +33,8 @@ import {
3333import { OpenIDConnectAuth } from './oidc_auth.js' ;
3434import WebSocket from 'isomorphic-ws' ;
3535import child_process from 'node:child_process' ;
36+ import { SocksProxyAgent } from 'socks-proxy-agent' ;
37+ import { HttpProxyAgent , HttpProxyAgentOptions , HttpsProxyAgent , HttpsProxyAgentOptions } from 'hpagent' ;
3638
3739const SERVICEACCOUNT_ROOT : string = '/var/run/secrets/kubernetes.io/serviceaccount' ;
3840const SERVICEACCOUNT_CA_PATH : string = SERVICEACCOUNT_ROOT + '/ca.crt' ;
@@ -171,6 +173,7 @@ export class KubeConfig implements SecurityAuthentication {
171173
172174 public async applyToHTTPSOptions ( opts : https . RequestOptions | WebSocket . ClientOptions ) : Promise < void > {
173175 const user = this . getCurrentUser ( ) ;
176+ const cluster = this . getCurrentCluster ( ) ;
174177
175178 await this . applyOptions ( opts ) ;
176179
@@ -205,7 +208,7 @@ export class KubeConfig implements SecurityAuthentication {
205208 agentOptions . secureProtocol = opts . secureProtocol ;
206209 agentOptions . sessionIdContext = opts . sessionIdContext ;
207210
208- opts . agent = new https . Agent ( agentOptions ) ;
211+ opts . agent = this . createAgent ( cluster , agentOptions ) ;
209212 }
210213
211214 /**
@@ -248,7 +251,7 @@ export class KubeConfig implements SecurityAuthentication {
248251 agentOptions . passphrase = httpsOptions . passphrase ;
249252 agentOptions . rejectUnauthorized = httpsOptions . rejectUnauthorized ;
250253
251- context . setAgent ( new https . Agent ( agentOptions ) ) ;
254+ context . setAgent ( this . createAgent ( cluster , agentOptions ) ) ;
252255 }
253256
254257 /**
@@ -509,6 +512,32 @@ export class KubeConfig implements SecurityAuthentication {
509512 return this . getContextObject ( this . currentContext ) ;
510513 }
511514
515+ private createAgent (
516+ cluster : Cluster | null ,
517+ agentOptions : https . AgentOptions ,
518+ ) : https . Agent | SocksProxyAgent | HttpProxyAgent | HttpsProxyAgent {
519+ let agent : https . Agent | SocksProxyAgent | HttpProxyAgent | HttpsProxyAgent ;
520+
521+ if ( cluster && cluster . proxyUrl ) {
522+ if ( cluster . proxyUrl . startsWith ( 'socks' ) ) {
523+ agent = new SocksProxyAgent ( cluster . proxyUrl , agentOptions ) ;
524+ } else if ( cluster . server . startsWith ( 'https' ) ) {
525+ const httpsProxyAgentOptions : HttpsProxyAgentOptions = agentOptions as HttpsProxyAgentOptions ;
526+ httpsProxyAgentOptions . proxy = cluster . proxyUrl ;
527+ agent = new HttpsProxyAgent ( httpsProxyAgentOptions ) ;
528+ } else if ( cluster . server . startsWith ( 'http' ) ) {
529+ const httpProxyAgentOptions : HttpProxyAgentOptions = agentOptions as HttpProxyAgentOptions ;
530+ httpProxyAgentOptions . proxy = cluster . proxyUrl ;
531+ agent = new HttpProxyAgent ( httpProxyAgentOptions ) ;
532+ } else {
533+ throw new Error ( 'Unsupported proxy type' ) ;
534+ }
535+ } else {
536+ agent = new https . Agent ( agentOptions ) ;
537+ }
538+ return agent ;
539+ }
540+
512541 private applyHTTPSOptions ( opts : https . RequestOptions | WebSocket . ClientOptions ) : void {
513542 const cluster = this . getCurrentCluster ( ) ;
514543 const user = this . getCurrentUser ( ) ;
0 commit comments