3 let fs = require("fs");
5 function assert(cond, message) {
14 let script_dir = sourcepath(0, true);
15 if (fs.basename(script_dir) == "scripts") {
16 unet_tool = `${fs.dirname(script_dir)}/unet-tool`;
17 assert(fs.access(unet_tool, "x"), "unet-tool missing");
19 unet_tool = "unet-tool";
30 const usage_message = `
31 Usage: ${fs.basename(sourcepath())} [<flags>] <file> <command> [<args>] [<option>=<value> ...]
34 - create: Create a new network file
35 - set-config: Change network config parameters
36 - add-host <name>: Add a host
37 - add-ssh-host <name> <host>: Add a remote OpenWrt host via SSH
38 (<host> can contain SSH options as well)
39 - set-host <name>: Change host settings
40 - set-ssh-host <name> <host>: Update local and remote host settings
41 - add-service <name>: Add a service
42 - set-service <name>: Change service settings
43 - sign Sign network data
46 -p: Print modified JSON instead of updating file
49 - config options (create, set-config):
50 port=<val> set tunnel port (default: ${defaults.port})
51 pex_port=<val> set peer-exchange port (default: ${defaults.pex_port})
52 keepalive=<val> set keepalive interval (seconds, 0: off, default: ${defaults.keepalive})
53 host options (add-host, add-ssh-host, set-host):
54 key=<val> set host public key (required for add-host)
55 port=<val> set host tunnel port number
56 groups=[+|-]<val>[,<val>...] set/add/remove groups that the host is a member of
57 ipaddr=[+|-]<val>[,<val>...] set/add/remove host ip addresses
58 subnet=[+|-]<val>[,<val>...] set/add/remove host announced subnets
59 endpoint=<val> set host endpoint address
60 gateway=<name> set host gateway (using name of other host)
61 ssh host options (add-ssh-host, set-ssh-host)
62 auth_key=<key> use <key> as public auth key on the remote host
63 priv_key=<key> use <key> as private host key on the remote host (default: generate a new key)
64 interface=<name> use <name> as interface in /etc/config/network on the remote host
65 domain=<name> use <name> as hosts file domain on the remote host (default: unet)
66 connect=<val>[,<val>...] set IP addresses that the host will contact for network updates
67 tunnels=<ifname>:<service>[,...] set active tunnel devices
68 service options (add-service, set-service):
69 type=<val> set service type (required for add-service)
70 members=[+|-]<val>[,<val>...] set/add/remove service member hosts/groups
71 vxlan service options (add-service, set-service):
73 port=<val> set VXLAN port
74 mtu=<val> set VXLAN device MTU
75 forward_ports=[+|-]<val>[,<val>...] set members allowed to receive broadcast/multicast/unknown-unicast
77 upload=<ip>[,<ip>...] upload signed file to hosts
90 command = shift(ARGV);
93 int: function(object, name, val) {
94 object[name] = int(val);
96 string: function(object, name, val) {
99 array: function(object, name, val) {
100 let op = substr(val, 0, 1);
102 if (op == "+" || op == "-") {
103 val = substr(val, 1);
110 let vals = split(val, ",");
112 object[name] = filter(object[name], function(v) {
116 push(object[name], val);
119 if (!length(object[name]))
124 service_field_types = {
129 forward_ports: "array",
141 if [ -n "$first" ]; then
146 uci $cmd "network.$INTERFACE.$field=$cur"
150 set_interface_attrs() {
151 [ -n "$AUTH_KEY" ] && uci set "network.$INTERFACE.auth_key=$AUTH_KEY"
152 set_list connect "$CONNECT"
153 set_list tunnels "$TUNNELS"
154 uci set "network.$INTERFACE.domain=$DOMAIN"
158 [ "$(uci -q get "network.$INTERFACE")" = "interface" -a "$(uci -q get "network.$INTERFACE.proto")" = "unet" ] && return 0
160 set network.$INTERFACE=interface
161 set network.$INTERFACE.proto=unet
162 set network.$INTERFACE.device=$INTERFACE
166 check_interface_key() {
167 key="$(uci -q get "network.$INTERFACE.key" | unet-tool -q -H -K -)"
169 uci set "network.$INTERFACE.key=$(unet-tool -G)"
170 key="$(uci get "network.$INTERFACE.key" | unet-tool -H -K -)"
186 function fetch_args() {
188 vals = match(arg, /^(.[[:alnum:]_-]*)=(.*)$/);
189 assert(vals, `Invalid argument: ${arg}`);
190 args[vals[1]] = vals[2]
194 function set_field(typename, object, name, val) {
195 if (!field_types[typename]) {
196 warn(`Invalid type ${type}\n`);
200 if (type(val) != "string")
208 field_types[typename](object, name, val);
211 function set_fields(object, list) {
213 set_field(list[f], object, f, args[f]);
216 function set_host(name) {
217 let host = net_data.hosts[name];
230 function set_service(name) {
231 let service = net_data.services[name];
233 set_fields(service, {
238 if (service_field_types[service.type])
239 set_fields(service.config, service_field_types[service.type]);
242 function sync_ssh_host(host) {
243 let interface = args.interface ?? "unet";
244 let connect = replace(args.connect ?? "", ",", " ");
245 let auth_key = args.auth_key;
246 let tunnels = replace(replace(args.tunnels ?? "", ",", " "), ":", "=");
247 let domain = args.domain ?? "unet";
250 let fh = fs.mkstemp();
251 system(`${unet_tool} -q -P -K ${file}.key >&${fh.fileno()}`);
253 auth_key = fh.read("line");
255 auth_key = replace(auth_key, "\n", "");
256 if (auth_key == "") {
257 warn("Could not read auth key\n");
262 let fh = fs.mkstemp();
263 fh.write(`INTERFACE='${interface}'\n`);
264 fh.write(`CONNECT='${connect}'\n`);
265 fh.write(`AUTH_KEY='${auth_key}'\n`);
266 fh.write(`TUNNELS='${tunnels}'\n`);
267 fh.write(`DOMAIN='${domain}'\n`);
268 fh.write(ssh_script);
273 system(`ssh ${host} sh <&${fh.fileno()} >&${fh2.fileno()}`);
279 while (line = fh2.read("line")) {
280 let vals = match(line, /^(.[[:alnum:]_-]*)=(.*)\n$/);
281 assert(vals, `Invalid argument: ${arg}`);
282 data[vals[1]] = vals[2]
286 assert(data.key, "Could not read host key from SSH host");
291 while (substr(ARGV[0], 0, 1) == "-") {
295 else if (opt == "-p")
301 if (command in [ "add-host", "set-host", "add-ssh-host", "set-ssh-host" ]) {
302 hostname = shift(ARGV);
303 assert(hostname, "Missing host name argument");
306 if (command in [ "add-ssh-host", "set-ssh-host" ]) {
307 ssh_host = shift(ARGV);
308 assert(ssh_host, "Missing SSH host/user argument");
311 if (command in [ "add-service", "set-service" ]) {
312 servicename = shift(ARGV);
313 assert(servicename, "Missing service name argument");
318 if (command in [ "add-ssh-host", "set-ssh-host" ]) {
319 sync_ssh_host(ssh_host);
320 command = replace(command, "ssh-", "");
323 if (command == "create") {
331 assert(fh, `Could not open input file ${file}`);
336 assert(false, `Could not parse input file ${file}`);
340 if (command == "create") {
341 for (key, val in defaults)
342 args[key] ??= `${val}`;
343 if (!fs.access(`${file}.key`))
344 system(`${unet_tool} -G > ${file}.key`);
347 if (command == "sign") {
348 ret = system(`${unet_tool} -S -K ${file}.key -o ${file}.bin ${file}`);
353 hosts = split(args.upload, ",");
354 for (host in hosts) {
355 warn(`Uploading ${file}.bin to ${host}\n`);
356 ret = system(`${unet_tool} -U ${host} -K ${file}.key ${file}.bin`);
358 warn("Upload failed\n");
367 set_fields(net_data.config, {
371 set_field("int", net_data.config, "peer-exchange-port", args.pex_port);
375 net_data.hosts[hostname] = {};
376 assert(args.key, "Missing host key");
381 assert(net_data.hosts[hostname], `Host '${hostname}' does not exist`);
386 net_data.services[servicename] = {
390 assert(args.type, "Missing service type");
391 set_service(servicename);
395 assert(net_data.services[servicename], `Service '${servicename}' does not exist`);
396 set_service(servicename);
400 assert(false, "Unknown command");
403 net_data_json = sprintf("%.J\n", net_data);
405 print(net_data_json);
407 fs.writefile(file, net_data_json);