init-per.sh 7.0 KB

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