11import re
22
33from . import converters
4+ from ...schema import DEFAULT_FILE_MODE
45from ..base .backend import BaseBackend
56from .renderer import OpenVpnRenderer
67from .schema import schema
@@ -40,7 +41,7 @@ def _generate_contents(self, tar):
4041 contents = text_contents )
4142
4243 @classmethod
43- def auto_client (self , host , server , ca_path = None , ca_contents = None ,
44+ def auto_client (cls , host , server , ca_path = None , ca_contents = None ,
4445 cert_path = None , cert_contents = None , key_path = None ,
4546 key_contents = None ):
4647 """
@@ -61,58 +62,68 @@ def auto_client(self, host, server, ca_path=None, ca_contents=None,
6162 :returns: dictionary representing a single OpenVPN client configuration
6263 """
6364 # client defaults
64- c = {
65+ client = {
6566 "mode" : "p2p" ,
6667 "nobind" : True ,
6768 "resolv_retry" : "infinite" ,
6869 "tls_client" : True
6970 }
7071 # remote
7172 port = server .get ('port' ) or 1195
72- c ['remote' ] = [{'host' : host , 'port' : port }]
73+ client ['remote' ] = [{'host' : host , 'port' : port }]
7374 # proto
7475 if server .get ('proto' ) == 'tcp-server' :
75- c ['proto' ] = 'tcp-client'
76+ client ['proto' ] = 'tcp-client'
7677 else :
77- c ['proto' ] = 'udp'
78+ client ['proto' ] = 'udp'
7879 # determine if pull must be True
7980 if 'server' in server or 'server_bridge' in server :
80- c ['pull' ] = True
81+ client ['pull' ] = True
8182 # tls_client
8283 if 'tls_server' not in server or not server ['tls_server' ]:
83- c ['tls_client' ] = False
84+ client ['tls_client' ] = False
8485 # ns_cert_type
8586 if not server .get ('ns_cert_type' ):
86- c ['ns_cert_type' ] = ''
87+ client ['ns_cert_type' ] = ''
8788 elif server .get ('ns_cert_type' ) == 'client' :
88- c ['ns_cert_type' ] = 'server'
89+ client ['ns_cert_type' ] = 'server'
8990 copy_keys = ['name' , 'dev_type' , 'dev' , 'comp_lzo' , 'auth' ,
9091 'cipher' , 'ca' , 'cert' , 'key' , 'mtu_disc' , 'mtu_test' ,
9192 'fragment' , 'mssfix' , 'keepalive' , 'persist_tun' , 'mute' ,
9293 'persist_key' , 'script_security' , 'user' , 'group' , 'log' ,
9394 'mute_replay_warnings' , 'secret' , 'fast_io' , 'verb' ]
9495 for key in copy_keys :
9596 if key in server :
96- c [key ] = server [key ]
97- # prepare files if necessary
97+ client [key ] = server [key ]
98+ files = cls ._auto_client_files (client , ca_path , ca_contents ,
99+ cert_path , cert_contents ,
100+ key_path , key_contents )
101+ return {
102+ 'openvpn' : [client ],
103+ 'files' : files
104+ }
105+
106+ @classmethod
107+ def _auto_client_files (cls , client , ca_path = None , ca_contents = None , cert_path = None ,
108+ cert_contents = None , key_path = None , key_contents = None ):
109+ """
110+ returns a list of NetJSON extra files for automatically generated clients
111+ produces side effects in ``client`` dictionary
112+ """
98113 files = []
99114 if ca_path and ca_contents :
100- c ['ca' ] = ca_path
115+ client ['ca' ] = ca_path
101116 files .append (dict (path = ca_path ,
102- mode = '0644' ,
103- contents = ca_contents ))
117+ contents = ca_contents ,
118+ mode = DEFAULT_FILE_MODE ))
104119 if cert_path and cert_contents :
105- c ['cert' ] = cert_path
120+ client ['cert' ] = cert_path
106121 files .append (dict (path = cert_path ,
107- mode = '0644' ,
108- contents = cert_contents ))
122+ contents = cert_contents ,
123+ mode = DEFAULT_FILE_MODE ))
109124 if key_path and key_contents :
110- c ['key' ] = key_path
125+ client ['key' ] = key_path
111126 files .append (dict (path = key_path ,
112- mode = '0644' ,
113- contents = key_contents ))
114- # prepare result
115- netjson = {'openvpn' : [c ]}
116- if files :
117- netjson ['files' ] = files
118- return netjson
127+ contents = key_contents ,
128+ mode = DEFAULT_FILE_MODE ,))
129+ return files
0 commit comments