Skip to content

Commit aeb2a04

Browse files
author
chenjianhua
committed
Pass http proxy as env to command (fix #136, #117)
We use leetcode-cli and leetcode-cli uses request to call LeetCode web API. It's lucky that request respects the http proxy env variables(http_proxy, https_proxy), so all we need to do is passing proxy as env variable http_proxy to every command. TODO: do we need to set https_proxy for https request?
1 parent b781eef commit aeb2a04

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/leetCodeManager.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { leetCodeExecutor } from "./leetCodeExecutor";
99
import { UserStatus } from "./shared";
1010
import { DialogType, promptForOpenOutputChannel } from "./utils/uiUtils";
1111
import * as wsl from "./utils/wslUtils";
12+
import { createEnvOption } from "./utils/workspaceUtils";
1213

1314
class LeetCodeManager extends EventEmitter {
1415
private currentUser: string | undefined;
@@ -42,7 +43,10 @@ class LeetCodeManager extends EventEmitter {
4243

4344
const childProc: cp.ChildProcess = wsl.useWsl()
4445
? cp.spawn("wsl", ["node", leetCodeBinaryPath, "user", "-l"], { shell: true })
45-
: cp.spawn("node", [leetCodeBinaryPath, "user", "-l"], { shell: true });
46+
: cp.spawn("node", [leetCodeBinaryPath, "user", "-l"], {
47+
shell: true,
48+
env: createEnvOption()
49+
})
4650

4751
childProc.stdout.on("data", (data: string | Buffer) => {
4852
data = data.toString();

src/utils/cpUtils.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
import * as cp from "child_process";
55
import * as vscode from "vscode";
66
import { leetCodeChannel } from "../leetCodeChannel";
7+
import { createEnvOption } from "./workspaceUtils";
78

89
export async function executeCommand(command: string, args: string[], options: cp.SpawnOptions = { shell: true }): Promise<string> {
910
return new Promise((resolve: (res: string) => void, reject: (e: Error) => void): void => {
1011
let result: string = "";
1112

12-
const childProc: cp.ChildProcess = cp.spawn(command, args, options);
13+
const childProc: cp.ChildProcess = cp.spawn(command, args, { ...options, env: createEnvOption() });
1314

1415
childProc.stdout.on("data", (data: string | Buffer) => {
1516
data = data.toString();

src/utils/workspaceUtils.ts

+16
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,19 @@ export async function getActiveFilePath(uri?: vscode.Uri): Promise<string | unde
4444
export function getWorkspaceConfiguration(): vscode.WorkspaceConfiguration {
4545
return vscode.workspace.getConfiguration("leetcode");
4646
}
47+
48+
function getHttpAgent() {
49+
const proxy = vscode.workspace.getConfiguration("http").get("proxy");
50+
return proxy;
51+
}
52+
53+
// clone process.env and add http proxy
54+
export function createEnvOption() {
55+
const proxy = getHttpAgent();
56+
if (proxy) {
57+
const env = Object.create(process.env);
58+
env.http_proxy = proxy;
59+
return env;
60+
}
61+
return process.env;
62+
}

0 commit comments

Comments
 (0)