unet-cli: add support for setting interface zone
authorFelix Fietkau <nbd@nbd.name>
Tue, 17 Dec 2024 13:41:33 +0000 (14:41 +0100)
committerFelix Fietkau <nbd@nbd.name>
Tue, 17 Dec 2024 13:41:39 +0000 (14:41 +0100)
Default to "lan"

Signed-off-by: Felix Fietkau <nbd@nbd.name>
scripts/unet-cli

index f7d883925f4dea92b1cfe016f04b969f5308e33d..376162dd1fcd192d3ca7c4d96e758699ffbd123e 100755 (executable)
@@ -72,6 +72,7 @@ const usage_message = `Usage: ${basename(sourcepath())} [<flags>] <file> <comman
        connect=<val>[,<val>...]                set IP addresses that the host will contact for network updates
        tunnels=<ifname>:<service>[,...]        set active tunnel devices
        dht=0|1                                 set DHT enabled
+       zone=<name>                             set interface zone (default: lan, "-": unset)
      - service options (add-service, set-service):
        type=<val>                              set service type (required for add-service)
        members=[+|-]<val>[,<val>...]           set/add/remove service member hosts/groups
@@ -160,6 +161,14 @@ set_interface_attrs() {
        set_list connect "$CONNECT"
        set_list tunnels "$TUNNELS"
        uci set "network.$INTERFACE.domain=$DOMAIN"
+       [ -n "$ZONE" ] && {
+               if [ "$ZONE" = "-" ]; then
+                       uci delete "network.$INTERFACE.zone"
+               else
+                       uci set "network.$INTERFACE.zone=$ZONE"
+               fi
+       }
+
 }
 
 check_interface() {
@@ -251,14 +260,20 @@ function key_arg(file, net_data) {
        return `-K ${file}.key`;
 }
 
-function sync_host(host, net_data) {
+function sync_host(host, net_data, command) {
        let interface = args.interface ?? "unet";
        let connect = replace(args.connect ?? "", ",", " ");
        let auth_key = args.auth_key ?? net_data.config.id;
        let tunnels = replace(replace(args.tunnels ?? "", ",", " "), ":", "=");
        let domain = args.domain ?? "unet";
+       let zone = args.zone;
        let dht;
 
+       if (command == "add-host")
+               zone ??= "lan";
+       else
+               zone ??= "";
+
        if (args.dht == "1" || args.dht == "0")
                dht = args.dht;
        else
@@ -278,12 +293,15 @@ function sync_host(host, net_data) {
        }
 
        let fh = mkstemp();
-       fh.write(`INTERFACE='${interface}'\n`);
-       fh.write(`CONNECT='${connect}'\n`);
-       fh.write(`AUTH_KEY='${auth_key}'\n`);
-       fh.write(`TUNNELS='${tunnels}'\n`);
-       fh.write(`DOMAIN='${domain}'\n`);
-       fh.write(`DHT='${dht}'\n`);
+       fh.write(`
+INTERFACE='${interface}'
+CONNECT='${connect}'
+AUTH_KEY='${auth_key}'
+TUNNELS='${tunnels}'
+DOMAIN='${domain}'
+DHT='${dht}'
+ZONE='${zone}'
+`);
        fh.write(ssh_script);
        fh.flush();
        fh.seek();
@@ -372,13 +390,13 @@ if (command == "create") {
 }
 
 if (command in [ "add-local-host", "set-local-host" ]) {
-       sync_host(null, net_data);
        command = replace(command, "local-", "");
+       sync_host(null, net_data, command);
 }
 
 if (command in [ "add-ssh-host", "set-ssh-host" ]) {
-       sync_host(ssh_host, net_data);
        command = replace(command, "ssh-", "");
+       sync_host(ssh_host, net_data, command);
 }
 
 if (command == "sign") {