-
Notifications
You must be signed in to change notification settings - Fork 2
[EIM-371] [Test] Add proxy isolation for GUI offline installer #285
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
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 |
|---|---|---|
|
|
@@ -4,18 +4,51 @@ import net from "net"; | |
| import url from "url"; | ||
|
|
||
| class TestProxy { | ||
| constructor({ mode = "log" } = {}) { | ||
| constructor({ mode = "log", blockedDomains = [] } = {}) { | ||
| this.port = 8888; | ||
| this.host = "127.0.0.1"; | ||
| this.mode = mode; | ||
| this.mode = mode; // "log", "block" or "block-list" | ||
| this.attempts = []; | ||
| this.server = null; | ||
| this.blockedDomains = blockedDomains.map((domain) => domain.toLowerCase()); | ||
| } | ||
|
|
||
| matchesBlocked(hostname) { | ||
| if (!hostname) return false; | ||
| const host = hostname.toLowerCase(); | ||
| return this.blockedDomains.some( | ||
| (d) => host === d || host.endsWith("." + d) | ||
| ); | ||
| } | ||
|
|
||
| setEnvironment() { | ||
| logger.info("Setting proxy environment variables"); | ||
| const proxyUrl = "http://127.0.0.1:8888"; | ||
| let environment = { ...process.env }; | ||
| environment.HTTP_PROXY = proxyUrl; | ||
| environment.HTTPS_PROXY = proxyUrl; | ||
| environment.http_proxy = proxyUrl; | ||
| environment.https_proxy = proxyUrl; | ||
| environment.FTP_PROXY = proxyUrl; | ||
| environment.ftp_proxy = proxyUrl; | ||
| environment.NO_PROXY = "127.0.0.1,localhost,::1"; | ||
| environment.no_proxy = "127.0.0.1,localhost,::1"; | ||
| environment.CARGO_HTTP_PROXY = proxyUrl; | ||
| environment.CARGO_HTTPS_PROXY = proxyUrl; | ||
| environment.GIT_PROXY_COMMAND = ""; | ||
| environment.npm_config_proxy = proxyUrl; | ||
| environment.npm_config_https_proxy = proxyUrl; | ||
| environment.PIP_PROXY = proxyUrl; | ||
| process.env = { ...environment }; | ||
| } | ||
|
Comment on lines
24
to
41
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. Replacing the setEnvironment() {
logger.info("Setting proxy environment variables");
const proxyUrl = "http://127.0.0.1:8888";
process.env.HTTP_PROXY = proxyUrl;
process.env.HTTPS_PROXY = proxyUrl;
process.env.http_proxy = proxyUrl;
process.env.https_proxy = proxyUrl;
process.env.FTP_PROXY = proxyUrl;
process.env.ftp_proxy = proxyUrl;
process.env.NO_PROXY = "127.0.0.1,localhost,::1";
process.env.no_proxy = "127.0.0.1,localhost,::1";
process.env.CARGO_HTTP_PROXY = proxyUrl;
process.env.CARGO_HTTPS_PROXY = proxyUrl;
process.env.GIT_PROXY_COMMAND = "";
process.env.npm_config_proxy = proxyUrl;
process.env.npm_config_https_proxy = proxyUrl;
process.env.PIP_PROXY = proxyUrl;
} |
||
|
|
||
| async start() { | ||
| if (this.server) { | ||
| logger.info("Proxy server already running"); | ||
| return; | ||
| } | ||
| logger.info(`Starting proxy server with mode ${this.mode}`); | ||
| if (this.server) return; | ||
|
|
||
| this.setEnvironment(); | ||
| this.server = http.createServer((req, res) => { | ||
| this.attempts.push({ type: "http", method: req.method, url: req.url }); | ||
| logger.info(`New HTTP connection attempt: ${req.url}`); | ||
|
|
@@ -26,7 +59,6 @@ class TestProxy { | |
| logger.debug("HTTP Connection blocked"); | ||
| return; | ||
| } | ||
|
|
||
| const hostHeader = req.headers["host"]; | ||
| let hostname, port; | ||
|
|
||
|
|
@@ -40,6 +72,19 @@ class TestProxy { | |
| port = port || 80; | ||
| } | ||
|
|
||
| if (this.mode === "block-list" && this.matchesBlocked(hostname)) { | ||
| this.attempts.push({ | ||
| type: "http", | ||
| method: req.method, | ||
| url: req.url, | ||
| blocked: true, | ||
| }); | ||
| res.writeHead(503, { "Content-Type": "text/plain" }); | ||
| res.end("Blocked by test proxy (domain block-list)"); | ||
| logger.debug(`HTTP Connection to ${hostname} blocked by domain list`); | ||
| return; | ||
| } | ||
|
|
||
| const options = { | ||
| hostname, | ||
| port, | ||
|
|
@@ -48,6 +93,9 @@ class TestProxy { | |
| headers: req.headers, | ||
| }; | ||
|
|
||
| logger.info(`Proxying request to ${hostname}:${port}${req.url}`); | ||
| logger.info(options); | ||
|
|
||
| const proxyReq = http.request(options, (proxyRes) => { | ||
| res.writeHead(proxyRes.statusCode, proxyRes.headers); | ||
| proxyRes.pipe(res); | ||
|
|
@@ -56,6 +104,7 @@ class TestProxy { | |
| req.pipe(proxyReq); | ||
|
|
||
| proxyReq.on("error", (err) => { | ||
| logger.info("HTTP Proxy request error:", err); | ||
| res.writeHead(500); | ||
| res.end("Proxy error: " + err.message); | ||
| }); | ||
|
|
@@ -74,14 +123,25 @@ class TestProxy { | |
| } | ||
|
|
||
| const [host, port] = req.url.split(":"); | ||
|
|
||
| if (this.mode === "block-list" && this.matchesBlocked(host)) { | ||
| this.attempts.push({ type: "https", host, blocked: true }); | ||
| logger.info(`HTTPS Connection to ${host} blocked by domain list`); | ||
| clientSocket.write("HTTP/1.1 503 Service Unavailable\r\n\r\n"); | ||
| clientSocket.end(); | ||
| return; | ||
| } | ||
| logger.info(`Proxying CONNECT to ${host}:${port}`); | ||
|
|
||
| const serverSocket = net.connect(port || 443, host, () => { | ||
| clientSocket.write("HTTP/1.1 200 Connection Established\r\n\r\n"); | ||
| serverSocket.write(head); | ||
| serverSocket.pipe(clientSocket); | ||
| clientSocket.pipe(serverSocket); | ||
| }); | ||
|
|
||
| serverSocket.on("error", () => { | ||
| serverSocket.on("error", (err) => { | ||
| logger.info("HTTPS Connection error:", err); | ||
| clientSocket.write("HTTP/1.1 500 Connection error\r\n\r\n"); | ||
| clientSocket.end(); | ||
| }); | ||
|
|
||
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.
This refactoring has introduced a critical issue. The
CLITestRunnerinstance is created before the proxy is started in the test scripts. TheCLITestRunnerconstructor cachesprocess.envintothis.environment. Later,TestProxy.start()modifiesprocess.env, but theCLITestRunnerinstance still holds the old, stale environment. Consequently, whentestRunner.start()is called, it spawns a process using the stale environment, and the proxy settings are not applied.To fix this,
CLITestRunnershould get the latest environment when spawning the process. A possible fix is to change thepty.spawncall inside thestartmethod to useprocess.envdirectly instead ofthis.environment.