1
0

InterfaceController_admin.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 <http://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_getInt(args, String_CONST("page"));
  39. int i = (page) ? *page * ENTRIES_PER_PAGE : 0;
  40. int count = InterfaceController_getPeerStats(context->ic, alloc, &stats);
  41. String* bytesIn = String_CONST("bytesIn");
  42. String* bytesOut = String_CONST("bytesOut");
  43. String* pubKey = String_CONST("publicKey");
  44. String* addr = String_CONST("addr");
  45. String* state = String_CONST("state");
  46. String* last = String_CONST("last");
  47. String* switchLabel = String_CONST("switchLabel");
  48. String* isIncoming = String_CONST("isIncoming");
  49. String* user = String_CONST("user");
  50. String* version = String_CONST("version");
  51. String* recvKbps = String_CONST("recvKbps");
  52. String* sendKbps = String_CONST("sendKbps");
  53. String* duplicates = String_CONST("duplicates");
  54. String* lostPackets = String_CONST("lostPackets");
  55. String* receivedOutOfRange = String_CONST("receivedOutOfRange");
  56. List* list = List_new(alloc);
  57. for (int counter=0; i < count && counter++ < ENTRIES_PER_PAGE; i++) {
  58. Dict* d = Dict_new(alloc);
  59. Dict_putInt(d, bytesIn, stats[i].bytesIn, alloc);
  60. Dict_putInt(d, bytesOut, stats[i].bytesOut, alloc);
  61. Dict_putInt(d, recvKbps, stats[i].recvKbps, alloc);
  62. Dict_putInt(d, sendKbps, stats[i].sendKbps, alloc);
  63. Dict_putString(d, addr, Address_toString(&stats[i].addr, alloc), alloc);
  64. Dict_putString(d, pubKey, Key_stringify(stats[i].addr.key, alloc), alloc);
  65. String* stateString = String_new(InterfaceController_stateString(stats[i].state), alloc);
  66. Dict_putString(d, state, stateString, alloc);
  67. Dict_putInt(d, last, stats[i].timeOfLastMessage, alloc);
  68. uint8_t labelStack[20];
  69. AddrTools_printPath(labelStack, stats[i].addr.path);
  70. Dict_putString(d, switchLabel, String_new((char*)labelStack, alloc), alloc);
  71. Dict_putInt(d, isIncoming, stats[i].isIncomingConnection, alloc);
  72. Dict_putInt(d, duplicates, stats[i].duplicates, alloc);
  73. Dict_putInt(d, lostPackets, stats[i].lostPackets, alloc);
  74. Dict_putInt(d, receivedOutOfRange, stats[i].receivedOutOfRange, alloc);
  75. if (stats[i].user) {
  76. Dict_putString(d, user, stats[i].user, alloc);
  77. }
  78. uint8_t address[16];
  79. AddressCalc_addressForPublicKey(address, stats[i].addr.key);
  80. Dict_putInt(d, version, stats[i].addr.protocolVersion, alloc);
  81. List_addDict(list, d, alloc);
  82. }
  83. Dict* resp = Dict_new(alloc);
  84. Dict_putList(resp, String_CONST("peers"), list, alloc);
  85. Dict_putInt(resp, String_CONST("total"), count, alloc);
  86. if (i < count) {
  87. Dict_putInt(resp, String_CONST("more"), 1, alloc);
  88. }
  89. Dict_putString(resp, String_CONST("deprecation"),
  90. String_CONST("publicKey,switchLabel,version will soon be removed"), alloc);
  91. Admin_sendMessage(resp, txid, context->admin);
  92. }
  93. static void adminDisconnectPeer(Dict* args,
  94. void* vcontext,
  95. String* txid,
  96. struct Allocator* requestAlloc)
  97. {
  98. struct Context* context = Identity_check((struct Context*)vcontext);
  99. String* pubkeyString = Dict_getString(args, String_CONST("pubkey"));
  100. // parse the key
  101. uint8_t pubkey[32];
  102. uint8_t addr[16];
  103. int error = Key_parse(pubkeyString, pubkey, addr);
  104. char* errorMsg = NULL;
  105. if (error) {
  106. errorMsg = "bad key";
  107. } else {
  108. // try to remove the peer if the key is valid
  109. error = InterfaceController_disconnectPeer(context->ic,pubkey);
  110. if (error) {
  111. errorMsg = "no peer found for that key";
  112. }
  113. }
  114. Dict* response = Dict_new(requestAlloc);
  115. Dict_putInt(response, String_CONST("success"), error ? 0 : 1, requestAlloc);
  116. if (error) {
  117. Dict_putString(response, String_CONST("error"), String_CONST(errorMsg), requestAlloc);
  118. }
  119. Admin_sendMessage(response, txid, context->admin);
  120. }
  121. /*
  122. static resetSession(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  123. {
  124. struct Context* context = Identity_check((struct Context*)vcontext);
  125. String* pubkeyString = Dict_getString(args, String_CONST("pubkey"));
  126. // parse the key
  127. uint8_t pubkey[32];
  128. uint8_t addr[16];
  129. int error = Key_parse(pubkeyString, pubkey, addr);
  130. char* errorMsg = NULL;
  131. if (error) {
  132. errorMsg = "bad key";
  133. } else {
  134. // try to remove the peer if the key is valid
  135. error = InterfaceController_disconnectPeer(context->ic,pubkey);
  136. if (error) {
  137. errorMsg = "no peer found for that key";
  138. }
  139. }
  140. Dict* response = Dict_new(requestAlloc);
  141. Dict_putInt(response, String_CONST("success"), error ? 0 : 1, requestAlloc);
  142. if (error) {
  143. Dict_putString(response, String_CONST("error"), String_CONST(errorMsg), requestAlloc);
  144. }
  145. Admin_sendMessage(response, txid, context->admin);
  146. }*/
  147. void InterfaceController_admin_register(struct InterfaceController* ic,
  148. struct Admin* admin,
  149. struct Allocator* allocator)
  150. {
  151. struct Allocator* alloc = Allocator_child(allocator);
  152. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  153. .alloc = alloc,
  154. .ic = ic,
  155. .admin = admin
  156. }));
  157. Identity_set(ctx);
  158. Admin_registerFunction("InterfaceController_peerStats", adminPeerStats, ctx, false,
  159. ((struct Admin_FunctionArg[]) {
  160. { .name = "page", .required = 0, .type = "Int" }
  161. }), admin);
  162. Admin_registerFunction("InterfaceController_disconnectPeer", adminDisconnectPeer, ctx, true,
  163. ((struct Admin_FunctionArg[]) {
  164. { .name = "pubkey", .required = 1, .type = "String" }
  165. }), admin);
  166. }