Browse Source

rebuild client config

master
Robin Thoni 8 years ago
parent
commit
44539c3c25
2 changed files with 71 additions and 17 deletions
  1. 23
    3
      vpngen-cli.py
  2. 48
    14
      vpngen.py

+ 23
- 3
vpngen-cli.py View File

20
         default = variables_set[variable] if variable in variables_set else ''
20
         default = variables_set[variable] if variable in variables_set else ''
21
         print("Enter a value for '%s' [%s]: " % (variable, default), end='', flush=True)
21
         print("Enter a value for '%s' [%s]: " % (variable, default), end='', flush=True)
22
         value = sys.stdin.readline()[:-1]
22
         value = sys.stdin.readline()[:-1]
23
-        if value != '':
23
+        if value != '' or variable not in variables_set:
24
             variables_set[variable] = value
24
             variables_set[variable] = value
25
     return variables_set
25
     return variables_set
26
 
26
 
34
     parser.add_argument('--remove', help='Remove a VPN', action='store_true')
34
     parser.add_argument('--remove', help='Remove a VPN', action='store_true')
35
     parser.add_argument('--create-client', help='Create a client for the VPN', metavar='CLIENT')
35
     parser.add_argument('--create-client', help='Create a client for the VPN', metavar='CLIENT')
36
     parser.add_argument('--remove-client', help='Remove a client for the VPN', metavar='CLIENT')
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
     parser.add_argument('--rebuild-clients', help='Rebuild clients configurations', action='store_true')
38
     parser.add_argument('--rebuild-clients', help='Rebuild clients configurations', action='store_true')
38
 
39
 
39
     args = parser.parse_args()
40
     args = parser.parse_args()
46
         client_name = args.create_client
47
         client_name = args.create_client
47
     elif args.remove_client is not None:
48
     elif args.remove_client is not None:
48
         client_name = args.remove_client
49
         client_name = args.remove_client
50
+    elif args.rebuild_client is not None:
51
+        client_name = args.rebuild_client
49
     else:
52
     else:
50
         client_name = None
53
         client_name = None
51
     if client_name is not None:
54
     if client_name is not None:
60
         config['defaults'].update(data['variables'])
63
         config['defaults'].update(data['variables'])
61
 
64
 
62
     if args.create:
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
         res = vpng.create_vpn(vpn_name, variables)
68
         res = vpng.create_vpn(vpn_name, variables)
65
         if res == vpngen.VpnGenError.Success:
69
         if res == vpngen.VpnGenError.Success:
66
             print("VPN %s created successfully" % vpn_name)
70
             print("VPN %s created successfully" % vpn_name)
75
             eprint("Failed to remove VPN %s: %s" % (vpn_name, res))
79
             eprint("Failed to remove VPN %s: %s" % (vpn_name, res))
76
             exit(1)
80
             exit(1)
77
     elif args.create_client:
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
         if variables is None:
85
         if variables is None:
80
             res = vpngen.VpnGenError.VpnDoesNotExists
86
             res = vpngen.VpnGenError.VpnDoesNotExists
81
         else:
87
         else:
92
         else:
98
         else:
93
             eprint("Failed to remove client %s on VPN %s: %s" % (client_name, vpn_name, res))
99
             eprint("Failed to remove client %s on VPN %s: %s" % (client_name, vpn_name, res))
94
             exit(1)
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
     elif args.rebuild_clients:
112
     elif args.rebuild_clients:
113
+        default_variables = config['defaults'].copy()
114
+        default_variables.update(vpng.get_server_variables(vpn_name))
115
+
96
         res = vpng.rebuild_clients(vpn_name)
116
         res = vpng.rebuild_clients(vpn_name)
97
         if res == vpngen.VpnGenError.Success:
117
         if res == vpngen.VpnGenError.Success:
98
             print("Clients configurations rebuilt successfully on VPN %s" % vpn_name)
118
             print("Clients configurations rebuilt successfully on VPN %s" % vpn_name)

+ 48
- 14
vpngen.py View File

53
         variables = re.findall('\$\{([^}]+)}', default_config)
53
         variables = re.findall('\$\{([^}]+)}', default_config)
54
         variables = self.f7(variables)
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
     def get_base_dir(self, vpn_name):
