Skip to content

Instantly share code, notes, and snippets.

@cjybyjk
Last active October 3, 2019 00:43
Show Gist options
  • Save cjybyjk/78cdc94dcb4d637a482940344f623df0 to your computer and use it in GitHub Desktop.
Save cjybyjk/78cdc94dcb4d637a482940344f623df0 to your computer and use it in GitHub Desktop.
Fake gsettings for electron-ssr PAC mode
#!/usr/bin/bash
# Fake gsettings for electron-ssr PAC mode
# Author: sford([email protected])
manual_pac="/tmp/proxy_manual.pac"
tmp_conf="/tmp/proxy_conf.txt"
# 对所有设备进行设置
# $1:prop $2:value
function set_proxy_prop()
{
for netdev in `nmcli connection show | awk -F ' ' '{if (NR>1){print $(NF-2)}}' `; do
nmcli connection modify uuid "$netdev" "$1" "$2"
done
}
# 由于nmcli不支持全局代理,使用PAC实现
function write_tmp_pac()
{
if [ ! -f "$manual_pac" ]; then
cat > "$manual_pac" << EOF
var proxy = "SOCKS5 127.0.0.1:1080;";
function FindProxyForURL(url, host) {
if (isPlainHostName(host))
return "DIRECT";
else
return proxy;
}
EOF
fi
}
# $1:mode $2:value
function set_pac_proxy()
{
tmp=$(grep "^var proxy" "$manual_pac" | awk -F ' ' '{print $NF}')
proxy_host=$(echo $tmp | awk -F ':' '{print $1}')
proxy_port=$(echo $tmp | awk -F ':' '{print $2}' | tr -cd "[0-9]")
proxy_mode="$1"
if [ "$2" == "host" ]; then
proxy_host="$3"
elif [ "$2" == "port" ]; then
proxy_port="$3"
fi
sed -i "s#^var proxy .\+;#var proxy = \"$proxy_mode $proxy_host:$proxy_port;\";#g" "$manual_pac"
set_proxy_prop "proxy.pac-script" "$manual_pac"
}
# $1:name $2:value
function write_value()
{
local tmp=$(grep "^$1.$2=" $tmp_conf)
if [ -z "$tmp" ]; then
echo "$1.$2=$3" >> "$tmp_conf"
else
sed -i "s#^$tmp#$1.$2=$3#g" "$tmp_conf"
fi
return $?
}
# set
if [ "$1" == "set" ]; then
write_value "$2" "$3" "$4"
if [ "$2" == "org.gnome.system.proxy" ]; then
set_proxy_prop "proxy.pac-url" ""
set_proxy_prop "proxy.pac-script" ""
if [ "$3" == "mode" ]; then
if [ "$4" == "manual" ]; then
write_tmp_pac
set_proxy_prop "proxy.method" "auto"
set_proxy_prop "proxy.pac-script" "$manual_pac"
else
set_proxy_prop "proxy.method" "$4"
fi
elif [ "$3" == "autoconfig-url" ]; then
set_proxy_prop "proxy.pac-url" "$4"
fi
elif [ "$2" == "org.gnome.system.proxy.socks" ]; then
set_pac_proxy "SOCKS5" "$3" "$4"
elif [ "$2" == "org.gnome.system.proxy.http" ]; then
set_pac_proxy "PROXY" "$3" "$4"
fi
elif [ "$1" == "get" ]; then
grep "^$2.$3=" $tmp_conf | awk -F '=' '{print $NF}'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment