Check an IP address against an online list in Inno Setup

与世无争的帅哥 提交于 2021-02-19 08:07:14

问题


I am getting the user IP address using the below code

function GetIp: string;
var
  WinHttpReq: Variant;
begin
    try
      WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
      WinHttpReq.Open('GET', 'http://ipinfo.io/ip', False);
      WinHttpReq.Send;
      Result := Trim(WinHttpReq.ResponseText);
    except
      Log(GetExceptionMessage);
      Result := '8.8.8.8';
    end;
end;

After getting the users IP address I need to check if that IP address already exists in my online JSON list.

Thanks


回答1:


The simplest solution is to download your JSON text file and search your IP address.

Reuse your code to retrieve a document using HTTP (or better HTTPS):

function HttpGet(Url: string): string;
var
  WinHttpReq: Variant;
begin
  WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
  WinHttpReq.Open('GET', Url, False);
  WinHttpReq.Send;
  Result := Trim(WinHttpReq.ResponseText);
end;

And then you can use it like:

var
  Ip: string;
  List: string;
begin
  try
    Ip := HttpGet('https://ipinfo.io/ip');
    List := HttpGet('https://www.example.com/publicly/available/list.json');

    if Pos('["' + Ip + '"]', List) > 0 then
    begin
      Log(Format('IP %s is in the list', [Ip]));
    end
      else
    begin
      Log(Format('IP %s is not in the list', [Ip]));
    end;
  except
    Log(Format('Error testing if IP is in the list - %s', [GetExceptionMessage]));
  end;
end;

Though you will have to make your list publicly available. Currently your URL cannot be accessed, without logging to Google first.


If you want to process your JSON properly, see
How to parse a JSON string in Inno Setup?



来源:https://stackoverflow.com/questions/62330068/check-an-ip-address-against-an-online-list-in-inno-setup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!