forked from CyberShadow/MyWormNET
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Base.pas
272 lines (226 loc) · 8.01 KB
/
Base.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
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
unit Base;
{-------------------------------------------------------------------------------
| Base unit for logging and general settings.
| (C) CyberShadow - 2006
| (C) StepS - 2013-2014
|-------------------------------------------------------------------------------
| FEATURES:
|
| - Load and reload the server settings
| - Log into text files WNServer.log (verbose) and EventLog.log (important)
| - Some timestamp-related extensions: OS/Compiler differences acknowledged
-------------------------------------------------------------------------------}
{$IFDEF FPC}
{$mode DELPHI}
{$ENDIF}
interface
uses
IniFiles, DateUtils, Version, Lists;
const
APPVERSION = '1.4.0.0';
var
Config: TMemIniFile;
ServerHost: string; // our hostname
IRCPort, HTTPPort, WormNATPort: Word;
StartupTime, CreationTime: string;
IRCOperPassword: string;
IRCPassword: string;
StealthIP: string;
NetworkName: string;
AppLanguage: string;
MinimumVersion: TVersion;
VerboseLogging, AllowArbitrary, SeenService, HTTPtoConsole: Boolean;
ChatAntiFlood: Boolean;
AntiFloodFactor: Double=1.0;
MaxIRCUsers: Integer=0;
IRCConnections: Int64=0;
procedure Log(S: string; DiskOnly: Boolean=False; Important: Boolean=False);
procedure EventLog(S: string; DiskOnly: Boolean=False);
function WinSockErrorCodeStr(Code: Integer): string;
procedure LoadParams;
function TextDateTime(T: TDateTime; IsUTC: Boolean = false) : string;
function ExecutableTimestamp: TDateTime;
implementation
uses
{$IFDEF MSWINDOWS}
Windows,
{$ELSE}
errors,
{$ENDIF}
SysUtils, SyncObjs,
HTTPServer, IRCServer, WormNATServer, Localization, Data;
var
PCS, LCS, ECS: TCriticalSection;
procedure InitSettings;
begin
if Config = nil then
begin
ChDir(ExtractFilePath(ExpandFileName(ParamStr(0))));
StartupTime :=TextDateTime(Now);
CreationTime :=TextDateTime(ExecutableTimestamp, true);
EventLog('------------------ '+DateTimeToStr(Now)+' ------------------',true);
Config := TMemIniFile.Create('WNServer.ini');
AppLanguage :=Config.ReadString ('WormNet','Language', 'Default');
GetLocalizations(AppLanguage);
EventLog(Format(L_START, [APPVERSION]));
IRCPort :=Config.ReadInteger('WormNet','IRCPort', 6667);
HTTPPort :=Config.ReadInteger('WormNet','HTTPPort', 80);
WormNATPort :=Config.ReadInteger('WormNet','WormNATPort', 0);
end;
end;
procedure LoadParams;
begin
PCS.Enter;
AppLanguage :=Config.ReadString ('WormNet','Language', 'Default');
ServerHost :=Config.ReadString ('WormNet','ServerHost', 'localhost');
IRCOperPassword :=Config.ReadString ('WormNet','IRCOperPassword', 'Random');
IRCPassword :=Config.ReadString ('WormNet','IRCPassword', IRCDefPassword);
StealthIP :=Config.ReadString ('WormNet','StealthIP', 'no.address.for.you');
NetworkName :=Config.ReadString ('WormNet','NetworkName', 'MyWormNET');
MinimumVersion.Str :=Config.ReadString ('WormNet','MinimumVersion', '0.0');
SeenService :=Config.ReadBool ('WormNet','SeenService', false);
ChatAntiFlood :=Config.ReadBool ('WormNet','ChatAntiFlood', true);
AntiFloodFactor :=Config.ReadFloat ('WormNet','AntiFloodFactor', 1.0);
VerboseLogging :=Config.ReadBool ('Debug','VerboseConsoleLogging', false);
AllowArbitrary :=Config.ReadBool ('Debug','AllowArbitraryPages', false);
ServerHost:=StringSection(ServerHost, 0);
IRCOperPassword:=StringSection(IRCOperPassword, 0);
IRCPassword:=StringSection(IRCPassword,0);
StealthIP:=StringSection(StealthIP, 0);
while Pos(' ',NetworkName) <> 0 do
NetworkName[Pos(' ',NetworkName)]:=#160;
if not GetLocalizations(AppLanguage) then
EventLog('MyWormNET: failed to find the language file - "languages'+PathDelim+AppLanguage+'.txt"');
if TextMatch(IRCPassword, 'Default') then
IRCPassword:=IRCDefPassword
else if TextMatch(IRCPassword, 'Random') then
begin
IRCPassword:=RandomString(12);
if IRCPort <> 0 then
EventLog('MyWormNET: '+Format(L_IRC_RANDOM_PASS, [IRCPassword]));
end;
if TextMatch(IRCOperPassword, 'Default') or TextMatch(IRCOperPassword, 'Random') then
begin
IRCOperPassword:=RandomString(8);
if IRCPort <> 0 then
EventLog('MyWormNET: '+Format(L_IRC_RANDOM_OPERPASS, [IRCOperPassword]));
end;
if DirectoryExists('lists') then
begin
GetListFromFile(NickBanThreadList, 'lists'+PathDelim+'ban_nicks.txt');
GetListFromFile(IPBanThreadList, 'lists'+PathDelim+'ban_ips.txt');
GetListFromFile(WhiteIPThreadList, 'lists'+PathDelim+'white_throttle.txt');
GetListFromFile(WhiteNickThreadList, 'lists'+PathDelim+'white_nickipauth.txt');
GetListFromFile(WhitePassauthThreadList, 'lists'+PathDelim+'white_passauth.txt');
end;
if VerboseLogging or (IRCPort = 0) then
HTTPtoConsole:=true
else
HTTPtoConsole:=false;
PCS.Leave;
end;
procedure Log(S: string; DiskOnly: Boolean=False; Important: Boolean=False);
var
SX: string;
begin
// if Copy(S, 1, 1)<>'-' then
LCS.Enter;
// logging to disk will work only if the file WNServer.log exists
TextToFile('['+DateTimeToStr(Now)+'] '+S, 'WNServer.log');
if (VerboseLogging or Important) and not DiskOnly then
begin
// logging to console, if it's enabled
{$I-}
SX:=S;
{$IF Defined(MSWINDOWS) and (CompilerVersion < 20)}
if CharToOemA(PAnsiChar(S), PAnsiChar(SX)) then
{$IFEND}
WriteLn('['+TimeToStr(Now)+'] '+SX);
{$I+}
end;
LCS.Leave;
end;
procedure EventLog(S: string; DiskOnly: Boolean=false);
begin
Log(S,DiskOnly,true);
//echo to IRC Admins and Owners
LogToOper(S);
if Copy(S, 1, 1)<>'-' then
S:='['+DateTimeToStr(Now)+'] '+S;
ECS.Enter;
// logging to disk will work only if the file EventLog.log exists
TextToFile(S, 'EventLog.log');
ECS.Leave;
end;
{$IFDEF MSWINDOWS}
{$I WinSockCodes.inc}
function WinSockErrorCodeStr(Code: Integer): string;
var
I: Integer;
begin
Result:=L_ERROR+' #'+IntToStr(Code);
for I:=1 to High(WinSockErrors) do
if (WinSockErrors[I].Code=Code)or(WinSockErrors[I].Code=Code+10000) then
Result:=WinSockErrors[I].Text;
end;
{$ELSE}
function WinSockErrorCodeStr(Code: Integer): string;
begin
Result:=StrError(Code);
end;
{$ENDIF}
{$I stuff.inc}
{function IRCDateTime(T: TDateTime) : Int64;
begin
Result := Round(T * SecsPerDay) - UnixDateDelta;
end;}
{$IFDEF MSWINDOWS}
function TextDateTime(T: TDateTime; IsUTC: Boolean = false) : string;
var
Timezone: TTimeZoneInformation;
Side: Char;
UTCOffset: TDateTime;
StrOffset: String;
begin
EnglishDates;
GetTimeZoneInformation(Timezone);
if IsUTC then
T:=IncMinute(T, Timezone.Bias);
UTCOffset := Timezone.Bias/MinsPerDay;
StrOffset := TimeToStr(UTCOffset);
StrOffset := Copy(StrOffset,1,Length(StrOffset)-3);
if UTCOffset>0 then Side:='-'
else Side:='+';
Result := FormatDateTime('dddd mmmm d yyyy',T)+' -- '+TimeToStr(T)+' UTC'+Side+StrOffset;
end;
function ExecutableTimestamp: TDateTime;
begin
{$IF CompilerVersion >= 20}
Result := PImageNtHeaders(HInstance + UIntPtr(PImageDosHeader(HInstance)^._lfanew))^.FileHeader.TimeDateStamp / SecsPerDay + UnixDateDelta;
{$ELSE}
Result := FileDateToDateTime(FileAge(ExtractFileName(ParamStr(0))));
{$IFEND}
end;
{$ELSE}
function TextDateTime(T: TDateTime; IsUTC: Boolean = false) : string;
begin
Result := FormatDateTime('dddd mmmm d yyyy',T)+' -- '+TimeToStr(T)+' server local time';
end;
function ExecutableTimestamp: TDateTime;
begin
Result := FileDateToDateTime(FileAge(ExtractFileName(ParamStr(0))));
end;
{$ENDIF}
initialization
MinimumVersion:=TVersion.Create;
PCS:=TCriticalSection.Create;
LCS:=TCriticalSection.Create;
ECS:=TCriticalSection.Create;
InitSettings;
finalization
MinimumVersion.Free;
LCS.Free;
ECS.Free;
PCS.Free;
Config.Free;
end.