Skip to content

Commit 7ef2999

Browse files
Finished first version
1 parent 7fbbe0c commit 7ef2999

File tree

6 files changed

+1400
-0
lines changed

6 files changed

+1400
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
program Ipify4Pascal_Demo_Delphi;
2+
3+
uses
4+
System.StartUpCopy,
5+
FMX.Forms,
6+
UnitMain in 'UnitMain.pas' {Form1},
7+
LibIpify4Pascal in '..\..\src\LibIpify4Pascal.pas';
8+
9+
{$R *.res}
10+
11+
begin
12+
Application.Initialize;
13+
Application.CreateForm(TForm1, Form1);
14+
Application.Run;
15+
end.

demos/Delphi/Ipify4Pascal_Demo_Delphi.dproj

Lines changed: 1079 additions & 0 deletions
Large diffs are not rendered by default.
110 KB
Binary file not shown.

demos/Delphi/UnitMain.fmx

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
object Form1: TForm1
2+
Left = 0
3+
Top = 0
4+
Caption = 'Ipify4Pascal - Demo - Delphi'
5+
ClientHeight = 480
6+
ClientWidth = 340
7+
FormFactor.Width = 320
8+
FormFactor.Height = 480
9+
FormFactor.Devices = [Desktop]
10+
DesignerMasterStyle = 0
11+
object BtnGetIPv4: TButton
12+
StyledSettings = [Style, FontColor]
13+
Position.X = 70.000000000000000000
14+
Position.Y = 17.000000000000000000
15+
Size.Width = 200.000000000000000000
16+
Size.Height = 30.000000000000000000
17+
Size.PlatformDefault = False
18+
TabOrder = 1
19+
Text = 'Get my IPv4'
20+
TextSettings.Font.Family = 'Roboto'
21+
TextSettings.Font.Size = 14.000000000000000000
22+
OnClick = BtnGetIPv4Click
23+
end
24+
object BtnGetIPv6: TButton
25+
StyledSettings = [Style, FontColor]
26+
Position.X = 70.000000000000000000
27+
Position.Y = 120.000000000000000000
28+
Size.Width = 200.000000000000000000
29+
Size.Height = 30.000000000000000000
30+
Size.PlatformDefault = False
31+
TabOrder = 3
32+
Text = 'Get my IPv6'
33+
TextSettings.Font.Family = 'Roboto'
34+
TextSettings.Font.Size = 14.000000000000000000
35+
OnClick = BtnGetIPv6Click
36+
end
37+
object BtnGeIpifyVersion: TButton
38+
StyledSettings = [Style, FontColor]
39+
Position.X = 70.000000000000000000
40+
Position.Y = 233.000000000000000000
41+
Size.Width = 200.000000000000000000
42+
Size.Height = 30.000000000000000000
43+
Size.PlatformDefault = False
44+
TabOrder = 2
45+
Text = 'Get Ipify4Pascal'#39's version'
46+
TextSettings.Font.Family = 'Roboto'
47+
TextSettings.Font.Size = 14.000000000000000000
48+
OnClick = BtnGeIpifyVersionClick
49+
end
50+
object lbIPv4: TLabel
51+
StyledSettings = [Style, FontColor]
52+
Position.X = 16.000000000000000000
53+
Position.Y = 71.000000000000000000
54+
Size.Width = 316.000000000000000000
55+
Size.Height = 17.000000000000000000
56+
Size.PlatformDefault = False
57+
TextSettings.Font.Family = 'Roboto'
58+
TextSettings.Font.Size = 14.000000000000000000
59+
TextSettings.HorzAlign = Center
60+
TabOrder = 6
61+
end
62+
object lbIPv6: TLabel
63+
StyledSettings = [Style, FontColor]
64+
Position.X = 16.000000000000000000
65+
Position.Y = 183.000000000000000000
66+
Size.Width = 316.000000000000000000
67+
Size.Height = 17.000000000000000000
68+
Size.PlatformDefault = False
69+
TextSettings.Font.Family = 'Roboto'
70+
TextSettings.Font.Size = 14.000000000000000000
71+
TextSettings.HorzAlign = Center
72+
TabOrder = 5
73+
end
74+
object lbIpifyVersion: TLabel
75+
StyledSettings = [Style, FontColor]
76+
Position.X = 16.000000000000000000
77+
Position.Y = 295.000000000000000000
78+
Size.Width = 316.000000000000000000
79+
Size.Height = 17.000000000000000000
80+
Size.PlatformDefault = False
81+
TextSettings.Font.Family = 'Roboto'
82+
TextSettings.Font.Size = 14.000000000000000000
83+
TextSettings.HorzAlign = Center
84+
TabOrder = 4
85+
end
86+
end

