-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIpRightServiceUnit.pas
More file actions
1393 lines (1258 loc) · 47.2 KB
/
IpRightServiceUnit.pas
File metadata and controls
1393 lines (1258 loc) · 47.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit IpRightServiceUnit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
ScktComp, ExtCtrls, Contnrs, XmlUnit;
type
c_Account = class(TObject)
private
m_strDisabled: string;
m_strServer: string;
m_strPort: string;
m_strUsername: string;
m_strPassword: string;
m_olHost: TObjectList;
// Task 51.
m_strMx: string;
// Task 51.
m_strMxPri: string;
public
constructor Create(
);
destructor Destroy(
); override;
procedure Load(
const p_xe: c_XmlElement
);
procedure Save(
const p_xe: c_XmlElement
);
end;
TIpRightService = class(TService)
procedure ServiceExecute(Sender: TService);
procedure ServiceShutdown(Sender: TService);
private
m_strLog: string; // master switch, if this is not '1', nothing logs.
m_strLogMethod: string; // entry into some methods.
m_strLogOption: string; // what's loaded and saved.
m_strLogCheck: string; // what's sent and received.
m_strLogUpdate: string; // what's sent and received.
m_strCheckIp: string; // master switch, if this is not '1', no checks are done.
m_strCheckIpTick: string;
m_strCheckIpInternet: string; // when '1' uses server, port, usename, password, and get to determine the IP address, otherwise gets the machine's IP addresss.
m_strCheckIpServer: string;
m_strCheckIpPort: string;
m_strCheckIpUsername: string;
m_strCheckIpPassword: string;
m_strCheckIpGet: string;
m_strCheckIpExcludeIpAddresses: string;
m_csCheckIpAddress: TClientSocket;
m_strCheckIpReply: string;
m_csUpdateIpAddress: TClientSocket;
m_strUpdateIpReply: string;
m_tmrCheckIpAddress: TTimer;
m_nTicker: integer;
m_strIpAddress: string;
m_acc: c_Account;
// m_olHost: TObjectList;
m_strHostNames: string;
m_ss: TServerSocket;
procedure Log(p_str: string);
procedure LogMethod(
const p_str: string
);
procedure LogOptions(
const p_xe: c_XmlElement
);
procedure Load();
procedure Save();
procedure EnableTimer(
);
procedure DisableTimer(
);
procedure m_csCheckIpAddressWrite(
p_objSender: TObject;
p_sck: TCustomWinSocket
);
procedure m_csCheckIpAddressRead(
p_objSender: TObject;
p_sck: TCustomWinSocket
);
procedure m_csCheckIpAddressDisconnect(
p_objSender: TObject;
p_sck: TCustomWinSocket
);
procedure m_csCheckIpAddressError(
p_objSender: TObject;
p_cws: TCustomWinSocket;
p_ee: TErrorEvent;
var p_nErrorCode: integer
);
procedure m_csUpdateIpAddressWrite(
p_objSender: TObject;
p_sck: TCustomWinSocket
);
procedure m_csUpdateIpAddressRead(
p_objSender: TObject;
p_sck: TCustomWinSocket
);
procedure m_csUpdateIpAddressDisconnect(
p_objSender: TObject;
p_sck: TCustomWinSocket
);
procedure m_csUpdateIpAddressError(
p_objSender: TObject;
p_cws: TCustomWinSocket;
p_ee: TErrorEvent;
var p_nErrorCode: integer
);
procedure m_ssClientError(
p_objSender: TObject;
p_cws: TCustomWinSocket;
p_ee: TErrorEvent;
var p_nErrorCode: integer
);
procedure m_ssClientRead(
p_objSender: TObject;
p_cws: TCustomWinSocket
);
procedure m_tmrInterval(
p_objSender: TObject
);
function GetIpAddresses(
const p_str: string
): string;
procedure CheckIpAddress(
);
procedure UpdateIpAddress(
);
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;
c_Host = class(TObject)
private
m_strDisabled: string;
m_strName: string;
m_strUpdated: string; // when host was last successfully updated.
m_strIpAddress: string; // last ip address successfully updated.
m_strResultCode: string; // last reply status
public
procedure Load(
const p_xe: c_XmlElement
);
procedure Save(
const p_xe: c_XmlElement
);
end;
var
IpRightService: TIpRightService;
implementation
{$R *.DFM}
uses
FileCtrl,
UtilityUnit;
const
LOG_FILENAME = 'IpRight.log';
XML_FILENAME = 'IpRight.xml';
CLIENT_VERSION = '0.9.8';
CLIENT_COMPANY = 'Independent Rapid Development Limited';
CLIENT_EMAIL = 'stacey.richards@ird.co.nz';
CONFIG_PORT = 54321;
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
IpRightService.Controller(CtrlCode);
end;
procedure TIpRightService.CheckIpAddress(
);
begin
LogMethod('Checking IP address.');
if ('1' <> m_strCheckIp) then begin
Log('Checking is disabled.');
exit;
end;
DisableTimer();
if ('1' <> m_strCheckIpInternet) then begin
m_strCheckIpReply := #$D#$A + UtilityUnit.GetIpAddresses();
Log(m_strCheckIpReply);
m_csCheckIpAddressDisconnect(nil, nil);
exit;
end;
FreeAndNil(m_csCheckIpAddress);
m_strCheckIpReply := '';
m_csCheckIpAddress := TClientSocket.Create(nil);
m_csCheckIpAddress.Host := m_strCheckIpServer;
m_csCheckIpAddress.Port := StrToIntDef(m_strCheckIpPort, 80);
m_csCheckIpAddress.OnWrite := m_csCheckIpAddressWrite;
m_csCheckIpAddress.OnRead := m_csCheckIpAddressRead;
m_csCheckIpAddress.OnDisconnect := m_csCheckIpAddressDisconnect;
m_csCheckIpAddress.OnError := m_csCheckIpAddressError;
m_csCheckIpAddress.Open();
end;
procedure TIpRightService.DisableTimer(
);
begin
m_tmrCheckIpAddress.Enabled := false;
end;
procedure TIpRightService.EnableTimer(
);
const
DNS_PARK_DOMAIN = 'dnspark.com';
begin
m_nTicker := StrToIntDef(m_strCheckIpTick, 600);
if ((0 <> pos(DNS_PARK_DOMAIN, lowercase(m_strCheckIpServer))) and (
m_nTicker < 600)) then begin
m_nTicker := 600;
end;
m_tmrCheckIpAddress.Enabled := true;
end;
function TIpRightService.GetIpAddresses(
const p_str: string
): string;
var
l_b: boolean;
l_i: integer;
l_j: integer;
l_an: array[0..3] of integer;
begin
result := '';
l_b := false;
l_i := 0;
l_j := 0;
while (l_i < length(p_str)) do begin
inc(l_i);
if (p_str[l_i] in ['0'..'9']) then begin
if (not l_b) then begin
l_b := true;
l_j := 0;
ZeroMemory(@l_an, sizeof(l_an));
end;
l_an[l_j] := l_an[l_j] * 10 + byte(p_str[l_i]) - ord('0');
if (l_an[l_j] > 255) then begin
l_b := false;
end;
end else if ('.' = p_str[l_i]) then begin
if (l_b) then begin
inc(l_j);
if (l_j > 3) then begin
l_b := false;
end;
end;
end else begin
if (l_b) then begin
if (3 = l_j) then begin
result := result + IntToStr(l_an[0]) + '.' + IntToStr(l_an[1]) +
'.' + IntToStr(l_an[2]) + '.' + IntToStr(l_an[3]) + #$D#$A;
end;
l_b := false;
end;
end;
end;
if (l_b) then begin
if (3 = l_j) then begin
result := result + IntToStr(l_an[0]) + '.' + IntToStr(l_an[1]) + '.' +
IntToStr(l_an[2]) + '.' + IntToStr(l_an[3]) + #$D#$A;
end;
end;
end;
function TIpRightService.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TIpRightService.Load(
);
var
filename: string;
filestream: TFileStream;
l_xeRoot: c_XmlElement;
l_i: integer;
l_xe: c_XmlElement;
begin
filename := ExtractFilePath(ParamStr(0)) + XML_FILENAME;
if (not FileExists(filename)) then begin
exit;
end;
l_xeRoot := c_XmlElement.Create('');
try
filestream := TFileStream.Create(filename, (fmOpenRead or fmShareDenyWrite));
try
l_xeRoot.LoadFromStream(filestream);
finally
FreeAndNil(filestream);
end;
for l_i := 0 to l_xeRoot.m_strlElement.Count - 1 do begin
l_xe := c_XMLElement(l_xeRoot.m_strlElement.Objects[l_i]);
if ('Log' = l_xe.m_strName) then begin
m_strLog := l_xe.GetAttribute('Enabled');
m_strLogMethod := l_xe.GetAttribute('Methods');
m_strLogOption := l_xe.GetAttribute('Options');
m_strLogCheck := l_xe.GetAttribute('Checks');
m_strLogUpdate := l_xe.GetAttribute('Updates');
end else if ('Check' = l_xe.m_strName) then begin
m_strCheckIp := l_xe.GetAttribute('Enabled');
m_strCheckIpTick := l_xe.GetAttribute('Tick');
m_strCheckIpInternet := l_xe.GetAttribute('Internet');
m_strCheckIpServer := l_xe.GetAttribute('Server');
m_strCheckIpPort := l_xe.GetAttribute('Port');
m_strCheckIpUsername := l_xe.GetAttribute('Username');
m_strCheckIpPassword := l_xe.GetAttribute('Password');
m_strCheckIpGet := l_xe.GetAttribute('Get');
m_strCheckIpExcludeIpAddresses := l_xe.GetAttribute('ExcludeIpAddresses');
end else if ('Account' = l_xe.m_strName) then begin
m_acc.Load(l_xe);
end;
end;
// Usually a log like this would go at the top of the method. This is a
// little different though because the log option needs to be loaded before
// the attempt to log occurs.
LogMethod('Loading.');
LogOptions(l_xeRoot);
finally
FreeAndNil(l_xeRoot);
end;
end;
procedure TIpRightService.Log(p_str: string);
var
filename: string;
filestream: TFileStream;
begin
filename := ExtractFilePath(ParamStr(0)) + LOG_FILENAME;
if ('1' <> m_strLog) then begin
exit;
end;
// Make sure there is a log file that we can log to.
if (not FileExists(filename)) then begin
try
filestream := TFileStream.Create(filename, (fmCreate or fmShareDenyWrite));
finally
FreeAndNil(filestream);
end;
end;
filestream := TFileStream.Create(filename, fmOpenReadWrite or fmShareDenyWrite);
try
filestream.Seek(0, soFromEnd);
filestream.Position := filestream.Size;
p_str := FormatDateTime('yyyy-mm-dd hh:nn:ss', Now()) + ' ' + p_str + #$D#$A;
filestream.WriteBuffer(pchar(p_str)^, length(p_str));
finally
FreeAndNil(filestream);
end;
end;
procedure TIpRightService.m_csCheckIpAddressDisconnect(
p_objSender: TObject;
p_sck: TCustomWinSocket
);
var
l_sl: TStringList;
l_str: string;
l_strHostNames: string;
l_i: integer;
l_hst: c_Host;
l_slExcludeIpAddress: TStringList;
begin
LogMethod('Check IP address socket disconnected.');
l_sl := TStringList.Create();
try
l_sl.Text := m_strCheckIpReply;
// remove header.
while (l_sl.Count > 0) and ('' <> l_sl.Strings[0]) do begin
l_sl.Delete(0);
end;
// remove the blank line between the header and the body.
if (l_sl.Count > 0) then begin
l_sl.Delete(0);
end;
// now we can get ip addresses from the body of the reply.
l_sl.Text := GetIpAddresses(l_sl.Text);
l_sl.Sorted := true;
// exclude any known not right ip addresses.
l_slExcludeIpAddress := TStringList.Create();
try
l_slExcludeIpAddress.CommaText := m_strCheckIpExcludeIpAddresses;
for l_i := l_sl.Count - 1 downto 0 do begin
if (- 1 <> l_slExcludeIpAddress.IndexOf(l_sl.Strings[l_i])) then begin
l_sl.Delete(0);
end;
end;
finally
FreeAndNil(l_slExcludeIpAddress);
end;
if (0 = l_sl.Count) then begin
Log('Got no IP addresses.');
EnableTimer();
exit;
end;
if (l_sl.Count > 1) then begin
Log('Got too many IP addresses:'#$D#$A + l_sl.Text);
EnableTimer();
exit;
end;
l_str := l_sl.Strings[0];
finally
FreeAndNil(l_sl);
end;
Log('Got IP address "' + l_str + '".');
l_strHostNames := '';
for l_i := 0 to m_acc.m_olHost.Count - 1 do begin
l_hst := m_acc.m_olHost.Items[l_i] as c_Host;
if (('1' <> l_hst.m_strDisabled) and (l_str <>
l_hst.m_strIpAddress)) then begin
if ('' <> l_strHostNames) then begin
l_strHostNames := l_strHostNames + ',';
end;
l_strHostNames := l_strHostNames + l_hst.m_strName;
end;
end;
if ('' <> l_strHostNames) then begin
m_strIpAddress := l_str;
m_strHostNames := l_strHostNames;
Log('Out of date hosts: ' + m_strHostNames);
UpdateIpAddress();
end else begin
EnableTimer();
end;
end;
procedure TIpRightService.m_csCheckIpAddressRead(
p_objSender: TObject;
p_sck: TCustomWinSocket
);
var
l_str: string;
begin
LogMethod('Reading from check IP address socket.');
l_str := p_sck.ReceiveText();
if ('1' = m_strLogCheck) then begin
Log('Reading: ' + l_str);
end;
m_strCheckIpReply := m_strCheckIpReply + l_str
end;
procedure TIpRightService.m_csCheckIpAddressWrite(
p_objSender: TObject;
p_sck: TCustomWinSocket
);
var
l_str: string;
l_strUsernamePassword: string;
l_strGet: string;
begin
LogMethod('Writing to check IP address socket.');
if (('' <> m_strCheckIpUsername) or ('' <> m_strCheckIpPassword)) then begin
l_strUsernamePassword := StringToBase64(m_strCheckIpUsername + ':' +
m_strCheckIpPassword);
end else begin
l_strUsernamePassword := '';
end;
// Make sure there is at least a "/" in our get.
l_strGet := m_strCheckIpGet;
if ('' = l_strGet) then begin
l_strGet := '/';
end;
l_str := 'GET ' + l_strGet + ' HTTP/1.0'#$D#$A'Host: ' +
m_strCheckIpServer + #$D#$A;
if ('' <> l_strUsernamePassword) then begin
l_str := l_str + 'Authorization: Basic ' + l_strUsernamePassword + #$D#$A;
end;
if ('1' = m_strLogCheck) then begin
Log('Writing: ' + l_str);
end;
p_sck.SendText(l_str + #$D#$A);
end;
procedure TIpRightService.m_csUpdateIpAddressDisconnect(
p_objSender: TObject;
p_sck: TCustomWinSocket
);
var
l_str: string;
l_sl: TStringList;
l_strResultCode: string;
l_hst: c_Host;
l_i: integer;
l_bSave: boolean;
begin
LogMethod('Update IP address socket disconnected.');
// see if we get an ok response for each of the host names.
l_sl := TStringList.Create();
try
l_sl.Text := m_strUpdateIpReply;
// remove header.
while (l_sl.Count > 0) and ('' <> l_sl.Strings[0]) do begin
l_str := l_sl.Strings[0];
while ('' <> l_str) do begin
if ('401' = Trim(ExtractWord(l_str))) then begin
Log('Unauthorized. Check update server, username, and password.');
Log('Disabling update.');
m_acc.m_strDisabled := '1';
Save();
EnableTimer();
exit;
end;
end;
l_sl.Delete(0);
end;
// remove the blank line between the header and the body.
if (l_sl.Count > 0) then begin
l_sl.Delete(0);
end;
l_bSave := false;
l_str := m_strIpAddress;
for l_i := 0 to m_acc.m_olHost.Count - 1 do begin
l_hst := m_acc.m_olHost.Items[l_i] as c_Host;
if (('1' <> l_hst.m_strDisabled) and (l_str <>
l_hst.m_strIpAddress) and (l_sl.Count > 0)) then begin
l_strResultCode := l_sl.Strings[0];
l_sl.Delete(0);
l_hst := m_acc.m_olHost.Items[l_i] as c_Host;
l_hst.m_strResultCode := l_strResultCode;
Log('Result status for ' + l_hst.m_strName + ': ' + l_strResultCode);
//Normal Result Codes
if ('ok' = l_strResultCode) then begin // The update was successful.
Log('The update was successful.');
// if ok save change.
Log('Update IP address for ' + l_hst.m_strName + ' changed from "' +
l_hst.m_strIpAddress + '" to "' + l_str + '".');
l_hst.m_strIpAddress := l_str;
l_hst.m_strUpdated := FormatDateTime('', Now());
end else if ('nochange' = l_strResultCode) then begin // No changes were made to the hostname(s). Continual updates with no changes will lead to blocked clients.
Log('No changes were made to the hostname(s). Continual updates with no changes will lead to blocked clients.');
// if nochange save change - ip must have been corrected externally.
Log('Update IP address for ' + l_hst.m_strName + ' changed from "' +
l_hst.m_strIpAddress + '" to "' + l_str + '".');
l_hst.m_strIpAddress := l_str;
l_hst.m_strUpdated := FormatDateTime('', Now());
//Input Error Result Codes
end else if ('nofqdn' = l_strResultCode) then begin // No valid FQDN (fully qualified domain name) was specified.
Log('No valid FQDN (fully qualified domain name) was specified.');
Log('Disabling host.');
l_hst.m_strDisabled := '1';
end else if ('nohost' = l_strResultCode) then begin // An invalid hostname was specified. This due to the fact the hostname has not been created in the system. Creating new host names via clients is not supported.
Log('An invalid hostname was specified. This due to the fact the hostname has not been created in the system. Creating new host names via clients is not supported.');
Log('Disabling host.');
l_hst.m_strDisabled := '1';
end else if ('abuse' = l_strResultCode) then begin // The hostname specified has been blocked for abuse.
Log('The hostname specified has been blocked for abuse.');
Log('Disabling host.');
l_hst.m_strDisabled := '1';
//System Error Result Codes
end else if ('unauth' = l_strResultCode) then begin // The username specified is not authorized to update this hostname and domain.
Log('The username specified is not authorized to update this hostname and domain.');
Log('Disabling host.');
l_hst.m_strDisabled := '1';
end else if ('blocked' = l_strResultCode) then begin // The dynamic update client (specified by the user-agent) has been blocked from the system.
Log('The dynamic update client (specified by the user-agent) has been blocked from the system.');
Log('Disabling host.');
l_hst.m_strDisabled := '1';
end else if ('notdyn' = l_strResultCode) then begin // The hostname specified has not been marked as a dynamic host. Hosts must be marked as dynamic in the system in order to be updated via clients. This prevents unwanted or accidental updates.
Log('The hostname specified has not been marked as a dynamic host. Hosts must be marked as dynamic in the system in order to be updated via clients. This prevents unwanted or accidental updates.');
Log('Disabling host.');
l_hst.m_strDisabled := '1';
end;
l_bSave := true;
end;
end;
if (l_bSave) then begin
Save();
end;
finally
FreeAndNil(l_sl);
end;
EnableTimer();
end;
procedure TIpRightService.m_csUpdateIpAddressRead(
p_objSender: TObject;
p_sck: TCustomWinSocket
);
var
l_str: string;
begin
LogMethod('Reading from update IP address socket.');
l_str := p_sck.ReceiveText();
if ('1' = m_strLogUpdate) then begin
Log('Reading: ' + l_str);
end;
m_strUpdateIpReply := m_strUpdateIpReply + l_str
end;
procedure TIpRightService.m_csUpdateIpAddressWrite(
p_objSender: TObject;
p_sck: TCustomWinSocket
);
var
l_str: string;
l_strUsernamePassword: string;
l_strMxMxPri: string;
begin
LogMethod('Writing to update IP address socket.');
l_strUsernamePassword := StringToBase64(m_acc.m_strUsername + ':' +
m_acc.m_strPassword);
// Task 51.
if ('1' = m_acc.m_strMx) then begin
l_strMxMxPri := '&mx=ON&mxpri=' + IntToStr(StrToIntDef(m_acc.m_strMxPri,
0));
end else begin
l_strMxMxPri := '';
end;
l_str := 'GET /visitors/update.html?hostname=' + m_strHostNames +
'&myip=' + m_strIpAddress + l_strMxMxPri + ' HTTP/1.0'#$D#$A'Host: ' +
m_acc.m_strServer + #$D#$A'Authorization: Basic ' +
l_strUsernamePassword + #$D#$A +
'User-Agent: IP Right/' + CLIENT_VERSION + ' ' + CLIENT_EMAIL + #$D#$A;
if ('1' = m_strLogUpdate) then begin
Log('Writing: ' + l_str);
end;
p_sck.SendText(l_str + #$D#$A);
end;
procedure TIpRightService.m_tmrInterval(
p_objSender: TObject
);
begin
if (0 = m_nTicker) then begin
CheckIpAddress();
end else begin
dec(m_nTicker);
end;
end;
procedure TIpRightService.Save();
var
filestream: TFileStream;
l_xeRoot: c_XMLElement;
l_xe: c_XmlElement;
begin
LogMethod('Saving.');
l_xeRoot := c_XMLElement.Create('IpRight');
try
l_xe := l_xeRoot.AddElement('Log');
l_xe.SetAttribute('Enabled', m_strLog);
l_xe.SetAttribute('Methods', m_strLogMethod);
l_xe.SetAttribute('Options', m_strLogOption);
l_xe.SetAttribute('Checks', m_strLogCheck);
l_xe.SetAttribute('Updates', m_strLogUpdate);
l_xe := l_xeRoot.AddElement('Check');
l_xe.SetAttribute('Enabled', m_strCheckIp);
l_xe.SetAttribute('Tick', m_strCheckIpTick);
l_xe.SetAttribute('Internet', m_strCheckIpInternet);
l_xe.SetAttribute('Server', m_strCheckIpServer);
l_xe.SetAttribute('Port', m_strCheckIpPort);
l_xe.SetAttribute('Username', m_strCheckIpUsername);
l_xe.SetAttribute('Password', m_strCheckIpPassword);
l_xe.SetAttribute('Get', m_strCheckIpGet);
l_xe.SetAttribute('ExcludeIpAddresses', m_strCheckIpExcludeIpAddresses);
m_acc.Save(l_xeRoot);
filestream := TFileStream.Create(ExtractFilePath(ParamStr(0)) + XML_FILENAME, (fmCreate or fmShareDenyWrite));
try
l_xeRoot.SaveToStream(filestream, '');
finally
FreeAndNil(filestream);
end;
LogOptions(l_xeRoot);
finally
FreeAndNil(l_xeRoot);
end;
end;
procedure TIpRightService.ServiceExecute(Sender: TService);
begin
m_acc := c_Account.Create();
Load();
m_tmrCheckIpAddress := TTimer.Create(nil);
m_tmrCheckIpAddress.Interval := 1000;
m_tmrCheckIpAddress.OnTimer := m_tmrInterval;
m_tmrCheckIpAddress.Enabled := true;
m_ss := TServerSocket.Create(nil);
m_ss.Port := CONFIG_PORT;
m_ss.OnClientError := m_ssClientError;
m_ss.OnClientRead := m_ssClientRead;
m_ss.Active := true;
ServiceThread.ProcessRequests(true);
end;
procedure TIpRightService.UpdateIpAddress(
);
begin
LogMethod('Updating IP address.');
if ('1' = m_acc.m_strDisabled) then begin
Log('Updating is disabled.');
EnableTimer();
exit;
end;
Log('Updating hosts: ' + m_strHostNames);
FreeAndNil(m_csUpdateIpAddress);
m_strUpdateIpReply := '';
m_csUpdateIpAddress := TClientSocket.Create(nil);
m_csUpdateIpAddress.Host := m_acc.m_strServer;
m_csUpdateIpAddress.Port := StrToIntDef(m_acc.m_strPort, 80);
m_csUpdateIpAddress.OnWrite := m_csUpdateIpAddressWrite;
m_csUpdateIpAddress.OnRead := m_csUpdateIpAddressRead;
m_csUpdateIpAddress.OnDisconnect := m_csUpdateIpAddressDisconnect;
m_csUpdateIpAddress.OnError := m_csUpdateIpAddressError;
m_csUpdateIpAddress.Open();
end;
procedure TIpRightService.ServiceShutdown(Sender: TService);
begin
FreeAndNil(m_ss);
FreeAndNil(m_acc);
end;
procedure c_Host.Load(
const p_xe: c_XmlElement
);
begin
m_strDisabled := p_xe.GetAttribute('Disabled');
m_strName := p_xe.GetAttribute('Name');
m_strIpAddress := p_xe.GetAttribute('IpAddress');
m_strUpdated := p_xe.GetAttribute('Updated');
m_strResultCode := p_xe.GetAttribute('ResultCode');
end;
procedure c_Host.Save(
const p_xe: c_XmlElement
);
var
l_xe: c_XmlElement;
begin
l_xe := p_xe.AddElement('Host');
l_xe.SetAttribute('Disabled', m_strDisabled);
l_xe.SetAttribute('Name', m_strName);
l_xe.SetAttribute('IpAddress', m_strIpAddress);
l_xe.SetAttribute('Updated', m_strUpdated);
l_xe.SetAttribute('ResultCode', m_strResultCode);
end;
procedure TIpRightService.LogMethod(
const p_str: string
);
begin
if ('1' = m_strLogMethod) then begin
Log(p_str);
end;
end;
procedure TIpRightService.LogOptions(
const p_xe: c_XmlElement
);
begin
if ('1' = m_strLogOption) then begin
Log(p_xe.AsString);
end;
end;
constructor c_Account.Create(
);
begin
m_olHost := TObjectList.Create();
end;
destructor c_Account.Destroy(
);
begin
FreeAndNil(m_olHost);
inherited;
end;
procedure c_Account.Load(
const p_xe: c_XmlElement
);
var
l_i: integer;
l_xe: c_XmlElement;
l_hst: c_Host;
begin
m_strDisabled := p_xe.GetAttribute('Disabled');
m_strServer := 'www.dnspark.com'; //p_xe.GetAttribute('Server');
m_strPort := '80'; //p_xe.GetAttribute('Port');
m_strUsername := p_xe.GetAttribute('Username');
m_strPassword := p_xe.GetAttribute('Password');
m_strMx := p_xe.GetAttribute('Mx');
m_strMxPri := p_xe.GetAttribute('MxPri');
for l_i := 0 to p_xe.m_strlElement.Count - 1 do begin
l_xe := p_xe.GetElement(l_i);
if ('Host' = l_xe.m_strName) then begin
l_hst := c_Host.Create();
try
l_hst.Load(l_xe);
m_olHost.Add(l_hst);
except
FreeAndNil(l_hst);
end;
end;
end;
end;
procedure c_Account.Save(
const p_xe: c_XmlElement
);
var
l_i: integer;
l_xe: c_XmlElement;
begin
l_xe := p_xe.AddElement('Account');
l_xe.SetAttribute('Disabled', m_strDisabled);
// l_xe.SetAttribute('Server', m_strServer);
// l_xe.SetAttribute('Port', m_strPort);
l_xe.SetAttribute('Username', m_strUsername);
l_xe.SetAttribute('Password', m_strPassword);
l_xe.SetAttribute('Mx', m_strMx);
l_xe.SetAttribute('MxPri', m_strMxPri);
for l_i := 0 to m_olHost.Count - 1 do begin
(m_olHost.Items[l_i] as c_Host).Save(l_xe);
end;
end;
const
ERROR: array[eeGeneral..eeAccept] of string = (
'The socket received an error message that does not fit into any of the following categories.',
'An error occurred when trying to write to the socket connection.',
'An error occurred when trying to read from the socket connection.',
'A connection request that was already accepted could not be completed.',
'An error occurred when trying to close a connection.',
'A problem occurred when trying to accept a client connection request.');
procedure TIpRightService.m_csCheckIpAddressError(
p_objSender: TObject;
p_cws: TCustomWinSocket;
p_ee: TErrorEvent;
var p_nErrorCode: integer
);
begin
LogMessage(ERROR[p_ee]);
Log(ERROR[p_ee]);
p_nErrorCode := 0;
EnableTimer();
end;
procedure TIpRightService.m_csUpdateIpAddressError(p_objSender: TObject;
p_cws: TCustomWinSocket; p_ee: TErrorEvent; var p_nErrorCode: integer);
begin
LogMessage(ERROR[p_ee]);
Log(ERROR[p_ee]);
p_nErrorCode := 0;
EnableTimer();
end;
procedure TIpRightService.m_ssClientError(
p_objSender: TObject;
p_cws: TCustomWinSocket;
p_ee: TErrorEvent;
var p_nErrorCode: integer
);
begin
LogMessage(ERROR[p_ee]);
Log(ERROR[p_ee]);
p_nErrorCode := 0;
end;
procedure TIpRightService.m_ssClientRead(
p_objSender: TObject;
p_cws: TCustomWinSocket
);
const
HOST = 'host_';
HOST_LENGTH = length(HOST);
GET = 'GET ';
GET_LENGTH = length(GET);
TAIL = #$D#$A#$D#$A;
TAIL_LENGTH = length(TAIL);
var
hosts: string;
response: string;
l_str: string;
l_i: integer;
l_hst: c_Host;
l_strName: string;
l_strValue: string;
l_n: integer;
l_strlHostEnabled: TStringList;
l_strlHostName: TStringList;
l_strlHostAddress: TStringList;
l_strlHostUpdated: TStringList;
l_strlHostResult: TStringList;
l_strGet: string;
function Checkbox(p_label: string; p_name: string; p_value: boolean): string;
var
checked: string;
begin
if (p_value) then begin
checked := 'checked="checked" ';
end else begin
checked := '';
end;
result := '<label for="' + p_name + '">' + p_label + '</label><input ' + checked + 'id="' + p_name + '" name="' + p_name + '" type="checkbox" />';
end;
function Text(p_label: string; p_name: string; p_value: string): string;
begin
result := '<label for="' + p_name + '">' + p_label + '</label><input id="' + p_name + '" name="' + p_name + '" type="text" value="' + p_value + '" />';
end;
function Password(p_label: string; p_name: string; p_value: string): string;
begin
result := '<label for="' + p_name + '">' + p_label + '</label><input id="' + p_name + '" name="' + p_name + '" type="password" value="' + p_value + '" />';
end;
begin
l_str := '';
repeat
l_str := l_str + p_cws.ReceiveText();
until (TAIL = copy(l_str, length(l_str) - (TAIL_LENGTH - 1), length(TAIL)));
// Expect "GET " then some path and filename then a "?" then our name/value
// pairs then a space.
if (GET = copy(l_str, 1, GET_LENGTH)) then begin