init-per.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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_frpc=false
  17. if ! sudo docker ps -q -f name="frpc" | grep -q .; then
  18. create_frpc=true
  19. else
  20. read -p "frpc容器已经存在,是否删除? (y/n): " confirm_frpc
  21. # 判断用户的回答
  22. if [ "$confirm_frpc" == "y" ] || [ "$confirm_frpc" == "Y" ]; then
  23. sudo docker rm -f frpc
  24. create_frpc=true
  25. fi
  26. fi
  27. # 1. 视频NRV录像机穿透
  28. # 2. VPN穿透
  29. # 3. SSH穿透,仅供阿里云内网使用
  30. if $create_frpc; then
  31. if [ ! -e "frpc.ini" ]; then
  32. cat <<EOF > frpc.ini
  33. [common]
  34. server_addr = 120.27.243.131
  35. server_port = 7000
  36. [nvr-$proj]
  37. type = tcp
  38. local_ip = 192.168.3.200
  39. local_port = 554
  40. remote_port = 119$port
  41. [vpn-$proj]
  42. type = udp
  43. local_ip = 0.0.0.0
  44. local_port = 101$port
  45. remote_port = 101$port
  46. [ssh-$proj]
  47. type = tcp
  48. local_ip = 0.0.0.0
  49. local_port = 22
  50. remote_port = 220$port
  51. [ptr-$proj]
  52. type = tcp
  53. local_ip = 0.0.0.0
  54. local_port = 9001
  55. remote_port = 290$port
  56. [node-$proj]
  57. type = tcp
  58. local_ip = 0.0.0.0
  59. local_port = 9100
  60. remote_port = 230$port
  61. EOF
  62. fi
  63. cat <<EOF > Dockerfile
  64. FROM snowdreamtech/frpc:0.50.0
  65. COPY frpc.ini /etc/frp/frpc.ini
  66. EOF
  67. sudo docker build -t frpc .
  68. rm -rf frpc.ini Dockerfile
  69. sudo docker run --name frpc --restart always --network host -d frpc
  70. fi
  71. # VPN搭建
  72. # 判断内网穿透服务
  73. create_vpn=false
  74. if ! sudo docker ps -q -f name="vpn" | grep -q .; then
  75. create_vpn=true
  76. else
  77. read -p "VPN容器已经存在,是否删除? (y/n): " confirm_vpn
  78. # 判断用户的回答
  79. if [ "$confirm_vpn" == "y" ] || [ "$confirm_vpn" == "Y" ]; then
  80. sudo docker rm -f vpn
  81. sudo docker volume rm openvpn
  82. create_vpn=true
  83. fi
  84. fi
  85. if $create_vpn; then
  86. sudo docker volume create --name openvpn
  87. if ! grep -q "net.ipv6.conf.default.forwarding=1" /etc/sysctl.conf; then
  88. sudo echo net.ipv6.conf.default.forwarding=1 >> /etc/sysctl.conf
  89. sudo echo net.ipv6.conf.all.forwarding=1 >> /etc/sysctl.conf
  90. sudo sysctl -p
  91. fi
  92. ip -o link show | awk -F': ' '{print $2}' | while read interface; do
  93. # 处理192.168.3网段的网卡
  94. mac=$(ip link show dev "$interface" | awk '/link\/ether/ {print $2}')
  95. ip=$(ip addr show dev "$interface" | awk '/inet 192.168.6/ {print $2}')
  96. if [ -n "$ip" ] && [ -n "$mac" ]; then
  97. subnet=$(echo "$ip" | awk -F'/' '{print $1}' | awk -F'.' '{print $1"."$2"."$3}')
  98. echo "网卡名称: $interface,MAC地址: $mac,IP地址: $(echo $ip | cut -d '/' -f 1), subnet: $subnet"
  99. 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 \
  100. -t -B -D -d -b \
  101. --bridge-name 'br0' \
  102. --bridge-eth-if $interface \
  103. --bridge-eth-ip $(echo $ip | cut -d '/' -f 1) \
  104. --bridge-eth-subnet '255.255.255.0' \
  105. --bridge-eth-broadcast "$subnet.255" \
  106. --bridge-eth-mac $mac \
  107. --bridge-eth-gateway "$subnet.1" \
  108. --bridge-dhcp-start "$subnet.180" \
  109. --bridge-dhcp-end "$subnet.189"
  110. if ip route | grep "default via $subnet.1 dev br0"; then
  111. # 如果存在,则删除路由
  112. sudo ip route del default via $subnet.1 dev br0
  113. echo "已删除指定路由."
  114. else
  115. echo "指定路由不存在."
  116. fi
  117. sudo sed -i "s/192.168.255.0/$subnet.0/g" /var/lib/docker/volumes/openvpn/_data/openvpn.conf
  118. fi
  119. done
  120. # 密码: qx123456
  121. sudo docker run -v openvpn:/etc/openvpn --rm -it jaikuai/openvpn-tap:1.0.1 ovpn_initpki
  122. 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
  123. # 单网卡补充默认路由
  124. # sudo ip route add default via 192.168.0.1
  125. cat <<EOF > vpn-user.sh
  126. #!/bin/sh
  127. user=\$1
  128. array=(\${user//,/ })
  129. for item in \${array[@]}
  130. do
  131. sudo docker run -v openvpn:/etc/openvpn --rm -it jaikuai/openvpn-tap:1.0.1 easyrsa build-client-full \$item-$proj nopass
  132. sudo docker run -v openvpn:/etc/openvpn --log-driver=none --rm jaikuai/openvpn-tap:1.0.1 ovpn_getclient \$item-$proj > \$item-$proj.ovpn
  133. done
  134. EOF
  135. sudo chmod a+x vpn-user.sh
  136. echo "# 执行 ./vpn-user.sh 即可生成客户端配置文件."
  137. echo "# bash vpn-user.sh hzw,jxt"
  138. fi