Search code examples
jsoninno-setuppascalscript

Checking all values in JSON object in Inno Setup when parsing response from ipregistry


I am trying to prevent bot installs using ipregistry IP Geolocation and Threat Detection API. But I am getting results in the below format. Can you help me integrating this with Inno Setup to exit the installer, if any of the below is true?

API URL https://api.ipregistry.co/50.75.90.218?key=roygrcxz372rjb&fields=security

API result

{
    "security": {
        "is_bogon": false,
        "is_cloud_provider": false,
        "is_tor": false,
        "is_tor_exit": false,
        "is_proxy": false,
        "is_anonymous": false,
        "is_abuser": false,
        "is_attacker": false,
        "is_threat": false
    }
}

Thanks


Solution

  • You already know how to call the online API:
    Check an IP address against an online list in Inno Setup

    You basically already know even how to parse the JSON, but ok, this is bit specific. With use of JsonParser library and my functions from How to parse a JSON string in Inno Setup?, you can write:

    function AllowInstallation(Json: string): Boolean;
    var
      JsonParser: TJsonParser;
      JsonRoot, SecurityObject: TJsonObject;
      I: Integer;
    begin
      Result := True;
      Log('Parsing');
      if not ParseJsonAndLogErrors(JsonParser, Json) then
      begin
        Log('Cannot parse, allowing installation');
      end
        else
      begin
        Log('Parsed');
        JsonRoot := GetJsonRoot(JsonParser.Output);
        if not FindJsonObject(JsonParser.Output, JsonRoot, 'security', SecurityObject) then
        begin
          Log('Cannot find "security", allowing installation');
        end
          else
        begin
          for I := 0 to Length(SecurityObject) - 1 do
          begin
            if SecurityObject[I].Value.Kind <> JVKWord then
            begin
              Log(Format('"%s" is not "word", ignoring', [SecurityObject[I].Key]));
            end
              else
            if JsonParser.Output.Words[SecurityObject[I].Value.Index] = JWTrue then
            begin
              Log(Format('"%s" is "true", not allowing the installation', [
                SecurityObject[I].Key]));
              Result := False;
            end
              else
            begin
              Log(Format('"%s" is not "true"', [SecurityObject[I].Key]));
            end;
          end;
        end;
      end;
      ClearJsonParser(JsonParser);
    end;