Explorar el Código

rebuild client config

master
Robin Thoni hace 8 años
padre
commit
44539c3c25
Se han modificado 2 ficheros con 71 adiciones y 17 borrados
  1. 23
    3
      vpngen-cli.py
  2. 48
    14
      vpngen.py

+ 23
- 3
vpngen-cli.py Ver fichero

@@ -20,7 +20,7 @@ def create_variables(variables, defaults):
20 20
         default = variables_set[variable] if variable in variables_set else ''
21 21
         print("Enter a value for '%s' [%s]: " % (variable, default), end='', flush=True)
22 22
         value = sys.stdin.readline()[:-1]
23
-        if value != '':
23
+        if value != '' or variable not in variables_set:
24 24
             variables_set[variable] = value
25 25
     return variables_set
26 26
 
@@ -34,6 +34,7 @@ def main():
34 34
     parser.add_argument('--remove', help='Remove a VPN', action='store_true')
35 35
     parser.add_argument('--create-client', help='Create a client for the VPN', metavar='CLIENT')
36 36
     parser.add_argument('--remove-client', help='Remove a client for the VPN', metavar='CLIENT')
37
+    parser.add_argument('--rebuild-client', help='Rebuild a client configuration', metavar='CLIENT')
37 38
     parser.add_argument('--rebuild-clients', help='Rebuild clients configurations', action='store_true')
38 39
 
39 40
     args = parser.parse_args()
@@ -46,6 +47,8 @@ def main():
46 47
         client_name = args.create_client
47 48
     elif args.remove_client is not None:
48 49
         client_name = args.remove_client
50
+    elif args.rebuild_client is not None:
51
+        client_name = args.rebuild_client
49 52
     else:
50 53
         client_name = None
51 54
     if client_name is not None:
@@ -60,7 +63,8 @@ def main():
60 63
         config['defaults'].update(data['variables'])
61 64
 
62 65
     if args.create:
63
-        variables = create_variables(vpng.get_vpn_vars(), config['defaults'])
66
+        default_variables = config['defaults'].copy()
67
+        variables = create_variables(vpng.get_vpn_vars(), default_variables)
64 68
         res = vpng.create_vpn(vpn_name, variables)
65 69
         if res == vpngen.VpnGenError.Success:
66 70
             print("VPN %s created successfully" % vpn_name)
@@ -75,7 +79,9 @@ def main():
75 79
             eprint("Failed to remove VPN %s: %s" % (vpn_name, res))
76 80
             exit(1)
77 81
     elif args.create_client:
78
-        variables = create_variables(vpng.get_client_vars(vpn_name), config['defaults'])
82
+        default_variables = config['defaults'].copy()
83
+        default_variables.update(vpng.get_server_variables(vpn_name))
84
+        variables = create_variables(vpng.get_client_vars(vpn_name), default_variables)
79 85
         if variables is None:
80 86
             res = vpngen.VpnGenError.VpnDoesNotExists
81 87
         else:
@@ -92,7 +98,21 @@ def main():
92 98
         else:
93 99
             eprint("Failed to remove client %s on VPN %s: %s" % (client_name, vpn_name, res))
94 100
             exit(1)
101
+    elif args.rebuild_client:
102
+        default_variables = config['defaults'].copy()
103
+        default_variables.update(vpng.get_server_variables(vpn_name))
104
+        default_variables.update(vpng.get_client_variables(vpn_name, client_name))
105
+
106
+        res = vpng.rebuild_client(vpn_name, client_name, default_variables)
107
+        if res == vpngen.VpnGenError.Success:
108
+            print("Client %s configuration rebuilt successfully on VPN %s" % (client_name, vpn_name))
109
+        else:
110
+            eprint("Failed to rebuild client %s configuration on VPN %s: %s" % (client_name, vpn_name, res))
111
+            exit(1)
95 112
     elif args.rebuild_clients:
113
+        default_variables = config['defaults'].copy()
114
+        default_variables.update(vpng.get_server_variables(vpn_name))
115
+
96 116
         res = vpng.rebuild_clients(vpn_name)
97 117
         if res == vpngen.VpnGenError.Success:
98 118
             print("Clients configurations rebuilt successfully on VPN %s" % vpn_name)

+ 48
- 14
vpngen.py Ver fichero

@@ -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

Loading…
Cancelar
Guardar