-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon.pas
163 lines (135 loc) · 3.44 KB
/
daemon.pas
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
unit Daemon;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, DaemonApp, EventLog,
Sniffer;
type
{ TNetMonitorThread }
TNetMonitorThread = Class(TThread)
private
FConfigFile : String;
FLog : TEventLog;
FSniffer : TSniffer;
public
constructor Create(AConfigFile: String; ALog: TEventLog; ATerminate: TNotifyEvent);
procedure Execute; override;
end;
{ TNetMonitorDaemon }
TNetMonitorDaemon = class(TDaemon)
procedure DataModuleCreate(Sender: TObject);
procedure DataModuleExecute(Sender: TCustomDaemon);
procedure DataModuleStart(Sender: TCustomDaemon; var OK: Boolean);
procedure DataModuleStop(Sender: TCustomDaemon; var OK: Boolean);
private
{ private declarations }
FConfigFile : String;
FThread : TNetMonitorThread;
FLog : TEventLog;
procedure ThreadStopped(Sender: TObject);
public
{ public declarations }
end;
procedure RegisterDaemon;
var
NetMonitorDaemon: TNetMonitorDaemon;
implementation
resourcestring
SErrNoConfigFile = 'No configuration file found';
// Include windows messages for eventlog component.
{$ifdef mswindows}
{$R fclel.res}
{$endif}
procedure RegisterDaemon;
begin
RegisterDaemonClass(TNetMonitorDaemon)
end;
{$R *.lfm}
{ TNetMonitorThread }
constructor TNetMonitorThread.Create(AConfigFile: String; ALog: TEventLog; ATerminate: TNotifyEvent);
begin
FConfigFile := AConfigFile;
FLog := ALog;
FreeOnTerminate := false;
OnTerminate := ATerminate;
inherited Create(false);
end;
procedure TNetMonitorThread.Execute;
begin
try
FLog.Info('Creating Sniffer');
FSniffer := TSniffer.Create(nil);
FLog.Info('Loading Sniffer config');
FSniffer.LoadFromConfig(FConfigFile);
FLog.Info('Setting up Sniffer');
FSniffer.Setup;
FLog.Info('Sniffer setup completed');
repeat
Sleep(1000 * 10);
FLog.Info(DateTimeToStr(Now()));
until Terminated;
finally
FSniffer.Free;
end;
end;
{ TNetMonitorDaemon }
procedure TNetMonitorDaemon.DataModuleCreate(Sender: TObject);
begin
FLog := Self.Logger;
FLog.Info('Service Create');
if Application.HasOption('c', 'config') then
begin
FConfigFile := Application.GetOptionValue('c', 'config');
end
else
begin
FConfigFile := GetAppConfigFile(false, false);
if not FileExistsUTF8(FConfigFile) then
begin
FConfigFile := GetAppConfigFile(true, false);
if not FileExistsUTF8(FConfigFile) then
FConfigFile := 'NetMonitor.cfg';
end;
end;
end;
procedure TNetMonitorDaemon.DataModuleExecute(Sender: TCustomDaemon);
begin
FLog.Info('Service Execute');
end;
procedure TNetMonitorDaemon.DataModuleStart(Sender: TCustomDaemon;
var OK: Boolean);
begin
FLog.Info('Service Start');
if (FConfigFile = '') then
FLog.Error(SErrNoConfigFile);
OK := (FThread = nil) and (FConfigFile <> '');
if OK then
FThread := TNetMonitorThread.Create(FConfigFile, FLog, @ThreadStopped);
end;
procedure TNetMonitorDaemon.DataModuleStop(Sender: TCustomDaemon;
var OK: Boolean);
var
I : integer;
begin
FLog.Info('Service Stop');
if Assigned(FThread) then
begin
FThread.Terminate;
I := 0;
while (FThread <> nil) and (I < 50) do
begin
Sleep(100);
ReportStatus;
end;
if (FThread <> nil) then
FThread.OnTerminate := nil;
end;
OK := FThread = nil;
end;
procedure TNetMonitorDaemon.ThreadStopped(Sender: TObject);
begin
FThread := nil;
end;
initialization
RegisterDaemon;
end.