@@ -18,9 +18,11 @@ function load_config(name) {
18
18
let ts = ctx .get_all (name , "tunspace" );
19
19
let cfg = {
20
20
"debug" : int(ts .debug ) != 0 ,
21
- "uplink_netns" : "" +ts .uplink_netns ,
22
- "uplink_ifname" : "" +ts .uplink_ifname ,
23
- "uplink_mode" : "" +ts .uplink_mode ,
21
+ "uplink_netns" : type (ts .uplink_netns ) ? ts .uplink_netns : "" ,
22
+ "uplink_ifname" : type (ts .uplink_ifname ) ? ts .uplink_ifname : "" ,
23
+ "uplink_mode" : type (ts .uplink_mode ) ? ts .uplink_mode : "" ,
24
+ "uplink_ipv4" : type (ts .uplink_ipv4 ) ? ts .uplink_ipv4 : "" ,
25
+ "uplink_gateway" : type (ts .uplink_gateway ) ? ts .uplink_gateway : "" ,
24
26
"maintenance_interval" : int(ts .maintenance_interval ),
25
27
"wireguard_servers" : {},
26
28
"wireguard_interfaces" : {},
@@ -30,19 +32,21 @@ function load_config(name) {
30
32
};
31
33
32
34
ctx .foreach (name , "wg-server" , function (c ) {
33
- cfg .wireguard_servers ["" +c .name ] = {
34
- "name" : "" +c .name ,
35
- "url" : "" +c .url ,
35
+ let name = type (c .name ) ? c .name : "" ;
36
+ cfg .wireguard_servers [name ] = {
37
+ "name" : name ,
38
+ "url" : type (c .url ) ? c .url : "" ,
36
39
"insecure_cert" : int (c .insecure_cert ) != 0 ,
37
40
"disabled" : int (c .disabled ) != 0 ,
38
41
};
39
42
});
40
43
41
44
ctx .foreach (name , "wg-interface" , function (c ) {
42
- cfg .wireguard_interfaces ["" +c .ifname ] = {
43
- "ifname" : "" +c .ifname ,
44
- "ipv6" : "" +c .ipv6 ,
45
- "ipv4" : "" +c .ipv4 ,
45
+ let ifname = type (c .ifname ) ? c .ifname : "" ;
46
+ cfg .wireguard_interfaces [ifname ] = {
47
+ "ifname" : ifname ,
48
+ "ipv6" : type (c .ipv6 ) ? c .ipv6 : "" ,
49
+ "ipv4" : type (c .ipv4 ) ? c .ipv4 : "" ,
46
50
"mtu" : int (c .mtu ),
47
51
"port" : int (c .port ),
48
52
"disabled" : int (c .disabled ) != 0 ,
@@ -382,12 +386,47 @@ function wireguard_maintenance(st, cfg) {
382
386
}
383
387
}
384
388
389
+ function uplink_dhcp (netns , netnsifname ) {
390
+ // if we already have an IP, we'll try to renew it.
391
+ // some routers will otherwise give us a different new IP, exhausting the IP pool.
392
+ let p = fs .popen ("ip -j -n " +netns +" a s " +netnsifname );
393
+ let out = p .read ("all" );
394
+ p .close ();
395
+ if (out == null ) {
396
+ log ("unable to read current ip address of " +netnsifname )
397
+ }
398
+ let reqip = "0.0.0.0" ;
399
+ let iplist = json (out );
400
+ for (ipobj in iplist ) {
401
+ for (ipaddr in ipobj .addr_info ) {
402
+ if (ipaddr .family == "inet " && ipaddr .scope == "global ") {
403
+ reqip = ipaddr.local;
404
+ }
405
+ }
406
+ }
407
+
408
+ // try dhcp for 5 seconds
409
+ shell_command(" ip netns exec "+netns +" udhcpc -f -n -q -A 5 -i " +netnsifname +" -r " +reqip +" -s /usr/share/tunspace/udhcpc.script 2>&1 | grep 'ip addr add'" );
410
+ }
411
+
412
+ function uplink_static (netns , netnsifname , ipv4 , gw ) {
413
+ shell_command ("ip -n " +netns +" addr show dev " +netnsifname +" | grep -F '" +ipv4 +"' >/dev/null || ip -n " +netns +" addr add " +ipv4 +" dev " +netnsifname );
414
+ shell_command ("ip -n " +netns +" route show default dev " +netnsifname +" | grep -F '" +gw +"' >/dev/null || ip -n " +netns +" route add default via " +gw );
415
+ }
416
+
385
417
// TODO: ts_uplink interface leaks into default namespace when uplink namespace is deleted
386
- function uplink_maintenance (nsid , netns , ifname , mode ) {
418
+ function uplink_maintenance (cfg ) {
387
419
let netnsifname = UPLINK_NETNS_IFNAME ;
388
420
421
+ let netns = cfg .uplink_netns ;
422
+ let ifname = cfg .uplink_ifname ;
423
+ let mode = cfg .uplink_mode ;
424
+ let ipv4 = cfg .uplink_ipv4 ;
425
+ let gw = cfg .uplink_gateway ;
426
+
389
427
if (interface_exists (netnsifname )) {
390
- // the uplink interface will sometimes leak out of the namespace on shutdown
428
+ // the uplink interface will sometimes leak out of the namespace on shutdown.
429
+ // in that case we'll just reuse it.
391
430
shell_command ("ip link set " +netnsifname +" netns " +netns );
392
431
}
393
432
@@ -411,27 +450,12 @@ function uplink_maintenance(nsid, netns, ifname, mode) {
411
450
return false ;
412
451
}
413
452
414
- // if we already have an IP, we'll try to renew it.
415
- // some routers will otherwise give us a different new IP, exhausting the IP pool.
416
- let p = fs .popen ("ip -j -n " +netns +" a s " +netnsifname );
417
- let out = p .read ("all" );
418
- p .close ();
419
- if (out == null ) {
420
- log ("unable to read current ip address of " +netnsifname )
421
- }
422
- let reqip = "0.0.0.0" ;
423
- let iplist = json (out );
424
- for (ipobj in iplist ) {
425
- for (ipaddr in ipobj .addr_info ) {
426
- if (ipaddr .family == "inet " && ipaddr .scope == "global ") {
427
- reqip = ipaddr.local;
428
- }
429
- }
453
+ if (length (ipv4 ) > 0 ) {
454
+ uplink_static (netns , netnsifname , ipv4 , gw );
455
+ } else {
456
+ uplink_dhcp (netns , netnsifname );
430
457
}
431
458
432
- // try dhcp for 5 seconds
433
- shell_command(" ip netns exec "+netns +" udhcpc -f -n -q -A 5 -i " +netnsifname +" -r " +reqip +" -s /usr/share/tunspace/udhcpc.script 2>&1 | grep 'ip addr add'" );
434
-
435
459
return true ;
436
460
}
437
461
@@ -460,7 +484,7 @@ function boot(st, cfg) {
460
484
function tick (st , cfg ) {
461
485
debug ("tick" );
462
486
463
- if (!uplink_maintenance (st . nsid , cfg . uplink_netns , cfg . uplink_ifname , cfg . uplink_mode )) {
487
+ if (!uplink_maintenance (cfg )) {
464
488
log ("uplink maintenance failed" );
465
489
}
466
490
wireguard_maintenance (st , cfg );
0 commit comments