init-per.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #!/bin/bash
  2. if [ "$1" = "xxx" ]; then
  3. echo "请填写站名简称和编号"
  4. # 在这里可以添加其他需要执行的命令
  5. exit 1 # 结束执行
  6. fi
  7. # 项目名称
  8. proj=$1
  9. # 透传编号
  10. if [ "$2" -ge 100 ]; then
  11. port=$2
  12. elif [ "$2" -ge 10 ]; then
  13. port="0$2"
  14. else
  15. port="00$2"
  16. fi
  17. # 授权docker
  18. sudo usermod -aG docker zhili
  19. # 判断组网服务
  20. create_one=false
  21. if ! sudo docker ps -q -f name="one" | grep -q .; then
  22. create_one=true
  23. else
  24. read -p "One容器已经存在,是否删除? (y/n): " confirm_one
  25. # 判断用户的回答
  26. if [ "$confirm_one" == "y" ] || [ "$confirm_one" == "Y" ]; then
  27. sudo docker rm -f one
  28. create_one=true
  29. fi
  30. fi
  31. if $create_one; then
  32. DIR="/opt/zerotier-one"
  33. if [ -d "$DIR" ]; then
  34. # 如果目录存在,则删除
  35. sudo rm -rf "$DIR"
  36. fi
  37. sudo mkdir "$DIR"
  38. sudo docker run -d --restart always --name one --device /dev/net/tun --net host --cap-add NET_ADMIN --cap-add SYS_ADMIN -v /opt/zerotier-one:/var/lib/zerotier-one registry.cn-chengdu.aliyuncs.com/jaikuai/zerotier-one:1.12
  39. sleep 3
  40. sudo rm -f /opt/zerotier-one/planet && sudo wget http://git.fast-fun.cn:92/ruili-station/readme/raw/master/planet -P /opt/zerotier-one
  41. sudo docker restart one
  42. sudo docker exec one zerotier-cli join 2eb45e6989f9a0d2
  43. member_id=$(sudo docker exec one zerotier-cli status)
  44. access_token="c15b57c30df2efede131f460e84a80d8c008bb3365031a175d3efe42105ba6f9"
  45. dingtalk_api="https://oapi.dingtalk.com/robot/send?access_token=$access_token"
  46. message="告警:新的站需要组网 $member_id, 需要指定位 http://10.10.64.1:9443/ 站点编号: $proj "
  47. curl -H "Content-Type: application/json" -d "{\"msgtype\": \"text\", \"text\": {\"content\": \"$message\"},\"at\": {\"atMobiles\": [\"13452319860\"]}}" $dingtalk_api
  48. fi
  49. # 联网端口
  50. create_ha=false
  51. if ! sudo docker ps -q -f name="ha" | grep -q .; then
  52. create_ha=true
  53. else
  54. read -p "Ha容器已经存在,是否删除? (y/n): " confirm_ha
  55. # 判断用户的回答
  56. if [ "$confirm_ha" == "y" ] || [ "$confirm_ha" == "Y" ]; then
  57. sudo docker rm -f ha
  58. create_ha=true
  59. fi
  60. fi
  61. if $create_one; then
  62. sudo docker run -d -p 6667:6667 -p 20-21:20-21 -p 21100-21110:21100-21110 -p 554:554 -p 10166:10166 -p 98:98 -p 200:200 --name=ha --restart=always nas.fast-fun.cn:5000/zk/ha:0.22
  63. fi
  64. # 联网端口
  65. create_ptr=false
  66. if ! sudo docker ps -q -f name="portainer_agent" | grep -q .; then
  67. create_ptr=true
  68. else
  69. read -p "Ptr容器已经存在,是否删除? (y/n): " confirm_ptr
  70. # 判断用户的回答
  71. if [ "$confirm_ptr" == "y" ] || [ "$confirm_ptr" == "Y" ]; then
  72. sudo docker rm -f ha
  73. create_ptr=true
  74. fi
  75. fi
  76. if $create_ptr; then
  77. sudo docker run -d \
  78. -p 9001:9001 \
  79. --name portainer_agent \
  80. --restart=always \
  81. -v /var/run/docker.sock:/var/run/docker.sock \
  82. -v /var/lib/docker/volumes:/var/lib/docker/volumes \
  83. nas.fast-fun.cn:5000/portainer/agent:2.19.1
  84. fi
  85. # 判断内网穿透服务
  86. create_frpc=false
  87. if ! sudo docker ps -q -f name="frpc" | grep -q .; then
  88. create_frpc=true
  89. else
  90. read -p "frpc容器已经存在,是否删除? (y/n): " confirm_frpc
  91. # 判断用户的回答
  92. if [ "$confirm_frpc" == "y" ] || [ "$confirm_frpc" == "Y" ]; then
  93. sudo docker rm -f frpc
  94. create_frpc=true
  95. fi
  96. fi
  97. # 1. 视频NRV录像机穿透
  98. # 2. VPN穿透
  99. # 3. SSH穿透,仅供阿里云内网使用
  100. if $create_frpc; then
  101. if [ ! -e "frpc.ini" ]; then
  102. cat <<EOF > frpc.ini
  103. [common]
  104. server_addr = 120.27.243.131
  105. server_port = 7000
  106. [nvr-$proj]
  107. type = tcp
  108. local_ip = 192.168.3.200
  109. local_port = 554
  110. remote_port = 12$port
  111. [vpn-$proj]
  112. type = udp
  113. local_ip = 0.0.0.0
  114. local_port = 10$port
  115. remote_port = 10$port
  116. [ssh-$proj]
  117. type = tcp
  118. local_ip = 0.0.0.0
  119. local_port = 22
  120. remote_port = 22$port
  121. [ptr-$proj]
  122. type = tcp
  123. local_ip = 0.0.0.0
  124. local_port = 9001
  125. remote_port = 29$port
  126. [node-$proj]
  127. type = tcp
  128. local_ip = 0.0.0.0
  129. local_port = 9100
  130. remote_port = 23$port
  131. EOF
  132. fi
  133. cat <<EOF > Dockerfile
  134. FROM nas.fast-fun.cn:5000/snowdreamtech/frpc:0.50.0
  135. COPY frpc.ini /etc/frp/frpc.ini
  136. EOF
  137. sudo docker build -t frpc .
  138. rm -rf frpc.ini Dockerfile
  139. sudo docker run --name frpc --restart always --network host -d frpc
  140. fi
  141. # VPN搭建
  142. # 判断内网穿透服务
  143. create_vpn=false
  144. if ! sudo docker ps -q -f name="vpn" | grep -q .; then
  145. create_vpn=true
  146. else
  147. read -p "VPN容器已经存在,是否删除? (y/n): " confirm_vpn
  148. # 判断用户的回答
  149. if [ "$confirm_vpn" == "y" ] || [ "$confirm_vpn" == "Y" ]; then
  150. sudo docker rm -f vpn
  151. sudo docker volume rm openvpn
  152. create_vpn=true
  153. fi
  154. fi
  155. if $create_vpn; then
  156. sudo docker volume create --name openvpn
  157. if ! grep -q "net.ipv6.conf.default.forwarding=1" /etc/sysctl.conf; then
  158. sudo echo net.ipv6.conf.default.forwarding=1 >> /etc/sysctl.conf
  159. sudo echo net.ipv6.conf.all.forwarding=1 >> /etc/sysctl.conf
  160. sudo sysctl -p
  161. fi
  162. ip -o link show | awk -F': ' '{print $2}' | while read interface; do
  163. # 处理192.168.3网段的网卡
  164. mac=$(ip link show dev "$interface" | awk '/link\/ether/ {print $2}')
  165. ip=$(ip addr show dev "$interface" | awk '/inet (192\.168\.(3|5|6))/ {print $2}')
  166. if [ -n "$ip" ] && [ -n "$mac" ]; then
  167. subnet=$(echo "$ip" | awk -F'/' '{print $1}' | awk -F'.' '{print $1"."$2"."$3}')
  168. echo "网卡名称: $interface,MAC地址: $mac,IP地址: $(echo $ip | cut -d '/' -f 1), subnet: $subnet"
  169. sudo docker run -v openvpn:/etc/openvpn --privileged --rm nas.fast-fun.cn:5000/jaikuai/openvpn-tap:1.0.1 ovpn_genconfig -u udp://120.27.243.131:10$port \
  170. -t -B -D -d -b \
  171. --bridge-name 'br0' \
  172. --bridge-eth-if $interface \
  173. --bridge-eth-ip $(echo $ip | cut -d '/' -f 1) \
  174. --bridge-eth-subnet '255.255.255.0' \
  175. --bridge-eth-broadcast "$subnet.255" \
  176. --bridge-eth-mac $mac \
  177. --bridge-eth-gateway "$subnet.1" \
  178. --bridge-dhcp-start "$subnet.180" \
  179. --bridge-dhcp-end "$subnet.189"
  180. if ip route | grep "default via $subnet.1 dev br0"; then
  181. # 如果存在,则删除路由
  182. sudo ip route del default via $subnet.1 dev br0
  183. echo "已删除指定路由."
  184. else
  185. echo "指定路由不存在."
  186. fi
  187. sudo sed -i "s/192.168.255.0/$subnet.0/g" /var/lib/docker/volumes/openvpn/_data/openvpn.conf
  188. fi
  189. done
  190. # 密码: qx123456
  191. sudo docker run -v openvpn:/etc/openvpn --rm -it nas.fast-fun.cn:5000/jaikuai/openvpn-tap:1.0.1 ovpn_initpki
  192. sudo docker run -v openvpn:/etc/openvpn --privileged --network host --cap-add=NET_ADMIN --restart=always --name vpn -d nas.fast-fun.cn:5000/jaikuai/openvpn-tap:1.0.1
  193. # 单网卡补充默认路由
  194. # sudo ip route add default via 192.168.0.1
  195. cat <<EOF > vpn-user.sh
  196. #!/bin/sh
  197. user=\$1
  198. array=(\${user//,/ })
  199. for item in \${array[@]}
  200. do
  201. sudo docker run -v openvpn:/etc/openvpn --rm -it nas.fast-fun.cn:5000/jaikuai/openvpn-tap:1.0.1 easyrsa build-client-full \$item-$proj nopass
  202. sudo docker run -v openvpn:/etc/openvpn --log-driver=none --rm nas.fast-fun.cn:5000/jaikuai/openvpn-tap:1.0.1 ovpn_getclient \$item-$proj > \$item-$proj.ovpn
  203. done
  204. EOF
  205. sudo chmod a+x vpn-user.sh
  206. echo "# 执行 ./vpn-user.sh 即可生成客户端配置文件."
  207. echo "# bash vpn-user.sh hzw,jxt"
  208. fi
  209. sudo docker exec one zerotier-cli status
  210. # 读取路由器信息的定时任务
  211. curl https://sh.it5000.com/arp | bash
  212. sudo systemctl restart cron