#!/bin/sh

socks_port="23456"
socks_user="root"
socks_pass="wawacloud"

echo "配置iptables中..."
# 配置iptables
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -t nat -F
iptables -t mangle -F
iptables -F
iptables -X
iptables-save
echo "iptables配置完成"

echo "绑定额外的IP到网卡中..."
# 绑定额外的IP到网卡
for i in {11..19}; do
    ip addr add 192.168.0.$i/24 dev eth0
    echo "绑定IP: 192.168.0.$i"
done
echo "额外IP绑定完成"

ips=(
$(hostname -I)
)

echo "安装Xray中..."
# Xray 安装
wget -O /usr/local/bin/xray http://wawaok.cn/static/xray
chmod +x /usr/local/bin/xray
echo "Xray安装完成"

echo "配置Xray服务..."
cat <<EOF > /etc/systemd/system/xray.service
[Unit]
Description=The Xray Proxy Serve
After=network-online.target

[Service]
ExecStart=/usr/local/bin/xray -c /etc/xray/serve.toml
ExecStop=/bin/kill -s QUIT \$MAINPID
Restart=always
RestartSec=15s

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable xray
echo "Xray服务配置完成"

echo "生成Xray配置文件..."
# Xray 配置
mkdir -p /etc/xray
echo -n "" > /etc/xray/serve.toml

for ((i = 0; i < ${#ips[@]}; i++)); do
cat <<EOF >> /etc/xray/serve.toml
[[inbounds]]
listen = "${ips[i]}"
port = $socks_port
protocol = "socks"
tag = "$((i+1))"
[inbounds.settings]
auth = "password"
udp = true
ip = "${ips[i]}"
[[inbounds.settings.accounts]]
user = "$socks_user"
pass = "$socks_pass"
[[routing.rules]]
type = "field"
inboundTag = "$((i+1))"
outboundTag = "$((i+1))"
[[outbounds]]
sendThrough = "${ips[i]}"
protocol = "freedom"
tag = "$((i+1))"
EOF
echo "配置Xray inbound: ${ips[i]}"
done
echo "Xray配置文件生成完成"

echo "重启Xray服务..."
systemctl stop xray
systemctl start xray
echo "Xray服务已重启"

#echo "使用每个IP访问 http://whatismyip.akamai.com 并保存结果到文件 /root/socks5"
# 使用每个IP访问 http://whatismyip.akamai.com 并输出结果并保存到文件
echo "IP/端口/账号/密码"
output_file="/root/socks5.txt"
echo -n "" > "$output_file"

for ip in "${ips[@]}"; do
    result=$(curl -s --interface "$ip" http://whatismyip.akamai.com)
    echo "$result/$socks_port/$socks_user/$socks_pass" | tee -a "$output_file"
done
echo"本次技术支持仅限于合法的交流、内部网络搭建和远程办公。任何违法用途由客户自行承担，本公司不承担法律责任。"
echo "#本次技术支持仅限于合法的交流、远程办公。任何违法用途由客户自行承担，本公司不承担法律责任。"
echo "#本脚本仅用于合法合规的网络技术交流用途，禁止用于违法用途"
echo "#微信: Textwo"
echo "#天翼云折扣QQ群: 159500025"
echo "#请遵守当地法律法规，使用者需自行承担一切行为责任"
