136 lines
3.2 KiB
JavaScript
136 lines
3.2 KiB
JavaScript
"use strict";
|
|
|
|
let sendAuth;
|
|
const credential = {
|
|
username: "",
|
|
password: "",
|
|
};
|
|
|
|
const colors = {
|
|
Green: [0, 128, 0, 255],
|
|
Red: [255, 0, 0, 255],
|
|
Orange: [255, 165, 0, 255],
|
|
};
|
|
|
|
function setProxy(proxy) {
|
|
const requestProxy = {
|
|
type: proxy.type,
|
|
host: proxy.host,
|
|
port: proxy.port,
|
|
proxyDNS: ["socks", "socks4"].includes(proxy.type),
|
|
};
|
|
|
|
// delete sendAuth old
|
|
if (typeof sendAuth === "function") {
|
|
browser.webRequest.onAuthRequired.removeListener(sendAuth);
|
|
}
|
|
|
|
if (proxy.username) {
|
|
requestProxy.username = credential.username = proxy.username;
|
|
requestProxy.password = credential.password = proxy.password;
|
|
sendAuth = async () => {
|
|
return {
|
|
authCredentials: credential,
|
|
};
|
|
};
|
|
browser.webRequest.onAuthRequired.addListener(
|
|
sendAuth,
|
|
{urls: ["<all_urls>"]},
|
|
["blocking"]
|
|
);
|
|
}
|
|
|
|
// final handle
|
|
browser.proxy.onRequest.addListener(
|
|
(requestInfo) => {
|
|
return requestProxy;
|
|
},
|
|
{urls: ["<all_urls>"]}
|
|
);
|
|
}
|
|
|
|
function setStorage(proxy) {
|
|
chrome.storage.sync.set({proxy: proxy}, function () {
|
|
console.log("Settings saved");
|
|
});
|
|
}
|
|
|
|
async function getStorage() {
|
|
const promise = new Promise((res, rej) => {
|
|
chrome.storage.sync.get(["proxy"], function (values) {
|
|
res(values);
|
|
});
|
|
});
|
|
return promise;
|
|
}
|
|
|
|
function switchBadge(first, second) {
|
|
chrome.browserAction.getBadgeText({}, function (color) {
|
|
if (color === first) {
|
|
browser.browserAction.setBadgeText({text: second});
|
|
} else {
|
|
browser.browserAction.setBadgeText({text: first});
|
|
}
|
|
});
|
|
}
|
|
|
|
function intervalSetProxy(interval = 10000) {
|
|
if (!interval) {
|
|
interval = 10000;
|
|
}
|
|
|
|
if (typeof _intervalSetProxy === "number") {
|
|
clearInterval(_intervalSetProxy);
|
|
}
|
|
|
|
var _intervalSetProxy = setInterval(() => {
|
|
getStorage().then((values) => {
|
|
if (values?.proxy) {
|
|
console.count(values?.proxy.password);
|
|
switchBadge('P1', 'P2');
|
|
setProxy(values?.proxy);
|
|
} else {
|
|
clearInterval(_intervalSetProxy);
|
|
}
|
|
});
|
|
}, interval);
|
|
}
|
|
|
|
document.addEventListener("DOMContentLoaded", function () {
|
|
browser.browserAction.setBadgeTextColor({color: "#fff"});
|
|
|
|
const socket = io("https://seotool.nswteam.net");
|
|
socket.on("connect", function () {
|
|
browser.browserAction.setBadgeText({text: "IO"});
|
|
browser.browserAction.setBadgeBackgroundColor({color: colors.Green});
|
|
intervalSetProxy();
|
|
console.log("socket.io connected");
|
|
});
|
|
|
|
socket.on("disconnect", function () {
|
|
browser.browserAction.setBadgeText({text: "IO"});
|
|
browser.browserAction.setBadgeBackgroundColor({color: colors.Red});
|
|
|
|
console.log("socket.io disconnected");
|
|
});
|
|
|
|
socket.emit("join", {join: "set_proxy"});
|
|
// TODO get message set proxy
|
|
socket.on("message", function (message) {
|
|
/**
|
|
* {
|
|
* browser_name: ...
|
|
* type: socks | socks4, http, https,
|
|
* host: '127.0.0.1',
|
|
* port: 80,
|
|
* username?: ''
|
|
* password?: ''
|
|
* proxyDNS: ['socks', 'socks4'].includes(message.type)
|
|
* }
|
|
*/
|
|
console.log({message});
|
|
setStorage(message);
|
|
intervalSetProxy();
|
|
});
|
|
}); // end ready
|