0

I am trying to use a 3rd party API to filter proxy traffic and Virtual machine and different file for each, I have use the following code ( https://pastebin.com/hWZX1Sxq ) JasonParser File (https://pastebin.com/hNspZxVY) but the function does not work also there is no error message while compiling.

function ProxyDetected(ip:string):Boolean;
var
  WinHttpReq: Variant;
  jsonText : String;
  JsonParser: TJsonParser;
  jBool : TJsonWord;
  I:integer;
begin    
    try 
      WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
      WinHttpReq.Open('GET', 'https://api.ipregistry.co/'+ip+'?key=roygrcxz372rjb&fields=security&pretty=true', False);
      WinHttpReq.Send;
      jsonText := Trim(WinHttpReq.ResponseText);
      Log(jsonText);
      ParseJson(JsonParser, jsonText);
      if GetArrayLength(JsonParser.Output.Errors) > 0 then
      begin
        Log('Error parsing JSON');
        for I := 0 to GetArrayLength(JsonParser.Output.Errors) - 1 do
        begin
          Log(JsonParser.Output.Errors[I]);
        end;
      end
      else
      begin
        //if FindJsonWord(JsonParser.Output, GetJsonRoot(JsonParser.Output), 'proxy', jBool) then
        if FindJsonWord(JsonParser.Output, GetJsonRoot(JsonParser.Output), 'is_cloud_provider', jBool) then
        begin
          if jBool = JWTrue then
            //Log('proxy:true')
            Log('is_cloud_provider:true')
          else
            //Log('proxy:false');
            Log('is_cloud_provider:false')
        end;
      end;
 
      ClearJsonParser(JsonParser);
 
 
      Result := (Trim(WinHttpReq.ResponseText) = '1');
    except
      Log(GetExceptionMessage);
      Result := false;
    end;
end;
Tuk Tuk
  • 57
  • 5
  • The *"function does not work"* does not work as a problem description. + What does your log say? – Martin Prikryl Jan 22 '21 at 15:10
  • The function is being called but its does not seems to be executed and there is no error on the log – Tuk Tuk Jan 22 '21 at 17:03
  • But you have your own logging in place. What does it show? – Martin Prikryl Jan 22 '21 at 17:29
  • @MartinPrikryl At the IpRegistry its showing that the API call is successful but on the code the response doesn't seems to be anything – Tuk Tuk Jan 22 '21 at 17:36
  • Your pastebin links fail with *"Forbidden (#403) Error, this is a private paste or is pending moderation. If this paste belongs to you, please login to Pastebin to view it."* + When I use your code with original `JsonParser.pas` and other functions from my https://stackoverflow.com/q/34290115/850848#34291316 and my IP address, the `Log(jsonText);` logs a complete JSON response. – Martin Prikryl Jan 23 '21 at 11:30
  • @MartinPrikryl I have fixed the Pastebin issue also the original code is used only for proxy but even the regkeyexist function in my code is not working I don't know what exactly am doing wrong – Tuk Tuk Jan 23 '21 at 15:42
  • Please focus on one problem at a time. You didn't even really respond to my main point in the comment that contradict your claim that *"on the code the response doesn't seems to be anything"* – I get the response form the server without any problem. We need [mcve]. No links to external files. If I understand you correctly, you claim that `WinHttpReq.ResponseText` is empty. If that's the case, you do not need any JSON to demonstrate the problem. – Martin Prikryl Jan 23 '21 at 15:55
  • @MartinPrikryl I get response from the server but the response doesn't trigger the expected function when I check for the proxy or not example if its a proxy i wanted to download a different file I have that part on the full code which is in the pastebin link – Tuk Tuk Jan 23 '21 at 16:05
  • So once again, **what does your logging show**? You wrote that nothing, what is apparently not true. – Martin Prikryl Jan 23 '21 at 16:16
  • @MartinPrikryl This is what I have in my log https://pastebin.com/418YyYVK – Tuk Tuk Jan 23 '21 at 16:47
  • Yes, obviously, as you never descend to the `security` object. I've already wrote you the same at your previous question. – Martin Prikryl Jan 23 '21 at 16:59
  • @MartinPrikryl I have modified the code like this https://pastebin.com/6ZxRckZq but i am getting out of range error at this point (if SecurityObject[I].Value.Kind <> JVKWord then) reason to modified the code is because i have other functions in the code which is using the jasonparser file which i have uploaded and this is the log https://pastebin.com/Zz05HVsN – Tuk Tuk Jan 23 '21 at 17:23
  • Come on: `if not FindJsonObject(JsonParser.Output, JsonRoot, 'security', SecurityObject) then` – Martin Prikryl Jan 23 '21 at 17:29
  • @MartinPrikryl Thank you for helping with this I give up Because I tried to modify the code you have mentioned in the other posts but when I run the code its showing me out of range on this line Log(Format('"%s" is not "word", ignoring', [SecurityObject[I].Key])); I honestly have no idea how this works but I have tried its seems this is not me, thank you for you time – Tuk Tuk Jan 23 '21 at 17:58
  • There's no `SecurityObject[I].Key` in your code. This question shows how to read a value from JSON object https://stackoverflow.com/q/34290115/850848 – Exactly what you want. Just there it is `Info`/`User` (`Number` type) and you want `security`/`is_cloud_provider` (`Word` type) (or `is_proxy`, no idea why your function is called `ProxyDetected`, but you check `is_cloud_provider`). – Martin Prikryl Jan 23 '21 at 20:17

0 Answers0