ZeroTierNode.jsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. var ZeroTierNode = React.createClass({
  2. getInitialState: function() {
  3. return {
  4. address: '----------',
  5. online: false,
  6. version: '_._._',
  7. _networks: [],
  8. _peers: []
  9. };
  10. },
  11. ago: function(ms) {
  12. if (ms > 0) {
  13. var tmp = Math.round((Date.now() - ms) / 1000);
  14. return ((tmp > 0) ? tmp : 0);
  15. } else return 0;
  16. },
  17. updatePeers: function() {
  18. Ajax.call({
  19. url: 'peer?auth='+this.props.authToken,
  20. cache: false,
  21. type: 'GET',
  22. success: function(data) {
  23. if (data) {
  24. var pl = JSON.parse(data);
  25. if (Array.isArray(pl)) {
  26. this.setState({_peers: pl});
  27. }
  28. }
  29. }.bind(this),
  30. error: function() {
  31. }.bind(this)
  32. });
  33. },
  34. updateNetworks: function() {
  35. Ajax.call({
  36. url: 'network?auth='+this.props.authToken,
  37. cache: false,
  38. type: 'GET',
  39. success: function(data) {
  40. if (data) {
  41. var nwl = JSON.parse(data);
  42. if (Array.isArray(nwl)) {
  43. this.setState({_networks: nwl});
  44. }
  45. }
  46. }.bind(this),
  47. error: function() {
  48. }.bind(this)
  49. });
  50. },
  51. updateAll: function() {
  52. Ajax.call({
  53. url: 'status?auth='+this.props.authToken,
  54. cache: false,
  55. type: 'GET',
  56. success: function(data) {
  57. this.alertedToFailure = false;
  58. if (data) {
  59. var status = JSON.parse(data);
  60. this.setState(status);
  61. document.title = 'ZeroTier One [' + status.address + ']';
  62. }
  63. this.updateNetworks();
  64. this.updatePeers();
  65. }.bind(this),
  66. error: function() {
  67. this.setState(this.getInitialState());
  68. if (!this.alertedToFailure) {
  69. this.alertedToFailure = true;
  70. alert('Authorization token invalid or ZeroTier One service not running.');
  71. }
  72. }.bind(this)
  73. });
  74. },
  75. joinNetwork: function(event) {
  76. event.preventDefault();
  77. if ((this.networkToJoin)&&(this.networkToJoin.length === 16)) {
  78. Ajax.call({
  79. url: 'network/'+this.networkToJoin+'?auth='+this.props.authToken,
  80. cache: false,
  81. type: 'POST',
  82. success: function(data) {
  83. this.networkToJoin = '';
  84. if (this.networkInputElement)
  85. this.networkInputElement.value = '';
  86. this.updateNetworks();
  87. }.bind(this),
  88. error: function() {
  89. }.bind(this)
  90. });
  91. } else {
  92. alert('To join a network, enter its 16-digit network ID.');
  93. }
  94. },
  95. handleNetworkIdEntry: function(event) {
  96. this.networkInputElement = event.target;
  97. var nid = this.networkInputElement.value;
  98. if (nid) {
  99. nid = nid.toLowerCase();
  100. var nnid = '';
  101. for(var i=0;((i<nid.length)&&(i<16));++i) {
  102. if ("0123456789abcdef".indexOf(nid.charAt(i)) >= 0)
  103. nnid += nid.charAt(i);
  104. }
  105. this.networkToJoin = nnid;
  106. this.networkInputElement.value = nnid;
  107. } else {
  108. this.networkToJoin = '';
  109. this.networkInputElement.value = '';
  110. }
  111. },
  112. handleNetworkDelete: function(nwid) {
  113. var networks = [];
  114. for(var i=0;i<this.state._networks.length;++i) {
  115. if (this.state._networks[i].nwid !== nwid)
  116. networks.push(this.state._networks[i]);
  117. }
  118. this.setState({_networks: networks});
  119. },
  120. componentDidMount: function() {
  121. this.updateAll();
  122. this.updateIntervalId = setInterval(this.updateAll,2500);
  123. },
  124. componentWillUnmount: function() {
  125. clearInterval(this.updateIntervalId);
  126. },
  127. render: function() {
  128. return (
  129. <div className="zeroTierNode">
  130. <div className="middle"><div className="middleCell">
  131. <div className="middleScroll">
  132. <div className="networks" key="_networks">
  133. {
  134. this.state._networks.map(function(network) {
  135. network['authToken'] = this.props.authToken;
  136. network['onNetworkDeleted'] = this.handleNetworkDelete;
  137. return React.createElement('div',{className: 'network',key: network.nwid},React.createElement(ZeroTierNetwork,network));
  138. }.bind(this))
  139. }
  140. </div>
  141. </div>
  142. </div></div>
  143. <div className="bottom">
  144. <div className="left">
  145. <span className="statusLine"><span className="zeroTierAddress">{this.state.address}</span>&nbsp;&nbsp;{this.state.online ? (this.state.tcpFallbackActive ? 'TUNNELED' : 'ONLINE') : 'OFFLINE'}&nbsp;&nbsp;{this.state.version}</span>
  146. </div>
  147. <div className="right">
  148. <form onSubmit={this.joinNetwork}><input type="text" maxlength="16" placeholder="[ Network ID ]" onChange={this.handleNetworkIdEntry} size="16"/><button type="button" onClick={this.joinNetwork}>Join</button></form>
  149. </div>
  150. </div>
  151. </div>
  152. );
  153. }
  154. });