forked from sockeye44/instavpn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
217 lines (166 loc) · 6.26 KB
/
Copy pathutil.py
File metadata and controls
217 lines (166 loc) · 6.26 KB
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
215
216
217
import platform, os, logging_subprocess, random, string, logging, sys, json, urllib2, fileinput
logger = logging.getLogger()
isUbuntu = True
string_pool = string.ascii_letters + string.digits
gen_random_text = lambda s: ''.join(map(lambda _: random.choice(string_pool), range(s)))
def run_command(cmd):
return not (logging_subprocess.call(cmd,
stdout_log_level=logging.DEBUG,
stderr_log_level=logging.DEBUG,
shell=True))
def check_os():
(a,b,c) = platform.linux_distribution()
if a == 'Ubuntu'and float(b) >= 14.04:
return True
elif a[:6] == 'CentOS' and float(b[:3]) >= 6.5:
global isUbuntu
isUbuntu = False
return True
return False
def not_sudo():
return os.getuid() != 0
def install_packages():
if isUbuntu:
return install_packages_ubuntu()
return install_packages_centos()
def install_packages_centos():
logger.debug('Update package lists')
if not run_command("yum -y update"):
return False
logger.debug('Update packages')
if not run_command("yum -y upgrade"):
return False
if not install_node():
return False
logger.debug('Install vnstat')
if not run_command("yum install -y vnstat vnstati"):
return False
logger.debug('Install VPN server packages')
if not run_command("DEBIAN_FRONTEND=noninteractive yum install -q -y openswan xl2tpd ppp lsof"):
return False
return True
def install_packages_ubuntu():
logger.debug('Update package lists')
if not run_command("apt-get update"):
return False
logger.debug('Update packages')
if not run_command("apt-get -y upgrade"):
return False
#logger.debug('Install node.js')
#if not run_command("apt-get install -y nodejs-legacy npm build-essential libssl-dev"):
# return False
if not install_node():
return False
logger.debug('Install vnstat')
if not run_command("apt-get install -y vnstat vnstati"):
return False
logger.debug('Install VPN server packages')
if not run_command("DEBIAN_FRONTEND=noninteractive apt-get install -q -y openswan xl2tpd ppp lsof"):
return False
return True
def install_node():
if run_command("node -v"):
logger.info("node already installed")
return True
if not run_command("nvm --version"):
logger.debug("Install nvm")
if not run_command("curl https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash"):
return False
run_command("source ~/.nvm/nvm.sh")
if not run_command("nvm use 0.12"):
logger.debug('Install node.js')
if not run_command("nvm install 0.12"):
return False
return True
def setup_sysctl():
if not run_command("sh files/sysctl.sh"):
return False
return True
def setup_passwords():
try:
char_set = string.ascii_lowercase + string.ascii_uppercase + string.digits
f = open('/etc/ppp/chap-secrets', 'w')
pw1 = gen_random_text(12)
pw2 = gen_random_text(12)
f.write("username1 l2tpd {} *\n".format(pw1))
f.write("username2 l2tpd {} *\n".format(pw2))
f.close()
f = open('/etc/ipsec.secrets', 'w')
f.write('1.2.3.4 %any: PSK "{}"'.format(gen_random_text(16)))
f.close()
except:
logger.exception("Exception creating passwords:")
return False
return True
def cp_configs():
logger.debug('xl2tpd.conf')
if not run_command("cp files/xl2tpd.conf /etc/xl2tpd/xl2tpd.conf"):
return False
logger.debug('options.xl2tpd')
if not run_command("cp files/options.xl2tpd /etc/ppp/options.xl2tpd"):
return False
logger.debug('ipsec.conf.template')
if not run_command("cp files/ipsec.conf.template /etc/ipsec.conf.template"):
return False
return True
def setup_vpn():
logger.debug('Write setup-vpn.sh to /etc')
if not run_command("cp files/setup-vpn.sh /etc/setup-vpn.sh"):
return False
logger.debug('Add to rc.local')
try:
f = open('/etc/rc.local', 'w')
f.write('bash /etc/setup-vpn.sh\nexit 0')
f.close()
except:
logger.exception("Exception setting up vpn:")
return False
logger.debug('Execute setup-vpn.sh')
if not run_command("bash /etc/setup-vpn.sh"):
return False
logger.debug('Ufw default forward policy')
try:
for line in fileinput.input("/etc/default/ufw", inplace=True):
print line.replace('DEFAULT_FORWARD_POLICY="DROP"', 'DEFAULT_FORWARD_POLICY="ACCEPT"'),
run_command("service ufw restart")
except OSError as e:
logger.warn('ufw not found')
logger.debug('Copy CLI')
if not run_command("chmod +x files/instavpn && cp files/instavpn /usr/bin/instavpn"):
return False
return True
CRONTAB = 'crontab -l | { cat; echo "* * * * * vnstati -s -i eth0 -o /opt/instavpn/public/images/vnstat.png"; } | crontab -'
def webui():
logger.debug('Generate random password')
char_set = string.ascii_lowercase + string.ascii_uppercase + string.digits
with open('web/server/credentials.json', 'w') as f:
json.dump({
"admin": {
"login": "admin",
"password": gen_random_text(16)
}
}, f)
logger.debug('Copy web UI directory')
if not run_command("cp -rf web/ /opt/instavpn"):
return False
logger.debug('Install node_modules')
if not run_command("cd /opt/instavpn && npm install"):
return False
logger.debug('Copy upstart script')
if not run_command("cp files/instavpn.conf /etc/init"):
return False
logger.debug('Add vnstati to cron')
if not run_command(CRONTAB):
return False
logger.debug('Start service')
if not run_command("start instavpn"):
return False
return True
def info():
logger.info('')
with open('/opt/instavpn/server/credentials.json') as f:
json_data = json.load(f)
logger.info('Browse web UI at http://' + urllib2.urlopen("http://myip.dnsdynamic.org/").read() + ':7000/')
logger.info(" Username: {}".format(json_data["admin"]["login"]))
logger.info(" Password: {}".format(json_data["admin"]["password"]))
logger.info("Completed. Run 'instavpn -h' for help")