init-per.sh 6.9 KB

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