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