summarylogtreecommitdiffstats
path: root/purevpn
blob: 2d54aed5cfe743ee2f29a84c838d75e7e18c0f73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash

bold=$(tput bold)
normal=$(tput sgr0)

if [ "$EUID" -ne 0 ]
  then 
  echo "This script must be run as root."
  exit 1
fi

usage() {
  cat <<EOF
usage: ${0##*/} [OPTION]

Options:
    -h            Print this help message
    -l            List the available cities files
    -c [city]     Choose a city for which to install the OpenVPN config
                  (NOTE: just use the city name, the part that is ${bold}bold
                  ${normal}when you run "purevpn  -l") 
    -t [protocol] Choose the protocol to install (either "pptp" or "ovpn")
    -u [username] Set the username
    -p [password] Set the password
    -x            Purge all the PureVPN files in /etc/NetworkManager

If 'OPTION' is unspecified, ${0##*/} will not make any changes.

EOF
}

list() {

  echo "==> The following VPN locations are available"
  while IFS=, read region country city pptp udp tcp
  do
    echo "$region/$country, ${bold}$city${normal}"
  done < /usr/lib/purevpn/vpn-list.csv
}

get_pptp_url() {
  while IFS=, read region country city pptp udp tcp
  do
    if [ "${city}" == "${config}" ]; then
    echo "$pptp"
    fi
  done < /usr/lib/purevpn/vpn-list.csv
}

get_tcp_url() {
  while IFS=, read region country city pptp udp tcp
  do
    if [ "${city}" == "${config}" ]; then
    echo "$tcp"
    fi
  done < /usr/lib/purevpn/vpn-list.csv
}

get_upd_url() {
  while IFS=, read region country city pptp udp tcp
  do
    if [ "${city}" == "${config}" ]; then
    echo "$udp"
    fi
  done < /usr/lib/purevpn/vpn-list.csv
}

process_ovpn() {
  echo "Creating OpenVPN config for $config"
  mkdir -p /tmp/purevpn
  for proto in TCP UDP
  do
    filename=$(echo "${config}-${proto}" | tr '[:lower:]' '[:upper:]')
    cp "/usr/lib/purevpn/template-ovpn" "/tmp/purevpn/${filename}-PUREVPN"
    if [ "$proto" == "UDP" ]
    then
      host=$(get_upd_url)
      port=53
    elif [ "$proto" == "TCP" ]
    then
      host=$(get_tcp_url)
      port=80
      sed -i '/port=/a \proto-tcp=yes' "/tmp/purevpn/${filename}-PUREVPN"
    fi
    if [[ $host == "" ]]; then
      echo "Looks like there is no host for this city and protocol."
      exit 1
    fi
    sed -i "s/\bid=/&${filename}/" "/tmp/purevpn/${filename}-PUREVPN" #id
    sed -i "s/\buuid=/&$(uuidgen)/" "/tmp/purevpn/${filename}-PUREVPN" #uuid
    sed -i "s/\bport=/&${port}/" "/tmp/purevpn/${filename}-PUREVPN" #port
    sed -i "s/\bremote=/&$host:${port}/" "/tmp/purevpn/${filename}-PUREVPN" #host
    chmod 600 "/tmp/purevpn/${filename}-PUREVPN" #permissions
    mv "/tmp/purevpn/${filename}-PUREVPN" /etc/NetworkManager/system-connections
  done
}

process_pptp() {
  echo "Creating PPTP config for $config"
  mkdir -p /tmp/purevpn
  filename=$(echo "${config}-PPTP" | tr '[:lower:]' '[:upper:]')
    cp "/usr/lib/purevpn/template-pptp" "/tmp/purevpn/${filename}-PUREVPN"
    host=$(get_pptp_url)
    if [[ $host == "" ]]; then
      echo "Looks like there is no host for this city and protocol."
      exit 1
    fi
    sed -i "s/\bid=/&${filename}/" "/tmp/purevpn/${filename}-PUREVPN" #id
    sed -i "s/\buuid=/&$(uuidgen)/" "/tmp/purevpn/${filename}-PUREVPN" #uuid
    sed -i "s/\bgateway=/&$host/" "/tmp/purevpn/${filename}-PUREVPN" #host
    chmod 600 "/tmp/purevpn/${filename}-PUREVPN" #permissions
    mv "/tmp/purevpn/${filename}-PUREVPN" /etc/NetworkManager/system-connections
}

addpass() {
  cd /etc/NetworkManager/system-connections
  find *-PUREVPN -print0 | while read -d $'\0' file
  do
    sed -i "s/\bpassword=.*/password=${passw}/g" "$file"
  done
}

adduser() {
  cd /etc/NetworkManager/system-connections
  find *-PUREVPN -print0 | while read -d $'\0' file
  do
    if [[ $file == *"PPTP"* ]]; then
      sed -i "s/\buser=.*/user=${uname}/g" "$file"
    else
      sed -i "s/\busername=.*/username=${uname}/g" "$file"
    fi
  done
}

clean() {
  echo "Purging all PUREVPN files!"
  find /etc/NetworkManager/system-connections -name "*PUREVPN" -exec rm -v {} \;
}

while getopts "hlc:t:u:p:x" opt
  do
    case $opt in
      h) 
        usage
        exit 0
        ;;
      l)
        list
        exit 0
        ;;
      c)
        config=$OPTARG
        ;;
      t)
        protoc=$OPTARG
        ;;
      u)
        uname=$OPTARG
        ;;
      p) 
        passw=$OPTARG
	    ;;
      x)
        clean
        exit 0
        ;;
      \?)
        usage
        exit 1
        ;;
    esac
  done

## Main script

if [ -z "$protoc" ];
then
  echo "No protocol was specified. No files will be installed."
  exit 1;
fi

if [ -z "$config" ];
then
  echo "No city specified. No files will be installed."
elif list | grep "${config}" > /dev/null; then
  if [ "$protoc" == "pptp" ];
  then
    process_pptp
  else
    process_ovpn
  fi
else
  echo "We couldn't find that city."
  echo "Try running purevpn -l to list available cities."
  exit 1;
fi
      
if [[ -z "$uname" ]]; then 
  echo "No username was specified"
else
  echo "Adding the username to the installed config files."
  adduser
fi

if [[ -z "$passw" ]]; then 
  echo "No password was specified."
else
  echo "Adding the password to the installed config files."
  addpass
fi

echo "Restarting NetworkManager"
systemctl restart NetworkManager