2
0

relay.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. 'use strict';
  2. 'require uci';
  3. 'require form';
  4. 'require network';
  5. 'require tools.widgets as widgets';
  6. network.registerPatternVirtual(/^relay-.+$/);
  7. var RelayDevicePrototype = {
  8. __init__: function(ifname, network) {
  9. this.ifname = ifname;
  10. this.network = network;
  11. },
  12. _aggregateDevices: function(fn, first) {
  13. var devices = this.network ? this.network.getDevices() : [],
  14. rv = 0;
  15. for (var i = 0; i < devices.length; i++) {
  16. var v = devices[i][fn].apply(devices[i]);
  17. if (v != null) {
  18. if (first)
  19. return v;
  20. rv += v;
  21. }
  22. }
  23. return first ? null : [ rv, devices.length ];
  24. },
  25. getPorts: function() { return this.network ? this.network.getDevices() : [] },
  26. getType: function() { return 'tunnel' },
  27. getTypeI18n: function() { return _('Relay Bridge') },
  28. getShortName: function() {
  29. return '%s "%h"'.format(_('Relay'), this.ifname);
  30. },
  31. isUp: function() {
  32. var res = this._aggregateDevices('isUp');
  33. return (res[1] > 0 && res[0] == res[1]);
  34. },
  35. getTXBytes: function() { return this._aggregateDevices('getTXBytes')[0] },
  36. getRXBytes: function() { return this._aggregateDevices('getRXBytes')[0] },
  37. getTXPackets: function() { return this._aggregateDevices('getTXPackets')[0] },
  38. getRXPackets: function() { return this._aggregateDevices('getRXPackets')[0] },
  39. getMAC: function() { return this._aggregateDevices('getMAC', true) },
  40. getIPAddrs: function() {
  41. var ipaddr = this.network ? L.toArray(uci.get('network', this.network.getName(), 'ipaddr'))[0] : null;
  42. return (ipaddr != null ? [ ipaddr ] : []);
  43. },
  44. getIP6Addrs: function() { return [] }
  45. };
  46. return network.registerProtocol('relay', {
  47. getI18n: function() {
  48. return _('Relay bridge');
  49. },
  50. getIfname: function() {
  51. return 'relay-%s'.format(this.sid);
  52. },
  53. getOpkgPackage: function() {
  54. return 'relayd';
  55. },
  56. isFloating: function() {
  57. return true;
  58. },
  59. isVirtual: function() {
  60. return true;
  61. },
  62. containsDevice: function(ifname) {
  63. return (network.getIfnameOf(ifname) == this.getIfname());
  64. },
  65. isUp: function() {
  66. var dev = this.getDevice();
  67. return (dev ? dev.isUp() : false);
  68. },
  69. getDevice: function() {
  70. return network.instantiateDevice(this.sid, this, RelayDevicePrototype);
  71. },
  72. getDevices: function() {
  73. if (this.devices)
  74. return this.devices;
  75. var networkNames = L.toArray(uci.get('network', this.sid, 'network')),
  76. deviceNames = L.toArray(uci.get('network', this.sid, 'ifname')),
  77. devices = {},
  78. rv = [];
  79. for (var i = 0; i < networkNames.length; i++) {
  80. var net = network.instantiateNetwork(networkNames[i]),
  81. dev = net ? net.getDevice() : null;
  82. if (dev)
  83. devices[dev.getName()] = dev;
  84. }
  85. for (var i = 0; i < deviceNames.length; i++) {
  86. var dev = network.getDevice(deviceNames[i]);
  87. if (dev)
  88. devices[dev.getName()] = dev;
  89. }
  90. deviceNames = Object.keys(devices);
  91. deviceNames.sort();
  92. for (var i = 0; i < deviceNames.length; i++)
  93. rv.push(devices[deviceNames[i]]);
  94. this.devices = rv;
  95. return rv;
  96. },
  97. getUptime: function() {
  98. var networkNames = L.toArray(uci.get('network', this.sid, 'network')),
  99. uptime = 0;
  100. for (var i = 0; i < networkNames.length; i++) {
  101. var net = network.instantiateNetwork(networkNames[i]);
  102. if (net)
  103. uptime = Math.max(uptime, net.getUptime());
  104. }
  105. return uptime;
  106. },
  107. getErrors: function() {
  108. return null;
  109. },
  110. renderFormOptions: function(s) {
  111. var o;
  112. o = s.taboption('general', form.Value, 'ipaddr', _('Local IPv4 address'), _('Address to access local relay bridge'));
  113. o.datatype = 'ip4addr("nomask")';
  114. o = s.taboption('general', widgets.NetworkSelect, 'network', _('Relay between networks'));
  115. o.exclude = s.section;
  116. o.multiple = true;
  117. o.nocreate = true;
  118. o.nobridges = true;
  119. o.novirtual = true;
  120. o = s.taboption('advanced', form.Flag, 'forward_bcast', _('Forward broadcast traffic'));
  121. o.default = o.enabled;
  122. o = s.taboption('advanced', form.Flag, 'forward_dhcp', _('Forward DHCP traffic'));
  123. o.default = o.enabled;
  124. o = s.taboption('advanced', form.Value, 'gateway', _('Use DHCP gateway'), _('Override the gateway in DHCP responses'));
  125. o.datatype = 'ip4addr("nomask")';
  126. o.depends('forward_dhcp', '1');
  127. o = s.taboption('advanced', form.Value, 'expiry', _('Host expiry timeout'), _('Specifies the maximum amount of seconds after which hosts are presumed to be dead'));
  128. o.placeholder = '30';
  129. o.datatype = 'min(1)';
  130. o = s.taboption('advanced', form.Value, 'retry', _('ARP retry threshold'), _('Specifies the maximum amount of failed ARP requests until hosts are presumed to be dead'));
  131. o.placeholder = '5';
  132. o.datatype = 'min(1)';
  133. o = s.taboption('advanced', form.Value, 'table', _('Use routing table'), _('Override the table used for internal routes'));
  134. o.placeholder = '16800';
  135. o.datatype = 'range(0,65535)';
  136. }
  137. });