65
     def get_base_dir(self, vpn_name):
59
         return "%s%s%s%s" % (self.ovpn_config_path, os.sep, vpn_name, os.sep)
66
         return "%s%s%s%s" % (self.ovpn_config_path, os.sep, vpn_name, os.sep)
79
 
86
 
80
     def get_client_default_config_path(self, vpn_name):
87
     def get_client_default_config_path(self, vpn_name):
81
         base_dir = self.get_base_dir(vpn_name)
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
     def get_client_dir(self, vpn_name, client_name):
91
     def get_client_dir(self, vpn_name, client_name):
85
         base_dir = self.get_base_dir(vpn_name)
92
         base_dir = self.get_base_dir(vpn_name)
89
         client_dir = self.get_client_dir(vpn_name, client_name)
96
         client_dir = self.get_client_dir(vpn_name, client_name)
90
         return "%s%s-%s.conf" % (client_dir, client_name, vpn_name)
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
     def get_client_generated_files_paths(self, vpn_name, client_name):
103
     def get_client_generated_files_paths(self, vpn_name, client_name):
93
         keys_dir = self.get_easy_rsa_key_dir(vpn_name,)
104
         keys_dir = self.get_easy_rsa_key_dir(vpn_name,)
94
         return [
105
         return [
107
             "%sta.key" % keys_dir
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
     def get_all_needed_files_paths(self, vpn_name, client_name):
125
     def get_all_needed_files_paths(self, vpn_name, client_name):
111
         return self.get_client_generated_files_paths(vpn_name, client_name) +\
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
     def setup_vars(self, vpn_name, variables):
138
     def setup_vars(self, vpn_name, variables):
115
         easyrsadir = self.get_easy_rsa_dir(vpn_name)
139
         easyrsadir = self.get_easy_rsa_dir(vpn_name)
216
 
240
 
217
         os.chdir(curdir)
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
     def remove_client(self, vpn_name, client_name):
247
     def remove_client(self, vpn_name, client_name):
224
         base_dir = self.get_base_dir(vpn_name)
248
         base_dir = self.get_base_dir(vpn_name)
229
             return VpnGenError.ClientDoesNotExists
253
             return VpnGenError.ClientDoesNotExists
230
         return VpnGenError.Success
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
         client_dir = self.get_client_dir(vpn_name, client_name)
257
         client_dir = self.get_client_dir(vpn_name, client_name)
238
         client_conf_file = self.get_client_config_path(vpn_name, client_name)
258
         client_conf_file = self.get_client_config_path(vpn_name, client_name)
239
         client_default_config_path = self.get_client_default_config_path(vpn_name)
259
         client_default_config_path = self.get_client_default_config_path(vpn_name)
240
-
241
         with open(client_default_config_path, "r") as f:
260
         with open(client_default_config_path, "r") as f:
242
             client_default_config = f.read()
261
             client_default_config = f.read()
243
 
262
 
244
-        variables['name'] = vpn_name
245
-        variables['client'] = client_name
246
         for variable in variables:
263
         for variable in variables:
247
             client_default_config = client_default_config.replace("${%s}" % variable, variables[variable])
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
         with open(client_conf_file, "w") as f:
270
         with open(client_conf_file, "w") as f:
251
             f.write(client_default_config)
271
             f.write(client_default_config)
252
 
272
 
256
             dest = "%s%s-%s%s" % (client_dir, os.path.basename(split[0]), vpn_name, split[1])
276
             dest = "%s%s-%s%s" % (client_dir, os.path.basename(split[0]), vpn_name, split[1])
257
             shutil.copy(file_path, dest)
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
         files_names = glob.glob("%s%s*" % (client_dir, os.sep))
283
         files_names = glob.glob("%s%s*" % (client_dir, os.sep))
260
         files_names = list(map(lambda file_path: os.path.basename(file_path), files_names))
284
         files_names = list(map(lambda file_path: os.path.basename(file_path), files_names))
261
 
285
 
286
+
262
         call(["tar", "cfj", self.get_client_tarball_path(vpn_name, client_name),
287
         call(["tar", "cfj", self.get_client_tarball_path(vpn_name, client_name),
263
               "-C", client_dir] + files_names)
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…
Cancel
Save