vpn.sh 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #!/bin/bash
  2. #
  3. # Mod by SL
  4. # ==================================================
  5. # initialisasi var
  6. export DEBIAN_FRONTEND=noninteractive
  7. OS=`uname -m`;
  8. MYIP=$(wget -qO- ipv4.icanhazip.com);
  9. MYIP2="s/xxxxxxxxx/$MYIP/g";
  10. ANU=$(ip -o $ANU -4 route show to default | awk '{print $5}');
  11. # Install OpenVPN dan Easy-RSA
  12. apt install openvpn easy-rsa openssl -y
  13. apt install openssl iptables iptables-persistent -y
  14. cp -r /usr/share/easy-rsa/ /etc/openvpn
  15. mkdir /etc/openvpn/easy-rsa/keys
  16. cp /etc/openvpn/easy-rsa/vars.example /etc/openvpn/easy-rsa/vars
  17. # Kemudian edit file variabel easy-rsa
  18. # nano /etc/openvpn/easy-rsa/vars
  19. wget -O /etc/openvpn/easy-rsa/vars "https://raw.githubusercontent.com/irwanmohi/aws/main/vars.conf"
  20. # edit projek export KEY_NAME="vpn"
  21. # Save dan keluar dari editor
  22. # generate Diffie hellman parameters
  23. openssl dhparam -out /etc/openvpn/dh2048.pem 2048
  24. # Create PKI
  25. cd /etc/openvpn/easy-rsa
  26. cp openssl-1.0.0.cnf openssl.cnf
  27. . ./vars
  28. ./clean-all
  29. export EASY_RSA="${EASY_RSA:-.}"
  30. "$EASY_RSA/pkitool" --initca $*
  31. # Create key server
  32. export EASY_RSA="${EASY_RSA:-.}"
  33. "$EASY_RSA/pkitool" --server server
  34. # Setting KEY CN
  35. export EASY_RSA="${EASY_RSA:-.}"
  36. "$EASY_RSA/pkitool" client
  37. # cp /etc/openvpn/easy-rsa/keys/{server.crt,server.key,ca.crt} /etc/openvpn
  38. cd
  39. cp /etc/openvpn/easy-rsa/keys/server.crt /etc/openvpn/server.crt
  40. cp /etc/openvpn/easy-rsa/keys/server.key /etc/openvpn/server.key
  41. cp /etc/openvpn/easy-rsa/keys/ca.crt /etc/openvpn/ca.crt
  42. chmod +x /etc/openvpn/ca.crt
  43. # install openvpn
  44. # wget -O /etc/openvpn/vpn.zip "https://github.com//raw/main/vpn.zip"
  45. # cd /etc/openvpn/
  46. # unzip vpn.zip
  47. # rm -f vpn.zip
  48. cd
  49. # Buat config server TCP 1194
  50. cd /etc/openvpn
  51. cat > /etc/openvpn/server-tcp-1194.conf <<-END
  52. port 1194
  53. proto tcp
  54. dev tun
  55. ca ca.crt
  56. cert server.crt
  57. key server.key
  58. dh dh2048.pem
  59. plugin /usr/lib/openvpn/openvpn-plugin-auth-pam.so login
  60. verify-client-cert none
  61. username-as-common-name
  62. server 10.6.0.0 255.255.255.0
  63. ifconfig-pool-persist ipp.txt
  64. push "redirect-gateway def1 bypass-dhcp"
  65. push "dhcp-option DNS 1.1.1.1"
  66. push "dhcp-option DNS 1.0.0.1"
  67. keepalive 5 30
  68. comp-lzo
  69. persist-key
  70. persist-tun
  71. status server-tcp-1194.log
  72. verb 3
  73. END
  74. # Buat config server TCP 1197
  75. cd /etc/openvpn
  76. cat > /etc/openvpn/server-tcp-1720.conf <<-END
  77. port 1720
  78. proto tcp
  79. dev tun
  80. ca ca.crt
  81. cert server.crt
  82. key server.key
  83. dh dh2048.pem
  84. plugin /usr/lib/openvpn/openvpn-plugin-auth-pam.so login
  85. verify-client-cert none
  86. username-as-common-name
  87. server 10.6.0.0 255.255.255.0
  88. ifconfig-pool-persist ipp.txt
  89. push "redirect-gateway def1 bypass-dhcp"
  90. push "dhcp-option DNS 1.1.1.1"
  91. push "dhcp-option DNS 1.0.0.1"
  92. keepalive 5 30
  93. comp-lzo
  94. persist-key
  95. persist-tun
  96. status server-tcp-1720.log
  97. verb 3
  98. END
  99. # Buat config server UDP 2200
  100. cat > /etc/openvpn/server-udp-2200.conf <<-END
  101. port 2200
  102. proto udp
  103. dev tun
  104. ca ca.crt
  105. cert server.crt
  106. key server.key
  107. dh dh2048.pem
  108. plugin /usr/lib/openvpn/openvpn-plugin-auth-pam.so login
  109. verify-client-cert none
  110. username-as-common-name
  111. server 10.7.0.0 255.255.255.0
  112. ifconfig-pool-persist ipp.txt
  113. push "redirect-gateway def1 bypass-dhcp"
  114. push "dhcp-option DNS 1.1.1.1"
  115. push "dhcp-option DNS 1.0.0.1"
  116. keepalive 5 30
  117. comp-lzo
  118. persist-key
  119. persist-tun
  120. status server-udp-2200.log
  121. verb 3
  122. END
  123. cd
  124. mkdir -p /usr/lib/openvpn/
  125. cp /usr/lib/x86_64-linux-gnu/openvpn/plugins/openvpn-plugin-auth-pam.so /usr/lib/openvpn/openvpn-plugin-auth-pam.so
  126. # nano /etc/default/openvpn
  127. sed -i 's/#AUTOSTART="all"/AUTOSTART="all"/g' /etc/default/openvpn
  128. # Cari pada baris #AUTOSTART=”all” hilangkan tanda pagar # didepannya sehingga menjadi AUTOSTART=”all”. Save dan keluar dari editor
  129. # restart openvpn dan cek status openvpn
  130. /etc/init.d/openvpn restart
  131. /etc/init.d/openvpn status
  132. # aktifkan ip4 forwarding
  133. echo 1 > /proc/sys/net/ipv4/ip_forward
  134. sed -i 's/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/g' /etc/sysctl.conf
  135. # edit file sysctl.conf
  136. # nano /etc/sysctl.conf
  137. # Uncomment hilangkan tanda pagar pada #net.ipv4.ip_forward=1
  138. # Konfigurasi dan Setting untuk Client
  139. mkdir clientconfig
  140. cp /etc/openvpn/{server.crt,server.key,ca.crt,ta.key} clientconfig/
  141. cd clientconfig
  142. # Buat config client TCP 1197
  143. cd /etc/openvpn
  144. cat > /etc/openvpn/client-tcp-1720.ovpn <<-END
  145. ############## WELCOME TO ###############
  146. ########## syapik96 ###########
  147. ####### DONT FORGET TO SUPPORT US #######
  148. client
  149. dev tun
  150. proto tcp
  151. remote xxxxxxxxx 1720
  152. resolv-retry infinite
  153. route-method exe
  154. nobind
  155. persist-key
  156. persist-tun
  157. auth-user-pass
  158. comp-lzo
  159. verb 3
  160. END
  161. sed -i $MYIP2 /etc/openvpn/client-tcp-1194.ovpn;
  162. # Buat config client UDP 1194
  163. cd /etc/openvpn
  164. cat > /etc/openvpn/client-udp-1194.ovpn <<-END
  165. ############## WELCOME TO ###############
  166. ########## syapik96 ###########
  167. ####### DONT FORGET TO SUPPORT US #######
  168. client
  169. dev tun
  170. proto udp
  171. remote xxxxxxxxx 1194
  172. resolv-retry infinite
  173. route-method exe
  174. nobind
  175. persist-key
  176. persist-tun
  177. auth-user-pass
  178. comp-lzo
  179. verb 3
  180. END
  181. sed -i $MYIP2 /etc/openvpn/client-udp-1194.ovpn;
  182. # Buat config client TCP 1194
  183. cat > /etc/openvpn/client-tcp-1194.ovpn <<-END
  184. ############## WELCOME TO ###############
  185. ########## syapik96 ###########
  186. ####### DONT FORGET TO SUPPORT US #######
  187. client
  188. dev tun
  189. proto tcp
  190. remote xxxxxxxxx 1194
  191. resolv-retry infinite
  192. route-method exe
  193. nobind
  194. persist-key
  195. persist-tun
  196. auth-user-pass
  197. comp-lzo
  198. verb 3
  199. END
  200. sed -i $MYIP2 /etc/openvpn/client-tcp-1194.ovpn;
  201. # Buat config client UDP 2200
  202. cat > /etc/openvpn/client-udp-2200.ovpn <<-END
  203. ############## WELCOME TO ###############
  204. ########## syapik96 ###########
  205. ####### DONT FORGET TO SUPPORT US #######
  206. client
  207. dev tun
  208. proto udp
  209. remote xxxxxxxxx 2200
  210. resolv-retry infinite
  211. route-method exe
  212. nobind
  213. persist-key
  214. persist-tun
  215. auth-user-pass
  216. comp-lzo
  217. verb 3
  218. END
  219. sed -i $MYIP2 /etc/openvpn/client-udp-2200.ovpn;
  220. # Buat config client TCP 2200
  221. cat > /etc/openvpn/client-tcp-2200.ovpn <<-END
  222. ############## WELCOME TO ###############
  223. ########## syapik96 ###########
  224. ####### DONT FORGET TO SUPPORT US #######
  225. client
  226. dev tun
  227. proto tcp
  228. remote xxxxxxxxx 2200
  229. resolv-retry infinite
  230. route-method exe
  231. nobind
  232. persist-key
  233. persist-tun
  234. auth-user-pass
  235. comp-lzo
  236. verb 3
  237. END
  238. sed -i $MYIP2 /etc/openvpn/client-tcp-2200.ovpn;
  239. # Buat config client SSL
  240. cat > /etc/openvpn/client-tcp-ssl.ovpn <<-END
  241. ############## WELCOME TO ###############
  242. ########## syapik96 ###########
  243. ####### DONT FORGET TO SUPPORT US #######
  244. client
  245. dev tun
  246. proto tcp
  247. remote xxxxxxxxx 442
  248. resolv-retry infinite
  249. route-method exe
  250. nobind
  251. persist-key
  252. persist-tun
  253. auth-user-pass
  254. comp-lzo
  255. verb 3
  256. END
  257. sed -i $MYIP2 /etc/openvpn/client-tcp-ssl.ovpn;
  258. cd
  259. # pada tulisan xxx ganti dengan alamat ip address VPS anda
  260. /etc/init.d/openvpn restart
  261. #1197
  262. # masukkan certificatenya ke dalam config client TCP 1197
  263. echo '<ca>' >> /etc/openvpn/client-tcp-1720.ovpn
  264. cat /etc/openvpn/ca.crt >> /etc/openvpn/client-tcp-1720.ovpn
  265. echo '</ca>' >> /etc/openvpn/client-tcp-1720.ovpn
  266. # Copy config OpenVPN client ke home directory root agar mudah didownload ( TCP 1197 )
  267. cp /etc/openvpn/client-tcp-1720.ovpn /home/vps/public_html/client-tcp-1720.ovpn
  268. #1194
  269. # masukkan certificatenya ke dalam config client TCP 1194
  270. echo '<ca>' >> /etc/openvpn/client-tcp-1194.ovpn
  271. cat /etc/openvpn/ca.crt >> /etc/openvpn/client-tcp-1194.ovpn
  272. echo '</ca>' >> /etc/openvpn/client-tcp-1194.ovpn
  273. # Copy config OpenVPN client ke home directory root agar mudah didownload ( TCP 1194 )
  274. cp /etc/openvpn/client-tcp-1194.ovpn /home/vps/public_html/client-tcp-1194.ovpn
  275. #2200
  276. # masukkan certificatenya ke dalam config client UDP 2200
  277. echo '<ca>' >> /etc/openvpn/client-udp-2200.ovpn
  278. cat /etc/openvpn/ca.crt >> /etc/openvpn/client-udp-2200.ovpn
  279. echo '</ca>' >> /etc/openvpn/client-udp-2200.ovpn
  280. # Copy config OpenVPN client ke home directory root agar mudah didownload ( UDP 2200 )
  281. cp /etc/openvpn/client-udp-2200.ovpn /home/vps/public_html/client-udp-2200.ovpn
  282. # masukkan certificatenya ke dalam config client SSL
  283. echo '<ca>' >> /etc/openvpn/client-tcp-ssl.ovpn
  284. cat /etc/openvpn/ca.crt >> /etc/openvpn/client-tcp-ssl.ovpn
  285. echo '</ca>' >> /etc/openvpn/client-tcp-ssl.ovpn
  286. # Copy config OpenVPN client ke home directory root agar mudah didownload ( SSL )
  287. cp /etc/openvpn/client-tcp-ssl.ovpn /home/vps/public_html/client-tcp-ssl.ovpn
  288. #firewall untuk memperbolehkan akses UDP dan akses jalur TCP
  289. iptables -t nat -I POSTROUTING -s 10.6.0.0/24 -o $ANU -j MASQUERADE
  290. iptables -t nat -I POSTROUTING -s 10.7.0.0/24 -o $ANU -j MASQUERADE
  291. iptables-save > /etc/iptables.up.rules
  292. chmod +x /etc/iptables.up.rules
  293. iptables-restore -t < /etc/iptables.up.rules
  294. netfilter-persistent save
  295. netfilter-persistent reload
  296. # Restart service openvpn
  297. systemctl enable openvpn
  298. systemctl start openvpn
  299. /etc/init.d/openvpn restart
  300. # Restore iptables
  301. cat > /etc/network/if-up.d/iptables <<-END
  302. iptables-restore < /etc/iptables.up.rules
  303. iptables -t nat -A POSTROUTING -s 10.6.0.0/24 -o $ANU -j SNAT --to xxxxxxxxx
  304. iptables -t nat -A POSTROUTING -s 10.7.0.0/24 -o $ANU -j SNAT --to xxxxxxxxx
  305. END
  306. sed -i $MYIP2 /etc/network/if-up.d/iptables
  307. chmod +x /etc/network/if-up.d/iptables
  308. # restart opevpn
  309. /etc/init.d/openvpn restart
  310. # Delete script
  311. history -c
  312. rm -f /root/vpn.sh