demos/Delphi/UnitMain.pas

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
unit UnitMain;
2+
3+
interface
4+
5+
uses
6+
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
7+
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
8+
FMX.Controls.Presentation, FMX.StdCtrls;
9+
10+
type
11+
TForm1 = class(TForm)
12+
BtnGetIPv4: TButton;
13+
BtnGetIPv6: TButton;
14+
BtnGeIpifyVersion: TButton;
15+
lbIPv4: TLabel;
16+
lbIPv6: TLabel;
17+
lbIpifyVersion: TLabel;
18+
procedure BtnGetIPv4Click(Sender: TObject);
19+
procedure BtnGeIpifyVersionClick(Sender: TObject);
20+
procedure BtnGetIPv6Click(Sender: TObject);
21+
private
22+
{ Private declarations }
23+
public
24+
{ Public declarations }
25+
end;
26+
27+
var
28+
Form1: TForm1;
29+
30+
implementation
31+
32+
{$R *.fmx}
33+
34+
uses LibIpify4Pascal;
35+
36+
procedure TForm1.BtnGeIpifyVersionClick(Sender: TObject);
37+
begin
38+
lbIpifyVersion.Text := Ipify4Pascal.Version;
39+
end;
40+
41+
procedure TForm1.BtnGetIPv4Click(Sender: TObject);
42+
begin
43+
lbIPv4.Text := Ipify4Pascal.GetIPv4;
44+
end;
45+
46+
procedure TForm1.BtnGetIPv6Click(Sender: TObject);
47+
begin
48+
lbIPv6.Text := Ipify4Pascal.GetIPv6;
49+
end;
50+
51+
end.

