#!/bin/bash

# define var
SS_ADDRESS=$(curl ifconfig.me)
SS_PORT=1988
SS_METHOD=aes-256-cfb
SS_PASSWORD=P@33w0rd


# define fuction
function ss_install {
    yum -y install python3 2>/dev/null
    pip3 install shadowsocks-py 2>/dev/null

    cat > /etc/shadowsocks.json <<EOF
{
"server":"0.0.0.0",
"server_port":"${SS_PORT}",
"local_address":"${SS_ADDRESS}",
"local_port":1080,
"password":"${SS_PASSWORD}",
"timeout":300,
"method":"${SS_METHOD}",
"fast_open":false,
"workers": 5
}
EOF
}

function ss_onboot {
    SS_CMD="/usr/local/bin/ssserver -c /etc/shadowsocks.json --user nobody -d start"
    sed -i "/touch/i${SS_CMD}" /etc/rc.d/rc.local
    chmod +x /etc/rc.d/rc.local
    systemctl enable --now rc-local
}

function ss_client {
    SS_CLIENT=$(curl -s https://github.com/shadowsocks/ShadowsocksX-NG/releases | grep -o /shadowsocks.*[0-9].zip | head -n 1)
    cat <<EOF>/dev/null
. client- macOS
- 1/2
curl -L# https://github.com${SS_CLIENT} -o ShadowsocksX-NG.zip
- 2/2
echo "  地址：${SS_ADDRESS}"
echo "  Port: ${SS_PORT}"
echo "  加密方法：${SS_METHOD}"
echo "  密码：${SS_PASSWORD}"
}

# Main area
ss_install
ss_onboot
ss_client
