Route GitLab stats and health checks through a configurable proxy
This commit is contained in:
+48
-31
@@ -15,6 +15,7 @@ const https = require("https");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const { URL } = require("url");
|
||||
const { proxyFor, connectThroughProxy, proxiedRequestOptions } = require("./proxy");
|
||||
|
||||
const OUT_FILE = process.env.OUT_FILE || "/usr/share/nginx/html/health.json";
|
||||
const TIMEOUT = parseInt(process.env.HEALTH_TIMEOUT || "5000", 10);
|
||||
@@ -51,40 +52,56 @@ function checkUrl(url) {
|
||||
resolve(result);
|
||||
};
|
||||
|
||||
let req;
|
||||
try {
|
||||
const u = new URL(url);
|
||||
const client = u.protocol === "https:" ? https : http;
|
||||
req = client.request(
|
||||
u,
|
||||
{
|
||||
(async () => {
|
||||
let req;
|
||||
try {
|
||||
const u = new URL(url);
|
||||
const client = u.protocol === "https:" ? https : http;
|
||||
const options = {
|
||||
method: "GET",
|
||||
agent: u.protocol === "https:" ? httpsAgent : httpAgent,
|
||||
timeout: TIMEOUT,
|
||||
},
|
||||
(res) => {
|
||||
const code = res.statusCode;
|
||||
res.destroy(); // response headers are all we need
|
||||
done({
|
||||
status: code >= 500 ? "error" : "online",
|
||||
code,
|
||||
latencyMs: Date.now() - started,
|
||||
error: null,
|
||||
});
|
||||
}
|
||||
);
|
||||
} catch (e) {
|
||||
return done({ status: "offline", code: null, latencyMs: null, error: e.message });
|
||||
}
|
||||
};
|
||||
|
||||
req.on("timeout", () => {
|
||||
req.destroy();
|
||||
done({ status: "offline", code: null, latencyMs: null, error: "Timeout" });
|
||||
});
|
||||
req.on("error", (e) => {
|
||||
done({ status: "offline", code: null, latencyMs: null, error: e.message });
|
||||
});
|
||||
req.end();
|
||||
// Honor the outbound proxy (GITLAB_PROXY) so the GitLab badge checks
|
||||
// the same path the git stats actually take (see api/proxy.js).
|
||||
const proxy = proxyFor(url);
|
||||
if (proxy) {
|
||||
let socket = null;
|
||||
if (u.protocol === "https:") {
|
||||
socket = await connectThroughProxy(proxy, url, TIMEOUT, { rejectUnauthorized: false });
|
||||
}
|
||||
Object.assign(options, proxiedRequestOptions(proxy, url, socket));
|
||||
} else {
|
||||
options.agent = u.protocol === "https:" ? httpsAgent : httpAgent;
|
||||
}
|
||||
|
||||
req = client.request(
|
||||
u,
|
||||
options,
|
||||
(res) => {
|
||||
const code = res.statusCode;
|
||||
res.destroy(); // response headers are all we need
|
||||
done({
|
||||
status: code >= 500 ? "error" : "online",
|
||||
code,
|
||||
latencyMs: Date.now() - started,
|
||||
error: null,
|
||||
});
|
||||
}
|
||||
);
|
||||
} catch (e) {
|
||||
return done({ status: "offline", code: null, latencyMs: null, error: e.message });
|
||||
}
|
||||
|
||||
req.on("timeout", () => {
|
||||
req.destroy();
|
||||
done({ status: "offline", code: null, latencyMs: null, error: "Timeout" });
|
||||
});
|
||||
req.on("error", (e) => {
|
||||
done({ status: "offline", code: null, latencyMs: null, error: e.message });
|
||||
});
|
||||
req.end();
|
||||
})();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user