找回密码
 立即注册
搜索
查看: 298|回复: 0

shadowsocks-all.sh

[复制链接]

989

主题

92

回帖

5万

积分

管理员

积分
55362
发表于 2022-6-22 15:40:40 | 显示全部楼层 |阅读模式

1 T1 Z/ O/ ~& G9 V$ U
  1. #!/bin/bash
    / @  b! O7 g: t  U5 F( k& t
  2. #) ?( O# J3 A3 b1 I# z; o+ p
  3. # https://github.com/Nyr/openvpn-install) D. o4 L5 T( a) _- x
  4. #" ]3 W8 ]# i; o# x
  5. # Copyright (c) 2013 Nyr. Released under the MIT License.
    ( J5 H4 |& i$ p9 m8 Y  p) w

  6. & x9 r* [' Q6 C& ~$ H# G$ e; W
  7. 3 P6 J/ C2 B$ Q
  8. # Detect Debian users running the script with "sh" instead of bash
    0 y7 L1 c2 l7 o3 T9 V" P4 N) b
  9. if readlink /proc/$$/exe | grep -q "dash"; then
    % w8 j4 E: P5 _) F# g- n% \
  10.         echo 'This installer needs to be run with "bash", not "sh".'
    : X  ~% V) i; H" _4 T
  11.         exit
    " h$ R7 Y$ K/ R7 A3 F; K, C8 V
  12. fi
    - M. c! W0 ?6 m2 ?0 ~- B
  13. % }) r, T$ o- l5 o1 x$ [
  14. # Discard stdin. Needed when running from an one-liner which includes a newline. L4 Z' X3 r' G
  15. read -N 999999 -t 0.001
    9 R2 L! v/ ]& m% H* }

  16. 9 g- z8 Q( ~" O1 K
  17. # Detect OpenVZ 60 _  z# l1 k: u( ?
  18. if [[ $(uname -r | cut -d "." -f 1) -eq 2 ]]; then
    9 U% F9 Q" g/ I% h& ^/ p6 Q- e
  19.         echo "The system is running an old kernel, which is incompatible with this installer."
    ) i$ O4 S: a: X- f6 `3 @
  20.         exit
      v6 Z, [+ g" }, p% {' R
  21. fi0 J3 [* j; T0 q: D- c
  22. : J, e8 ^1 x) ^# Q) ~, C4 L% P
  23. # Detect OS: c* ]4 X0 m- v
  24. # $os_version variables aren't always in use, but are kept here for convenience& O, c+ n) {& y4 m  k+ o0 I$ w
  25. if grep -qs "ubuntu" /etc/os-release; then
    2 D' v1 a4 ~$ p) f8 j0 `
  26.         os="ubuntu"
    . F1 u2 m# `, I6 p: x1 `+ y0 A+ V
  27.         os_version=$(grep 'VERSION_ID' /etc/os-release | cut -d '"' -f 2 | tr -d '.')
    " @" G  L5 |1 n- d0 k0 r
  28.         group_name="nogroup"
    8 N/ T9 ~: e3 \  Z' x# Q
  29. elif [[ -e /etc/debian_version ]]; then
    / I3 j+ W7 ?- r& G2 @* O' @
  30.         os="debian"
    ; S7 j7 P$ n! l4 e
  31.         os_version=$(grep -oE '[0-9]+' /etc/debian_version | head -1)
    & u! k; K3 E% v# e: E/ {- r/ j
  32.         group_name="nogroup", k0 [) i% k* y9 j
  33. elif [[ -e /etc/almalinux-release || -e /etc/rocky-release || -e /etc/centos-release ]]; then
    ! x, |+ t! A  u7 @
  34.         os="centos"1 l1 P. M9 K- k2 ^: S
  35.         os_version=$(grep -shoE '[0-9]+' /etc/almalinux-release /etc/rocky-release /etc/centos-release | head -1). V8 Y- ^; }# ?/ @2 i7 J
  36.         group_name="nobody"( {4 u) j, k& T: `( q$ K8 h7 F
  37. elif [[ -e /etc/fedora-release ]]; then
    % Q6 p7 D0 g! q
  38.         os="fedora"
    2 j" h1 }# H  e* ]$ N0 ~
  39.         os_version=$(grep -oE '[0-9]+' /etc/fedora-release | head -1)+ q  D2 }5 b7 I1 W8 s
  40.         group_name="nobody"& t( A' [& }; t3 N) \
  41. else
    / y$ O/ F) ~5 K  O
  42.         echo "This installer seems to be running on an unsupported distribution.1 N  {5 W: E# V5 H) |# o$ r+ L
  43. Supported distros are Ubuntu, Debian, AlmaLinux, Rocky Linux, CentOS and Fedora.") A1 Z: f9 T+ t+ O3 I% q$ v
  44.         exit0 N  l9 U; p1 p/ F* u5 G
  45. fi3 |' a- d" s8 o; n. g
  46. / v# A9 H8 }3 d
  47. if [[ "$os" == "ubuntu" && "$os_version" -lt 1804 ]]; then( t. ]5 ?( i; U
  48.         echo "Ubuntu 18.04 or higher is required to use this installer.# D" C7 s( {, X; m
  49. This version of Ubuntu is too old and unsupported."
    $ P4 Q& Z4 O8 ?  @! ~; J$ K
  50.         exit! N, i; ?, K1 e8 V8 s% W) ~
  51. fi  ^9 D4 @8 ^5 x! a) b1 d. h: K, k

  52. ; R" Q5 H  H5 D- q
  53. if [[ "$os" == "debian" && "$os_version" -lt 9 ]]; then; M$ I1 n$ B. z
  54.         echo "Debian 9 or higher is required to use this installer.3 B/ k6 V$ W5 b( \
  55. This version of Debian is too old and unsupported."& T) A3 B6 W+ V! K: F5 X* Z! b
  56.         exit
    $ z1 {8 z* z& D2 i  G
  57. fi0 \/ @% [1 {& w

  58. , F6 p. W- h- r
  59. if [[ "$os" == "centos" && "$os_version" -lt 7 ]]; then, I4 K$ R5 D" }. j5 c5 x: z: \. F8 \
  60.         echo "CentOS 7 or higher is required to use this installer.8 e1 D- D+ L$ q
  61. This version of CentOS is too old and unsupported."
    % g! e2 C5 T  H1 r& ^. x# S1 B: N
  62.         exit8 {) r' |9 O9 Y& M2 J  J8 n6 ^0 h
  63. fi$ n. C  H; \$ d& j

  64. 0 D3 U! Y4 E4 p8 \; e1 i" Y7 u: n( @' z
  65. # Detect environments where $PATH does not include the sbin directories
    9 h2 q. p" L" M. g4 z. Y5 k2 {
  66. if ! grep -q sbin <<< "$PATH"; then' C& Q( o+ j5 Z) D( L% i
  67.         echo '$PATH does not include sbin. Try using "su -" instead of "su".'. l/ q- L$ L; x1 Q
  68.         exit6 N$ B# r1 S- w
  69. fi
    # F. B2 M6 Z/ a8 [
  70. . _; L5 k' ~) {) L
  71. if [[ "$EUID" -ne 0 ]]; then
    ( W' h( r2 a* v! V! Y/ G
  72.         echo "This installer needs to be run with superuser privileges."6 w! w4 ~7 S: E4 k
  73.         exit
    * t+ K5 \' _! T6 b
  74. fi7 i$ S) t2 d: M

  75. 9 P$ s. `% x: V- L  B
  76. if [[ ! -e /dev/net/tun ]] || ! ( exec 7<>/dev/net/tun ) 2>/dev/null; then- |7 ?, Y' ^8 S: {% F8 I
  77.         echo "The system does not have the TUN device available.
      P& g; k) Y. J
  78. TUN needs to be enabled before running this installer."9 p, h9 v2 u+ E9 o3 y2 N
  79.         exit
    . V* A5 h1 @$ a9 l/ B" r& c" U
  80. fi
    * u2 g5 l2 Q* O4 k* r
  81. , G% F, H& v! n5 K0 n
  82. new_client () {
    2 ?) D. }/ D/ m( k+ y( b' k0 s
  83.         # Generates the custom client.ovpn
    3 P2 u5 A) r3 y- s" N3 Y
  84.         {
    " s  k# X. B$ y5 s# H& G( |% }
  85.         cat /etc/openvpn/server/client-common.txt4 y# s) `; s4 c  C, Y
  86.         echo "<ca>"# e9 K# n1 T7 |7 l, _  ~& m3 R
  87.         cat /etc/openvpn/server/easy-rsa/pki/ca.crt# i  C( O4 y+ r' J# N, F, o! X3 ^
  88.         echo "</ca>"
    ' r. c" f# I; a
  89.         echo "<cert>"4 E4 M8 a! c8 X0 i2 ~1 @
  90.         sed -ne '/BEGIN CERTIFICATE/,$ p' /etc/openvpn/server/easy-rsa/pki/issued/"$client".crt
    8 a% t0 ]! [; `6 m; A
  91.         echo "</cert>"
      n& `, K, Y8 L" _/ [8 g
  92.         echo "<key>"
    $ C8 x0 M3 E1 e
  93.         cat /etc/openvpn/server/easy-rsa/pki/private/"$client".key
    1 D0 u) |, w+ w; ~( O
  94.         echo "</key>"
    & c  k- v$ {& k; g0 S7 \, \
  95.         echo "<tls-crypt>"1 P, z2 N5 `- b  _
  96.         sed -ne '/BEGIN OpenVPN Static key/,$ p' /etc/openvpn/server/tc.key
    6 R0 ^: g( J5 b
  97.         echo "</tls-crypt>"
    ; O# F4 h- h6 u  [; d
  98.         } > ~/"$client".ovpn9 @9 E$ o3 f/ Y4 Z
  99. }
    8 @9 W% u% x* |; O

  100. 3 b; P- g/ @' L) D6 S5 c% f
  101. if [[ ! -e /etc/openvpn/server/server.conf ]]; then
    " w0 ?- y; F5 M0 I
  102.         # Detect some Debian minimal setups where neither wget nor curl are installed
    # B0 @* c/ M' q# R
  103.         if ! hash wget 2>/dev/null && ! hash curl 2>/dev/null; then
    5 [7 D! j( ?1 C/ c5 G
  104.                 echo "Wget is required to use this installer."9 Z8 H  v+ I% |
  105.                 read -n1 -r -p "Press any key to install Wget and continue..."
    - [8 Z2 I0 S5 K+ ^
  106.                 apt-get update6 b& `8 S  L" z' p2 a" T9 r
  107.                 apt-get install -y wget- ]- l5 C$ T+ H) n0 C. _
  108.         fi6 R$ S# D! F8 A5 f
  109.         clear
      r) R; L; D8 x2 F/ S
  110.         echo 'Welcome to this OpenVPN road warrior installer!'4 T6 V+ C& h5 Z6 }: E1 Z/ a7 H
  111.         # If system has a single IPv4, it is selected automatically. Else, ask the user4 q8 l5 q* C- t! q4 O& A
  112.         if [[ $(ip -4 addr | grep inet | grep -vEc '127(\.[0-9]{1,3}){3}') -eq 1 ]]; then
    4 Z, y  W+ }% _0 ]+ v; f: q  f
  113.                 ip=$(ip -4 addr | grep inet | grep -vE '127(\.[0-9]{1,3}){3}' | cut -d '/' -f 1 | grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}')
    : |: t4 I* @. H$ @/ Y! `- {# W
  114.         else; f' I9 g6 W2 p( E& Y
  115.                 number_of_ip=$(ip -4 addr | grep inet | grep -vEc '127(\.[0-9]{1,3}){3}')7 b: `$ K7 n& s* C* r
  116.                 echo
    : G2 u$ q$ Q: I
  117.                 echo "Which IPv4 address should be used?"5 P6 N% ?8 N5 l" r) w
  118.                 ip -4 addr | grep inet | grep -vE '127(\.[0-9]{1,3}){3}' | cut -d '/' -f 1 | grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}' | nl -s ') '
    # E3 v- `( f! L7 L$ k. k
  119.                 read -p "IPv4 address [1]: " ip_number
    8 i4 q  l* O$ o+ Z
  120.                 until [[ -z "$ip_number" || "$ip_number" =~ ^[0-9]+$ && "$ip_number" -le "$number_of_ip" ]]; do1 ^! G. @) j! o
  121.                         echo "$ip_number: invalid selection."
    : D6 C- a% ]" |7 R
  122.                         read -p "IPv4 address [1]: " ip_number. D" \# i( ~0 a& o7 z  B* P3 ~
  123.                 done, w" q* v2 G0 ?3 q/ r) |# z# e
  124.                 [[ -z "$ip_number" ]] && ip_number="1"6 s8 V1 N8 q4 v% w. }8 _9 S
  125.                 ip=$(ip -4 addr | grep inet | grep -vE '127(\.[0-9]{1,3}){3}' | cut -d '/' -f 1 | grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}' | sed -n "$ip_number"p)3 f% ?4 V. \8 c4 c8 h1 E
  126.         fi
    8 B# n  X1 J6 A+ a' ?2 y! F$ H
  127.         # If $ip is a private IP address, the server must be behind NAT1 M# ^, [7 I; D) y$ f2 U1 a4 Z
  128.         if echo "$ip" | grep -qE '^(10\.|172\.1[6789]\.|172\.2[0-9]\.|172\.3[01]\.|192\.168)'; then# B9 E5 H0 l6 e
  129.                 echo
    0 C) I. m7 {2 J/ z
  130.                 echo "This server is behind NAT. What is the public IPv4 address or hostname?"* {- {0 D2 C" h
  131.                 # Get public IP and sanitize with grep
    * B5 c% q1 ]' J0 p! i/ K
  132.                 get_public_ip=$(grep -m 1 -oE '^[0-9]{1,3}(\.[0-9]{1,3}){3}$' <<< "$(wget -T 10 -t 1 -4qO- "http://ip1.dynupdate.no-ip.com/" || curl -m 10 -4Ls "http://ip1.dynupdate.no-ip.com/")")+ ?3 {: I  {8 ~7 J1 v) P+ i
  133.                 read -p "Public IPv4 address / hostname [$get_public_ip]: " public_ip
    ; g: ]/ h+ p' U2 w; P
  134.                 # If the checkip service is unavailable and user didn't provide input, ask again
    + }$ v* @3 j& B) u. N* I/ ?+ d& Y
  135.                 until [[ -n "$get_public_ip" || -n "$public_ip" ]]; do" O7 m7 N( D/ ?- C- q+ ]. Y/ _
  136.                         echo "Invalid input."  [; ~5 z* D  R
  137.                         read -p "Public IPv4 address / hostname: " public_ip
    - B( F' ]8 a) Y2 Q) s
  138.                 done
    3 F0 F2 u* U; x+ Y9 T0 T0 `
  139.                 [[ -z "$public_ip" ]] && public_ip="$get_public_ip"5 X6 [5 N$ f1 N; W: s
  140.         fi& B5 T& a" p) F! s3 w* H0 `7 O
  141.         # If system has a single IPv6, it is selected automatically
    * z/ ~  T9 q$ m! ]4 J/ V# K) d
  142.         if [[ $(ip -6 addr | grep -c 'inet6 [23]') -eq 1 ]]; then
    & \- l! b+ Q6 f, k0 i* V1 z. P
  143.                 ip6=$(ip -6 addr | grep 'inet6 [23]' | cut -d '/' -f 1 | grep -oE '([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}')
    , L9 m- ]6 y, Q# e# r
  144.         fi* ?1 s- T" n+ A2 F- |
  145.         # If system has multiple IPv6, ask the user to select one
    / C% J, e! h6 I  r
  146.         if [[ $(ip -6 addr | grep -c 'inet6 [23]') -gt 1 ]]; then% `3 C3 j/ ?" e# I/ ]( A  E
  147.                 number_of_ip6=$(ip -6 addr | grep -c 'inet6 [23]')
    ) V+ j4 h3 q2 d- c. t! U5 G( O
  148.                 echo6 u8 b+ K* {  N; J1 {
  149.                 echo "Which IPv6 address should be used?". j4 C% ], p% M! J. t+ u: a
  150.                 ip -6 addr | grep 'inet6 [23]' | cut -d '/' -f 1 | grep -oE '([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}' | nl -s ') '' g' I8 |2 h% Y: a3 [
  151.                 read -p "IPv6 address [1]: " ip6_number
    0 \6 a" e. z& h( K, u( B" {& p
  152.                 until [[ -z "$ip6_number" || "$ip6_number" =~ ^[0-9]+$ && "$ip6_number" -le "$number_of_ip6" ]]; do3 O" j1 Q4 @/ y; G) S
  153.                         echo "$ip6_number: invalid selection."
    . f, U  J. u. L% x& H9 ?! Z
  154.                         read -p "IPv6 address [1]: " ip6_number
    1 ~* j3 B! @8 R
  155.                 done# u$ ~$ _! t: \' {9 n
  156.                 [[ -z "$ip6_number" ]] && ip6_number="1"& S: E( ?2 ~8 S9 a
  157.                 ip6=$(ip -6 addr | grep 'inet6 [23]' | cut -d '/' -f 1 | grep -oE '([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}' | sed -n "$ip6_number"p)
    5 U! G% y& M& c! D% t0 Z
  158.         fi; |. G5 C( n9 D
  159.         echo1 `' x( K$ J6 u, z) Z$ M
  160.         echo "Which protocol should OpenVPN use?"5 ~; O! Z2 s. X& m
  161.         echo "   1) UDP (recommended)"
    2 j2 x' z: f1 L' D5 i& l  t8 z
  162.         echo "   2) TCP"
    1 K- Q8 Q! {6 {3 ~' ?! W7 x
  163.         read -p "Protocol [1]: " protocol/ }# ^! }8 C  W: N0 _
  164.         until [[ -z "$protocol" || "$protocol" =~ ^[12]$ ]]; do- I/ d4 |0 b1 f  ]
  165.                 echo "$protocol: invalid selection."
    , p5 }6 T, v1 u$ u
  166.                 read -p "Protocol [1]: " protocol
    ; i; u6 X) q1 Q
  167.         done# X% W4 c6 a, t! t6 u2 ^3 E
  168.         case "$protocol" in7 j: R5 I* \3 R4 K- g% s
  169.                 1|"")
    - c- D- I% c. t% I; B
  170.                 protocol=udp& ?2 V* B5 M+ \
  171.                 ;;
    ) ~0 R' `: u6 @2 |  B; s5 c
  172.                 2)
    * k) A- U/ A% E8 {2 I8 v$ Y4 a
  173.                 protocol=tcp- O) R1 N: n  ^+ M* r+ N
  174.                 ;;
    ) D) p% ^0 d: j- U; H
  175.         esac
    2 h% ~$ n1 [. z
  176.         echo
    / u- _4 O2 O6 T# }
  177.         echo "What port should OpenVPN listen to?"1 w, N" ^, B' q( k, U: b  r
  178.         read -p "Port [1194]: " port
    2 ~5 s5 _4 [. H4 w5 u% B
  179.         until [[ -z "$port" || "$port" =~ ^[0-9]+$ && "$port" -le 65535 ]]; do
    3 M1 P  E, \, t( `8 f% ?8 {
  180.                 echo "$port: invalid port."
    # g' n1 `" X. }) Q8 g
  181.                 read -p "Port [1194]: " port% A7 Z' X& X: d8 D- a
  182.         done
    ( v' H' _# d; l" @% f
  183.         [[ -z "$port" ]] && port="1194"
    ' n: |. F# B- Z" T
  184.         echo
    ; _7 _4 o$ G% Y
  185.         echo "Select a DNS server for the clients:"
    ( K! J7 y7 s% M) A
  186.         echo "   1) Current system resolvers"" x+ w9 \- A7 m! P0 m/ c
  187.         echo "   2) Google": v1 w3 G( X5 X
  188.         echo "   3) 1.1.1.1": o) U4 z( C: y8 |' ?
  189.         echo "   4) OpenDNS"
    9 K7 Q" r8 w, N/ U& a3 r0 N! ?
  190.         echo "   5) Quad9"
    4 \1 I" T. ]6 _
  191.         echo "   6) AdGuard"
    2 s$ q1 J, H8 M' Z/ v; t
  192.         read -p "DNS server [1]: " dns
    # L) Z" m! j7 t$ x( @7 M
  193.         until [[ -z "$dns" || "$dns" =~ ^[1-6]$ ]]; do# n9 s: S" l: N  b
  194.                 echo "$dns: invalid selection."
    9 }  H! Y/ B: V0 K" B) ^
  195.                 read -p "DNS server [1]: " dns% Z) G+ l9 `  C9 R. G. Y
  196.         done$ s8 P8 m9 Y& ], i7 S
  197.         echo/ g  ?8 v5 [- t7 h: {
  198.         echo "Enter a name for the first client:": F; v7 v6 U1 y2 p" B
  199.         read -p "Name [client]: " unsanitized_client
    6 k4 f8 r2 r/ r
  200.         # Allow a limited set of characters to avoid conflicts
    0 k& V4 s' H& y1 K) X
  201.         client=$(sed 's/[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-]/_/g' <<< "$unsanitized_client")
    * z$ P- a/ ~4 G- [0 d
  202.         [[ -z "$client" ]] && client="client"
    / t$ }, E1 o6 M1 f) e
  203.         echo5 B2 m7 P0 \' Z
  204.         echo "OpenVPN installation is ready to begin."8 U1 W1 v$ X- v
  205.         # Install a firewall if firewalld or iptables are not already available
      D, w/ d: P4 Q+ D
  206.         if ! systemctl is-active --quiet firewalld.service && ! hash iptables 2>/dev/null; then
    ( B* f0 a& l2 m* b, ~( l" V5 i
  207.                 if [[ "$os" == "centos" || "$os" == "fedora" ]]; then, \' ^" x2 M% ?( E5 E# _
  208.                         firewall="firewalld". S& W1 H) p& }9 F6 N
  209.                         # We don't want to silently enable firewalld, so we give a subtle warning
    " s7 W! Y1 {9 ^/ F# R( B
  210.                         # If the user continues, firewalld will be installed and enabled during setup3 J& J% s4 x" G# x2 S2 }
  211.                         echo "firewalld, which is required to manage routing tables, will also be installed."* ~/ T) Q& e7 J- k6 J$ Z) q: z
  212.                 elif [[ "$os" == "debian" || "$os" == "ubuntu" ]]; then
    8 p  \- U+ y1 |: U& A' r' X2 y7 d
  213.                         # iptables is way less invasive than firewalld so no warning is given6 x& B4 V/ ^% J% Y$ E
  214.                         firewall="iptables"
    : s2 L2 e! [) X8 P+ o4 v
  215.                 fi  a* \: r" b% v
  216.         fi, @/ V5 g. |5 K8 Y+ j
  217.         read -n1 -r -p "Press any key to continue...", {3 L! d- q) M
  218.         # If running inside a container, disable LimitNPROC to prevent conflicts
    $ }* d1 s" e3 Q6 M" {& X
  219.         if systemd-detect-virt -cq; then
    6 \# L/ K$ r5 T2 P1 L/ u6 f
  220.                 mkdir /etc/systemd/system/openvpn-server@server.service.d/ 2>/dev/null
    ' v! D: f6 f* X- G  x
  221.                 echo "[Service]! v1 @( a3 F4 {5 t' S8 ]
  222. LimitNPROC=infinity" > /etc/systemd/system/openvpn-server@server.service.d/disable-limitnproc.conf
    2 Y9 \) ?' m+ p5 n0 `3 Y
  223.         fi
    # R) P3 h5 q, t: k
  224.         if [[ "$os" = "debian" || "$os" = "ubuntu" ]]; then) n+ W3 u" d. {' J
  225.                 apt-get update- \2 I" g; j3 x/ a
  226.                 apt-get install -y openvpn openssl ca-certificates $firewall
    % {4 s  l1 m& |  u( z
  227.         elif [[ "$os" = "centos" ]]; then5 L2 g$ h  u/ A3 I' a
  228.                 yum install -y epel-release
    $ V0 u. ^- x1 b7 B; o4 a, A4 }
  229.                 yum install -y openvpn openssl ca-certificates tar $firewall+ c! \; g3 p* j; [% _, f) T7 i1 L
  230.         else
    " \+ r+ P; b' A' C8 [) \
  231.                 # Else, OS must be Fedora- H" z, G( _% D; j- A" l
  232.                 dnf install -y openvpn openssl ca-certificates tar $firewall
    7 T/ e" h  l. w4 J/ v  r) J9 @4 c
  233.         fi
      K, S7 ^8 r# |% Y/ m
  234.         # If firewalld was just installed, enable it" }) ~& N. G3 _& G
  235.         if [[ "$firewall" == "firewalld" ]]; then
    % v! }  W' M, |! M* I: s3 A. I
  236.                 systemctl enable --now firewalld.service$ v0 R5 N$ C' d3 C9 A
  237.         fi3 M- r. F7 V  w9 v: e! i) U
  238.         # Get easy-rsa
    0 Z! g* y2 a' E3 R5 W
  239.         easy_rsa_url='https://github.com/OpenVPN/easy-rsa/releases/download/v3.1.0/EasyRSA-3.1.0.tgz'6 N) P" s! B8 g- s& a: E2 v9 F# r
  240.         mkdir -p /etc/openvpn/server/easy-rsa/- m" V# ?/ D0 G' ]5 P; b
  241.         { wget -qO- "$easy_rsa_url" 2>/dev/null || curl -sL "$easy_rsa_url" ; } | tar xz -C /etc/openvpn/server/easy-rsa/ --strip-components 1* e1 C; W6 S6 H# w
  242.         chown -R root:root /etc/openvpn/server/easy-rsa/5 p' b& K" v7 l4 y" \& H" n' M
  243.         cd /etc/openvpn/server/easy-rsa/
    ; _  ^0 v( e, M+ t* K4 {! J: [
  244.         # Create the PKI, set up the CA and the server and client certificates/ G* {/ Q9 g; X  O% J
  245.         ./easyrsa init-pki
    " m7 l+ d- X' O- a. w8 {
  246.         ./easyrsa --batch build-ca nopass
    " Y8 N. t6 M' G, I! }
  247.         EASYRSA_CERT_EXPIRE=3650 ./easyrsa build-server-full server nopass# g2 X, Y" @" P: \, ]# @  k
  248.         EASYRSA_CERT_EXPIRE=3650 ./easyrsa build-client-full "$client" nopass
    ! s1 Q4 ~* m) i% Y" w
  249.         EASYRSA_CRL_DAYS=3650 ./easyrsa gen-crl
    5 Y0 {& ]+ U: _/ {1 m% E
  250.         # Move the stuff we need
    8 `$ x9 K/ \9 @: f- y$ w8 B1 t! X
  251.         cp pki/ca.crt pki/private/ca.key pki/issued/server.crt pki/private/server.key pki/crl.pem /etc/openvpn/server
    % T5 D: |* T$ i
  252.         # CRL is read with each client connection, while OpenVPN is dropped to nobody
      O; S& y2 y" N5 A4 L( Z& a
  253.         chown nobody:"$group_name" /etc/openvpn/server/crl.pem, B4 _; g7 d8 V, x# i4 s
  254.         # Without +x in the directory, OpenVPN can't run a stat() on the CRL file9 T4 Q/ K6 D; T( x1 s
  255.         chmod o+x /etc/openvpn/server/
    + @/ P; M+ b9 l5 K, d
  256.         # Generate key for tls-crypt
    6 }/ L- M" x; e7 [
  257.         openvpn --genkey --secret /etc/openvpn/server/tc.key6 o* H- s& T: [- t$ H
  258.         # Create the DH parameters file using the predefined ffdhe2048 group6 W8 j3 y1 _2 Q) \1 e/ f  H: M
  259.         echo '-----BEGIN DH PARAMETERS-----
    " W3 _$ a. F, N$ J. m
  260. MIIBCAKCAQEA//////////+t+FRYortKmq/cViAnPTzx2LnFg84tNpWp4TZBFGQz
    3 R4 p- l8 ~* e* f
  261. +8yTnc4kmz75fS/jY2MMddj2gbICrsRhetPfHtXV/WVhJDP1H18GbtCFY2VVPe0a! N3 v) E% A! N9 h" L9 t+ b1 z- }2 T
  262. 87VXE15/V8k1mE8McODmi3fipona8+/och3xWKE2rec1MKzKT0g6eXq8CrGCsyT7+ z* Z3 R4 G; X' R
  263. YdEIqUuyyOP7uWrat2DX9GgdT0Kj3jlN9K5W7edjcrsZCwenyO4KbXCeAvzhzffi
    + s8 v+ G* R+ F
  264. 7MA0BM0oNC9hkXL+nOmFg/+OTxIy7vKBg8P+OxtMb61zO7X8vC7CIAXFjvGDfRaD
    + B% @- x/ R6 I! Z& a
  265. ssbzSibBsu/6iGtCOGEoXJf//////////wIBAg==/ K+ u5 z' o- W
  266. -----END DH PARAMETERS-----' > /etc/openvpn/server/dh.pem
    6 t) a: U- q9 H: g
  267.         # Generate server.conf
    & w7 K; D5 Q# F
  268.         echo "local $ip
    ' u& D: [( {% n7 [
  269. port $port
    / t" F8 F5 u; A. ]) @7 R
  270. proto $protocol* t0 U! Y% W- R5 s+ G/ E
  271. dev tun
    4 v: i3 C( h' O" D! Y+ l* w
  272. ca ca.crt
    , d8 t: e# O6 P, S! A- E3 A
  273. cert server.crt
    ! c6 S- J! L  c4 x) w$ y
  274. key server.key
    8 D7 Q% m" d' {" l# k) e3 K! @* {
  275. dh dh.pem
    # F2 _6 n$ M; H
  276. auth SHA512
    % h4 R4 Z4 @2 |0 O& R  P
  277. tls-crypt tc.key
    & p+ J; U/ K' w, y' S$ k- H* E' r
  278. topology subnet
      k, @, B" v& L3 n6 v
  279. server 10.8.0.0 255.255.255.0" > /etc/openvpn/server/server.conf
    8 `3 `6 }9 E3 M8 X, A
  280.         # IPv6
    $ v1 c) q3 [/ ^$ E$ j: Q* L
  281.         if [[ -z "$ip6" ]]; then0 ^. ]6 _/ B/ T2 k
  282.                 echo 'push "redirect-gateway def1 bypass-dhcp"' >> /etc/openvpn/server/server.conf  }7 j3 ^: a& O0 l% i2 ?& x
  283.         else  ^- y1 ^+ w& b, T# W3 F. K  a
  284.                 echo 'server-ipv6 fddd:1194:1194:1194::/64' >> /etc/openvpn/server/server.conf  t) ~" V! t# }; a# Q3 A+ X
  285.                 echo 'push "redirect-gateway def1 ipv6 bypass-dhcp"' >> /etc/openvpn/server/server.conf
    ; A: U. ~; G0 |. T$ N
  286.         fi0 v0 T% ]$ W3 i+ N9 q
  287.         echo 'ifconfig-pool-persist ipp.txt' >> /etc/openvpn/server/server.conf: ~! D8 B6 M1 ?  V1 Q" g  a+ C" \
  288.         # DNS
    0 _4 U. e. I1 t2 m, r- d
  289.         case "$dns" in3 Y- V# d! |# c. g6 K/ N! ]/ E1 a
  290.                 1|"")
    ! l0 p" y1 M3 D, p7 v& _" a4 s; y
  291.                         # Locate the proper resolv.conf
    ) B. u. u" C6 L2 T7 i& t9 W1 t" }
  292.                         # Needed for systems running systemd-resolved8 \+ a' l6 R( c  E. ~
  293.                         if grep -q '^nameserver 127.0.0.53' "/etc/resolv.conf"; then* \: R) B0 p3 W" A% x
  294.                                 resolv_conf="/run/systemd/resolve/resolv.conf"
    & g1 w' s( j1 K0 ]
  295.                         else
    % i) I( p6 K& l7 o- G, m
  296.                                 resolv_conf="/etc/resolv.conf"
    " X& p$ C3 a% z* B2 _9 i
  297.                         fi
    / _  C3 m. C7 F9 n* o
  298.                         # Obtain the resolvers from resolv.conf and use them for OpenVPN: \- o7 @# ~8 M' B7 z* R
  299.                         grep -v '^#\|^;' "$resolv_conf" | grep '^nameserver' | grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}' | while read line; do
    . B+ u4 b) ?# L6 N
  300.                                 echo "push "dhcp-option DNS $line"" >> /etc/openvpn/server/server.conf
    / Q8 w' i2 Q! W5 O
  301.                         done" d6 ~. a7 H. {& [: ]' ]( v
  302.                 ;;: r6 \1 u2 F4 f- f5 K
  303.                 2)
    7 h4 \; X/ S1 N& @1 T. ~: r
  304.                         echo 'push "dhcp-option DNS 8.8.8.8"' >> /etc/openvpn/server/server.conf
    2 J9 P! d. D" n
  305.                         echo 'push "dhcp-option DNS 8.8.4.4"' >> /etc/openvpn/server/server.conf
    : D7 X. |5 {, |2 u1 [
  306.                 ;;' M! w+ b* }  J. n% N! G( {( M
  307.                 3)
    ) l# u* c* ^( i3 g! W# l) ^. j
  308.                         echo 'push "dhcp-option DNS 1.1.1.1"' >> /etc/openvpn/server/server.conf* l8 O2 [8 d- N9 {
  309.                         echo 'push "dhcp-option DNS 1.0.0.1"' >> /etc/openvpn/server/server.conf
    ; W" b" t; X- P  d; C, S
  310.                 ;;: C% B  i2 B" q- @: [$ J0 I. F) g
  311.                 4)
    : N; S: t. E4 t! o0 o5 |
  312.                         echo 'push "dhcp-option DNS 208.67.222.222"' >> /etc/openvpn/server/server.conf/ E5 G. j. t8 R# p, P
  313.                         echo 'push "dhcp-option DNS 208.67.220.220"' >> /etc/openvpn/server/server.conf! j- g: o; r/ A
  314.                 ;;- j" ~. Y( b, T2 O! }% R
  315.                 5)* s  P9 W# m0 e+ y' J
  316.                         echo 'push "dhcp-option DNS 9.9.9.9"' >> /etc/openvpn/server/server.conf# G# x# |  }/ g& t. M! u
  317.                         echo 'push "dhcp-option DNS 149.112.112.112"' >> /etc/openvpn/server/server.conf
    $ F$ @5 F8 A  |
  318.                 ;;9 u% k, i. E! ^1 c% t# `1 |
  319.                 6)
    / _  q+ t( v7 ]. g- k
  320.                         echo 'push "dhcp-option DNS 94.140.14.14"' >> /etc/openvpn/server/server.conf
    + ?( R) f9 J: Y6 Q
  321.                         echo 'push "dhcp-option DNS 94.140.15.15"' >> /etc/openvpn/server/server.conf* ?. {, G! i, G1 {0 w8 C
  322.                 ;;- V- h$ h( Q+ S8 Q- B. H
  323.         esac
    7 t! D/ |# x9 |7 f  b1 J7 m7 y2 J9 a
  324.         echo "keepalive 10 1208 N/ |& @6 {+ _8 z$ J
  325. cipher AES-256-CBC
    : \; g& S; \" R: E2 a4 H9 s
  326. user nobody# \6 [: S. s, Z: P- Y: M# V7 [
  327. group $group_name
    8 `0 D( R) O4 y% m6 d
  328. persist-key! S$ Y+ x, o3 `/ Z7 x0 i% e
  329. persist-tun
    # ^* N/ T$ j4 g) R
  330. verb 3
    9 h8 W% E' \2 V( E9 e7 h, U+ B
  331. crl-verify crl.pem" >> /etc/openvpn/server/server.conf
    $ h( @# T! R3 g+ |& i( y
  332.         if [[ "$protocol" = "udp" ]]; then* c2 K8 E6 S9 v# ~6 z
  333.                 echo "explicit-exit-notify" >> /etc/openvpn/server/server.conf
    ) E, r2 Q4 c) b5 a4 X
  334.         fi
    / N) C& \: F0 i* x: ]. r
  335.         # Enable net.ipv4.ip_forward for the system* V: B2 ]5 N! `* c/ p5 r
  336.         echo 'net.ipv4.ip_forward=1' > /etc/sysctl.d/99-openvpn-forward.conf' _% B3 U: Z8 w6 t. O! s* n8 b+ T8 j
  337.         # Enable without waiting for a reboot or service restart
    4 X3 y, T" [8 r  @+ w+ \' u$ c
  338.         echo 1 > /proc/sys/net/ipv4/ip_forward6 k3 k5 p: {; D
  339.         if [[ -n "$ip6" ]]; then, o3 a; x3 }3 e4 C1 d
  340.                 # Enable net.ipv6.conf.all.forwarding for the system
    4 ?2 v- T* C% o, G, R' j- L* Y( H& t( k
  341.                 echo "net.ipv6.conf.all.forwarding=1" >> /etc/sysctl.d/99-openvpn-forward.conf
    1 B# z' Y; L) Y; ]% ]3 K4 G- R
  342.                 # Enable without waiting for a reboot or service restart, f9 T* i8 ?' V+ ~; I
  343.                 echo 1 > /proc/sys/net/ipv6/conf/all/forwarding
    & Z9 i+ R: O+ B, G. [0 A
  344.         fi
    # @, L- |! \" e
  345.         if systemctl is-active --quiet firewalld.service; then4 w6 z" b# y( H6 \9 [) v
  346.                 # Using both permanent and not permanent rules to avoid a firewalld
    7 ?# e% R" N" X( u! X# F
  347.                 # reload.1 I: R  ~- M% ^6 ~
  348.                 # We don't use --add-service=openvpn because that would only work with
    ( |" n! i4 K" B) R/ `4 g
  349.                 # the default port and protocol.
    + o' L8 V( {- N% v
  350.                 firewall-cmd --add-port="$port"/"$protocol"# o/ u6 Z  v3 J4 `# H  f- J
  351.                 firewall-cmd --zone=trusted --add-source=10.8.0.0/24( ]! p; G& Z! p& K
  352.                 firewall-cmd --permanent --add-port="$port"/"$protocol"
    0 l. ?" F& {5 n
  353.                 firewall-cmd --permanent --zone=trusted --add-source=10.8.0.0/242 R/ r0 O0 s1 x$ |
  354.                 # Set NAT for the VPN subnet
    ( s9 A( z5 w$ z1 N, L/ L4 R
  355.                 firewall-cmd --direct --add-rule ipv4 nat POSTROUTING 0 -s 10.8.0.0/24 ! -d 10.8.0.0/24 -j SNAT --to "$ip"# D& p6 J, b+ U& o8 t5 C
  356.                 firewall-cmd --permanent --direct --add-rule ipv4 nat POSTROUTING 0 -s 10.8.0.0/24 ! -d 10.8.0.0/24 -j SNAT --to "$ip"0 K  {) D3 L5 A0 e
  357.                 if [[ -n "$ip6" ]]; then
    % g3 x* A! k: ^! ~( [8 R. x3 B
  358.                         firewall-cmd --zone=trusted --add-source=fddd:1194:1194:1194::/64$ _: e8 I" ~9 |  u2 a
  359.                         firewall-cmd --permanent --zone=trusted --add-source=fddd:1194:1194:1194::/64
    1 j3 l% d. Z( X; w+ F& }
  360.                         firewall-cmd --direct --add-rule ipv6 nat POSTROUTING 0 -s fddd:1194:1194:1194::/64 ! -d fddd:1194:1194:1194::/64 -j SNAT --to "$ip6"
    % C$ l2 [. s! D0 M' |
  361.                         firewall-cmd --permanent --direct --add-rule ipv6 nat POSTROUTING 0 -s fddd:1194:1194:1194::/64 ! -d fddd:1194:1194:1194::/64 -j SNAT --to "$ip6"
    ; s' {' L% j+ y. D7 ?
  362.                 fi5 \3 z- Y# M. f8 i% ^/ U- R
  363.         else
    : D$ J5 c7 f% I+ {) Q
  364.                 # Create a service to set up persistent iptables rules& r0 s3 y' y/ k( {* U
  365.                 iptables_path=$(command -v iptables)
    7 o" G* c! s5 l: ^
  366.                 ip6tables_path=$(command -v ip6tables)
    & [% E0 N* P& q+ ~7 d4 c; I
  367.                 # nf_tables is not available as standard in OVZ kernels. So use iptables-legacy" h# t( @, q6 q& e! j- H3 T3 W' j' V
  368.                 # if we are in OVZ, with a nf_tables backend and iptables-legacy is available.
    ! i' Y" `, {3 o* q$ |4 e
  369.                 if [[ $(systemd-detect-virt) == "openvz" ]] && readlink -f "$(command -v iptables)" | grep -q "nft" && hash iptables-legacy 2>/dev/null; then. g/ z4 A7 M9 @! |* T& m
  370.                         iptables_path=$(command -v iptables-legacy)- G! i2 q$ z* H  k8 x4 {
  371.                         ip6tables_path=$(command -v ip6tables-legacy)
    / x+ S# V4 U) \& \5 B! K  Z  m
  372.                 fi
    3 @+ ?6 X7 \. x# N
  373.                 echo "[Unit]; y- {* e$ ?) v0 R0 F
  374. Before=network.target
    0 `3 h5 `1 h7 M! L
  375. [Service]; V7 R3 ^% j% F, e7 c' o
  376. Type=oneshot$ @8 v$ Z* A$ ~# L! \% b5 c: i9 L
  377. ExecStart=$iptables_path -t nat -A POSTROUTING -s 10.8.0.0/24 ! -d 10.8.0.0/24 -j SNAT --to $ip
    , h; I, L( t+ y) a$ M3 m; u  Q6 r2 s
  378. ExecStart=$iptables_path -I INPUT -p $protocol --dport $port -j ACCEPT
    + w( c0 ~3 R# z
  379. ExecStart=$iptables_path -I FORWARD -s 10.8.0.0/24 -j ACCEPT* [- J4 c/ i4 r# H- {+ m3 s' K) P
  380. ExecStart=$iptables_path -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
    % B1 M$ k$ o+ p6 K# A
  381. ExecStop=$iptables_path -t nat -D POSTROUTING -s 10.8.0.0/24 ! -d 10.8.0.0/24 -j SNAT --to $ip
    1 j4 i" T5 Z4 [/ |
  382. ExecStop=$iptables_path -D INPUT -p $protocol --dport $port -j ACCEPT
    " F) Z; T) K3 R# e9 l( \6 p% T! {" c- Q
  383. ExecStop=$iptables_path -D FORWARD -s 10.8.0.0/24 -j ACCEPT
    # ^- {+ D6 ?( S. p7 `8 m
  384. ExecStop=$iptables_path -D FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT" > /etc/systemd/system/openvpn-iptables.service
    - @, X/ s5 {# `# d; i/ B
  385.                 if [[ -n "$ip6" ]]; then' j+ X  n7 ^, S2 g5 e, q9 E  g
  386.                         echo "ExecStart=$ip6tables_path -t nat -A POSTROUTING -s fddd:1194:1194:1194::/64 ! -d fddd:1194:1194:1194::/64 -j SNAT --to $ip6: [  S( r; A* I1 B+ K
  387. ExecStart=$ip6tables_path -I FORWARD -s fddd:1194:1194:1194::/64 -j ACCEPT
    3 a, R' N3 _0 [0 n; c
  388. ExecStart=$ip6tables_path -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT6 Q4 Q; A+ q7 m
  389. ExecStop=$ip6tables_path -t nat -D POSTROUTING -s fddd:1194:1194:1194::/64 ! -d fddd:1194:1194:1194::/64 -j SNAT --to $ip6
    / `- y  P  H( s1 Y
  390. ExecStop=$ip6tables_path -D FORWARD -s fddd:1194:1194:1194::/64 -j ACCEPT- s* f% l8 O7 o3 f# O" U- D
  391. ExecStop=$ip6tables_path -D FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT" >> /etc/systemd/system/openvpn-iptables.service! |1 w5 o+ |* ^# S7 U" R  b# a
  392.                 fi, B0 S4 r8 \1 ~& ^: y, N
  393.                 echo "RemainAfterExit=yes
    - J+ D7 [' @8 Y5 h
  394. [Install]( b# D0 B1 y4 c) t0 I- W6 s3 C
  395. WantedBy=multi-user.target" >> /etc/systemd/system/openvpn-iptables.service
    / K6 t  p- _; j" t
  396.                 systemctl enable --now openvpn-iptables.service
    $ }- U. Y6 u# {9 D
  397.         fi7 \# Q. W: K- \5 j" I3 h
  398.         # If SELinux is enabled and a custom port was selected, we need this
    . i0 Q. z, b# B
  399.         if sestatus 2>/dev/null | grep "Current mode" | grep -q "enforcing" && [[ "$port" != 1194 ]]; then
    5 K. D/ M* M  ^3 A! ?7 E9 g
  400.                 # Install semanage if not already present( @. y' [8 L; `
  401.                 if ! hash semanage 2>/dev/null; then& z0 J; E, I& k& @1 F: m8 R
  402.                         if [[ "$os_version" -eq 7 ]]; then& t; P% L5 Q( e! v6 V/ d; J
  403.                                 # Centos 74 U; V  A* C& v$ @  G
  404.                                 yum install -y policycoreutils-python
    + v: W* S( Z" b/ E; S* ?' F$ f
  405.                         else- ~* I+ v* s& i6 z
  406.                                 # CentOS 8 or Fedora. R/ `8 l' D- u0 b2 O* O
  407.                                 dnf install -y policycoreutils-python-utils+ s9 K$ U$ R2 s; [! n8 H
  408.                         fi
    % U4 d! w9 w; M7 s- u! H# D8 w
  409.                 fi
    * R( Y4 L9 j6 ?
  410.                 semanage port -a -t openvpn_port_t -p "$protocol" "$port"
    5 Q7 G: F+ r( O5 ~4 q2 g6 [+ N3 P
  411.         fi: i) ~/ m  O! [+ \1 E
  412.         # If the server is behind NAT, use the correct IP address
    9 k- \' f3 A! |0 i+ W  @: K: {
  413.         [[ -n "$public_ip" ]] && ip="$public_ip"; s. K+ u9 a" \3 b# b1 n3 c9 u5 P
  414.         # client-common.txt is created so we have a template to add further users later9 U, J$ \% e' M
  415.         echo "client
    3 \/ b. N% ]8 [# s5 E
  416. dev tun& ~5 N1 I6 w1 ]6 D7 j' K
  417. proto $protocol" b6 m" D( {' L4 x& k
  418. remote $ip $port
    2 x. e" O+ _3 m
  419. resolv-retry infinite) S' [( N( b& ?! x& e
  420. nobind+ A+ y2 m: d/ \; T5 }$ X$ P
  421. persist-key& C4 q7 [5 r0 {' B
  422. persist-tun3 o# [9 C, A, m8 O: g0 y5 b
  423. remote-cert-tls server4 f0 |8 M0 K- r% |  M' ?
  424. auth SHA512
    - [5 l5 E  x, E3 [% x
  425. cipher AES-256-CBC
    1 D! Q" J( K6 y$ }0 }9 z. C
  426. ignore-unknown-option block-outside-dns: P7 A$ O/ D% n& A" [; y' \
  427. block-outside-dns" ^; U1 ~; c4 U8 ]5 u4 X) u
  428. verb 3" > /etc/openvpn/server/client-common.txt
    / \7 P& E  _2 Z6 ~1 A
  429.         # Enable and start the OpenVPN service% _; O8 }5 M% c. P  V( c
  430.         systemctl enable --now openvpn-server@server.service, L$ o3 D' j8 {
  431.         # Generates the custom client.ovpn* J2 o6 h& ]) x0 M/ U5 M# }4 m3 ?
  432.         new_client- A5 ]" X& A5 H4 j) V, A
  433.         echo
    & R! _. X" A0 V- ?* y+ z
  434.         echo "Finished!"$ P) J, u9 \: Y' D2 T
  435.         echo
    & ^2 B' K6 e% H1 Z. H
  436.         echo "The client configuration is available in:" ~/"$client.ovpn"* X" u" {  v1 B7 L/ H* k
  437.         echo "New clients can be added by running this script again."
    % e% z- U- i/ i" D' t, i: W
  438. else+ B0 C- S+ N+ h% v- @; E$ o
  439.         clear6 }# l2 q! @. d# B; X# w, ~/ p' L( B
  440.         echo "OpenVPN is already installed."3 ]& W  j4 E7 R' ?" Q7 }
  441.         echo
    7 M7 ?6 f  H# ~
  442.         echo "Select an option:"9 B; C  v  n* o) H. v
  443.         echo "   1) Add a new client"+ O% I4 q+ B, o
  444.         echo "   2) Revoke an existing client"+ v0 C& [' T: H% [
  445.         echo "   3) Remove OpenVPN"- r7 M9 ?# E# S% B0 Q: \) [" F) Q
  446.         echo "   4) Exit"% h4 l2 t; d2 T* t6 A- K' P+ j
  447.         read -p "Option: " option4 m& u: z& @, g, S. @; H  J
  448.         until [[ "$option" =~ ^[1-4]$ ]]; do
    / H- u( \* p5 u' q  f, L" V
  449.                 echo "$option: invalid selection.". c* p5 Y+ d0 t7 i' ^
  450.                 read -p "Option: " option( w$ Z  q. ]9 I
  451.         done5 u* M  |; b3 Q
  452.         case "$option" in
    # @! M! y& d" {
  453.                 1)
    2 |& I# [" Y' E  p/ Z# P# e
  454.                         echo, D' I: B5 ^' Y
  455.                         echo "Provide a name for the client:"
    9 d4 e9 \6 ?9 U
  456.                         read -p "Name: " unsanitized_client" L9 y4 b8 e8 e* h! s+ _$ I
  457.                         client=$(sed 's/[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-]/_/g' <<< "$unsanitized_client")
    ) x5 n/ F3 T8 H  D3 H
  458.                         while [[ -z "$client" || -e /etc/openvpn/server/easy-rsa/pki/issued/"$client".crt ]]; do
    ( Y% p: c- Y0 g+ j6 j
  459.                                 echo "$client: invalid name.", U; d; L/ k1 r, E
  460.                                 read -p "Name: " unsanitized_client- f9 L5 ~0 E- m8 f' U1 }
  461.                                 client=$(sed 's/[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-]/_/g' <<< "$unsanitized_client")
    6 d8 `& I# g  c* I) U- J, Y
  462.                         done
    2 ~5 W& f* J3 A( k: J4 e
  463.                         cd /etc/openvpn/server/easy-rsa/( m! F3 ~: }6 i/ x- `( `
  464.                         EASYRSA_CERT_EXPIRE=3650 ./easyrsa build-client-full "$client" nopass
    , N" _2 o/ M, X/ X
  465.                         # Generates the custom client.ovpn. B6 s; y  o# ?2 _
  466.                         new_client; H/ ^& _9 W. _  l+ s& c3 B
  467.                         echo1 P! C( z9 M5 ^  R0 x$ ~
  468.                         echo "$client added. Configuration available in:" ~/"$client.ovpn"& M0 w2 K- j& s0 k* R. m; [
  469.                         exit* g0 N- f0 f% l
  470.                 ;;
    - `1 F. }! k" g, C# N' L
  471.                 2)
    . [& o& o; K! A" c! Z
  472.                         # This option could be documented a bit better and maybe even be simplified9 }2 z0 Y& v- S% P
  473.                         # ...but what can I say, I want some sleep too
    + c5 V3 L3 F; H2 y
  474.                         number_of_clients=$(tail -n +2 /etc/openvpn/server/easy-rsa/pki/index.txt | grep -c "^V"); Z; {. N- t, }7 C/ y( e
  475.                         if [[ "$number_of_clients" = 0 ]]; then6 r! Q1 m: X1 E8 x# z
  476.                                 echo" y/ h0 q; w4 c# c) C
  477.                                 echo "There are no existing clients!"; J$ S% Z! Z. m+ c) R, N$ |
  478.                                 exit- e/ s' S! j4 L) A$ B5 Q
  479.                         fi
    0 y4 g% F6 B7 M) i. ^2 b
  480.                         echo* F% T% |1 [( d( J
  481.                         echo "Select the client to revoke:"
    3 V1 K5 }; p1 X' u, e7 e
  482.                         tail -n +2 /etc/openvpn/server/easy-rsa/pki/index.txt | grep "^V" | cut -d '=' -f 2 | nl -s ') '' S7 Q9 k3 B: p; [. q5 ]3 P* K
  483.                         read -p "Client: " client_number3 h% q/ d+ T' f# D5 w# J! y
  484.                         until [[ "$client_number" =~ ^[0-9]+$ && "$client_number" -le "$number_of_clients" ]]; do* o+ s6 c( z4 p7 t& P8 O$ q  e
  485.                                 echo "$client_number: invalid selection.". T. T0 b, \* P; ^' `% m8 @
  486.                                 read -p "Client: " client_number/ |! d/ K9 M+ P% ], `; b
  487.                         done' i0 o/ |5 S- [8 ^5 _
  488.                         client=$(tail -n +2 /etc/openvpn/server/easy-rsa/pki/index.txt | grep "^V" | cut -d '=' -f 2 | sed -n "$client_number"p)
    3 Y- F) D3 Q) y( M! T, G- |1 y: w! g
  489.                         echo3 x, q; D5 Q  x: h0 Y8 T0 n
  490.                         read -p "Confirm $client revocation? [y/N]: " revoke4 M8 N8 |8 L& _1 q
  491.                         until [[ "$revoke" =~ ^[yYnN]*$ ]]; do
    ! X8 P% A& A1 O0 ^6 q( ~( ~
  492.                                 echo "$revoke: invalid selection."# Z0 s/ h, Y! v/ I8 d7 T9 K
  493.                                 read -p "Confirm $client revocation? [y/N]: " revoke
      R+ S  L; f6 N6 N: y# f+ G; h
  494.                         done
    , ~, H6 Z, ^) v' B) j( c0 G3 N5 y
  495.                         if [[ "$revoke" =~ ^[yY]$ ]]; then
    9 q2 t, c+ f/ Y; D* l5 b1 I. U% d- B
  496.                                 cd /etc/openvpn/server/easy-rsa/
    8 _0 y& V/ j, O
  497.                                 ./easyrsa --batch revoke "$client"
    & x# u* K* w) A% r
  498.                                 EASYRSA_CRL_DAYS=3650 ./easyrsa gen-crl# C0 K! `9 Z0 }+ r3 a
  499.                                 rm -f /etc/openvpn/server/crl.pem2 P2 ^) m( V) \4 I$ u) }: M
  500.                                 cp /etc/openvpn/server/easy-rsa/pki/crl.pem /etc/openvpn/server/crl.pem
    - i6 V4 J$ g9 m2 Q" P
  501.                                 # CRL is read with each client connection, when OpenVPN is dropped to nobody
    % f7 U' X, {; l% e; B6 F8 d
  502.                                 chown nobody:"$group_name" /etc/openvpn/server/crl.pem3 y& f7 \" ]- R+ q" d3 y; u4 f
  503.                                 echo' I: c3 x. [( @/ K! t8 h9 O
  504.                                 echo "$client revoked!"" l+ W' q8 e9 u! g2 a9 L
  505.                         else
    7 P6 ~; Z. J6 _8 e
  506.                                 echo
    : B/ A+ b1 Q3 J8 x% X: m# a
  507.                                 echo "$client revocation aborted!"8 `1 q  V1 o- t
  508.                         fi
    $ t9 T; s* I9 `& N0 j7 F
  509.                         exit
    3 R" f0 q+ O  }7 s
  510.                 ;;
    ( _$ T5 }# E- v4 q
  511.                 3)9 U$ f0 y4 r: |9 P
  512.                         echo
    - G+ P  O$ k5 V& E( x, f! I
  513.                         read -p "Confirm OpenVPN removal? [y/N]: " remove; Z' w9 }+ }) y. d9 U8 T
  514.                         until [[ "$remove" =~ ^[yYnN]*$ ]]; do  i1 Z. [/ X% V4 c% g: {
  515.                                 echo "$remove: invalid selection."7 q: R- v. t1 ~6 U- O; j2 X
  516.                                 read -p "Confirm OpenVPN removal? [y/N]: " remove# e; e8 }* i$ h
  517.                         done
    - M( N# @" h4 H! P& Z
  518.                         if [[ "$remove" =~ ^[yY]$ ]]; then
    - K7 J: C  F# e" S3 k
  519.                                 port=$(grep '^port ' /etc/openvpn/server/server.conf | cut -d " " -f 2)
    # U. F" R: M$ C
  520.                                 protocol=$(grep '^proto ' /etc/openvpn/server/server.conf | cut -d " " -f 2)! t! I+ D& e& N$ N( n
  521.                                 if systemctl is-active --quiet firewalld.service; then9 v  z7 {/ i3 h% y3 {
  522.                                         ip=$(firewall-cmd --direct --get-rules ipv4 nat POSTROUTING | grep '\-s 10.8.0.0/24 '"'"'!'"'"' -d 10.8.0.0/24' | grep -oE '[^ ]+$')/ f6 I# a4 ]/ i4 y5 @! p
  523.                                         # Using both permanent and not permanent rules to avoid a firewalld reload.
    1 m% b9 {3 V( t8 R
  524.                                         firewall-cmd --remove-port="$port"/"$protocol"% G/ ~. |) U! G+ l: Z9 e7 ^
  525.                                         firewall-cmd --zone=trusted --remove-source=10.8.0.0/24. N3 U" U% x% j$ U! _/ U
  526.                                         firewall-cmd --permanent --remove-port="$port"/"$protocol"/ B5 T& D+ c- Z0 U$ N+ M
  527.                                         firewall-cmd --permanent --zone=trusted --remove-source=10.8.0.0/24- t3 O/ U' s6 }/ C
  528.                                         firewall-cmd --direct --remove-rule ipv4 nat POSTROUTING 0 -s 10.8.0.0/24 ! -d 10.8.0.0/24 -j SNAT --to "$ip"  O0 i3 E* C: e& c5 Y. y, |
  529.                                         firewall-cmd --permanent --direct --remove-rule ipv4 nat POSTROUTING 0 -s 10.8.0.0/24 ! -d 10.8.0.0/24 -j SNAT --to "$ip"
    * O/ }+ Q& [4 ^1 L, Q# r+ F4 g
  530.                                         if grep -qs "server-ipv6" /etc/openvpn/server/server.conf; then
    3 j& N0 Y* s8 w9 U7 ~2 Z9 E- O( g
  531.                                                 ip6=$(firewall-cmd --direct --get-rules ipv6 nat POSTROUTING | grep '\-s fddd:1194:1194:1194::/64 '"'"'!'"'"' -d fddd:1194:1194:1194::/64' | grep -oE '[^ ]+$')! Y' c! n. L) [4 d  y
  532.                                                 firewall-cmd --zone=trusted --remove-source=fddd:1194:1194:1194::/64# x6 n" v7 R2 ^. ~2 S5 j9 n
  533.                                                 firewall-cmd --permanent --zone=trusted --remove-source=fddd:1194:1194:1194::/64
    8 Z  c' @5 ^3 K1 N  S3 K: E
  534.                                                 firewall-cmd --direct --remove-rule ipv6 nat POSTROUTING 0 -s fddd:1194:1194:1194::/64 ! -d fddd:1194:1194:1194::/64 -j SNAT --to "$ip6", d- K* G- ]* m4 }' c
  535.                                                 firewall-cmd --permanent --direct --remove-rule ipv6 nat POSTROUTING 0 -s fddd:1194:1194:1194::/64 ! -d fddd:1194:1194:1194::/64 -j SNAT --to "$ip6"% u% _# M, S( K1 O; y
  536.                                         fi9 q/ q, E  C% c) l; c
  537.                                 else
    ! ]; D' U2 [6 G
  538.                                         systemctl disable --now openvpn-iptables.service
    6 s7 `# t, G* y- E
  539.                                         rm -f /etc/systemd/system/openvpn-iptables.service
    % A9 U# [. b/ N8 _% T, u6 W
  540.                                 fi9 X1 H! G7 M: q3 E- |* v" W% g" q
  541.                                 if sestatus 2>/dev/null | grep "Current mode" | grep -q "enforcing" && [[ "$port" != 1194 ]]; then. y$ \) \! T/ B+ k: B
  542.                                         semanage port -d -t openvpn_port_t -p "$protocol" "$port"( l) X$ E0 G9 R( q9 x1 e6 h
  543.                                 fi
    " q$ h+ \! U. H5 C" N: P
  544.                                 systemctl disable --now openvpn-server@server.service
    5 B0 n5 V2 j  W7 w( }$ c
  545.                                 rm -f /etc/systemd/system/openvpn-server@server.service.d/disable-limitnproc.conf
    7 U: {, L1 O  _  u9 g  B
  546.                                 rm -f /etc/sysctl.d/99-openvpn-forward.conf
    ( i: }, O' V; [* }) f% i. \. @
  547.                                 if [[ "$os" = "debian" || "$os" = "ubuntu" ]]; then
    - [- X  }% u5 F3 N
  548.                                         rm -rf /etc/openvpn/server$ B3 S- }! A+ u$ i' t! S
  549.                                         apt-get remove --purge -y openvpn! @5 W5 Z7 L! I& o
  550.                                 else0 R5 Z7 q$ s4 \1 {
  551.                                         # Else, OS must be CentOS or Fedora  M' m" n; k' N" D  l% O5 y
  552.                                         yum remove -y openvpn; x) Q8 z$ ^# k
  553.                                         rm -rf /etc/openvpn/server
    8 L, t6 p! g# A- w
  554.                                 fi
    ( l  P8 ?$ w0 \9 c# _
  555.                                 echo
    & y" y* R. |4 W' d8 x; |5 [! q& N
  556.                                 echo "OpenVPN removed!"
    3 V3 F9 W7 n: k. f' \6 \- |: I/ b
  557.                         else+ A. R- |- Q4 k1 F  B+ t/ t
  558.                                 echo
    - p5 v5 z3 ~3 F7 W, x
  559.                                 echo "OpenVPN removal aborted!"
    1 Y8 |+ h5 I8 Z' Z
  560.                         fi
    & k$ X  U" z4 \9 n4 n
  561.                         exit" t9 `" b' B$ W2 F
  562.                 ;;0 W5 I; Z: _) n. ^
  563.                 4)3 L% `- }) H: B
  564.                         exit; M; J' o! @: B* ?2 `! ], s. P
  565.                 ;;1 x# `! V% F2 ^$ }% r: ^
  566.         esac, F$ Z( z# [$ v, c( U3 P
  567. fi2 l' P" z0 _& \+ u+ J0 T9 q9 E
复制代码

  E2 o! _2 P) S( z
) Q5 ^# A3 u8 B( @7 f9 J) e  G% V! X8 Y6 W, X) L' i% W" u" ?

6 I. M) S* N3 [$ ^, n

shadowsocks-all.sh

601.59 KB, 下载次数: 12, 下载积分: 金钱 -5

shadowsocks-all.sh

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|

GMT+8, 2026-5-6 12:39 PM , Processed in 0.172161 second(s), 26 queries .

Powered by xyh-moon X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表