Skip to content

Commit

Permalink
feat: add macos all proxy system
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-AshishRanjan committed Jan 21, 2024
1 parent e510809 commit fff0461
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 79 deletions.
20 changes: 15 additions & 5 deletions src/app/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
}

:root {
--primary-1: #03001c;
--primary-1: #0d121c;
--primary-2: #301e67;
--card: #301e678d;
--primary-3: #5b8fb9;
--primary-3: #9c83ea;
--primary-3: #0c34d6;
--primary-3-light: #9c83ea;
--primary-4: #b6eada;
--sidenav: #03001cce;
--gradient: #1c2f4b;
Expand All @@ -35,7 +36,8 @@
--primary-1: #f2f1ef;
--primary-2: #d9b18e;
--card: #d9b18e8d;
--primary-3: #a67564;
--primary-3: #8b6254;
--primary-3-light: #68493f;
--primary-4: #723e31;
--sidenav: #bce3ffce;
--gradient: #d9d2cc;
Expand Down Expand Up @@ -218,7 +220,7 @@ textarea,
outline: none;
border: 1px solid var(--primary-2);
background: var(--primary-2);
color: var(--primary-3);
color: var(--primary-3-light);
resize: none;
width: 15rem;
}
Expand Down Expand Up @@ -311,12 +313,14 @@ input[type=number] {
border-radius: 0.5rem;
position: absolute;
top: 0;
background: var(--primary-2);
color: var(--primary-3-light);
/* background: var(--primary-2); */
transform: translateY(-100%);
text-align: center;

span {
color: var(--primary-1);
color: var(--primary-4);
font-weight: 400;
}
}
Expand All @@ -330,6 +334,7 @@ input[type=number] {
justify-content: center;
z-index: 5;
background: var(--primary-2);
color: var(--primary-3-light);
cursor: pointer;
/* animation: 1.5s pulse infinite ease-in-out; */
}
Expand All @@ -346,13 +351,18 @@ input[type=number] {
}

/* notice */
.noticeHead {
color: var(--primary-3-light) !important;
}