src/LibIpify4Pascal.pas

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
unit LibIpify4Pascal;
2+
3+
{$IFDEF FPC}
4+
{$mode delphi}
5+
{$ENDIF}
6+
7+
interface
8+
9+
uses
10+
{$IFDEF FPC}
11+
idHTTP,
12+
IdSSLOpenSSL,
13+
IdSSLOpenSSLHeaders;
14+
{$else}
15+
Net.HttpClient;
16+
{$endif}
17+
18+
type
19+
//Enum to tell which kind of IP to get
20+
TIPKind = (IPv4, IPv6);
21+
22+
//Interface of the library
23+
iIpify4Pascal = interface
24+
['{A9FAD1F5-9060-4B47-93E8-0AF8F7D50365}']
25+
function GetIPv4: string;
26+
function GetIPv6: string;
27+
function Version: string;
28+
end;
29+
30+
//Concrete class to implement the interface
31+
TIpify4Pascal = class(TInterfacedObject, iIpify4Pascal)
32+
//Version of the lib
33+
const _Version = '1.0.0';
34+
35+
//IP4 URL
36+
const GetIPv4URL = 'https://api.ipify.org';
37+
38+
//IP6 URL
39+
const GetIPv6URL = 'https://api6.ipify.org';
40+
private
41+
{$IFDEF FPC}
42+
HttpClient : TIdHTTP;
43+
FIdSSLIOHandlerSocketOpenSSL : TIdSSLIOHandlerSocketOpenSSL;
44+
{$else}
45+
HttpClient : THttpClient;
46+
{$endif}
47+
48+
//Function to get the ip
49+
function GetIP(IPKind: TIPKind): string;
50+
public
51+
constructor Create;
52+
destructor Destroy; override;
53+
54+
class function New: iIpify4Pascal;
55+
56+
function GetIPv4: string;
57+
function GetIPv6: string;
58+
function Version: string;
59+
end;
60+
61+
62+
function Ipify4Pascal: iIpify4Pascal;
63+
64+
implementation
65+
66+
uses
67+
SysUtils, Classes, IdURI;
68+
69+
{ TIpify4Pascal }
70+
71+
/// <summary>
72+
/// Starting point of the library
73+
/// </summary>
74+
/// <returns>
75+
/// The Ipify4Pascal's interface
76+
/// </returns>
77+
function Ipify4Pascal: iIpify4Pascal;
78+
begin
79+
Result := TIpify4Pascal.New;
80+
end;
81+
82+
constructor TIpify4Pascal.Create;
83+
begin
84+
{$IFDEF FPC}
85+
HttpClient := TIdHTTP.Create(nil);
86+
HttpClient.Request.Connection := 'Keep-Alive';
87+
HttpClient.Request.UserAgent := 'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36';
88+
HttpClient.HandleRedirects := true;
89+
FIdSSLIOHandlerSocketOpenSSL := TIdSSLIOHandlerSocketOpenSSL.Create;
90+
HttpClient.IOHandler := FIdSSLIOHandlerSocketOpenSSL;
91+
FIdSSLIOHandlerSocketOpenSSL.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
92+
HttpClient.HTTPOptions:= [hoKeepOrigProtocol];
93+
{$else}
94+
HttpClient := THTTPClient.Create;
95+
{$endif}
96+
end;
97+
98+
destructor TIpify4Pascal.Destroy;
99+
begin
100+
FreeAndNil(HttpClient);
101+
{$IFDEF FPC}
102+
FreeAndNil(FIdSSLIOHandlerSocketOpenSSL);
103+
{$endif}
104+
inherited;
105+
end;
106+
107+
function TIpify4Pascal.GetIP(IPKind: TIPKind): string;
108+
var
109+
FStreamResult : TStringStream;
110+
URL : string;
111+
begin
112+
case IPKind of
113+
IPv4: URL := GetIPv4URL;
114+
IPv6: URL := GetIPv6URL;
115+
end;
116+
try
117+
FStreamResult := TStringStream.Create;
118+
{$IFDEF FPC}
119+
HttpClient.Get(tidURI.URLEncode(GetIPv4URL), FStreamResult);
120+
{$else}
121+
HttpClient.Get(URL, FStreamResult);
122+
{$endif}
123+
Result := FStreamResult.DataString;
124+
finally
125+
{$IFDEF FPC}
126+
FreeAndNil(FStreamResult);
127+
{$endif}
128+
end;
129+
end;
130+
131+
/// <summary>
132+
/// Get the IPv4
133+
/// </summary>
134+
/// <returns>
135+
/// string containing IPv4
136+
/// </returns>
137+
function TIpify4Pascal.GetIPv4: string;
138+
begin
139+
Result := GetIP(IPv4);
140+
end;
141+
142+
/// <summary>
143+
/// Get the IPv6
144+
/// </summary>
145+
/// <returns>
146+
/// string containing IPv6
147+
/// </returns>
148+
function TIpify4Pascal.GetIPv6: string;
149+
begin
150+
Result := GetIP(IPv6);
151+
end;
152+
153+
class function TIpify4Pascal.New: iIpify4Pascal;
154+
begin
155+
Result := self.Create;
156+
end;
157+
158+
/// <summary>
159+
/// Get the Ipify4Pascal's version
160+
/// </summary>
161+
/// <returns>
162+
/// string containing the Ipify4Pascal's version
163+
/// </returns>
164+
function TIpify4Pascal.Version: string;
165+
begin
166+
Result := 'Ipify4Pascal version ' + _Version;
167+
end;
168+
169+
end.

0 commit comments

Comments
 (0)