@@ -68,7 +68,7 @@ def get_default_providers():
68
68
)
69
69
else :
70
70
return dict (
71
- platform = OSError ('Your platform, {}, is unsupported' . format ( platform ) )
71
+ platform = OSError (f 'Your platform, { platform } , is unsupported' )
72
72
)
73
73
74
74
@@ -118,19 +118,19 @@ def do_disconnect(env, args):
118
118
try :
119
119
pid = int (open (pidfile ).read ())
120
120
except (OSError , ValueError ):
121
- print ("WARNING: could not read pid from %s" % pidfile , file = stderr )
121
+ print (f "WARNING: could not read pid from { pidfile } " , file = stderr )
122
122
else :
123
123
try : providers .process .kill (pid )
124
124
except OSError as e :
125
- print ("WARNING: could not kill pid %d from %s: %s" % ( pid , pidfile , str (e )) , file = stderr )
125
+ print (f "WARNING: could not kill pid { pid } from { pidfile } : { str (e )} " , file = stderr )
126
126
else :
127
127
if args .verbose :
128
- print ("Killed pid %d from %s" % ( pid , pidfile ) , file = stderr )
128
+ print (f "Killed pid { pid } from { pidfile } " , file = stderr )
129
129
130
130
if 'hosts' in providers :
131
131
removed = providers .hosts .write_hosts ({}, args .name )
132
132
if args .verbose :
133
- print ("Removed %d hosts from /etc/hosts" % removed , file = stderr )
133
+ print (f "Removed { removed } hosts from /etc/hosts" , file = stderr )
134
134
135
135
# delete all explicitly created routes:
136
136
# 1. Explicit route to gateway (won't get deleted automatically because
@@ -144,7 +144,7 @@ def do_disconnect(env, args):
144
144
try :
145
145
providers .route .remove_route (dest )
146
146
except CalledProcessError :
147
- print ("WARNING: could not delete %s (%s)" % ( desc , dest ) , file = stderr )
147
+ print (f "WARNING: could not delete { desc } ( { dest } )" , file = stderr )
148
148
else :
149
149
providers .route .flush_cache ()
150
150
@@ -153,7 +153,7 @@ def do_disconnect(env, args):
153
153
try :
154
154
providers .firewall .deconfigure_firewall (env .tundev )
155
155
except CalledProcessError :
156
- print ("WARNING: failed to deconfigure firewall for VPN interface (%s)" % env .tundev , file = stderr )
156
+ print (f "WARNING: failed to deconfigure firewall for VPN interface ({ env .tundev } )" , file = stderr )
157
157
158
158
if args .vpn_domains is not None :
159
159
try :
@@ -178,7 +178,7 @@ def do_connect(env, args):
178
178
if args .verbose > 1 :
179
179
print ("Set explicit route to VPN gateway %s (%s)" % (env .gateway , ', ' .join ('%s %s' % kv for kv in gwr .items ())), file = stderr )
180
180
else :
181
- print ("WARNING: no route to VPN gateway found %s ; cannot set explicit route to it." % env . gateway )
181
+ print (f "WARNING: no route to VPN gateway found { env . gateway } ; cannot set explicit route to it." )
182
182
183
183
# drop incoming traffic from VPN
184
184
if not args .incoming :
@@ -205,10 +205,10 @@ def do_connect(env, args):
205
205
if dev_mtu :
206
206
mtu = int (dev_mtu ) - 88
207
207
if mtu :
208
- print ("WARNING: guessing MTU is %d (the MTU of %s - 88)" % ( mtu , dev ) , file = stderr )
208
+ print (f "WARNING: guessing MTU is { mtu } (the MTU of { dev } - 88)" , file = stderr )
209
209
else :
210
210
mtu = 1412
211
- print ("WARNING: guessing default MTU of %d (couldn't determine MTU of %s)" % ( mtu , dev ) , file = stderr )
211
+ print (f "WARNING: guessing default MTU of { mtu } (couldn't determine MTU of { dev } )" , file = stderr )
212
212
providers .route .set_link_info (env .tundev , state = 'up' , mtu = mtu )
213
213
214
214
# set IPv4, IPv6 addresses for tunnel device
@@ -224,18 +224,18 @@ def do_connect(env, args):
224
224
if r :
225
225
exc_subnets .append ((dest , r ))
226
226
else :
227
- print ("WARNING: Ignoring unroutable split-exclude %s" % dest , file = stderr )
227
+ print (f "WARNING: Ignoring unroutable split-exclude { dest } " , file = stderr )
228
228
229
229
# set up routes to the DNS and Windows name servers, subnets, and local aliases
230
230
ns = env .dns + env .dns6 + (env .nbns if args .nbns else [])
231
231
for dest , tag in chain (tagged (ns , "nameserver" ), tagged (args .subnets , "subnet" ), tagged (args .aliases , "alias" )):
232
232
if args .verbose > 1 :
233
- print ("Adding route to %s %s through %s." % ( tag , dest , env .tundev ) , file = stderr )
233
+ print (f "Adding route to { tag } { dest } through { env .tundev } ." , file = stderr )
234
234
providers .route .replace_route (dest , dev = env .tundev )
235
235
else :
236
236
providers .route .flush_cache ()
237
237
if args .verbose :
238
- print ("Added routes for %d nameservers, %d subnets, %d aliases." % ( len (ns ), len (args .subnets ), len (args .aliases )) , file = stderr )
238
+ print (f "Added routes for { len (ns )} nameservers, { len (args .subnets )} subnets, { len (args .aliases )} aliases." , file = stderr )
239
239
240
240
# restore routes to excluded subnets
241
241
for dest , exc_route in exc_subnets :
@@ -245,7 +245,7 @@ def do_connect(env, args):
245
245
else :
246
246
providers .route .flush_cache ()
247
247
if args .verbose :
248
- print ("Restored routes for %d excluded subnets." % len ( exc_subnets ) , file = stderr )
248
+ print (f "Restored routes for { len ( exc_subnets ) } excluded subnets." , file = stderr )
249
249
250
250
# Use vpn dns for provided domains
251
251
if args .vpn_domains is not None :
@@ -263,45 +263,45 @@ def do_post_connect(env, args):
263
263
host_map = []
264
264
265
265
if args .ns_hosts :
266
- ns_names = [ (ip , ('dns%d.%s' % ( ii , args .name ) ,)) for ii , ip in enumerate (env .dns + env .dns6 ) ]
266
+ ns_names = [ (ip , (f 'dns{ ii } . { args .name } ' ,)) for ii , ip in enumerate (env .dns + env .dns6 ) ]
267
267
if args .nbns :
268
- ns_names += [ (ip , ('nbns%d.%s' % ( ii , args .name ) ,)) for ii , ip in enumerate (env .nbns ) ]
268
+ ns_names += [ (ip , (f 'nbns{ ii } . { args .name } ' ,)) for ii , ip in enumerate (env .nbns ) ]
269
269
host_map += ns_names
270
270
if args .verbose :
271
- print ("Adding /etc/hosts entries for %d nameservers..." % len ( ns_names ) , file = stderr )
271
+ print (f "Adding /etc/hosts entries for { len ( ns_names ) } nameservers..." , file = stderr )
272
272
for ip , names in ns_names :
273
- print (" %s = %s" % ( ip , ', ' .join (map (str , names ))) , file = stderr )
273
+ print (f " { ip } = { ', ' .join (map (str , names ))} " , file = stderr )
274
274
275
275
if args .hosts or args .prevent_idle_timeout or args .kerberos_dc :
276
276
providers .dns .configure (dns_servers = (env .dns + env .dns6 ), search_domains = args .domain , bind_addresses = env .myaddrs )
277
277
278
278
kdc_hosts = []
279
279
if args .kerberos_dc :
280
280
if args .verbose :
281
- print ("Looking up Kerberos5 DC hosts for realm %r using VPN DNS servers..." % args . kerberos_dc , file = stderr )
281
+ print (f "Looking up Kerberos5 DC hosts for realm { args . kerberos_dc !r } using VPN DNS servers..." , file = stderr )
282
282
try :
283
- kdc_hosts = providers .dns .lookup_srv ('_kerberos._tcp.%s' % args .kerberos_dc )
283
+ kdc_hosts = providers .dns .lookup_srv (f '_kerberos._tcp.{ args .kerberos_dc } ' )
284
284
except Exception as e :
285
- print ("WARNING: Lookup for Kerberos5 DC hosts for realm %r on VPN DNS servers failed:\n \t %s" % ( args . kerberos_dc , e ) , file = stderr )
285
+ print (f "WARNING: Lookup for Kerberos5 DC hosts for realm { args . kerberos_dc !r } on VPN DNS servers failed:\n \t { e } " , file = stderr )
286
286
else :
287
287
if args .verbose :
288
- print ("Got %d Kerberos5 DC hosts." % len ( kdc_hosts ) , file = stderr )
288
+ print (f "Got { len ( kdc_hosts ) } Kerberos5 DC hosts." , file = stderr )
289
289
290
290
hosts_to_lookup = list (chain (tagged (args .hosts , 'host' ), tagged (kdc_hosts , 'kdc' )))
291
291
if hosts_to_lookup :
292
292
if args .verbose :
293
- print ("Looking up %d hosts using VPN DNS servers..." % len ( hosts_to_lookup ) , file = stderr )
293
+ print (f "Looking up { len ( hosts_to_lookup ) } hosts using VPN DNS servers..." , file = stderr )
294
294
for host , why in hosts_to_lookup :
295
295
try :
296
296
ips = providers .dns .lookup_host (host )
297
297
except Exception as e :
298
- print ("WARNING: Lookup for %s on VPN DNS servers failed:\n \t %s" % ( host , e ) , file = stderr )
298
+ print (f "WARNING: Lookup for { host } on VPN DNS servers failed:\n \t { e } " , file = stderr )
299
299
else :
300
300
if ips is None :
301
- print ("WARNING: Lookup for %s on VPN DNS servers returned nothing." % host , file = stderr )
301
+ print (f "WARNING: Lookup for { host } on VPN DNS servers returned nothing." , file = stderr )
302
302
else :
303
303
if args .verbose :
304
- print (" %s = %s" % ( host , ', ' .join (map (str , ips ))) , file = stderr )
304
+ print (f " { host } = { ', ' .join (map (str , ips ))} " , file = stderr )
305
305
ip_routes .update (ips )
306
306
if why == 'kdc' :
307
307
host_map .extend ((ip , [host ]) for ip in ips )
@@ -315,35 +315,35 @@ def do_post_connect(env, args):
315
315
if host_map :
316
316
providers .hosts .write_hosts (host_map , args .name )
317
317
if args .verbose :
318
- print ("Added hostnames and aliases for %d addresses to /etc/hosts." % len ( host_map ) , file = stderr )
318
+ print (f "Added hostnames and aliases for { len ( host_map ) } addresses to /etc/hosts." , file = stderr )
319
319
320
320
# add routes to hosts
321
321
for ip in ip_routes :
322
322
if args .verbose > 1 :
323
- print ("Adding route to %s (for named hosts) through %s." % ( ip , env .tundev ) , file = stderr )
323
+ print (f "Adding route to { ip } (for named hosts) through { env .tundev } ." , file = stderr )
324
324
providers .route .replace_route (ip , dev = env .tundev )
325
325
else :
326
326
providers .route .flush_cache ()
327
327
if args .verbose :
328
- print ("Added %d routes for named hosts." % len ( ip_routes ) , file = stderr )
328
+ print (f "Added { len ( ip_routes ) } routes for named hosts." , file = stderr )
329
329
330
330
# run DNS queries in background to prevent idle timeout
331
331
if args .prevent_idle_timeout :
332
332
dns = env .dns + env .dns6
333
333
idle_timeout = env .idle_timeout
334
- setproctitle ('vpn-slice --prevent-idle-timeout --name %s' % args .name )
334
+ setproctitle (f 'vpn-slice --prevent-idle-timeout --name { args .name } ' )
335
335
if args .verbose :
336
- print ("Continuing in background as PID %d , attempting to prevent idle timeout every %d seconds." % ( providers . process . pid (), idle_timeout ) )
336
+ print (f "Continuing in background as PID { providers . process . pid () } , attempting to prevent idle timeout every { idle_timeout } seconds." )
337
337
338
338
while True :
339
339
delay = randint (2 * idle_timeout // 3 , 9 * idle_timeout // 10 )
340
340
if args .verbose > 1 :
341
- print ("Sleeping %d seconds until we issue a DNS query to prevent idle timeout..." % delay , file = stderr )
341
+ print (f "Sleeping { delay } seconds until we issue a DNS query to prevent idle timeout..." , file = stderr )
342
342
sleep (delay )
343
343
344
344
# FIXME: netlink(7) may be a much better way to poll here
345
345
if not providers .process .is_alive (args .ppid ):
346
- print ("Caller (PID %d ) has terminated; idle preventer exiting." % args . ppid , file = stderr )
346
+ print (f "Caller (PID { args . ppid } ) has terminated; idle preventer exiting." , file = stderr )
347
347
break
348
348
349
349
# pick random host or IP to look up without leaking any new information
@@ -353,11 +353,11 @@ def do_post_connect(env, args):
353
353
dummy = choice (pool )
354
354
shuffle (dns )
355
355
if args .verbose > 1 :
356
- print ("Issuing DNS lookup of %s to prevent idle timeout..." % dummy , file = stderr )
356
+ print (f "Issuing DNS lookup of { dummy } to prevent idle timeout..." , file = stderr )
357
357
providers .dns .lookup_host (dummy , keep_going = False )
358
358
359
359
elif args .verbose :
360
- print ("Connection setup done, child process %d exiting." % providers .process .pid ())
360
+ print (f "Connection setup done, child process { providers .process .pid ()} exiting." )
361
361
362
362
########################################
363
363
@@ -397,7 +397,7 @@ def parse_env(environ=os.environ):
397
397
if envar in environ :
398
398
try : val = maker (environ [envar ])
399
399
except Exception as e :
400
- print ('Exception while setting %s from environment variable %s=%r' % ( var , envar , environ [envar ]) , file = stderr )
400
+ print (f 'Exception while setting { var } from environment variable { envar } = { environ [envar ]!r } ' , file = stderr )
401
401
raise
402
402
elif default : val , = default
403
403
else : val = None
@@ -408,7 +408,7 @@ def parse_env(environ=os.environ):
408
408
orig_netaddr = env .network
409
409
env .network = IPv4Network (env .network ).supernet (new_prefix = env .netmasklen )
410
410
if env .network .network_address != orig_netaddr :
411
- print ("WARNING: IPv4 network %s/%d has host bits set, replacing with %s" % ( orig_netaddr , env .netmasklen , env . network ) , file = stderr )
411
+ print (f "WARNING: IPv4 network { orig_netaddr } / { env . netmasklen } has host bits set, replacing with { env .network } " , file = stderr )
412
412
assert env .network .netmask == env .netmask , \
413
413
"IPv4 network (INTERNAL_IP4_{{NETADDR,NETMASK}}) {ad}/{nm} does not match INTERNAL_IP4_NETMASKLEN={nml} (implies /{nmi})" .format (
414
414
ad = orig_netaddr , nm = env .netmask , nml = env .netmasklen , nmi = env .network .netmask )
@@ -435,9 +435,9 @@ def parse_env(environ=os.environ):
435
435
env .splitexc = []
436
436
for pfx , n in chain ((('INC' , n ) for n in range (env .nsplitinc )),
437
437
(('EXC' , n ) for n in range (env .nsplitexc ))):
438
- ad = IPv4Address (environ ['CISCO_SPLIT_%s_%d_ADDR' % ( pfx , n ) ])
439
- nm = IPv4Address (environ ['CISCO_SPLIT_%s_%d_MASK' % ( pfx , n ) ])
440
- nml = int (environ ['CISCO_SPLIT_%s_%d_MASKLEN' % ( pfx , n ) ])
438
+ ad = IPv4Address (environ [f 'CISCO_SPLIT_{ pfx } _ { n } _ADDR' ])
439
+ nm = IPv4Address (environ [f 'CISCO_SPLIT_{ pfx } _ { n } _MASK' ])
440
+ nml = int (environ [f 'CISCO_SPLIT_{ pfx } _ { n } _MASKLEN' ])
441
441
net = IPv4Network (ad ).supernet (new_prefix = nml )
442
442
if net .network_address != ad :
443
443
print ("WARNING: IPv4 split network (CISCO_SPLIT_%s_%d_{ADDR,MASK}) %s/%d has host bits set, replacing with %s" % (pfx , n , ad , nml , net ), file = stderr )
@@ -448,8 +448,8 @@ def parse_env(environ=os.environ):
448
448
449
449
for pfx , n in chain ((('INC' , n ) for n in range (env .nsplitinc6 )),
450
450
(('EXC' , n ) for n in range (env .nsplitexc6 ))):
451
- ad = IPv6Address (environ ['CISCO_IPV6_SPLIT_%s_%d_ADDR' % ( pfx , n ) ])
452
- nml = int (environ ['CISCO_IPV6_SPLIT_%s_%d_MASKLEN' % ( pfx , n ) ])
451
+ ad = IPv6Address (environ [f 'CISCO_IPV6_SPLIT_{ pfx } _ { n } _ADDR' ])
452
+ nml = int (environ [f 'CISCO_IPV6_SPLIT_{ pfx } _ { n } _MASKLEN' ])
453
453
net = IPv6Network (ad ).supernet (new_prefix = nml )
454
454
if net .network_address != ad :
455
455
print ("WARNING: IPv6 split network (CISCO_IPV6_SPLIT_%s_%d_{ADDR,MASKLEN}) %s/%d has host bits set, replacing with %s" % (pfx , n , ad , nml , net ), file = stderr )
@@ -552,7 +552,7 @@ def main(args=None, environ=os.environ):
552
552
raise pv
553
553
providers [pn ] = pv ()
554
554
except Exception as e :
555
- print ("WARNING: Couldn't configure {} provider: {}" . format ( pn , e ) , file = stderr )
555
+ print (f "WARNING: Couldn't configure { pn } provider: { e } " , file = stderr )
556
556
557
557
# Fail if necessary providers are missing
558
558
required = {'route' , 'process' }
@@ -572,7 +572,7 @@ def main(args=None, environ=os.environ):
572
572
required .add ('dns' )
573
573
missing_required = {p for p in required if p not in providers }
574
574
if missing_required :
575
- raise RuntimeError ("Aborting because providers for %s are required; use --help for more information" % ' ' . join ( missing_required ) )
575
+ raise RuntimeError (f "Aborting because providers for { ' ' . join ( missing_required ) } are required; use --help for more information" )
576
576
577
577
# Finalize arguments that depend on providers
578
578
finalize_args_and_env (args , env )
@@ -592,12 +592,12 @@ def main(args=None, environ=os.environ):
592
592
raise SystemExit ()
593
593
594
594
if env .myaddr6 or env .netmask6 :
595
- print ('WARNING: IPv6 address or netmask set. Support for IPv6 in %s should be considered BETA-QUALITY.' % p . prog , file = stderr )
595
+ print (f 'WARNING: IPv6 address or netmask set. Support for IPv6 in { p . prog } should be considered BETA-QUALITY.' , file = stderr )
596
596
if args .dump :
597
597
exe = providers .process .pid2exe (args .ppid )
598
- caller = '%s (PID %d)' % ( exe , args .ppid ) if exe else 'PID %d' % args .ppid
598
+ caller = f' { exe } (PID { args .ppid } )' if exe else f 'PID { args .ppid } '
599
599
600
- print ('Called by %s with environment variables for vpnc-script:' % caller , file = stderr )
600
+ print (f 'Called by { caller } with environment variables for vpnc-script:' , file = stderr )
601
601
width = max ((len (envar ) for var , envar , * rest in vpncenv if envar in environ ), default = 0 )
602
602
for var , envar , * rest in vpncenv :
603
603
if envar in environ :
@@ -640,7 +640,7 @@ def main(args=None, environ=os.environ):
640
640
# https://github.com/dlenski/vpn-slice/pull/14#issuecomment-488129621
641
641
642
642
if args .verbose :
643
- print ('WARNING: %s ignores reason=%s' % ( p . prog , env .reason .name ) , file = stderr )
643
+ print (f 'WARNING: { p . prog } ignores reason={ env .reason .name } ' , file = stderr )
644
644
elif env .reason == reasons .connect :
645
645
do_connect (env , args )
646
646
0 commit comments