-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aaf4bab
commit e5cb586
Showing
6 changed files
with
253 additions
and
111 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# adapted from https://gist.github.com/mikeperri/36e656f28e42aa9cee18ce88361f6c16 | ||
# refreshes wininet so it is aware of new registry changes | ||
|
||
$signature = @' | ||
[DllImport("wininet.dll", SetLastError = true, CharSet=CharSet.Auto)] | ||
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength); | ||
'@ | ||
|
||
$INTERNET_OPTION_SETTINGS_CHANGED = 39 | ||
$INTERNET_OPTION_REFRESH = 37 | ||
|
||
$type = Add-Type -MemberDefinition $signature -Name wininet -Namespace pinvoke -PassThru | ||
$a = $type::InternetSetOption(0, $INTERNET_OPTION_SETTINGS_CHANGED, 0, 0) | ||
$b = $type::InternetSetOption(0, $INTERNET_OPTION_REFRESH, 0, 0) | ||
|
||
$a -and $b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import * as shell from "shelljs"; | ||
import path from 'path'; | ||
|
||
const networkSetup = '/usr/sbin/networksetup'; | ||
|
||
export function isProxyAvaliable() { | ||
const proxy = process.env.HTTP_PROXY; | ||
return proxy && Array.isArray(proxy.split(':')) && proxy.split(':').length > 2; | ||
} | ||
|
||
function getProxySetting() { | ||
const proxy = process.env.HTTP_PROXY?.split(':') || []; | ||
if (proxy?.length < 2) { | ||
return; | ||
} | ||
return { | ||
proxyHost: proxy[1].replaceAll('/', ''), | ||
proxyPort: proxy[2], | ||
} | ||
} | ||
|
||
function findNetworkServiceOnMac() { | ||
const networkInfo = shell.exec(`${networkSetup} -listnetworkserviceorder`, {async: false}).stdout; | ||
const lines = networkInfo.split('\n'); | ||
for (let i = 0; i < lines.length; i++) { | ||
const line = lines[i]; | ||
if (line.includes('Device: en')) { | ||
// Network service name would be shown like: (1) Wi-Fi. | ||
// Extract "Wi-Fi" from this line. | ||
const service = lines[i-1].substring(4); | ||
const serviceInfo = shell.exec(`${networkSetup} -getinfo "${service}"`, {async: false}).stdout; | ||
for (const l of serviceInfo.split('\n')) { | ||
if (l.includes('IP address') && !l.includes('IPv6') && !l.includes('none')) { | ||
return service; | ||
} | ||
} | ||
} | ||
} | ||
return 'Ethernet'; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function setupMacProxy(proxy: any) { | ||
const { | ||
proxyHost, | ||
proxyPort, | ||
} = proxy; | ||
const networkService = findNetworkServiceOnMac(); | ||
shell.exec(`sudo ${networkSetup} -setwebproxy "${networkService}" ${proxyHost} ${proxyPort}`, {async: false}); | ||
shell.exec(`sudo ${networkSetup} -setsecurewebproxy "${networkService}" ${proxyHost} ${proxyPort}`, {async: false}); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function setupWinProxy(proxy: any) { | ||
const { | ||
proxyHost, | ||
proxyPort, | ||
} = proxy; | ||
const prefix = | ||
'reg add "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" /v '; | ||
const opts = { async: false}; | ||
shell.exec(`${prefix} MigrateProxy /t REG_DWORD /d 1 /f`, opts) | ||
shell.exec(`${prefix} ProxyEnable /t REG_DWORD /d 1 /f`, opts) | ||
shell.exec(`${prefix} ProxyHttp1.1 /t REG_DWORD /d 1 /f`, opts) | ||
shell.exec(`${prefix} EnableLegacyAutoProxyFeatures /t REG_DWORD /d 1 /f`, opts) | ||
shell.exec(`${prefix} ProxyServer /t REG_SZ /d "${proxyHost}:${proxyPort}" /f`, opts) | ||
shell.exec(`${prefix} ProxyOverride /t REG_SZ /d "localhost;127.0.0.1" /f`, opts) | ||
|
||
// Registry changes won't take effect immediately; we need to refresh wininet. | ||
const refreshScript = path.join(__dirname, '../', 'scripts', 'win-refresh-wininet.ps1'); | ||
shell.exec(`powershell.exe -ExecutionPolicy Bypass ${refreshScript}`, opts); | ||
} | ||
|
||
export function setupProxy() { | ||
const proxy = getProxySetting(); | ||
if (!proxy) { | ||
return; | ||
} | ||
console.log(`Setting system proxy settings: ${proxy.proxyHost}:${proxy.proxyPort}`); | ||
switch (process.platform) { | ||
case 'darwin': | ||
setupMacProxy(proxy); | ||
break; | ||
case 'win32': | ||
setupWinProxy(proxy); | ||
break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters