-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenigma_common.py
153 lines (122 loc) · 2.83 KB
/
enigma_common.py
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
# -*- coding: UTF-8 -*-
import os
import json
import urllib, httplib
import urllib2
__VERSION__ = '0.1'
__TIMEOUT__ = 10
__MAX_TRIES__ = 3
__DEBUG__ = False
def read_config():
configPath = os.path.dirname(os.path.abspath(__file__)) + "/config.conf"
if (not os.path.isfile(configPath)):
print "File config.conf not found"
os._exit(-1)
f = open(configPath)
configContent = f.read()
config = {}
try:
config = json.loads(configContent)
except Exception:
print "config parse failed"
os._exit(-1)
f.close()
if not (config['key'] and config['api']):
print "config error"
os._exit(-1)
if ('debug' in config) and config['debug']:
__DEBUG__ = True
return config
config = read_config()
def print_err(err):
if err:
if (type(err) == urllib2.HTTPError):
print 'HTTPError = ' + str(err.code)
elif (type(err) == urllib2.URLError):
print 'URLError = ' + str(err.reason)
elif (type(err) == urllib2.URLError):
print 'HTTPException'
else:
import traceback
print 'Generic Exception: ' + traceback.format_exc()
def urlopen_try(reqURL, data = None):
content = None
err = None
postdata = None
if data:
postdata = urllib.urlencode(data)
i_tries = __MAX_TRIES__
while (i_tries > 0):
try:
if (postdata):
f = urllib2.urlopen(reqURL, postdata, timeout = __TIMEOUT__)
else:
f = urllib2.urlopen(reqURL, timeout = __TIMEOUT__)
content = f.read()
err = None
break
except urllib2.HTTPError, e:
err = e
except urllib2.URLError, e:
err = e
except httplib.HTTPException, e:
err = e
except Exception, e:
err = e
i_tries -= 1
if err:
print_err(err)
return content
def enigma_request(method, data):
reqURL = config['api'] + method
data['key'] = config['key']
content = urlopen_try(reqURL, data)
if not content:
return False
try:
d = json.loads(content)
except Exception:
return False
return d
def enigma_exit(r):
if not r:
os._exit(-1)
elif type(r['err']) == int:
if r['err'] != 0:
if r['message']:
print r['message']
os._exit(r['err'])
else:
os._exit(0)
else:
os._exit(-1)
def enigma_getenv(method):
env_reads = [ 'username' ]
allowed_methods = [ 'auth', 'connect', 'disconnect' ]
if method not in allowed_methods:
print 'Not an allowed method'
return False
if method == 'auth':
env_reads.append('password')
else:
env_reads += [ 'trusted_ip', 'ifconfig_pool_local_ip', 'ifconfig_pool_remote_ip' ]
if method == 'disconnect':
env_reads += [ 'bytes_received', 'bytes_sent' ]
data = {}
for env in env_reads:
data[env] = os.environ.get(env)
return data
def enigma_call(method):
data = enigma_getenv(method)
if not data:
os._exit(-1)
return
r = enigma_request(method, data)
if __DEBUG__:
f = open('/tmp/enigma-openvpn-debug.log')
f.write(str(data))
f.write('\n')
f.write(str(r))
f.write('\n')
f.close()
enigma_exit(r)