Skip to content

Commit 1a21aa0

Browse files
authored
Create vpn.sh
1 parent 7514320 commit 1a21aa0

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

openvpn/vpn.sh

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/bin/bash
2+
# =========================================
3+
# SETUP OpenVPN
4+
# =========================================
5+
6+
export DEBIAN_FRONTEND=noninteractive
7+
OS=$(uname -m)
8+
MYIP=$(wget -qO- ipv4.icanhazip.com || curl -s ifconfig.me)
9+
MYIP2="s/xxxxxxxxx/$MYIP/g"
10+
NIC=$(ip -o -4 route show to default | awk '{print $5}')
11+
DOMAIN=$(cat /usr/local/etc/xray/domain 2>/dev/null || cat /root/domain 2>/dev/null)
12+
13+
# =========================================
14+
# Install OpenVPN and dependencies
15+
apt purge -y openvpn easy-rsa
16+
apt install -y openvpn easy-rsa unzip openssl iptables iptables-persistent netfilter-persistent
17+
18+
mkdir -p /etc/openvpn/server/easy-rsa/
19+
cd /etc/openvpn/
20+
wget https://raw.githubusercontent.com/givps/AutoScriptXray/master/udp-custom/openvpn/vpn.zip
21+
unzip vpn.zip && rm -f vpn.zip
22+
chown -R root:root /etc/openvpn/server/easy-rsa/
23+
24+
# PAM plugin
25+
mkdir -p /usr/lib/openvpn/
26+
cp /usr/lib/x86_64-linux-gnu/openvpn/plugins/openvpn-plugin-auth-pam.so \
27+
/usr/lib/openvpn/openvpn-plugin-auth-pam.so
28+
29+
# Enable OpenVPN services
30+
sed -i 's/#AUTOSTART="all"/AUTOSTART="all"/g' /etc/default/openvpn
31+
systemctl enable --now openvpn-server@server-tcp-1194
32+
systemctl enable --now openvpn-server@server-udp-1195
33+
34+
# IPv4 forwarding
35+
echo 1 > /proc/sys/net/ipv4/ip_forward
36+
sed -i 's/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/g' /etc/sysctl.conf
37+
38+
# =========================================
39+
# Generate client configs with embedded CA + login prompt
40+
CA_CONTENT=$(cat /etc/openvpn/server/ca.crt)
41+
42+
make_ovpn() {
43+
local NAME=$1
44+
local PROTO=$2
45+
local PORT=$3
46+
local EXTRA=$4
47+
48+
cat > /etc/openvpn/${NAME}.ovpn <<-EOF
49+
setenv FRIENDLY_NAME "${NAME^^}"
50+
client
51+
dev tun
52+
proto $PROTO
53+
remote $DOMAIN $PORT
54+
resolv-retry infinite
55+
nobind
56+
remote-cert-tls server
57+
cipher AES-256-CBC
58+
auth SHA256
59+
persist-key
60+
persist-tun
61+
auth-user-pass
62+
comp-lzo
63+
verb 3
64+
$EXTRA
65+
<ca>
66+
$CA_CONTENT
67+
</ca>
68+
EOF
69+
70+
sed -i $MYIP2 /etc/openvpn/${NAME}.ovpn
71+
cp /etc/openvpn/${NAME}.ovpn /home/vps/public_html/${NAME}.ovpn
72+
73+
# TCP 1194
74+
make_ovpn "client-tcp-1194" "tcp" "1194"
75+
76+
# UDP 1195
77+
make_ovpn "client-udp-1195" "udp" "1195"
78+
79+
# SSL (TCP 888)
80+
make_ovpn "client-ssl-888" "tcp" "888"
81+
82+
# =========================================
83+
# Firewall rules for VPN subnets
84+
iptables -t nat -C POSTROUTING -s 10.6.0.0/24 -o $NIC -j MASQUERADE 2>/dev/null
85+
iptables -t nat -I POSTROUTING -s 10.6.0.0/24 -o $NIC -j MASQUERADE
86+
iptables -t nat -C POSTROUTING -s 10.7.0.0/24 -o $NIC -j MASQUERADE 2>/dev/null
87+
iptables -t nat -I POSTROUTING -s 10.7.0.0/24 -o $NIC -j MASQUERADE
88+
iptables -t nat -C POSTROUTING -s 10.8.0.0/24 -o $NIC -j MASQUERADE 2>/dev/null
89+
iptables -t nat -I POSTROUTING -s 10.8.0.0/24 -o $NIC -j MASQUERADE
90+
91+
iptables -C INPUT -p tcp --dport 1194 -j ACCEPT 2>/dev/null || \
92+
iptables -A INPUT -p tcp --dport 1194 -j ACCEPT
93+
iptables -C INPUT -p udp --dport 1195 -j ACCEPT 2>/dev/null || \
94+
iptables -A INPUT -p udp --dport 1195 -j ACCEPT
95+
iptables -C INPUT -p tcp --dport 888 -j ACCEPT 2>/dev/null || \
96+
iptables -A INPUT -p tcp --dport 888 -j ACCEPT
97+
98+
netfilter-persistent save
99+
netfilter-persistent reload
100+
101+
# Restart OpenVPN
102+
systemctl restart openvpn
103+
104+
# =========================================
105+
# Cleanup
106+
history -c
107+
rm -f /root/vpn.sh

0 commit comments

Comments
 (0)