3 let fs = require("fs");
5 let script_dir = sourcepath(0, true);
6 if (fs.basename(script_dir) == "scripts") {
7 unet_tool = fs.dirname(script_dir) + "/unet-tool";
8 if (!fs.access(unet_tool, "x")) {
9 warn("unet-tool missing\n");
13 unet_tool = "unet-tool";
25 warn("Usage: ",fs.basename(sourcepath())," [<flags>] <file> <command> [<args>] [<option>=<value> ...]\n",
28 " - create: Create a new network file\n",
29 " - set-config: Change network config parameters\n",
30 " - add-host <name>: Add a host\n",
31 " - add-ssh-host <name> <host>: Add a remote OpenWrt host via SSH\n",
32 " (<host> can contain SSH options as well)\n",
33 " - set-host <name>: Change host settings\n",
34 " - set-ssh-host <name> <host>: Update local and remote host settings\n",
35 " - add-service <name>: Add a service\n",
36 " - set-service <name>: Change service settings\n",
37 " - sign Sign network data\n",
40 " -p: Print modified JSON instead of updating file\n",
43 " - config options (create, set-config):\n",
44 " port=<val> set tunnel port (default: ", defaults.port, ")\n",
45 " pex_port=<val> set peer-exchange port (default: ", defaults.pex_port, ")\n",
46 " keepalive=<val> set keepalive interval (seconds, 0: off, default: ", defaults.keepalive,")\n",
47 " host options (add-host, add-ssh-host, set-host):\n",
48 " key=<val> set host public key (required for add-host)\n",
49 " port=<val> set host tunnel port number\n",
50 " groups=[+|-]<val>[,<val>...] set/add/remove groups that the host is a member of\n",
51 " ipaddr=[+|-]<val>[,<val>...] set/add/remove host ip addresses\n",
52 " subnet=[+|-]<val>[,<val>...] set/add/remove host announced subnets\n",
53 " endpoint=<val> set host endpoint address\n",
54 " ssh host options (add-ssh-host, set-ssh-host)\n",
55 " auth_key=<key> use <key> as public auth key on the remote host\n",
56 " priv_key=<key> use <key> as private host key on the remote host (default: generate a new key)\n",
57 " interface=<name> use <name> as interface in /etc/config/network on the remote host\n",
58 " connect=<val>[,<val>...] set IP addresses that the host will contact for network updates\n",
59 " tunnels=<ifname>:<service>[,...] set active tunnel devices\n",
60 " service options (add-service, set-service):\n",
61 " type=<val> set service type (required for add-service)\n",
62 " members=[+|-]<val>[,<val>...] set/add/remove service member hosts/groups\n",
63 " vxlan service options (add-service, set-service):\n",
64 " id=<val> set VXLAN ID\n",
65 " port=<val> set VXLAN port\n",
66 " mtu=<val> set VXLAN device MTU\n",
67 " forward_ports=[+|-]<val>[,<val>...] set members allowed to receive broadcast/multicast/unknown-unicast\n",
69 " upload=<ip>[,<ip>...] upload signed file to hosts\n",
78 command = shift(ARGV);
81 int: function(object, name, val) {
82 object[name] = int(val);
84 string: function(object, name, val) {
87 array: function(object, name, val) {
88 let op = substr(val, 0, 1);
90 if (op == "+" || op == "-") {
98 let vals = split(val, ",");
100 object[name] = filter(object[name], function(v) {
104 push(object[name], val);
107 if (!length(object[name]))
112 service_field_types = {
117 forward_ports: "array",
129 if [ -n "$first" ]; then
134 uci $cmd "network.$INTERFACE.$field=$cur"
138 set_interface_attrs() {
139 [ -n "$AUTH_KEY" ] && uci set "network.$INTERFACE.auth_key=$AUTH_KEY"
140 set_list connect "$CONNECT"
141 set_list tunnels "$TUNNELS"
145 [ "$(uci -q get "network.$INTERFACE")" = "interface" -a "$(uci -q get "network.$INTERFACE.proto")" = "unet" ] && return 0
147 set network.$INTERFACE=interface
148 set network.$INTERFACE.proto=unet
149 set network.$INTERFACE.device=$INTERFACE
150 set network.$INTERFACE.domain=unet
154 check_interface_key() {
155 key="$(uci -q get "network.$INTERFACE.key" | unet-tool -q -H -K -)"
157 uci set "network.$INTERFACE.key=$(unet-tool -G)"
158 key="$(uci get "network.$INTERFACE.key" | unet-tool -H -K -)"
173 function fetch_args() {
175 vals = match(arg, /^(.[[:alnum:]_-]*)=(.*)$/);
177 warn("Invalid argument: ", arg, "\n");
180 args[vals[1]] = vals[2]
184 function set_field(typename, object, name, val) {
185 if (!field_types[typename]) {
186 warn("Invalid type ", type, "\n");
190 if (type(val) != "string")
198 field_types[typename](object, name, val);
201 function set_fields(object, list) {
203 set_field(list[f], object, f, args[f]);
206 function set_host(name) {
207 let host = net_data.hosts[name];
219 function set_service(name) {
220 let service = net_data.services[name];
222 set_fields(service, {
227 if (service_field_types[service.type])
228 set_fields(service.config, service_field_types[service.type]);
231 function sync_ssh_host(host) {
232 let interface = args.interface ?? "unet";
233 let connect = replace(args.connect ?? "", ",", " ");
234 let auth_key = args.auth_key;
235 let tunnels = replace(replace(args.tunnels ?? "", ",", " "), ":", "=");
238 let fh = fs.mkstemp();
239 system(unet_tool + " -q -P -K " + file + ".key >&" + fh.fileno());
241 auth_key = fh.read("line");
243 auth_key = replace(auth_key, "\n", "");
244 if (auth_key == "") {
245 warn("Could not read auth key\n");
250 let fh = fs.mkstemp();
251 fh.write("INTERFACE='" + interface + "'\n");
252 fh.write("CONNECT='" + connect + "'\n");
253 fh.write("AUTH_KEY='" + auth_key + "'\n");
254 fh.write("TUNNELS='" + tunnels + "'\n");
255 fh.write(ssh_script);
260 system(sprintf("ssh "+host+" sh <&%d >&%d", fh.fileno(), fh2.fileno()));
266 while (line = fh2.read("line")) {
267 let vals = match(line, /^(.[[:alnum:]_-]*)=(.*)\n$/);
269 warn("Invalid argument: ", arg, "\n");
272 data[vals[1]] = vals[2]
277 warn("Could not read host key from SSH host\n");
284 while (substr(ARGV[0], 0, 1) == "-") {
288 else if (opt == "-p")
294 if (command == "add-host" || command == "set-host" ||
295 command == "add-ssh-host" || command == "set-ssh-host") {
296 hostname = shift(ARGV);
298 warn("Missing host name argument\n");
303 if (command == "add-ssh-host" || command == "set-ssh-host") {
304 ssh_host = shift(ARGV);
306 warn("Missing SSH host/user argument\n");
311 if (command == "add-service" || command == "set-service") {
312 servicename = shift(ARGV);
314 warn("Missing service name argument\n");
321 if (command == "add-ssh-host" || command == "set-ssh-host") {
322 sync_ssh_host(ssh_host);
323 command = replace(command, "ssh-", "");
326 if (command == "create") {
335 warn("Could not open input file ", file, "\n");
341 warn("Could not parse input file ", file, "\n");
346 if (command == "create") {
347 for (key in keys(defaults))
348 args[key] ??= "" + defaults[key];
349 if (!fs.access(file + ".key"))
350 system(unet_tool + " -G > " + file + ".key");
353 if (command == "sign") {
354 ret = system(unet_tool + " -S -K " + file + ".key -o " + file + ".bin " + file);
359 hosts = split(args.upload, ",");
360 for (host in hosts) {
361 warn("Uploading " + file + ".bin to " + host + "\n");
362 ret = system(unet_tool + " -U " + host + " -K "+ file + ".key " + file + ".bin");
364 warn("Upload failed\n");
370 if (command == "create" || command == "set-config") {
371 set_fields(net_data.config, {
375 set_field("int", net_data.config, "peer-exchange-port", args.pex_port);
376 } else if (command == "add-host") {
377 net_data.hosts[hostname] = {};
379 warn("Missing host key\n");
383 } else if (command == "set-host") {
384 if (!net_data.hosts[hostname]) {
385 warn("Host '", hostname, "' does not exist\n");
389 } else if (command == "add-service") {
390 net_data.services[servicename] = {
395 warn("Missing service type\n");
398 set_service(servicename);
399 } else if (command == "set-service") {
400 if (!net_data.services[servicename]) {
401 warn("Service '", servicename, "' does not exist\n");
404 set_service(servicename);
406 warn("Unknown command\n");
410 net_data_json = sprintf("%.J\n", net_data);
412 print(net_data_json);
414 fs.writefile(file, net_data_json);