82 lines
2.1 KiB
JavaScript
82 lines
2.1 KiB
JavaScript
"use strict";
|
|
|
|
let sendAuth;
|
|
const credential = {
|
|
username: "",
|
|
password: "",
|
|
};
|
|
|
|
document.addEventListener("DOMContentLoaded", function () {
|
|
const socket = io("https://seotool.nswteam.net");
|
|
socket.on("connect", function () {
|
|
browser.browserAction.setBadgeText({text: "IO"});
|
|
browser.browserAction.setBadgeTextColor({color: "#fff"});
|
|
browser.browserAction.setBadgeBackgroundColor({color: "green"});
|
|
console.log("socket.io connected");
|
|
});
|
|
|
|
socket.on("disconnect", function () {
|
|
browser.browserAction.setBadgeText({text: "IO"});
|
|
browser.browserAction.setBadgeTextColor({color: "#fff"});
|
|
browser.browserAction.setBadgeBackgroundColor({color: "red"});
|
|
console.log("socket.io disconnected");
|
|
});
|
|
|
|
socket.emit("join", {join: "set_proxy"});
|
|
socket.on("message", function (message) {
|
|
console.log({message});
|
|
chrome.runtime.sendMessage({
|
|
to: "content",
|
|
data: {
|
|
socket_message: message,
|
|
},
|
|
});
|
|
/**
|
|
* {
|
|
* browser_name: ...
|
|
* type: socks | socks4, http, https,
|
|
* host: '127.0.0.1',
|
|
* port: 80,
|
|
* username?: ''
|
|
* password?: ''
|
|
* proxyDNS: ['socks', 'socks4'].includes(message.type)
|
|
* }
|
|
*/
|
|
const proxyDNS = ["socks", "socks4"].includes(message.type);
|
|
const proxy = {
|
|
type: message.type,
|
|
host: message.host,
|
|
port: message.port,
|
|
proxyDNS: proxyDNS,
|
|
};
|
|
|
|
// delete sendAuth old
|
|
if (typeof sendAuth === "function") {
|
|
browser.webRequest.onAuthRequired.removeListener(sendAuth);
|
|
}
|
|
|
|
if (message.username) {
|
|
proxy.username = credential.username = message.username;
|
|
proxy.password = credential.password = message.password;
|
|
sendAuth = async () => {
|
|
return {
|
|
authCredentials: credential,
|
|
};
|
|
};
|
|
browser.webRequest.onAuthRequired.addListener(
|
|
sendAuth,
|
|
{urls: ["<all_urls>"]},
|
|
["blocking"]
|
|
);
|
|
}
|
|
|
|
// final handle
|
|
browser.proxy.onRequest.addListener(
|
|
(requestInfo) => {
|
|
return proxy;
|
|
},
|
|
{urls: ["<all_urls>"]}
|
|
);
|
|
});
|
|
}); // end ready
|