Skip to content

Instantly share code, notes, and snippets.

@EkkoG
Created September 12, 2016 16:49
Show Gist options
  • Save EkkoG/b557d04ef8ec822520b32bd5bfb9935f to your computer and use it in GitHub Desktop.
Save EkkoG/b557d04ef8ec822520b32bd5bfb9935f to your computer and use it in GitHub Desktop.
#!/bin/sh
NAME=shadowsocks
SERVERS=servers
dns="119.29.29.29"
uci_set_by_type() {
uci set $NAME.$1.$2=$3 2>/dev/null
}
# 根据参数,通过 UCI 添加 server 配置并设置内容
uci_add_ss_server() {
new_id=$(uci add $NAME $SERVERS)
while getopts "s:p:k:m:a:d:" o
do
case "${o}" in
s)
uci_set_by_type $new_id server $OPTARG
;;
p)
uci_set_by_type $new_id server_port $OPTARG
;;
k)
uci_set_by_type $new_id password $OPTARG
;;
m)
uci_set_by_type $new_id encrypt_method $OPTARG
;;
a)
uci_set_by_type $new_id auth $OPTARG
;;
d)
uci_set_by_type $new_id alias $OPTARG
;;
*)
echo "illegal parameters!"
exit 1;
;;
esac
done
}
# 根据域名和子域名列表,以及密码,端口,加密方式和 OTA 选项批量添加设置
add_server() {
while getopts "r:s:p:k:m:a:" o
do
case "${o}" in
r)
root_domain=$OPTARG
;;
s)
s_domain=$OPTARG
;;
p)
port=$OPTARG
;;
k)
password=$OPTARG
;;
m)
encrypt_method=$OPTARG
;;
a)
auth=$OPTARG
;;
*)
echo "illegal parameters!"
exit 1;
;;
esac
done
domains=$(echo $s_domain | tr ' ' '\n')
echo "$domains" | while read p
do
domain="$p.$root_domain"
ip="$(echo "$(dig $domain '@'$dns +short | grep "^[0-9]")")"
echo "$domain ip $ip"
uci_add_ss_server -s $ip -p $port -k $password -m $encrypt_method -a $auth -d $domain
done
}
# 删除所有现有 servers 配置
delete_all_servers() {
while true
do
local config=$(uci get $NAME.@servers[0] 2>/dev/null)
if [ "${config}x" == "x" ];then
break
else
uci delete $NAME.@servers[0]
fi
done
}
delete_all_servers
# r 为域名,s 为子域名,空格格开,后面依次是端口,密码,加密方式和 OTA 选项(开启为1,不开启为 0)
add_server -r "domain.com" -s "hk1 tw1 kr1" -p 11111 -k 2222222 -m 'rc4-md5' -a 1
# 提交更改
uci commit shadowsocks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment