list-port-forwardings 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/badvpn-ncd
  2. process main {
  3. getargs() args;
  4. value(args) args;
  5. num_different(args.length, "0") bad_args;
  6. If (bad_args) {
  7. println("Usage: list-port-forwardings");
  8. exit("1");
  9. };
  10. var("0") exit_status;
  11. sys.request_client({"unix", "/run/ncd-control.socket"}) client;
  12. var({"list-port-forwardings"}) request_data;
  13. client->request(request_data, "reply_handler", "finished_handler", {});
  14. }
  15. template reply_handler {
  16. value(_reply.data) reply_data;
  17. reply_data->get("0") status;
  18. reply_data->get("1") arg;
  19. val_equal(status, "ok") is_ok;
  20. If (is_ok) {
  21. println("Protocol Start End Destination");
  22. Foreach (arg As entry) {
  23. value(entry) entry;
  24. entry->get("0") protocol;
  25. entry->get("1") port_start;
  26. entry->get("2") port_end;
  27. entry->get("3") dest_addr;
  28. call("append_spaces", {port_start, "5"}) fixed_start;
  29. call("append_spaces", {port_end, "5"}) fixed_end;
  30. println(protocol, " ", fixed_start.result, " ", fixed_end.result, " ", dest_addr);
  31. };
  32. } Else {
  33. _caller.exit_status->set("1");
  34. println("Error: ", arg);
  35. };
  36. }
  37. template finished_handler {
  38. exit(_caller.exit_status);
  39. }
  40. template append_spaces {
  41. alias("_arg0") input;
  42. alias("_arg1") min_length;
  43. value(input) result;
  44. backtrack_point() point;
  45. num_lesser(result.length, min_length) more;
  46. If (more) {
  47. result->append(" ");
  48. point->go();
  49. };
  50. }