Переглянути джерело

fixed file naming; added build_client method

master
Robin Thoni 8 роки тому
джерело
коміт
280adf04bd
1 змінених файлів з 30 додано та 24 видалено
  1. 30
    24
      vpngen.py

+ 30
- 24
vpngen.py Переглянути файл

@@ -82,7 +82,7 @@ class VpnGen:
82 82
 
83 83
     def get_client_dir(self, vpn_name, client_name):
84 84
         base_dir = self.get_base_dir(vpn_name)
85
-        return "%sclients%s%s%s" % (base_dir, os.sep, client_name, os.sep)
85
+        return "%sclients%s%s-%s%s" % (base_dir, os.sep, client_name, vpn_name, os.sep)
86 86
 
87 87
     def get_client_config_path(self, vpn_name, client_name):
88 88
         client_dir = self.get_client_dir(vpn_name, client_name)
@@ -198,19 +198,6 @@ class VpnGen:
198 198
         if os.path.exists(client_dir):
199 199
             return VpnGenError.ClientAlreadyExists
200 200
 
201
-        client_default_config_path = self.get_client_default_config_path(vpn_name)
202
-        with open(client_default_config_path, "r") as f:
203
-            client_default_config = f.read()
204
-
205
-        variables['name'] = vpn_name
206
-        variables['client'] = client_name
207
-        for variable in variables:
208
-            client_default_config = client_default_config.replace("${%s}" % variable, variables[variable])
209
-
210
-        os.makedirs(client_dir)
211
-        with open(client_conf_file, "w") as f:
212
-            f.write(client_default_config)
213
-
214 201
         curdir = os.curdir
215 202
         easyrsadir = self.get_easy_rsa_dir(vpn_name)
216 203
         pkitool = self.get_pkitool_path(vpn_name)
@@ -224,16 +211,7 @@ class VpnGen:
224 211
 
225 212
         os.chdir(curdir)
226 213
 
227
-        files_paths = self.get_all_needed_files_paths(vpn_name, client_name)
228
-        for file_path in files_paths:
229
-            split = os.path.splitext(file_path)
230
-            dest = "%s%s-%s%s" % (client_dir, os.path.basename(split[0]), vpn_name, split[1])
231
-            shutil.copy(file_path, dest)
232
-
233
-        files_names = list(map(lambda file_path: os.path.basename(file_path), files_paths))
234
-
235
-        call(["tar", "cfj", self.get_client_tarball_path(vpn_name, client_name),
236
-              "-C", self.get_easy_rsa_key_dir(vpn_name)] + files_names)
214
+        self.build_client(vpn_name, client_name, variables)
237 215
 
238 216
         return VpnGenError.Success
239 217
 
@@ -249,3 +227,31 @@ class VpnGen:
249 227
     def rebuild_clients(self, vpn_name):
250 228
         base_dir = self.get_base_dir(vpn_name)
251 229
         return VpnGenError.Success
230
+
231
+    def build_client(self, vpn_name, client_name, variables):
232
+        client_dir = self.get_client_dir(vpn_name, client_name)
233
+        client_conf_file = self.get_client_config_path(vpn_name, client_name)
234
+        client_default_config_path = self.get_client_default_config_path(vpn_name)
235
+
236
+        with open(client_default_config_path, "r") as f:
237
+            client_default_config = f.read()
238
+
239
+        variables['name'] = vpn_name
240
+        variables['client'] = client_name
241
+        for variable in variables:
242
+            client_default_config = client_default_config.replace("${%s}" % variable, variables[variable])
243
+
244
+        os.makedirs(client_dir)
245
+        with open(client_conf_file, "w") as f:
246
+            f.write(client_default_config)
247
+
248
+        files_paths = self.get_all_needed_files_paths(vpn_name, client_name)
249
+        for file_path in files_paths:
250
+            split = os.path.splitext(file_path)
251
+            dest = "%s%s-%s%s" % (client_dir, os.path.basename(split[0]), vpn_name, split[1])
252
+            shutil.copy(file_path, dest)
253
+
254
+        files_names = list(map(lambda file_path: os.path.basename(file_path), files_paths))
255
+
256
+        call(["tar", "cfj", self.get_client_tarball_path(vpn_name, client_name),
257
+              "-C", self.get_easy_rsa_key_dir(vpn_name)] + files_names)

Завантаження…
Відмінити
Зберегти