|
| 1 | +importfsfrom"fs"; |
| 2 | +import{ProxyAgent}from"proxy-agent"; |
| 3 | +import{typeWorkspaceConfiguration}from"vscode"; |
| 4 | +import{getProxyForUrl}from"../proxy"; |
| 5 | +import{expandPath}from"../util"; |
| 6 | + |
| 7 | +/** |
| 8 | + * Return whether the API will need a token for authorization. |
| 9 | + * If mTLS is in use (as specified by the cert or key files being set) then |
| 10 | + * token authorization is disabled. Otherwise, it is enabled. |
| 11 | + */ |
| 12 | +exportfunctionneedToken(cfg:WorkspaceConfiguration):boolean{ |
| 13 | +constcertFile=expandPath( |
| 14 | +String(cfg.get("coder.tlsCertFile")??"").trim(), |
| 15 | +); |
| 16 | +constkeyFile=expandPath(String(cfg.get("coder.tlsKeyFile")??"").trim()); |
| 17 | +return!certFile&&!keyFile; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Create a new HTTP agent based on the current VS Code settings. |
| 22 | + * Configures proxy, TLS certificates, and security options. |
| 23 | + */ |
| 24 | +exportfunctioncreateHttpAgent(cfg:WorkspaceConfiguration):ProxyAgent{ |
| 25 | +constinsecure=Boolean(cfg.get("coder.insecure")); |
| 26 | +constcertFile=expandPath( |
| 27 | +String(cfg.get("coder.tlsCertFile")??"").trim(), |
| 28 | +); |
| 29 | +constkeyFile=expandPath(String(cfg.get("coder.tlsKeyFile")??"").trim()); |
| 30 | +constcaFile=expandPath(String(cfg.get("coder.tlsCaFile")??"").trim()); |
| 31 | +constaltHost=expandPath(String(cfg.get("coder.tlsAltHost")??"").trim()); |
| 32 | + |
| 33 | +returnnewProxyAgent({ |
| 34 | +// Called each time a request is made. |
| 35 | +getProxyForUrl:(url:string)=>{ |
| 36 | +returngetProxyForUrl( |
| 37 | +url, |
| 38 | +cfg.get("http.proxy"), |
| 39 | +cfg.get("coder.proxyBypass"), |
| 40 | +); |
| 41 | +}, |
| 42 | +cert:certFile==="" ?undefined :fs.readFileSync(certFile), |
| 43 | +key:keyFile==="" ?undefined :fs.readFileSync(keyFile), |
| 44 | +ca:caFile==="" ?undefined :fs.readFileSync(caFile), |
| 45 | +servername:altHost==="" ?undefined :altHost, |
| 46 | +// rejectUnauthorized defaults to true, so we need to explicitly set it to |
| 47 | +// false if we want to allow self-signed certificates. |
| 48 | +rejectUnauthorized:!insecure, |
| 49 | +}); |
| 50 | +} |