ipsec.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #!/bin/bash
  2. # Debian 9 & 10 64bit
  3. # Ubuntu 18.04 & 20.04 bit
  4. # Centos 7 & 8 64bit
  5. # Mod By SL
  6. # SL
  7. # ==========================================
  8. # Color
  9. RED='\033[0;31m'
  10. NC='\033[0m'
  11. GREEN='\033[0;32m'
  12. ORANGE='\033[0;33m'
  13. BLUE='\033[0;34m'
  14. PURPLE='\033[0;35m'
  15. CYAN='\033[0;36m'
  16. LIGHT='\033[0;37m'
  17. # ==========================================
  18. # Getting
  19. MYIP=$(wget -qO- ipinfo.io/ip);
  20. echo "Checking VPS"
  21. IZIN=$( curl ipinfo.io/ip | grep $MYIP )
  22. if [ $MYIP = $MYIP ]; then
  23. echo -e "${NC}${GREEN}Permission Accepted...${NC}"
  24. else
  25. echo -e "${NC}${RED}Permission Denied!${NC}";
  26. echo -e "${NC}${LIGHT}Fuck You!!"
  27. exit 0
  28. fi
  29. # ==================================================
  30. # Link Hosting Kalian
  31. akbarvpn="raw.githubusercontent.com/fisabiliyusri/Mantap/main/ipsec"
  32. VPN_IPSEC_PSK='myvpn'
  33. NET_IFACE=$(ip -o $NET_IFACE -4 route show to default | awk '{print $5}');
  34. export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  35. source /etc/os-release
  36. OS=$ID
  37. ver=$VERSION_ID
  38. bigecho() { echo; echo "## $1"; echo; }
  39. bigecho "VPN setup in progress... Please be patient."
  40. # Create and change to working dir
  41. mkdir -p /opt/src
  42. cd /opt/src
  43. bigecho "Trying to auto discover IP of this server..."
  44. PUBLIC_IP=$(wget -qO- ipinfo.io/ip);
  45. bigecho "Installing packages required for the VPN..."
  46. if [[ ${OS} == "centos" ]]; then
  47. epel_url="https://dl.fedoraproject.org/pub/epel/epel-release-latest-$(rpm -E '%{rhel}').noarch.rpm"
  48. yum -y install epel-release || yum -y install "$epel_url"
  49. bigecho "Installing packages required for the VPN..."
  50. REPO1='--enablerepo=epel'
  51. REPO2='--enablerepo=*server-*optional*'
  52. REPO3='--enablerepo=*releases-optional*'
  53. REPO4='--enablerepo=PowerTools'
  54. yum -y install nss-devel nspr-devel pkgconfig pam-devel \
  55. libcap-ng-devel libselinux-devel curl-devel nss-tools \
  56. flex bison gcc make ppp
  57. yum "$REPO1" -y install xl2tpd
  58. if [[ $ver == '7' ]]; then
  59. yum -y install systemd-devel iptables-services
  60. yum "$REPO2" "$REPO3" -y install libevent-devel fipscheck-devel
  61. elif [[ $ver == '8' ]]; then
  62. yum "$REPO4" -y install systemd-devel libevent-devel fipscheck-devel
  63. fi
  64. else
  65. apt install openssl iptables iptables-persistent -y
  66. apt-get -y install libnss3-dev libnspr4-dev pkg-config \
  67. libpam0g-dev libcap-ng-dev libcap-ng-utils libselinux1-dev \
  68. libcurl4-nss-dev flex bison gcc make libnss3-tools \
  69. libevent-dev ppp xl2tpd pptpd
  70. fi
  71. bigecho "Compiling and installing Libreswan..."
  72. SWAN_VER=3.32
  73. swan_file="libreswan-$SWAN_VER.tar.gz"
  74. swan_url1="https://github.com/libreswan/libreswan/archive/v$SWAN_VER.tar.gz"
  75. swan_url2="https://download.libreswan.org/$swan_file"
  76. if ! { wget -t 3 -T 30 -nv -O "$swan_file" "$swan_url1" || wget -t 3 -T 30 -nv -O "$swan_file" "$swan_url2"; }; then
  77. exit 1
  78. fi
  79. /bin/rm -rf "/opt/src/libreswan-$SWAN_VER"
  80. tar xzf "$swan_file" && /bin/rm -f "$swan_file"
  81. cd "libreswan-$SWAN_VER" || exit 1
  82. cat > Makefile.inc.local <<'EOF'
  83. WERROR_CFLAGS = -w
  84. USE_DNSSEC = false
  85. USE_DH2 = true
  86. USE_DH31 = false
  87. USE_NSS_AVA_COPY = true
  88. USE_NSS_IPSEC_PROFILE = false
  89. USE_GLIBC_KERN_FLIP_HEADERS = true
  90. EOF
  91. if ! grep -qs IFLA_XFRM_LINK /usr/include/linux/if_link.h; then
  92. echo "USE_XFRM_INTERFACE_IFLA_HEADER = true" >> Makefile.inc.local
  93. fi
  94. if [[ ${OS} == "debian" ]]; then
  95. if [ "$(packaging/utils/lswan_detect.sh init)" = "systemd" ]; then
  96. apt-get -y install libsystemd-dev
  97. fi
  98. elif [[ ${OS} == "ubuntu" ]]; then
  99. if [ "$(packaging/utils/lswan_detect.sh init)" = "systemd" ]; then
  100. apt-get -y install libsystemd-dev
  101. fi
  102. fi
  103. NPROCS=$(grep -c ^processor /proc/cpuinfo)
  104. [ -z "$NPROCS" ] && NPROCS=1
  105. make "-j$((NPROCS+1))" -s base && make -s install-base
  106. cd /opt/src || exit 1
  107. /bin/rm -rf "/opt/src/libreswan-$SWAN_VER"
  108. if ! /usr/local/sbin/ipsec --version 2>/dev/null | grep -qF "$SWAN_VER"; then
  109. exiterr "Libreswan $SWAN_VER failed to build."
  110. fi
  111. bigecho "Creating VPN configuration..."
  112. L2TP_NET=192.168.42.0/24
  113. L2TP_LOCAL=192.168.42.1
  114. L2TP_POOL=192.168.42.10-192.168.42.250
  115. XAUTH_NET=192.168.43.0/24
  116. XAUTH_POOL=192.168.43.10-192.168.43.250
  117. DNS_SRV1=8.8.8.8
  118. DNS_SRV2=8.8.4.4
  119. DNS_SRVS="\"$DNS_SRV1 $DNS_SRV2\""
  120. [ -n "$VPN_DNS_SRV1" ] && [ -z "$VPN_DNS_SRV2" ] && DNS_SRVS="$DNS_SRV1"
  121. # Create IPsec config
  122. cat > /etc/ipsec.conf <<EOF
  123. version 2.0
  124. config setup
  125. virtual-private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12,%v4:!$L2TP_NET,%v4:!$XAUTH_NET
  126. protostack=netkey
  127. interfaces=%defaultroute
  128. uniqueids=no
  129. conn shared
  130. left=%defaultroute
  131. leftid=$PUBLIC_IP
  132. right=%any
  133. encapsulation=yes
  134. authby=secret
  135. pfs=no
  136. rekey=no
  137. keyingtries=5
  138. dpddelay=30
  139. dpdtimeout=120
  140. dpdaction=clear
  141. ikev2=never
  142. ike=aes256-sha2,aes128-sha2,aes256-sha1,aes128-sha1,aes256-sha2;modp1024,aes128-sha1;modp1024
  143. phase2alg=aes_gcm-null,aes128-sha1,aes256-sha1,aes256-sha2_512,aes128-sha2,aes256-sha2
  144. sha2-truncbug=no
  145. conn l2tp-psk
  146. auto=add
  147. leftprotoport=17/1701
  148. rightprotoport=17/%any
  149. type=transport
  150. phase2=esp
  151. also=shared
  152. conn xauth-psk
  153. auto=add
  154. leftsubnet=0.0.0.0/0
  155. rightaddresspool=$XAUTH_POOL
  156. modecfgdns=$DNS_SRVS
  157. leftxauthserver=yes
  158. rightxauthclient=yes
  159. leftmodecfgserver=yes
  160. rightmodecfgclient=yes
  161. modecfgpull=yes
  162. xauthby=file
  163. ike-frag=yes
  164. cisco-unity=yes
  165. also=shared
  166. include /etc/ipsec.d/*.conf
  167. EOF
  168. if uname -m | grep -qi '^arm'; then
  169. if ! modprobe -q sha512; then
  170. sed -i '/phase2alg/s/,aes256-sha2_512//' /etc/ipsec.conf
  171. fi
  172. fi
  173. # Specify IPsec PSK
  174. cat > /etc/ipsec.secrets <<EOF
  175. %any %any : PSK "$VPN_IPSEC_PSK"
  176. EOF
  177. # Create xl2tpd config
  178. cat > /etc/xl2tpd/xl2tpd.conf <<EOF
  179. [global]
  180. port = 1701
  181. [lns default]
  182. ip range = $L2TP_POOL
  183. local ip = $L2TP_LOCAL
  184. require chap = yes
  185. refuse pap = yes
  186. require authentication = yes
  187. name = l2tpd
  188. pppoptfile = /etc/ppp/options.xl2tpd
  189. length bit = yes
  190. EOF
  191. # Set xl2tpd options
  192. cat > /etc/ppp/options.xl2tpd <<EOF
  193. +mschap-v2
  194. ipcp-accept-local
  195. ipcp-accept-remote
  196. noccp
  197. auth
  198. mtu 1280
  199. mru 1280
  200. proxyarp
  201. lcp-echo-failure 4
  202. lcp-echo-interval 30
  203. connect-delay 5000
  204. ms-dns $DNS_SRV1
  205. EOF
  206. if [ -z "$VPN_DNS_SRV1" ] || [ -n "$VPN_DNS_SRV2" ]; then
  207. cat >> /etc/ppp/options.xl2tpd <<EOF
  208. ms-dns $DNS_SRV2
  209. EOF
  210. fi
  211. # Create VPN credentials
  212. cat > /etc/ppp/chap-secrets <<EOF
  213. "$VPN_USER" l2tpd "$VPN_PASSWORD" *
  214. EOF
  215. VPN_PASSWORD_ENC=$(openssl passwd -1 "$VPN_PASSWORD")
  216. cat > /etc/ipsec.d/passwd <<EOF
  217. $VPN_USER:$VPN_PASSWORD_ENC:xauth-psk
  218. EOF
  219. # Create PPTP config
  220. cat >/etc/pptpd.conf <<END
  221. option /etc/ppp/options.pptpd
  222. logwtmp
  223. localip 192.168.41.1
  224. remoteip 192.168.41.10-100
  225. END
  226. cat >/etc/ppp/options.pptpd <<END
  227. name pptpd
  228. refuse-pap
  229. refuse-chap
  230. refuse-mschap
  231. require-mschap-v2
  232. require-mppe-128
  233. ms-dns 8.8.8.8
  234. ms-dns 8.8.4.4
  235. proxyarp
  236. lock
  237. nobsdcomp
  238. novj
  239. novjccomp
  240. nologfd
  241. END
  242. bigecho "Updating IPTables rules..."
  243. service fail2ban stop >/dev/null 2>&1
  244. iptables -t nat -I POSTROUTING -s 192.168.43.0/24 -o $NET_IFACE -j MASQUERADE
  245. iptables -t nat -I POSTROUTING -s 192.168.42.0/24 -o $NET_IFACE -j MASQUERADE
  246. iptables -t nat -I POSTROUTING -s 192.168.41.0/24 -o $NET_IFACE -j MASQUERADE
  247. if [[ ${OS} == "centos" ]]; then
  248. service iptables save
  249. iptables-restore < /etc/sysconfig/iptables
  250. else
  251. iptables-save > /etc/iptables.up.rules
  252. iptables-restore -t < /etc/iptables.up.rules
  253. netfilter-persistent save
  254. netfilter-persistent reload
  255. fi
  256. bigecho "Enabling services on boot..."
  257. systemctl enable xl2tpd
  258. systemctl enable ipsec
  259. systemctl enable pptpd
  260. for svc in fail2ban ipsec xl2tpd; do
  261. update-rc.d "$svc" enable >/dev/null 2>&1
  262. systemctl enable "$svc" 2>/dev/null
  263. done
  264. bigecho "Starting services..."
  265. sysctl -e -q -p
  266. chmod 600 /etc/ipsec.secrets* /etc/ppp/chap-secrets* /etc/ipsec.d/passwd*
  267. mkdir -p /run/pluto
  268. service fail2ban restart 2>/dev/null
  269. service ipsec restart 2>/dev/null
  270. service xl2tpd restart 2>/dev/null
  271. wget -O /usr/bin/addl2tp https://${akbarvpn}/addl2tp.sh && chmod +x /usr/bin/addl2tp
  272. wget -O /usr/bin/dell2tp https://${akbarvpn}/dell2tp.sh && chmod +x /usr/bin/dell2tp
  273. wget -O /usr/bin/addpptp https://${akbarvpn}/addpptp.sh && chmod +x /usr/bin/addpptp
  274. wget -O /usr/bin/delpptp https://${akbarvpn}/delpptp.sh && chmod +x /usr/bin/delpptp
  275. wget -O /usr/bin/renewpptp https://${akbarvpn}/renewpptp.sh && chmod +x /usr/bin/renewpptp
  276. wget -O /usr/bin/renewl2tp https://${akbarvpn}/renewl2tp.sh && chmod +x /usr/bin/renewl2tp
  277. touch /var/lib/crot/data-user-l2tp
  278. touch /var/lib/crot/data-user-pptp
  279. rm -f /root/ipsec.sh