.noticeList {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
gap: 1rem;
width: 100%;
color: var(--primary-3-light) !important;

.notice {
width: 100%;
Expand Down
2 changes: 1 addition & 1 deletion src/app/notice.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<title>Proxy Manager | Notice</title>
</head>
<body>
<h1>Notice</h1>
<h1 class="noticeHead">Notice</h1>
<div class="body noticeList">
<div class="spinner"></div>
</div>
Expand Down
208 changes: 135 additions & 73 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const menu = [
{
label: "About",
click: () => createDynamicWindow("about.html"),
accelerator: "CmdOrCtrl+A",
accelerator: "CmdOrCtrl+B",
},
{
label: "Notice",
Expand All @@ -135,7 +135,7 @@ const menu = [
{
label: "Contact Us",
click: () => createDynamicWindow("contact.html"),
accelerator: "CmdOrCtrl+C",
accelerator: "CmdOrCtrl+M",
},
{
type: "separator",
Expand Down Expand Up @@ -186,7 +186,7 @@ const menu = [
{
label: "About",
click: () => createDynamicWindow("about.html"),
accelerator: "CmdOrCtrl+A",
accelerator: "CmdOrCtrl+B",
},
{
label: "Notice",
Expand All @@ -196,7 +196,7 @@ const menu = [
{
label: "Contact Us",
click: () => createDynamicWindow("contact.html"),
accelerator: "CmdOrCtrl+C",
accelerator: "CmdOrCtrl+M",
},
{
type: "separator",
Expand Down Expand Up @@ -229,7 +229,7 @@ const menu = [
},
]
: []),
...(isLinux || isMac
...(isLinux
? [
{
label: "More",
Expand Down Expand Up @@ -434,7 +434,11 @@ ipcMain.on("proxy:set", (e, options) => {
);
// setProxyForVSCode(`https://${options.ipAddress}:${options.port}`);
setProxyForPip(`https://${options.ipAddress}:${options.port}`);
setSystemEnvironmentVariables(`http://${options.ipAddress}:${options.port}`);
if (!isLinux && !isMac) {
setSystemEnvironmentVariables(
`http://${options.ipAddress}:${options.port}`
);
}
// for linux manage all proxy with sudo
mainWindow.webContents
.executeJavaScript('localStorage.getItem("proxyManagerSudo");', true)
Expand All @@ -444,26 +448,35 @@ ipcMain.on("proxy:set", (e, options) => {
result !== null && isLinux
? await setLinuxAllProxyPrompt(options.ipAddress, options.port)
: null;
result !== null && isMac
? await setMacAllProxyPrompt(options.ipAddress, options.port)
: null;
});

if (isMac) {
setMacAllProxy(
`http://${options.ipAddress}:${options.port}`,
options.ipAddress,
options.port
);
}
});
ipcMain.on("proxy:unset", (e, options) => {
console.log(options);
unsetProxy();
// unsetProxyForVSCode();
unsetProxyForPip();
unsetSystemEnvironmentVariables();
if (!isLinux && !isMac) {
unsetSystemEnvironmentVariables();
}
// for linux manage all proxy with sudo
mainWindow.webContents
.executeJavaScript('localStorage.getItem("proxyManagerSudo");', true)
.then(async (result) => {
console.log({ result });
proxyManagerSudo = result;
result !== null && isLinux ? await unsetLinuxAllProxyPrompt() : null;
result !== null && isMac ? await unsetMacAllProxyPrompt() : null;
});
if (isMac) {
unsetMacAllProxy();
}
});
ipcMain.on("proxy:check", (e, options) => {
console.log(options);
Expand Down Expand Up @@ -550,8 +563,8 @@ async function setProxy(proxyServer, host, port) {
allCommands.push(`gsettings set org.gnome.system.proxy.https port ${port}`);
} else if (process.platform === "darwin") {
// macOS
allCommands.push(`networksetup -setwebproxy Wi-Fi ${host} ${port}`);
allCommands.push(`networksetup -setsecurewebproxy Wi-Fi ${host} ${port}`);
allCommands.push(`networksetup -setwebproxy "Wi-Fi" ${host} ${port}`);
allCommands.push(`networksetup -setsecurewebproxy "Wi-Fi" ${host} ${port}`);
} else {
console.error("Unsupported operating system");
return;
Expand Down Expand Up @@ -631,8 +644,8 @@ async function unsetProxy() {
allCommands.push('gsettings set org.gnome.system.proxy mode "none"');
} else if (process.platform === "darwin") {
// macOS
allCommands.push('networksetup -setwebproxy Wi-Fi "" 0');
allCommands.push('networksetup -setsecurewebproxy Wi-Fi "" 0');
allCommands.push('networksetup -setwebproxy "Wi-Fi" "" ""');
allCommands.push('networksetup -setsecurewebproxy "Wi-Fi" "" ""');
} else {
console.error("Unsupported operating system");
return;
Expand Down Expand Up @@ -1042,66 +1055,115 @@ ${commandsApt}`,
// });
}

async function setMacAllProxyPrompt(host, port) {
const proxyserver = `http://${host}:${port}`;
const commandsEnv = `
http_proxy=${proxyserver}
https_proxy=${proxyserver}
ftp_proxy=${proxyserver}
no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com,127.0.0.0/8,::1"
HTTP_PROXY=${proxyserver}
HTTPS_PROXY=${proxyserver}
FTP_PROXY=${proxyserver}
NO_PROXY="localhost,127.0.0.1,localaddress,.localdomain.com,127.0.0.0/8,::1"
ALL_PROXY=${proxyserver}
all_proxy=${proxyserver}
`;
const cmd = `tee -a /etc/environment << EOF
${commandsEnv}
EOF`;
MacEnvCmd =
'/usr/bin/osascript -e \'do shell script "bash -c \\"' +
cmd +
'\\"" with administrator privileges\'';

exec(MacEnvCmd, (error, stdout, stderr) => {
if (error) {
console.error("Got an Error:", stderr);
// alert(stderr);
mainWindow.webContents.send("proxy:warning", {
msg: "Sudo implementation failed",
});
return;
}
console.log({ stdout });
console.log("Command executed successfully for environment variables.");
mainWindow.webContents.send("proxy:success", {
msg: "Success: Set env using sudo",
});
async function setMacAllProxy(proxyServer, host, port) {
console.log(proxyServer);
const allCommands = [
`npm set proxy ${proxyServer}`,
`npm set https-proxy ${proxyServer}`,
`networksetup -setmanual "USB 10/100/1000 LAN" 192.168.212.82 255.255.240.0 192.168.208.1`,
`networksetup -setwebproxy "USB 10/100/1000 LAN" ${host} ${port}`,
`networksetup -setsecurewebproxy "USB 10/100/1000 LAN" ${host} ${port}`,
];
allCommands.map((command) =>
exec(command, (error, stdout, stderr) => {
if (error) {
console.error("Got an Error : ", stderr);
return;
}
console.log({ stdout });
console.log(`command executed successfully: ${command}`);
})
);
mainWindow.webContents.send("proxy:success", {
msg: `success : set system env, npm`,
});
}

async function unsetMacAllProxyPrompt() {
const commandsEnv = `sed -i '/http_proxy=/d; /https_proxy=/d; /ftp_proxy=/d; /no_proxy=/d; /HTTP_PROXY=/d; /HTTPS_PROXY=/d; /FTP_PROXY=/d; /ALL_PROXY=/d; /all_proxy=/d; /NO_PROXY=/d' /etc/environment
`;
const MacEnvCmd =
'/usr/bin/osascript -e \'do shell script "bash -c \\"' +
commandsEnv +
'\\"" with administrator privileges\'';

exec(`${MacEnvCmd}`, (error, stdout, stderr) => {
if (error) {
console.error("Got an Error:", stderr);
// alert(stderr);
mainWindow.webContents.send("proxy:warning", {
msg: "Sudo implementation failed",
});
return;
}
console.log({ stdout });
console.log("Command executed successfully for environment variables.");
mainWindow.webContents.send("proxy:success", {
msg: "Success: Unset env using sudo",
});
async function unsetMacAllProxy() {
console.log(proxyServer);
// const allCommands = [
// `npm config delete proxy`,
// `npm config delete https-proxy`,
// `networksetup -setmanual "USB 10/100/1000 LAN" 192.168.212.82 255.255.240.0 192.168.208.1`,
// `networksetup -setwebproxy "USB 10/100/1000 LAN" "" ""`,
// `networksetup -setsecurewebproxy "USB 10/100/1000 LAN" "" ""`,
// ];
const allCommands = [
`npm config delete proxy`,
`npm config delete https-proxy`,
];
allCommands.map((command) =>
exec(command, (error, stdout, stderr) => {
if (error) {
console.error("Got an Error : ", stderr);
return;
}
console.log({ stdout });
console.log(`command executed successfully: ${command}`);
})
);
mainWindow.webContents.send("proxy:success", {
msg: `success : unset system env, npm`,
});
}

// async function setMacAllProxyPrompt(host, port) {
// const proxyserver = `http://${host}:${port}`;
// const commandsEnv = `
// http_proxy=${proxyserver}
// https_proxy=${proxyserver}
// ftp_proxy=${proxyserver}
// no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com,127.0.0.0/8,::1"
// HTTP_PROXY=${proxyserver}
// HTTPS_PROXY=${proxyserver}
// FTP_PROXY=${proxyserver}
// NO_PROXY="localhost,127.0.0.1,localaddress,.localdomain.com,127.0.0.0/8,::1"
// ALL_PROXY=${proxyserver}
// all_proxy=${proxyserver}
// `;
// const cmd = `tee -a /etc/environment << EOF
// ${commandsEnv}
// EOF`;
// MacEnvCmd = `osascript -e "do shell script \"${cmd}\" with administrator privileges"`;

// exec(MacEnvCmd, (error, stdout, stderr) => {
// if (error) {
// console.error("Got an Error:", stderr);
// // alert(stderr);
// mainWindow.webContents.send("proxy:warning", {
// msg: "Sudo implementation failed",
// });
// return;
// }
// console.log({ stdout });
// console.log("Command executed successfully for environment variables.");
// mainWindow.webContents.send("proxy:success", {
// msg: "Success: Set env using sudo",
// });
// });
// }

// async function unsetMacAllProxyPrompt() {
// const commandsEnv = `sed -i '/http_proxy=/d; /https_proxy=/d; /ftp_proxy=/d; /no_proxy=/d; /HTTP_PROXY=/d; /HTTPS_PROXY=/d; /FTP_PROXY=/d; /ALL_PROXY=/d; /all_proxy=/d; /NO_PROXY=/d' /etc/environment
// `;
// const MacEnvCmd =
// '/usr/bin/osascript -e \'do shell script "bash -c \\"' +
// commandsEnv +
// '\\"" with administrator privileges\'';

// exec(`${MacEnvCmd}`, (error, stdout, stderr) => {
// if (error) {
// console.error("Got an Error:", stderr);
// // alert(stderr);
// mainWindow.webContents.send("proxy:warning", {
// msg: "Sudo implementation failed",
// });
// return;
// }
// console.log({ stdout });
// console.log("Command executed successfully for environment variables.");
// mainWindow.webContents.send("proxy:success", {
// msg: "Success: Unset env using sudo",
// });
// });
// }

0 comments on commit fff0461

Please sign in to comment.