Browse Source

Adding node_ip_stats JSON-RPC call

This will return collected statistical info about all ip connected nodes. Stored at UNetProtection
PascalCoin 6 years ago
parent
commit
20c663d743
1 changed files with 45 additions and 1 deletions
  1. 45 1
      src/core/URPC.pas

+ 45 - 1
src/core/URPC.pas

@@ -24,7 +24,8 @@ interface
 
 Uses UThread, ULog, UConst, UNode, UAccounts, UCrypto, UBlockChain,
   UNetProtocol, UOpTransaction, UWallet, UTime, UAES, UECIES, UTxMultiOperation,
-  UJSONFunctions, classes, blcksock, synsock, IniFiles, Variants, math, UBaseTypes, UOpenSSL;
+  UJSONFunctions, classes, blcksock, synsock, IniFiles, Variants, math, UBaseTypes, UOpenSSL,
+  UNetProtection;
 
 Const
   CT_RPC_ErrNum_InternalError = 100;
@@ -864,6 +865,41 @@ function TRPCProcess.ProcessMethod(const method: String; params: TPCJSONObject;
     end;
   end;
 
+  Procedure Get_node_ip_stats;
+  var aip : String;
+    i : Integer;
+    json, newJson : TPCJSONObject;
+    ipInfo : TIpInfo;
+  begin
+    aip := Trim(params.AsString('ip',''));
+    if aip<>'' then begin
+      json := TNetData.NetData.IpInfos.Lock(aip,False);
+      Try
+        newJson := TPCJSONObject.Create;
+        newJson.GetAsVariant('ip').Value := aip;
+        newJson.GetAsObject('values').Assign(json);
+        GetResultArray.Insert(GetResultArray.Count,newJson);
+      Finally
+        TNetData.NetData.IpInfos.Unlock;
+      End;
+    end else begin
+      for i :=0 to TNetData.NetData.IpInfos.Count-1 do begin
+        ipInfo := TNetData.NetData.IpInfos.Lock(i);
+        Try
+          newJson := TPCJSONObject.Create;
+          newJson.GetAsVariant('ip').Value := ipInfo.ip;
+          newJson.GetAsObject('values').Assign(ipInfo.json);
+          GetResultArray.Insert(GetResultArray.Count,newJson);
+        Finally
+          TNetData.NetData.IpInfos.Unlock;
+        End;
+      end;
+    end;
+    if params.AsBoolean('clear',False) then begin
+      TNetData.NetData.IpInfos.Clear;
+    end;
+  end;
+
   // This function creates a TOpTransaction without looking for balance/private key of sender account
   // It assumes that sender,target,sender_last_n_operation,senderAccountKey and targetAccountKey are correct
   Function CreateOperationTransaction(current_protocol : Word; sender, target, sender_last_n_operation : Cardinal; amount, fee : UInt64;
@@ -3675,6 +3711,14 @@ begin
     end;
     jsonresponse.GetAsVariant('result').Value := TNetData.NetData.NodeServersAddresses.CleanBlackList(True);
     Result := True;
+  end else if (method='node_ip_stats') then begin
+    if (Not _RPCServer.AllowUsePrivateKeys) then begin
+      // Protection when server is locked to avoid private keys call
+      ErrorNum := CT_RPC_ErrNum_NotAllowedCall;
+      Exit;
+    end;
+    Get_node_ip_stats;
+    Result := True;
   end else begin
     ErrorNum := CT_RPC_ErrNum_MethodNotFound;
     ErrorDesc := 'Method not found: "'+method+'"';