1
0

admin.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * You may redistribute this program and/or modify it under the terms of
  3. * the GNU General Public License as published by the Free Software Foundation,
  4. * either version 3 of the License, or (at your option) any later version.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. // THIS SCRIPT IS ESSENTIALLY A BACKDOOR
  15. // DON'T RUN IT ON ANYTHING YOU CARE ABOUT
  16. // This script binds port 8084 and allows to use admin methods of cjdns
  17. var sys = require('util'),
  18. http = require('http'),
  19. fs = require('fs'),
  20. express = require('express'),
  21. path = require('path'),
  22. app = express(),
  23. CJDNS = require('./cjdns.js');
  24. var PATH = path.dirname(process.argv[1]),
  25. config = JSON.parse(fs.readFileSync(process.env['HOME'] + '/.cjdnsadmin')),
  26. cjdns = new CJDNS(config),
  27. gLogs = [];
  28. app.use(express.static(PATH + '/www'));
  29. app.use(express.bodyParser());
  30. app.post('/api', function (req, res, next) {
  31. if (req.body.lorem === 'ipsum') {
  32. //Kinda handshake
  33. res.send({hello: 'world!'});
  34. } else {
  35. next();
  36. }
  37. });
  38. app.post('/api/map', function (req, res, next) {
  39. var nodes = [];
  40. function getNodes (page) {
  41. cjdns.sendAuth({
  42. q: 'NodeStore_dumpTable',
  43. args: {
  44. page: page
  45. }
  46. }, function (err, data) {
  47. if (err) {
  48. res.send('502', 'Something happened!');
  49. return;
  50. }
  51. if (data.routingTable) {
  52. nodes = nodes.concat(data.routingTable);
  53. }
  54. if (data.more) {
  55. getNodes(page + 1);
  56. } else {
  57. checkNodes();
  58. }
  59. });
  60. }
  61. function checkNodes () {
  62. res.send({
  63. nodes: nodes,
  64. count: nodes.length
  65. });
  66. }
  67. getNodes(0);
  68. });
  69. app.post('/api/methods', function (req, res, next) {
  70. var methods = [];
  71. function getMethods (page) {
  72. cjdns.sendAuth({
  73. q: 'Admin_availableFunctions',
  74. args: {
  75. page: page
  76. }
  77. }, function (err, data) {
  78. var name;
  79. if (err) {
  80. res.send('502', 'Something happened!');
  81. return;
  82. }
  83. if (data.availableFunctions) {
  84. for (name in data.availableFunctions) {
  85. if (data.availableFunctions.hasOwnProperty(name)) {
  86. methods.push({
  87. name: name,
  88. params: data.availableFunctions[name]
  89. });
  90. }
  91. }
  92. }
  93. if (data.more) {
  94. getMethods(page + 1);
  95. } else {
  96. checkMethods();
  97. }
  98. });
  99. }
  100. function checkMethods () {
  101. res.send({
  102. methods: methods,
  103. count: methods.length
  104. });
  105. }
  106. getMethods(0);
  107. });
  108. app.post('/api/logs', function (req, res, next) {
  109. var ts = req.body.ts / 1000 || 0,
  110. logs = gLogs.filter(function (log) {
  111. if (log.time > ts) {
  112. return log;
  113. }
  114. }),
  115. response;
  116. logs.sort(function (a, b) {
  117. return a.time - b.time;
  118. });
  119. response = {
  120. logs: logs
  121. };
  122. if (logs.length > 0) {
  123. response.ts = logs[logs.length - 1].time * 1000;
  124. }
  125. res.send(response);
  126. });
  127. app.post('/api/cjdns', function (req, res, next) {
  128. if (req.body.q) {
  129. sys.log('Sending ' + sys.inspect(req.body));
  130. cjdns.sendAuth(req.body, function (err, msg) {
  131. if (err) {
  132. res.send('502', 'Something happened!');
  133. return;
  134. }
  135. res.send(msg);
  136. });
  137. } else {
  138. next();
  139. }
  140. });
  141. app.post('/api/config', function (req, res, next) {
  142. if (req.body.config) {
  143. cjdns.saveConfig(req.body.config, function (err, data) {
  144. if (err) {
  145. res.send('502', 'Something happened!');
  146. return;
  147. }
  148. res.send(data);
  149. });
  150. } else {
  151. next();
  152. }
  153. });
  154. app.post('/api/config', function (req, res, next) {
  155. //sys.log(sys.inspect(req.body));
  156. res.send(cjdns.config.interfaces);
  157. });
  158. app.get('/map', function (req, res) {
  159. res.redirect('/map.html');
  160. });
  161. app.get('/logs', function (req, res) {
  162. res.redirect('/logs.html');
  163. });
  164. app.get('/config', function (req, res) {
  165. res.redirect('/config.html');
  166. });
  167. app.get('*', function (req, res) {
  168. res.send(404, 'Not found!');
  169. });
  170. app.post('*', function (req, res) {
  171. res.send(404, {error: 'Not found!'});
  172. });
  173. cjdns.subscribe(function (err, resp) {
  174. if (resp.message) {
  175. gLogs.push(resp);
  176. }
  177. });
  178. app.listen(8084,'127.0.0.1',function(){
  179. console.log('\n****************\nCJDNS admin is started on http://localhost:8084/ \n****************\n ');
  180. });