|
@@ -53,7 +53,14 @@ class VpnGen:
|
53
|
53
|
variables = re.findall('\$\{([^}]+)}', default_config)
|
54
|
54
|
variables = self.f7(variables)
|
55
|
55
|
|
56
|
|
- return variables
|
|
56
|
+ vpn_variables = self.get_vpn_vars()
|
|
57
|
+ real_variables = []
|
|
58
|
+
|
|
59
|
+ for var in variables:
|
|
60
|
+ if var not in vpn_variables and var != "client":
|
|
61
|
+ real_variables.append(var)
|
|
62
|
+
|
|
63
|
+ return real_variables
|
57
|
64
|
|
58
|
65
|
def get_base_dir(self, vpn_name):
|
59
|
66
|
return "%s%s%s%s" % (self.ovpn_config_path, os.sep, vpn_name, os.sep)
|
|
@@ -79,7 +86,7 @@ class VpnGen:
|
79
|
86
|
|
80
|
87
|
def get_client_default_config_path(self, vpn_name):
|
81
|
88
|
base_dir = self.get_base_dir(vpn_name)
|
82
|
|
- return "%s%sclients/client.conf" % (base_dir, os.sep)
|
|
89
|
+ return "%s%sclients%sclient.conf" % (base_dir, os.sep, os.sep)
|
83
|
90
|
|
84
|
91
|
def get_client_dir(self, vpn_name, client_name):
|
85
|
92
|
base_dir = self.get_base_dir(vpn_name)
|
|
@@ -89,6 +96,10 @@ class VpnGen:
|
89
|
96
|
client_dir = self.get_client_dir(vpn_name, client_name)
|
90
|
97
|
return "%s%s-%s.conf" % (client_dir, client_name, vpn_name)
|
91
|
98
|
|
|
99
|
+ def get_client_variables_path(self, vpn_name, client_name):
|
|
100
|
+ client_dir = self.get_client_dir(vpn_name, client_name)
|
|
101
|
+ return "%svpngen.json" % client_dir
|
|
102
|
+
|
92
|
103
|
def get_client_generated_files_paths(self, vpn_name, client_name):
|
93
|
104
|
keys_dir = self.get_easy_rsa_key_dir(vpn_name,)
|
94
|
105
|
return [
|
|
@@ -107,9 +118,22 @@ class VpnGen:
|
107
|
118
|
"%sta.key" % keys_dir
|
108
|
119
|
]
|
109
|
120
|
|
|
121
|
+ def get_client_misc_files_paths(self, vpn_name):
|
|
122
|
+ base_dir = self.get_base_dir(vpn_name)
|
|
123
|
+ return glob.glob("%smisc-files%s*" % (base_dir, os.sep))
|
|
124
|
+
|
110
|
125
|
def get_all_needed_files_paths(self, vpn_name, client_name):
|
111
|
126
|
return self.get_client_generated_files_paths(vpn_name, client_name) +\
|
112
|
|
- self.get_server_needed_files_paths(vpn_name)
|
|
127
|
+ self.get_server_needed_files_paths(vpn_name) +\
|
|
128
|
+ self.get_client_misc_files_paths(vpn_name)
|
|
129
|
+
|
|
130
|
+ def get_server_variables(self, vpn_name):
|
|
131
|
+ with open(self.get_vpn_variables_path(vpn_name), "r") as f:
|
|
132
|
+ return json.load(f)['variables']
|
|
133
|
+
|
|
134
|
+ def get_client_variables(self, vpn_name, client_name):
|
|
135
|
+ with open(self.get_client_variables_path(vpn_name, client_name), "r") as f:
|
|
136
|
+ return json.load(f)['variables']
|
113
|
137
|
|
114
|
138
|
def setup_vars(self, vpn_name, variables):
|
115
|
139
|
easyrsadir = self.get_easy_rsa_dir(vpn_name)
|
|
@@ -216,9 +240,9 @@ class VpnGen:
|
216
|
240
|
|
217
|
241
|
os.chdir(curdir)
|
218
|
242
|
|
219
|
|
- self.build_client(vpn_name, client_name, variables)
|
|
243
|
+ os.makedirs(client_dir)
|
220
|
244
|
|
221
|
|
- return VpnGenError.Success
|
|
245
|
+ return self.rebuild_client(vpn_name, client_name, variables)
|
222
|
246
|
|
223
|
247
|
def remove_client(self, vpn_name, client_name):
|
224
|
248
|
base_dir = self.get_base_dir(vpn_name)
|
|
@@ -229,24 +253,20 @@ class VpnGen:
|
229
|
253
|
return VpnGenError.ClientDoesNotExists
|
230
|
254
|
return VpnGenError.Success
|
231
|
255
|
|
232
|
|
- def rebuild_clients(self, vpn_name):
|
233
|
|
- base_dir = self.get_base_dir(vpn_name)
|
234
|
|
- return VpnGenError.Success
|
235
|
|
-
|
236
|
|
- def build_client(self, vpn_name, client_name, variables):
|
|
256
|
+ def rebuild_client(self, vpn_name, client_name, variables):
|
237
|
257
|
client_dir = self.get_client_dir(vpn_name, client_name)
|
238
|
258
|
client_conf_file = self.get_client_config_path(vpn_name, client_name)
|
239
|
259
|
client_default_config_path = self.get_client_default_config_path(vpn_name)
|
240
|
|
-
|
241
|
260
|
with open(client_default_config_path, "r") as f:
|
242
|
261
|
client_default_config = f.read()
|
243
|
262
|
|
244
|
|
- variables['name'] = vpn_name
|
245
|
|
- variables['client'] = client_name
|
246
|
263
|
for variable in variables:
|
247
|
264
|
client_default_config = client_default_config.replace("${%s}" % variable, variables[variable])
|
248
|
265
|
|
249
|
|
- os.makedirs(client_dir)
|
|
266
|
+ files_names = glob.glob("%s%s*" % (client_dir, os.sep))
|
|
267
|
+ for file_name in files_names:
|
|
268
|
+ os.remove(file_name)
|
|
269
|
+
|
250
|
270
|
with open(client_conf_file, "w") as f:
|
251
|
271
|
f.write(client_default_config)
|
252
|
272
|
|
|
@@ -256,8 +276,22 @@ class VpnGen:
|
256
|
276
|
dest = "%s%s-%s%s" % (client_dir, os.path.basename(split[0]), vpn_name, split[1])
|
257
|
277
|
shutil.copy(file_path, dest)
|
258
|
278
|
|
|
279
|
+ split = os.path.splitext(client_conf_file)
|
|
280
|
+ client_ovpn_file = "%s.ovpn" % split[0]
|
|
281
|
+ shutil.copy(client_conf_file, client_ovpn_file)
|
|
282
|
+
|
259
|
283
|
files_names = glob.glob("%s%s*" % (client_dir, os.sep))
|
260
|
284
|
files_names = list(map(lambda file_path: os.path.basename(file_path), files_names))
|
261
|
285
|
|
|
286
|
+
|
262
|
287
|
call(["tar", "cfj", self.get_client_tarball_path(vpn_name, client_name),
|
263
|
288
|
"-C", client_dir] + files_names)
|
|
289
|
+
|
|
290
|
+ client_variables = {}
|
|
291
|
+ for variable in self.get_client_vars(vpn_name):
|
|
292
|
+ client_variables[variable] = variables[variable]
|
|
293
|
+
|
|
294
|
+ with open(self.get_client_variables_path(vpn_name, client_name), "w") as f:
|
|
295
|
+ json.dump({'variables': client_variables}, f, indent=4, separators=(',', ': '))
|
|
296
|
+
|
|
297
|
+ return VpnGenError.Success
|