app.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*jslint browser: true, continue: true, plusplus: true */
  2. /*global $: false, angular: false */
  3. 'use strict';
  4. var syncthing = angular.module('syncthing', []);
  5. var urlbase = 'rest';
  6. syncthing.controller('SyncthingCtrl', function ($scope, $http) {
  7. var prevDate = 0;
  8. var getOK = true;
  9. var restarting = false;
  10. $scope.connections = {};
  11. $scope.config = {};
  12. $scope.myID = '';
  13. $scope.nodes = [];
  14. $scope.configInSync = true;
  15. $scope.errors = [];
  16. $scope.seenError = '';
  17. $scope.model = {};
  18. $scope.repos = {};
  19. // Strings before bools look better
  20. $scope.settings = [
  21. {id: 'ListenStr', descr: 'Sync Protocol Listen Addresses', type: 'text', restart: true},
  22. {id: 'MaxSendKbps', descr: 'Outgoing Rate Limit (KiB/s)', type: 'number', restart: true},
  23. {id: 'RescanIntervalS', descr: 'Rescan Interval (s)', type: 'number', restart: true},
  24. {id: 'ReconnectIntervalS', descr: 'Reconnect Interval (s)', type: 'number', restart: true},
  25. {id: 'ParallelRequests', descr: 'Max Outstanding Requests', type: 'number', restart: true},
  26. {id: 'MaxChangeKbps', descr: 'Max File Change Rate (KiB/s)', type: 'number', restart: true},
  27. {id: 'GlobalAnnEnabled', descr: 'Global Discovery', type: 'bool', restart: true},
  28. {id: 'LocalAnnEnabled', descr: 'Local Discovery', type: 'bool', restart: true},
  29. {id: 'LocalAnnPort', descr: 'Local Discovery Port', type: 'number', restart: true},
  30. {id: 'StartBrowser', descr: 'Start Browser', type: 'bool'},
  31. {id: 'UPnPEnabled', descr: 'Enable UPnP', type: 'bool'},
  32. ];
  33. $scope.guiSettings = [
  34. {id: 'Address', descr: 'GUI Listen Addresses', type: 'text', restart: true},
  35. {id: 'User', descr: 'GUI Authentication User', type: 'text', restart: true},
  36. {id: 'Password', descr: 'GUI Authentication Password', type: 'password', restart: true},
  37. {id: 'UseTLS', descr: 'Use HTTPS for GUI', type: 'bool', restart: true},
  38. ];
  39. function getSucceeded() {
  40. if (!getOK) {
  41. $scope.init();
  42. $('#networkError').modal('hide');
  43. getOK = true;
  44. }
  45. if (restarting) {
  46. $scope.init();
  47. $('#restarting').modal('hide');
  48. $('#shutdown').modal('hide');
  49. restarting = false;
  50. }
  51. }
  52. function getFailed() {
  53. if (restarting) {
  54. return;
  55. }
  56. if (getOK) {
  57. $('#networkError').modal({backdrop: 'static', keyboard: false});
  58. getOK = false;
  59. }
  60. }
  61. $scope.refresh = function () {
  62. $http.get(urlbase + '/system').success(function (data) {
  63. getSucceeded();
  64. $scope.system = data;
  65. }).error(function () {
  66. getFailed();
  67. });
  68. Object.keys($scope.repos).forEach(function (id) {
  69. $http.get(urlbase + '/model?repo=' + encodeURIComponent(id)).success(function (data) {
  70. $scope.model[id] = data;
  71. });
  72. });
  73. $http.get(urlbase + '/connections').success(function (data) {
  74. var now = Date.now(),
  75. td = (now - prevDate) / 1000,
  76. id;
  77. prevDate = now;
  78. $scope.inbps = 0;
  79. $scope.outbps = 0;
  80. for (id in data) {
  81. if (!data.hasOwnProperty(id)) {
  82. continue;
  83. }
  84. try {
  85. data[id].inbps = Math.max(0, 8 * (data[id].InBytesTotal - $scope.connections[id].InBytesTotal) / td);
  86. data[id].outbps = Math.max(0, 8 * (data[id].OutBytesTotal - $scope.connections[id].OutBytesTotal) / td);
  87. } catch (e) {
  88. data[id].inbps = 0;
  89. data[id].outbps = 0;
  90. }
  91. $scope.inbps += data[id].inbps;
  92. $scope.outbps += data[id].outbps;
  93. }
  94. $scope.connections = data;
  95. });
  96. $http.get(urlbase + '/errors').success(function (data) {
  97. $scope.errors = data;
  98. });
  99. };
  100. $scope.repoStatus = function (repo) {
  101. if (typeof $scope.model[repo] === 'undefined') {
  102. return 'Unknown';
  103. }
  104. if ($scope.model[repo].invalid !== '') {
  105. return 'Stopped';
  106. }
  107. var state = '' + $scope.model[repo].state;
  108. state = state[0].toUpperCase() + state.substr(1);
  109. if (state == "Syncing" || state == "Idle") {
  110. state += " (" + $scope.syncPercentage(repo) + "%)";
  111. }
  112. return state;
  113. }
  114. $scope.repoClass = function (repo) {
  115. if (typeof $scope.model[repo] === 'undefined') {
  116. return 'info';
  117. }
  118. if ($scope.model[repo].invalid !== '') {
  119. return 'danger';
  120. }
  121. var state = '' + $scope.model[repo].state;
  122. if (state == 'idle') {
  123. return 'success';
  124. }
  125. if (state == 'syncing') {
  126. return 'primary';
  127. }
  128. return 'info';
  129. }
  130. $scope.syncPercentage = function (repo) {
  131. if (typeof $scope.model[repo] === 'undefined') {
  132. return 100;
  133. }
  134. if ($scope.model[repo].globalBytes === 0) {
  135. return 100;
  136. }
  137. var pct = 100 * $scope.model[repo].inSyncBytes / $scope.model[repo].globalBytes;
  138. return Math.floor(pct);
  139. };
  140. $scope.nodeStatus = function (nodeCfg) {
  141. var conn = $scope.connections[nodeCfg.NodeID];
  142. if (conn) {
  143. if (conn.Completion === 100) {
  144. return 'Up to Date';
  145. } else {
  146. return 'Syncing (' + conn.Completion + '%)';
  147. }
  148. }
  149. return 'Disconnected';
  150. };
  151. $scope.nodeIcon = function (nodeCfg) {
  152. var conn = $scope.connections[nodeCfg.NodeID];
  153. if (conn) {
  154. if (conn.Completion === 100) {
  155. return 'ok';
  156. } else {
  157. return 'refresh';
  158. }
  159. }
  160. return 'minus';
  161. };
  162. $scope.nodeClass = function (nodeCfg) {
  163. var conn = $scope.connections[nodeCfg.NodeID];
  164. if (conn) {
  165. if (conn.Completion === 100) {
  166. return 'success';
  167. } else {
  168. return 'primary';
  169. }
  170. }
  171. return 'info';
  172. };
  173. $scope.nodeAddr = function (nodeCfg) {
  174. var conn = $scope.connections[nodeCfg.NodeID];
  175. if (conn) {
  176. return conn.Address;
  177. }
  178. return '?';
  179. };
  180. $scope.nodeCompletion = function (nodeCfg) {
  181. var conn = $scope.connections[nodeCfg.NodeID];
  182. if (conn) {
  183. return conn.Completion + '%';
  184. }
  185. return '';
  186. };
  187. $scope.nodeVer = function (nodeCfg) {
  188. if (nodeCfg.NodeID === $scope.myID) {
  189. return $scope.version;
  190. }
  191. var conn = $scope.connections[nodeCfg.NodeID];
  192. if (conn) {
  193. return conn.ClientVersion;
  194. }
  195. return '?';
  196. };
  197. $scope.findNode = function (nodeID) {
  198. var matches = $scope.nodes.filter(function (n) { return n.NodeID == nodeID; });
  199. if (matches.length != 1) {
  200. return undefined;
  201. }
  202. return matches[0];
  203. };
  204. $scope.nodeName = function (nodeCfg) {
  205. if (typeof nodeCfg === 'undefined') {
  206. return "";
  207. }
  208. if (nodeCfg.Name) {
  209. return nodeCfg.Name;
  210. }
  211. return nodeCfg.NodeID.substr(0, 6);
  212. };
  213. $scope.thisNodeName = function () {
  214. var node = $scope.thisNode();
  215. if (typeof node === 'undefined') {
  216. return "(unknown node)";
  217. }
  218. if (node.Name) {
  219. return node.Name;
  220. }
  221. return node.NodeID.substr(0, 6);
  222. };
  223. $scope.editSettings = function () {
  224. // Make a working copy
  225. $scope.config.workingOptions = angular.copy($scope.config.Options);
  226. $scope.config.workingGUI = angular.copy($scope.config.GUI);
  227. $('#settings').modal({backdrop: 'static', keyboard: true});
  228. }
  229. $scope.saveSettings = function () {
  230. // Make sure something changed
  231. var changed = ! angular.equals($scope.config.Options, $scope.config.workingOptions) ||
  232. ! angular.equals($scope.config.GUI, $scope.config.workingGUI);
  233. if(changed){
  234. $scope.config.Options = angular.copy($scope.config.workingOptions);
  235. $scope.config.GUI = angular.copy($scope.config.workingGUI);
  236. $scope.configInSync = false;
  237. $scope.config.Options.ListenAddress = $scope.config.Options.ListenStr.split(',').map(function (x) { return x.trim(); });
  238. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  239. }
  240. $('#settings').modal("hide");
  241. };
  242. $scope.restart = function () {
  243. restarting = true;
  244. $('#restarting').modal({backdrop: 'static', keyboard: false});
  245. $http.post(urlbase + '/restart');
  246. $scope.configInSync = true;
  247. };
  248. $scope.shutdown = function () {
  249. restarting = true;
  250. $http.post(urlbase + '/shutdown').success(function () {
  251. $('#shutdown').modal({backdrop: 'static', keyboard: false});
  252. });
  253. $scope.configInSync = true;
  254. };
  255. $scope.editNode = function (nodeCfg) {
  256. $scope.currentNode = $.extend({}, nodeCfg);
  257. $scope.editingExisting = true;
  258. $scope.editingSelf = (nodeCfg.NodeID == $scope.myID);
  259. $scope.currentNode.AddressesStr = nodeCfg.Addresses.join(', ');
  260. $scope.nodeEditor.$setPristine();
  261. $('#editNode').modal({backdrop: 'static', keyboard: true});
  262. };
  263. $scope.idNode = function () {
  264. $('#idqr').modal('show');
  265. };
  266. $scope.addNode = function () {
  267. $scope.currentNode = {AddressesStr: 'dynamic'};
  268. $scope.editingExisting = false;
  269. $scope.editingSelf = false;
  270. $scope.nodeEditor.$setPristine();
  271. $('#editNode').modal({backdrop: 'static', keyboard: true});
  272. };
  273. $scope.deleteNode = function () {
  274. $('#editNode').modal('hide');
  275. if (!$scope.editingExisting) {
  276. return;
  277. }
  278. $scope.nodes = $scope.nodes.filter(function (n) {
  279. return n.NodeID !== $scope.currentNode.NodeID;
  280. });
  281. $scope.config.Nodes = $scope.nodes;
  282. for (var id in $scope.repos) {
  283. $scope.repos[id].Nodes = $scope.repos[id].Nodes.filter(function (n) {
  284. return n.NodeID !== $scope.currentNode.NodeID;
  285. });
  286. }
  287. $scope.configInSync = false;
  288. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  289. };
  290. $scope.saveNode = function () {
  291. var nodeCfg, done, i;
  292. $scope.configInSync = false;
  293. $('#editNode').modal('hide');
  294. nodeCfg = $scope.currentNode;
  295. nodeCfg.NodeID = nodeCfg.NodeID.replace(/ /g, '').replace(/-/g, '').toUpperCase().trim();
  296. nodeCfg.Addresses = nodeCfg.AddressesStr.split(',').map(function (x) { return x.trim(); });
  297. done = false;
  298. for (i = 0; i < $scope.nodes.length; i++) {
  299. if ($scope.nodes[i].NodeID === nodeCfg.NodeID) {
  300. $scope.nodes[i] = nodeCfg;
  301. done = true;
  302. break;
  303. }
  304. }
  305. if (!done) {
  306. $scope.nodes.push(nodeCfg);
  307. }
  308. $scope.nodes.sort(nodeCompare);
  309. $scope.config.Nodes = $scope.nodes;
  310. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  311. };
  312. $scope.otherNodes = function () {
  313. return $scope.nodes.filter(function (n){
  314. return n.NodeID !== $scope.myID;
  315. });
  316. };
  317. $scope.thisNode = function () {
  318. var i, n;
  319. for (i = 0; i < $scope.nodes.length; i++) {
  320. n = $scope.nodes[i];
  321. if (n.NodeID === $scope.myID) {
  322. return n;
  323. }
  324. }
  325. };
  326. $scope.allNodes = function () {
  327. var nodes = $scope.otherNodes();
  328. nodes.push($scope.thisNode());
  329. return nodes;
  330. };
  331. $scope.errorList = function () {
  332. return $scope.errors.filter(function (e) {
  333. return e.Time > $scope.seenError;
  334. });
  335. };
  336. $scope.clearErrors = function () {
  337. $scope.seenError = $scope.errors[$scope.errors.length - 1].Time;
  338. $http.post(urlbase + '/error/clear');
  339. };
  340. $scope.friendlyNodes = function (str) {
  341. for (var i = 0; i < $scope.nodes.length; i++) {
  342. var cfg = $scope.nodes[i];
  343. str = str.replace(cfg.NodeID, $scope.nodeName(cfg));
  344. }
  345. return str;
  346. };
  347. $scope.repoList = function () {
  348. return repoList($scope.repos);
  349. }
  350. $scope.editRepo = function (nodeCfg) {
  351. $scope.currentRepo = $.extend({selectedNodes: {}}, nodeCfg);
  352. $scope.currentRepo.Nodes.forEach(function (n) {
  353. $scope.currentRepo.selectedNodes[n.NodeID] = true;
  354. });
  355. $scope.editingExisting = true;
  356. $scope.repoEditor.$setPristine();
  357. $('#editRepo').modal({backdrop: 'static', keyboard: true});
  358. };
  359. $scope.addRepo = function () {
  360. $scope.currentRepo = {selectedNodes: {}};
  361. $scope.editingExisting = false;
  362. $scope.repoEditor.$setPristine();
  363. $('#editRepo').modal({backdrop: 'static', keyboard: true});
  364. };
  365. $scope.saveRepo = function () {
  366. var repoCfg, done, i;
  367. $scope.configInSync = false;
  368. $('#editRepo').modal('hide');
  369. repoCfg = $scope.currentRepo;
  370. repoCfg.Nodes = [];
  371. repoCfg.selectedNodes[$scope.myID] = true;
  372. for (var nodeID in repoCfg.selectedNodes) {
  373. if (repoCfg.selectedNodes[nodeID] === true) {
  374. repoCfg.Nodes.push({NodeID: nodeID});
  375. }
  376. }
  377. delete repoCfg.selectedNodes;
  378. $scope.repos[repoCfg.ID] = repoCfg;
  379. $scope.config.Repositories = repoList($scope.repos);
  380. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  381. };
  382. $scope.deleteRepo = function () {
  383. $('#editRepo').modal('hide');
  384. if (!$scope.editingExisting) {
  385. return;
  386. }
  387. delete $scope.repos[$scope.currentRepo.ID];
  388. $scope.config.Repositories = repoList($scope.repos);
  389. $scope.configInSync = false;
  390. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  391. };
  392. $scope.init = function() {
  393. $http.get(urlbase + '/version').success(function (data) {
  394. $scope.version = data;
  395. });
  396. $http.get(urlbase + '/system').success(function (data) {
  397. $scope.system = data;
  398. $scope.myID = data.myID;
  399. });
  400. $http.get(urlbase + '/config').success(function (data) {
  401. $scope.config = data;
  402. $scope.config.Options.ListenStr = $scope.config.Options.ListenAddress.join(', ');
  403. $scope.nodes = $scope.config.Nodes;
  404. $scope.nodes.sort(nodeCompare);
  405. $scope.repos = repoMap($scope.config.Repositories);
  406. $scope.refresh();
  407. });
  408. $http.get(urlbase + '/config/sync').success(function (data) {
  409. $scope.configInSync = data.configInSync;
  410. });
  411. };
  412. $scope.init();
  413. setInterval($scope.refresh, 10000);
  414. });
  415. function nodeCompare(a, b) {
  416. if (typeof a.Name !== 'undefined' && typeof b.Name !== 'undefined') {
  417. if (a.Name < b.Name)
  418. return -1;
  419. return a.Name > b.Name;
  420. }
  421. if (a.NodeID < b.NodeID) {
  422. return -1;
  423. }
  424. return a.NodeID > b.NodeID;
  425. }
  426. function repoCompare(a, b) {
  427. if (a.Directory < b.Directory) {
  428. return -1;
  429. }
  430. return a.Directory > b.Directory;
  431. }
  432. function repoMap(l) {
  433. var m = {};
  434. l.forEach(function (r) {
  435. m[r.ID] = r;
  436. });
  437. return m;
  438. }
  439. function repoList(m) {
  440. var l = [];
  441. for (var id in m) {
  442. l.push(m[id])
  443. }
  444. l.sort(repoCompare);
  445. return l;
  446. }
  447. function decimals(val, num) {
  448. var digits, decs;
  449. if (val === 0) {
  450. return 0;
  451. }
  452. digits = Math.floor(Math.log(Math.abs(val)) / Math.log(10));
  453. decs = Math.max(0, num - digits);
  454. return decs;
  455. }
  456. syncthing.filter('natural', function () {
  457. return function (input, valid) {
  458. return input.toFixed(decimals(input, valid));
  459. };
  460. });
  461. syncthing.filter('binary', function () {
  462. return function (input) {
  463. if (input === undefined) {
  464. return '0 ';
  465. }
  466. if (input > 1024 * 1024 * 1024) {
  467. input /= 1024 * 1024 * 1024;
  468. return input.toFixed(decimals(input, 2)) + ' Gi';
  469. }
  470. if (input > 1024 * 1024) {
  471. input /= 1024 * 1024;
  472. return input.toFixed(decimals(input, 2)) + ' Mi';
  473. }
  474. if (input > 1024) {
  475. input /= 1024;
  476. return input.toFixed(decimals(input, 2)) + ' Ki';
  477. }
  478. return Math.round(input) + ' ';
  479. };
  480. });
  481. syncthing.filter('metric', function () {
  482. return function (input) {
  483. if (input === undefined) {
  484. return '0 ';
  485. }
  486. if (input > 1000 * 1000 * 1000) {
  487. input /= 1000 * 1000 * 1000;
  488. return input.toFixed(decimals(input, 2)) + ' G';
  489. }
  490. if (input > 1000 * 1000) {
  491. input /= 1000 * 1000;
  492. return input.toFixed(decimals(input, 2)) + ' M';
  493. }
  494. if (input > 1000) {
  495. input /= 1000;
  496. return input.toFixed(decimals(input, 2)) + ' k';
  497. }
  498. return Math.round(input) + ' ';
  499. };
  500. });
  501. syncthing.filter('short', function () {
  502. return function (input) {
  503. return input.substr(0, 6);
  504. };
  505. });
  506. syncthing.filter('alwaysNumber', function () {
  507. return function (input) {
  508. if (input === undefined) {
  509. return 0;
  510. }
  511. return input;
  512. };
  513. });
  514. syncthing.filter('chunkID', function () {
  515. return function (input) {
  516. if (input === undefined)
  517. return "";
  518. var parts = input.match(/.{1,6}/g);
  519. if (!parts)
  520. return "";
  521. return parts.join('-');
  522. }
  523. });
  524. syncthing.filter('shortPath', function () {
  525. return function (input) {
  526. if (input === undefined)
  527. return "";
  528. var parts = input.split(/[\/\\]/);
  529. if (!parts || parts.length <= 3) {
  530. return input;
  531. }
  532. return ".../" + parts.slice(parts.length-2).join("/");
  533. }
  534. });
  535. syncthing.directive('optionEditor', function () {
  536. return {
  537. restrict: 'C',
  538. replace: true,
  539. transclude: true,
  540. scope: {
  541. setting: '=setting',
  542. },
  543. template: '<input type="text" ng-model="config.Options[setting.id]"></input>',
  544. };
  545. });
  546. syncthing.directive('uniqueRepo', function() {
  547. return {
  548. require: 'ngModel',
  549. link: function(scope, elm, attrs, ctrl) {
  550. ctrl.$parsers.unshift(function(viewValue) {
  551. if (scope.editingExisting) {
  552. // we shouldn't validate
  553. ctrl.$setValidity('uniqueRepo', true);
  554. } else if (scope.repos[viewValue]) {
  555. // the repo exists already
  556. ctrl.$setValidity('uniqueRepo', false);
  557. } else {
  558. // the repo is unique
  559. ctrl.$setValidity('uniqueRepo', true);
  560. }
  561. return viewValue;
  562. });
  563. }
  564. };
  565. });