InterfaceController_admin.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "admin/Admin.h"
  16. #include "benc/String.h"
  17. #include "benc/Dict.h"
  18. #include "benc/List.h"
  19. #include "benc/Int.h"
  20. #include "crypto/AddressCalc.h"
  21. #include "crypto/Key.h"
  22. #include "net/InterfaceController.h"
  23. #include "net/InterfaceController_admin.h"
  24. #include "util/AddrTools.h"
  25. struct Context
  26. {
  27. struct Allocator* alloc;
  28. struct InterfaceController* ic;
  29. struct Admin* admin;
  30. Identity
  31. };
  32. // typical peer record is around 140 benc chars, so can't have very many in 1023
  33. #define ENTRIES_PER_PAGE 6
  34. static void adminPeerStats(Dict* args, void* vcontext, String* txid, struct Allocator* alloc)
  35. {
  36. struct Context* context = Identity_check((struct Context*)vcontext);
  37. struct InterfaceController_PeerStats* stats = NULL;
  38. int64_t* page = Dict_getIntC(args, "page");
  39. int i = (page) ? *page * ENTRIES_PER_PAGE : 0;
  40. int count = InterfaceController_getPeerStats(context->ic, alloc, &stats);
  41. List* list = List_new(alloc);
  42. for (int counter=0; i < count && counter++ < ENTRIES_PER_PAGE; i++) {
  43. Dict* d = Dict_new(alloc);
  44. Dict_putIntC(d, "bytesIn", stats[i].bytesIn, alloc);
  45. Dict_putIntC(d, "bytesOut", stats[i].bytesOut, alloc);
  46. Dict_putIntC(d, "recvKbps", stats[i].recvKbps, alloc);
  47. Dict_putIntC(d, "sendKbps", stats[i].sendKbps, alloc);
  48. Dict_putStringC(d, "addr", Address_toString(&stats[i].addr, alloc), alloc);
  49. String* stateString = String_new(InterfaceController_stateString(stats[i].state), alloc);
  50. Dict_putStringC(d, "state", stateString, alloc);
  51. Dict_putIntC(d, "last", stats[i].timeOfLastMessage, alloc);
  52. Dict_putIntC(d, "isIncoming", stats[i].isIncomingConnection, alloc);
  53. Dict_putIntC(d, "duplicates", stats[i].duplicates, alloc);
  54. Dict_putIntC(d, "lostPackets", stats[i].lostPackets, alloc);
  55. Dict_putIntC(d, "receivedOutOfRange", stats[i].receivedOutOfRange, alloc);
  56. if (stats[i].user) {
  57. Dict_putStringC(d, "user", stats[i].user, alloc);
  58. }
  59. List_addDict(list, d, alloc);
  60. }
  61. Dict* resp = Dict_new(alloc);
  62. Dict_putListC(resp, "peers", list, alloc);
  63. Dict_putIntC(resp, "total", count, alloc);
  64. if (i < count) {
  65. Dict_putIntC(resp, "more", 1, alloc);
  66. }
  67. Admin_sendMessage(resp, txid, context->admin);
  68. }
  69. static void adminDisconnectPeer(Dict* args,
  70. void* vcontext,
  71. String* txid,
  72. struct Allocator* requestAlloc)
  73. {
  74. struct Context* context = Identity_check((struct Context*)vcontext);
  75. String* pubkeyString = Dict_getStringC(args, "pubkey");
  76. // parse the key
  77. uint8_t pubkey[32];
  78. uint8_t addr[16];
  79. int error = Key_parse(pubkeyString, pubkey, addr);
  80. char* errorMsg = NULL;
  81. if (error) {
  82. errorMsg = "bad key";
  83. } else {
  84. // try to remove the peer if the key is valid
  85. error = InterfaceController_disconnectPeer(context->ic,pubkey);
  86. if (error) {
  87. errorMsg = "no peer found for that key";
  88. }
  89. }
  90. Dict* response = Dict_new(requestAlloc);
  91. Dict_putIntC(response, "success", error ? 0 : 1, requestAlloc);
  92. if (error) {
  93. Dict_putStringCC(response, "error", errorMsg, requestAlloc);
  94. }
  95. Admin_sendMessage(response, txid, context->admin);
  96. }
  97. static void adminResetPeering(Dict* args,
  98. void* vcontext,
  99. String* txid,
  100. struct Allocator* requestAlloc)
  101. {
  102. struct Context* context = Identity_check((struct Context*)vcontext);
  103. String* pubkeyString = Dict_getStringC(args, "pubkey");
  104. int error = 0;
  105. char* errorMsg = NULL;
  106. if (pubkeyString) {
  107. // parse the key
  108. uint8_t pubkey[32];
  109. uint8_t addr[16];
  110. error = Key_parse(pubkeyString, pubkey, addr);
  111. if (error) {
  112. errorMsg = "bad key";
  113. } else {
  114. InterfaceController_resetPeering(context->ic, pubkey);
  115. }
  116. } else {
  117. // reset all
  118. InterfaceController_resetPeering(context->ic, NULL);
  119. }
  120. Dict* response = Dict_new(requestAlloc);
  121. Dict_putIntC(response, "success", error ? 0 : 1, requestAlloc);
  122. if (error) {
  123. Dict_putStringCC(response, "error", errorMsg, requestAlloc);
  124. }
  125. Admin_sendMessage(response, txid, context->admin);
  126. }
  127. /*
  128. static resetSession(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  129. {
  130. struct Context* context = Identity_check((struct Context*)vcontext);
  131. String* pubkeyString = Dict_getStringC(args, "pubkey");
  132. // parse the key
  133. uint8_t pubkey[32];
  134. uint8_t addr[16];
  135. int error = Key_parse(pubkeyString, pubkey, addr);
  136. char* errorMsg = NULL;
  137. if (error) {
  138. errorMsg = "bad key";
  139. } else {
  140. // try to remove the peer if the key is valid
  141. error = InterfaceController_disconnectPeer(context->ic,pubkey);
  142. if (error) {
  143. errorMsg = "no peer found for that key";
  144. }
  145. }
  146. Dict* response = Dict_new(requestAlloc);
  147. Dict_putIntC(response, "success", error ? 0 : 1, requestAlloc);
  148. if (error) {
  149. Dict_putStringCC(response, "error", errorMsg, requestAlloc);
  150. }
  151. Admin_sendMessage(response, txid, context->admin);
  152. }*/
  153. void InterfaceController_admin_register(struct InterfaceController* ic,
  154. struct Admin* admin,
  155. struct Allocator* allocator)
  156. {
  157. struct Allocator* alloc = Allocator_child(allocator);
  158. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  159. .alloc = alloc,
  160. .ic = ic,
  161. .admin = admin
  162. }));
  163. Identity_set(ctx);
  164. Admin_registerFunction("InterfaceController_peerStats", adminPeerStats, ctx, false,
  165. ((struct Admin_FunctionArg[]) {
  166. { .name = "page", .required = 0, .type = "Int" }
  167. }), admin);
  168. Admin_registerFunction("InterfaceController_resetPeering", adminResetPeering, ctx, true,
  169. ((struct Admin_FunctionArg[]) {
  170. { .name = "pubkey", .required = 0, .type = "String" }
  171. }), admin);
  172. Admin_registerFunction("InterfaceController_disconnectPeer", adminDisconnectPeer, ctx, true,
  173. ((struct Admin_FunctionArg[]) {
  174. { .name = "pubkey", .required = 1, .type = "String" }
  175. }), admin);
  176. }