Skip to content

Commit c38f1de

Browse files
committed
[network] added easy dnsresolver
1 parent 70a4fa5 commit c38f1de

File tree

3 files changed

+139
-53
lines changed

3 files changed

+139
-53
lines changed

Quick.Console.pas

+1-1
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ function RunConsoleCommand(const aCommand, aParameters : String; CallBack : TOut
876876
pBuffer[dRead] := #0;
877877
OemToCharA(pBuffer,dBuffer);
878878
if Assigned(CallBack) then CallBack(dBuffer);
879-
if Assigned(OutputLines) then OutputLines.Add(dBuffer);
879+
if Assigned(OutputLines) then OutputLines.Add(string(dBuffer));
880880
until (dRead < CReadBuffer);
881881
end;
882882
//Application.ProcessMessages;

Quick.Json.Serializer.pas

+55-51
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Author : Kike Pérez
88
Version : 1.12
99
Created : 21/05/2018
10-
Modified : 03/10/2021
10+
Modified : 27/12/2021
1111
1212
This file is part of QuickLib: https://github.com/exilon/QuickLib
1313
@@ -131,16 +131,18 @@ TRTTIJson = class
131131
fUseBase64Stream : Boolean;
132132
fUseNullStringsAsEmpty : Boolean;
133133
function GetValue(aAddr: Pointer; aType: TRTTIType): TValue; overload;
134+
{$IFDEF FPC}
134135
function GetValue(aAddr: Pointer; aTypeInfo: PTypeInfo): TValue; overload;
136+
{$ENDIF}
135137
function IsAllowedProperty(aObject : TObject; const aPropertyName : string) : Boolean;
136-
function GetPropertyValue(Instance : TObject; const PropertyName : string) : TValue;
138+
//function GetPropertyValue(Instance : TObject; const PropertyName : string) : TValue;
137139
function GetPropertyValueFromObject(Instance : TObject; const PropertyName : string) : TValue;
138140
{$IFNDEF FPC}
139141
function GetFieldValueFromRecord(const aValue : TValue; const FieldName : string) : TValue;
140142
{$ENDIF}
143+
{$IFDEF FPC}
141144
procedure SetPropertyValue(Instance : TObject; aPropInfo : PPropInfo; aValue : TValue); overload;
142145
procedure SetPropertyValue(Instance : TObject; const PropertyName : string; aValue : TValue); overload;
143-
{$IFDEF FPC}
144146
function FloatProperty(aObject : TObject; aPropInfo: PPropInfo): string;
145147
function GetPropType(aPropInfo: PPropInfo): PTypeInfo;
146148
procedure LoadSetProperty(aInstance : TObject; aPropInfo: PPropInfo; const aValue: string);
@@ -210,7 +212,7 @@ TJsonSerializer = class(TInterfacedObject,IJsonSerializer)
210212
property UseEnumNames : Boolean read fUseEnumNames write SetUseEnumNames;
211213
property UseJsonCaseSense : Boolean read fUseJsonCaseSense write SetUseJsonCaseSense;
212214
property UseBase64Stream : Boolean read fUseBase64Stream write SetUseBase64Stream;
213-
property UseNullStringsAsEmpty : Boolean read fUseNullStringsAsEmpty write fUseNullStringsAsEmpty;
215+
property UseNullStringsAsEmpty : Boolean read fUseNullStringsAsEmpty write SetUseNullStringsAsEmpty;
214216
function JsonToObject(aType : TClass; const aJson: string) : TObject; overload;
215217
function JsonToObject(aObject : TObject; const aJson: string) : TObject; overload;
216218
function JsonStreamToObject(aObject : TObject; aJsonStream : TStream) : TObject;
@@ -463,6 +465,7 @@ function TRTTIJson.DeserializeStream(aObject: TObject; const aJson: TJSONValue):
463465
finally
464466
stream.Free;
465467
end;
468+
Result := aObject;
466469
end;
467470

468471
constructor TRTTIJson.Create(aSerializeLevel : TSerializeLevel; aUseEnumNames : Boolean = True);
@@ -587,7 +590,7 @@ function TRTTIJson.DeserializeObject(aObject: TObject; const aJson: TJSONObject)
587590
if IsGenericList(propvalue.AsObject) then DeserializeList(propvalue.AsObject,'List',TJSONObject(aJson.GetValue(propertyname)))
588591
else Result := DeserializeProperty(Result,propertyname,rProp,aJson);
589592
end
590-
else if IsGenericXArray(propvalue{$IFNDEF NEXTGEN}.TypeInfo.Name{$ELSE}.TypeInfo.NameFld.ToString{$ENDIF}) then
593+
else if IsGenericXArray(string(propvalue{$IFNDEF NEXTGEN}.TypeInfo.Name{$ELSE}.TypeInfo.NameFld.ToString{$ENDIF})) then
591594
begin
592595
DeserializeXArray(Result,propvalue,rProp,propertyname,aJson);
593596
end
@@ -1089,49 +1092,49 @@ function TRTTIJson.GetJsonPairByName(aJson: TJSONObject; const aName: string): T
10891092
Result := nil;
10901093
end;
10911094

1092-
function TRTTIJson.GetPropertyValue(Instance : TObject; const PropertyName : string) : TValue;
1093-
var
1094-
pinfo : PPropInfo;
1095-
begin
1096-
Result := nil;
1097-
pinfo := GetPropInfo(Instance,PropertyName);
1098-
if pinfo = nil then raise EJsonSerializeError.CreateFmt('Property "%s" not found!',[PropertyName]);
1099-
case pinfo.PropType^.Kind of
1100-
tkInteger : Result := GetOrdProp(Instance,pinfo);
1101-
tkInt64 : Result := GetInt64Prop(Instance,PropertyName);
1102-
tkFloat : Result := GetFloatProp(Instance,PropertyName);
1103-
tkChar : Result := Char(GetOrdProp(Instance,PropertyName));
1104-
{$IFDEF FPC}
1105-
tkWString : Result := GetWideStrProp(Instance,PropertyName);
1106-
tkSString,
1107-
tkAString,
1108-
{$ELSE}
1109-
tkWString,
1110-
{$ENDIF}
1111-
tkLString : Result := GetStrProp(Instance,pinfo);
1112-
{$IFDEF FPC}
1113-
tkEnumeration :
1114-
begin
1115-
if fUseEnumNames then Result := GetEnumName(pinfo.PropType,GetOrdProp(Instance,PropertyName))
1116-
else Result := GetOrdProp(Instance,PropertyName);
1117-
end;
1118-
{$ELSE}
1119-
tkEnumeration :
1120-
begin
1121-
if fUseEnumNames then Result := GetEnumName(@pinfo.PropType,GetOrdProp(Instance,PropertyName))
1122-
else Result := GetOrdProp(Instance,PropertyName);
1123-
end;
1124-
{$ENDIF}
1125-
tkSet : Result := GetSetProp(Instance,pinfo,True);
1126-
{$IFNDEF FPC}
1127-
tkClass :
1128-
{$ELSE}
1129-
tkBool : Result := Boolean(GetOrdProp(Instance,pinfo));
1130-
tkObject :
1131-
{$ENDIF} Result := GetObjectProp(Instance,pinfo);
1132-
tkDynArray : Result := GetDynArrayProp(Instance,pinfo);
1133-
end;
1134-
end;
1095+
//function TRTTIJson.GetPropertyValue(Instance : TObject; const PropertyName : string) : TValue;
1096+
//var
1097+
// pinfo : PPropInfo;
1098+
//begin
1099+
// Result := nil;
1100+
// pinfo := GetPropInfo(Instance,PropertyName);
1101+
// if pinfo = nil then raise EJsonSerializeError.CreateFmt('Property "%s" not found!',[PropertyName]);
1102+
// case pinfo.PropType^.Kind of
1103+
// tkInteger : Result := GetOrdProp(Instance,pinfo);
1104+
// tkInt64 : Result := GetInt64Prop(Instance,PropertyName);
1105+
// tkFloat : Result := GetFloatProp(Instance,PropertyName);
1106+
// tkChar : Result := Char(GetOrdProp(Instance,PropertyName));
1107+
// {$IFDEF FPC}
1108+
// tkWString : Result := GetWideStrProp(Instance,PropertyName);
1109+
// tkSString,
1110+
// tkAString,
1111+
// {$ELSE}
1112+
// tkWString,
1113+
// {$ENDIF}
1114+
// tkLString : Result := GetStrProp(Instance,pinfo);
1115+
// {$IFDEF FPC}
1116+
// tkEnumeration :
1117+
// begin
1118+
// if fUseEnumNames then Result := GetEnumName(pinfo.PropType,GetOrdProp(Instance,PropertyName))
1119+
// else Result := GetOrdProp(Instance,PropertyName);
1120+
// end;
1121+
// {$ELSE}
1122+
// tkEnumeration :
1123+
// begin
1124+
// if fUseEnumNames then Result := GetEnumName(@pinfo.PropType,GetOrdProp(Instance,PropertyName))
1125+
// else Result := GetOrdProp(Instance,PropertyName);
1126+
// end;
1127+
// {$ENDIF}
1128+
// tkSet : Result := GetSetProp(Instance,pinfo,True);
1129+
// {$IFNDEF FPC}
1130+
// tkClass :
1131+
// {$ELSE}
1132+
// tkBool : Result := Boolean(GetOrdProp(Instance,pinfo));
1133+
// tkObject :
1134+
// {$ENDIF} Result := GetObjectProp(Instance,pinfo);
1135+
// tkDynArray : Result := GetDynArrayProp(Instance,pinfo);
1136+
// end;
1137+
//end;
11351138

11361139
function TRTTIJson.GetPropertyValueFromObject(Instance : TObject; const PropertyName : string) : TValue;
11371140
var
@@ -1156,6 +1159,7 @@ function TRTTIJson.GetFieldValueFromRecord(const aValue : TValue; const FieldNam
11561159
end;
11571160
{$ENDIF}
11581161

1162+
{$IFDEF FPC}
11591163
procedure TRTTIJson.SetPropertyValue(Instance : TObject; const PropertyName : string; aValue : TValue);
11601164
var
11611165
pinfo : PPropInfo;
@@ -1192,7 +1196,6 @@ procedure TRTTIJson.SetPropertyValue(Instance : TObject; aPropInfo : PPropInfo;
11921196
end;
11931197
end;
11941198

1195-
{$IFDEF FPC}
11961199
procedure TRTTIJson.LoadSetProperty(aInstance : TObject; aPropInfo: PPropInfo; const aValue: string);
11971200
type
11981201
TCardinalSet = set of 0..SizeOf(Cardinal) * 8 - 1;
@@ -1298,7 +1301,7 @@ function TRTTIJson.SerializeObject(aObject: TObject): TJSONObject;
12981301
// end
12991302
if propvalue.IsObject then jpair.JsonValue := SerializeObject(propvalue.AsObject)
13001303
{$IFNDEF FPC}
1301-
else if (not propvalue.IsObject) and (IsGenericXArray(propvalue{$IFNDEF NEXTGEN}.TypeInfo.Name{$ELSE}.TypeInfo.NameFld.ToString{$ENDIF})) then
1304+
else if (not propvalue.IsObject) and (IsGenericXArray(string(propvalue{$IFNDEF NEXTGEN}.TypeInfo.Name{$ELSE}.TypeInfo.NameFld.ToString{$ENDIF}))) then
13021305
begin
13031306
jpair.JsonValue := SerializeValue(GetFieldValueFromRecord(propvalue,'fArray'));
13041307
end
@@ -1336,10 +1339,12 @@ function TRTTIJson.GetValue(aAddr: Pointer; aType: TRTTIType): TValue;
13361339
TValue.Make(aAddr,aType.Handle,Result);
13371340
end;
13381341

1342+
{$IFDEF FPC}
13391343
function TRTTIJson.GetValue(aAddr: Pointer; aTypeInfo: PTypeInfo): TValue;
13401344
begin
13411345
TValue.Make(aAddr,aTypeInfo,Result);
13421346
end;
1347+
{$ENDIF}
13431348

13441349
function TRTTIJson.SerializeValue(const aValue : TValue) : TJSONValue;
13451350
begin
@@ -1453,7 +1458,6 @@ function TRTTIJson.SerializeValue(const aValue : TValue) : TJSONValue;
14531458
function TRTTIJson.SerializeStream(aObject: TObject): TJSONValue;
14541459
var
14551460
stream : TStream;
1456-
json : string;
14571461
begin
14581462
Result := nil;
14591463
try

Quick.Network.pas

+83-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,23 @@ interface
3636
uses
3737
Classes,
3838
SysUtils,
39-
Math;
39+
Math,
40+
IdDNSResolver;
41+
42+
type
43+
TDNSResolve = class
44+
private
45+
fHost : string;
46+
fPort : Integer;
47+
function ResolveDNS(const aDNS : string; aRegType: TQueryRecordTypes): string;
48+
public
49+
constructor Create(const aNameServer : string = '8.8.8.8'; aServerPort : Integer = 53);
50+
function ResolveA(const aDNS : string): string;
51+
function ResolveAAAA(const aDNS : string): string;
52+
function ResolveCNAME(const aDNS : string): string;
53+
function ResolveNS(const aDNS : string): string;
54+
function ResolveTXT(const aDNS : string): string;
55+
end;
4056

4157
function IntToIPv4(IPv4: LongWord): string;
4258
function IPv4ToInt(const IPv4: string) : LongWord;
@@ -130,4 +146,70 @@ procedure GetIPRange(const cIP, cMask : string; out LowIP, HighIP : string);
130146
end;
131147
end;
132148

149+
{ TDNSResolve }
150+
151+
constructor TDNSResolve.Create(const aNameServer : string = '8.8.8.8'; aServerPort : Integer = 53);
152+
begin
153+
fHost := aNameServer;
154+
fPort := aServerPort;
155+
end;
156+
157+
function TDNSResolve.ResolveDNS(const aDNS : string; aRegType: TQueryRecordTypes): string;
158+
var
159+
dnsresolver : TIdDNSResolver;
160+
begin
161+
Result := '';
162+
dnsresolver := TIdDNSResolver.Create(nil);
163+
try
164+
dnsresolver.Host := fHost;
165+
dnsresolver.Port := fPort;
166+
dnsresolver.QueryResult.Clear;
167+
dnsresolver.QueryType := [aRegType];
168+
dnsresolver.Resolve(aDNS);
169+
if dnsresolver.QueryResult.Count > 0 then
170+
begin
171+
if dnsresolver.QueryResult[0].RecType = aRegType then
172+
begin
173+
case aRegType of
174+
qtNS : Result := TNSRecord(dnsresolver.QueryResult.Items[0]).HostName;
175+
qtA : Result := TARecord(dnsresolver.QueryResult.Items[0]).IPAddress;
176+
qtAAAA : Result := TAAAARecord(dnsresolver.QueryResult.Items[0]).Address;
177+
qtNAME : Result := TCNRecord(dnsresolver.QueryResult.Items[0]).HostName;
178+
qtTXT : Result := TTextRecord(dnsresolver.QueryResult.Items[0]).Text.Text;
179+
else raise Exception.Create('Not implemented yet!');
180+
end;
181+
182+
end;
183+
end;
184+
finally
185+
dnsresolver.Free;
186+
end;
187+
end;
188+
189+
function TDNSResolve.ResolveNS(const aDNS : string): string;
190+
begin
191+
Result := ResolveDNS(aDNS,qtNS);
192+
end;
193+
194+
function TDNSResolve.ResolveA(const aDNS : string): string;
195+
begin
196+
Result := ResolveDNS(aDNS,qtA);
197+
end;
198+
199+
function TDNSResolve.ResolveAAAA(const aDNS : string): string;
200+
begin
201+
Result := ResolveDNS(aDNS,qtAAAA);
202+
end;
203+
204+
205+
function TDNSResolve.ResolveCNAME(const aDNS : string): string;
206+
begin
207+
Result := ResolveDNS(aDNS,qtName);
208+
end;
209+
210+
function TDNSResolve.ResolveTXT(const aDNS : string): string;
211+
begin
212+
Result := ResolveDNS(aDNS,qtTXT);
213+
end;
214+
133215
end.

0 commit comments

Comments
 (0)