port-ovpn.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/bin/bash
  2. MYIP=$(wget -qO- ipinfo.io/ip);
  3. ovpn="$(netstat -nlpt | grep -i openvpn | grep -i 0.0.0.0 | awk '{print $4}' | cut -d: -f2)"
  4. ovpn2="$(netstat -nlpu | grep -i openvpn | grep -i 0.0.0.0 | awk '{print $4}' | cut -d: -f2)"
  5. echo -e "======================================"
  6. echo -e ""
  7. echo -e " [1] Change Port TCP $ovpn"
  8. echo -e " [2] Change Port UDP $ovpn2"
  9. echo -e " [x] Exit"
  10. echo -e "======================================"
  11. echo -e ""
  12. read -p " Select From Options [1-2 or x] : " prot
  13. echo -e ""
  14. case $prot in
  15. 1)
  16. read -p "New Port OpenVPN: " vpn
  17. if [ -z $vpn ]; then
  18. echo "Please Input Port"
  19. exit 0
  20. fi
  21. cek=$(netstat -nutlp | grep -w $vpn)
  22. if [[ -z $cek ]]; then
  23. rm -f /etc/openvpn/server/server-tcp-$ovpn.conf
  24. rm -f /etc/openvpn/client-tcp-$ovpn.ovpn
  25. rm -f /home/vps/public_html/client-tcp-$ovpn.ovpn
  26. cat > /etc/openvpn/server/server-tcp-$vpn.conf<<END
  27. port $vpn
  28. proto tcp
  29. dev tun
  30. ca ca.crt
  31. cert server.crt
  32. key server.key
  33. dh dh2048.pem
  34. plugin /usr/lib/openvpn/openvpn-plugin-auth-pam.so login
  35. verify-client-cert none
  36. username-as-common-name
  37. server 10.6.0.0 255.255.255.0
  38. ifconfig-pool-persist ipp.txt
  39. push "redirect-gateway def1 bypass-dhcp"
  40. push "dhcp-option DNS 8.8.8.8"
  41. push "dhcp-option DNS 8.8.4.4"
  42. keepalive 5 30
  43. comp-lzo
  44. persist-key
  45. persist-tun
  46. status openvpn-tcp.log
  47. verb 3
  48. END
  49. cat > /etc/openvpn/client-tcp-$vpn.ovpn <<-END
  50. client
  51. dev tun
  52. proto tcp
  53. remote $MYIP $vpn
  54. resolv-retry infinite
  55. route-method exe
  56. nobind
  57. persist-key
  58. persist-tun
  59. auth-user-pass
  60. comp-lzo
  61. verb 3
  62. END
  63. echo '<ca>' >> /etc/openvpn/client-tcp-$vpn.ovpn
  64. cat /etc/openvpn/server/ca.crt >> /etc/openvpn/client-tcp-$vpn.ovpn
  65. echo '</ca>' >> /etc/openvpn/client-tcp-$vpn.ovpn
  66. cp /etc/openvpn/client-tcp-$vpn.ovpn /home/vps/public_html/client-tcp-$vpn.ovpn
  67. systemctl disable --now openvpn-server@server-tcp-$ovpn > /dev/null
  68. systemctl enable --now openvpn-server@server-tcp-$vpn > /dev/null
  69. sed -i "s/ - OpenVPN : TCP $ovpn, UDP $ovpn2, SSL 442/ - OpenVPN : TCP $vpn, UDP $ovpn2, SSL 442/g" /root/log-install.txt
  70. sed -i "s/$ovpn/$vpn/g" /etc/stunnel/stunnel.conf
  71. echo -e "\e[032;1mPort $vpn modified successfully\e[0m"
  72. else
  73. echo "Port $vpn is used"
  74. fi
  75. ;;
  76. 2)
  77. read -p "New Port OpenVPN: " vpn
  78. if [ -z $vpn ]; then
  79. echo "Please Input Port"
  80. exit 0
  81. fi
  82. cek=$(netstat -nutlp | grep -w $vpn)
  83. if [[ -z $cek ]]; then
  84. rm -f /etc/openvpn/server/server-udp-$ovpn2.conf
  85. rm -f /etc/openvpn/client-udp-$ovpn2.ovpn
  86. rm -f /home/vps/public_html/client-tcp-$ovpn2.ovpn
  87. cat > /etc/openvpn/server/server-udp-$vpn.conf<<END
  88. port $vpn
  89. proto udp
  90. dev tun
  91. ca ca.crt
  92. cert server.crt
  93. key server.key
  94. dh dh2048.pem
  95. plugin /usr/lib/openvpn/openvpn-plugin-auth-pam.so login
  96. verify-client-cert none
  97. username-as-common-name
  98. server 10.7.0.0 255.255.255.0
  99. ifconfig-pool-persist ipp.txt
  100. push "redirect-gateway def1 bypass-dhcp"
  101. push "dhcp-option DNS 8.8.8.8"
  102. push "dhcp-option DNS 8.8.4.4"
  103. keepalive 5 30
  104. comp-lzo
  105. persist-key
  106. persist-tun
  107. status openvpn-udp.log
  108. verb 3
  109. explicit-exit-notify
  110. END
  111. cat > /etc/openvpn/client-udp-$vpn.ovpn <<-END
  112. client
  113. dev tun
  114. proto udp
  115. remote $MYIP $vpn
  116. resolv-retry infinite
  117. route-method exe
  118. nobind
  119. persist-key
  120. persist-tun
  121. auth-user-pass
  122. comp-lzo
  123. verb 3
  124. END
  125. echo '<ca>' >> /etc/openvpn/client-udp-$vpn.ovpn
  126. cat /etc/openvpn/server/ca.crt >> /etc/openvpn/client-udp-$vpn.ovpn
  127. echo '</ca>' >> /etc/openvpn/client-udp-$vpn.ovpn
  128. cp /etc/openvpn/client-udp-$vpn.ovpn /home/vps/public_html/client-udp-$vpn.ovpn
  129. systemctl disable --now openvpn-server@server-udp-$ovpn2 > /dev/null
  130. systemctl enable --now openvpn-server@server-udp-$vpn > /dev/null
  131. sed -i "s/ - OpenVPN : TCP $ovpn, UDP $ovpn2, SSL 442/ - OpenVPN : TCP $ovpn, UDP $vpn, SSL 442/g" /root/log-install.txt
  132. echo -e "\e[032;1mPort $vpn modified successfully\e[0m"
  133. else
  134. echo "Port $vpn is used"
  135. fi
  136. ;;
  137. x)
  138. exit
  139. menu
  140. ;;
  141. *)
  142. echo "Please enter an correct number"
  143. ;;
  144. esac