Fix electron SSL
This commit is contained in:
@@ -290,63 +290,58 @@ ipcMain.handle("save-server-config", (event, config) => {
|
|||||||
|
|
||||||
ipcMain.handle("test-server-connection", async (event, serverUrl) => {
|
ipcMain.handle("test-server-connection", async (event, serverUrl) => {
|
||||||
try {
|
try {
|
||||||
let fetch;
|
const https = require("https");
|
||||||
try {
|
const http = require("http");
|
||||||
fetch = globalThis.fetch || require("node-fetch");
|
const { URL } = require("url");
|
||||||
} catch (e) {
|
|
||||||
const https = require("https");
|
|
||||||
const http = require("http");
|
|
||||||
const { URL } = require("url");
|
|
||||||
|
|
||||||
fetch = (url, options = {}) => {
|
const fetch = (url, options = {}) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const urlObj = new URL(url);
|
const urlObj = new URL(url);
|
||||||
const isHttps = urlObj.protocol === "https:";
|
const isHttps = urlObj.protocol === "https:";
|
||||||
const client = isHttps ? https : http;
|
const client = isHttps ? https : http;
|
||||||
|
|
||||||
const requestOptions = {
|
const requestOptions = {
|
||||||
method: options.method || "GET",
|
method: options.method || "GET",
|
||||||
headers: options.headers || {},
|
headers: options.headers || {},
|
||||||
timeout: options.timeout || 10000,
|
timeout: options.timeout || 10000,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (isHttps) {
|
if (isHttps) {
|
||||||
requestOptions.rejectUnauthorized = false;
|
requestOptions.rejectUnauthorized = false;
|
||||||
requestOptions.agent = new https.Agent({
|
requestOptions.agent = new https.Agent({
|
||||||
rejectUnauthorized: false,
|
rejectUnauthorized: false,
|
||||||
secureProtocol: 'TLSv1_2_method',
|
secureProtocol: 'TLSv1_2_method',
|
||||||
checkServerIdentity: () => undefined,
|
checkServerIdentity: () => undefined,
|
||||||
ciphers: 'ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH',
|
ciphers: 'ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH',
|
||||||
honorCipherOrder: true,
|
honorCipherOrder: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const req = client.request(url, requestOptions, (res) => {
|
const req = client.request(url, requestOptions, (res) => {
|
||||||
let data = "";
|
let data = "";
|
||||||
res.on("data", (chunk) => (data += chunk));
|
res.on("data", (chunk) => (data += chunk));
|
||||||
res.on("end", () => {
|
res.on("end", () => {
|
||||||
resolve({
|
resolve({
|
||||||
ok: res.statusCode >= 200 && res.statusCode < 300,
|
ok: res.statusCode >= 200 && res.statusCode < 300,
|
||||||
status: res.statusCode,
|
status: res.statusCode,
|
||||||
text: () => Promise.resolve(data),
|
text: () => Promise.resolve(data),
|
||||||
json: () => Promise.resolve(JSON.parse(data)),
|
json: () => Promise.resolve(JSON.parse(data)),
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
req.on("error", reject);
|
|
||||||
req.on("timeout", () => {
|
|
||||||
req.destroy();
|
|
||||||
reject(new Error("Request timeout"));
|
|
||||||
});
|
|
||||||
|
|
||||||
if (options.body) {
|
|
||||||
req.write(options.body);
|
|
||||||
}
|
|
||||||
req.end();
|
|
||||||
});
|
});
|
||||||
};
|
|
||||||
}
|
req.on("error", reject);
|
||||||
|
req.on("timeout", () => {
|
||||||
|
req.destroy();
|
||||||
|
reject(new Error("Request timeout"));
|
||||||
|
});
|
||||||
|
|
||||||
|
if (options.body) {
|
||||||
|
req.write(options.body);
|
||||||
|
}
|
||||||
|
req.end();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const normalizedServerUrl = serverUrl.replace(/\/$/, "");
|
const normalizedServerUrl = serverUrl.replace(/\/$/, "");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user