This repository has been archived by the owner on Sep 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
nosonosocfg.pas
277 lines (240 loc) · 7.22 KB
/
nosonosocfg.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
273
274
275
276
unit NosoNosoCFG;
{
NosoNosoCFG 1.1
March 23, 2024
Stand alone unit to control nosocfg file and functionalitys
}
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, strutils,
nosodebug, nosogeneral, nosotime, nosocrypto;
Procedure SetCFGHash();
Function GetCFGHash():String;
Procedure SetCFGFilename(Fname:String);
Function SaveCFGToFile(Content:String):Boolean;
Procedure GetCFGFromFile();
Procedure SetCFGDataStr(Content:String);
Function GetCFGDataStr(LParam:integer=-1):String;
Procedure AddCFGData(DataToAdd:String;CFGIndex:Integer);
Procedure RemoveCFGData(DataToRemove:String;CFGIndex:Integer);
Procedure SetCFGData(DataToSet:String;CFGIndex:Integer);
Procedure RestoreCFGData();
Procedure ClearCFGData(Index:string);
Function IsSeedNode(IP:String):boolean;
var
CFGFilename : string= 'nosocfg.psk';
CFGFile : Textfile;
MyCFGHash : string = '';
CS_CFGFile : TRTLCriticalSection;
CS_CFGData : TRTLCriticalSection;
CS_CFGHash : TRTLCriticalSection;
NosoCFGString : string = '';
LasTimeCFGRequest : int64 = 0;
DefaultNosoCFG : String = // CFG parameters
{0 Mainnet mode}'NORMAL '+
{1 Seed nodes }'84.247.143.153;8080:173.212.222.226;8080:20.199.50.27;8080:4.233.61.8;8080: '+
{2 NTP servers }'ts2.aco.net:hora.roa.es:time.esa.int:time.stdtime.gov.tw:stratum-1.sjc02.svwh.net:ntp1.sp.se:1.de.pool.ntp.org:ntps1.pads.ufrj.br:utcnist2.colorado.edu:tick.usask.ca:ntp1.st.keio.ac.jp: '+
{3 DEPRECATED }'null: '+
{4 DEPRECATED }'null: '+
{5 FREZZED }'NpryectdevepmentfundsGE:';
IMPLEMENTATION
{$REGION CFG hash}
Procedure SetCFGHash();
Begin
EnterCriticalSection(CS_CFGHash);
MyCFGHash := HashMD5String(GetCFGDataStr);
LeaveCriticalSection(CS_CFGHash);
End;
Function GetCFGHash():String;
Begin
EnterCriticalSection(CS_CFGHash);
Result := MyCFGHash;
LeaveCriticalSection(CS_CFGHash);
End;
{$ENDREGION CFG hash}
{$REGION File access}
Procedure SetCFGFilename(Fname:String);
var
defseeds : string = '';
Begin
CFGFilename := Fname;
//AssignFile(CFGFile, CFGFilename);
if not fileexists(CFGFilename) then
begin
SaveCFGToFile(DefaultNosoCFG);
GetCFGFromFile;
Defseeds := SendApiRequest('https://raw.githubusercontent.com/Noso-Project/NosoWallet/main/defseeds.nos');
if defseeds <> '' then
begin
SetCFGData(Defseeds,1);
Tolog('console','Defaults seeds downloaded from trustable source');
end
else
begin
ToLog('console','Unable to download default seeds. Please, use a fallback');
end;
end;
ToLog('main','all well here');
GetCFGFromFile;
SetCFGHash();
End;
Function SaveCFGToFile(Content:String):Boolean;
Begin
EnterCriticalSection(CS_CFGFile);
Result := SaveTextToDisk(CFGFilename,Content);
SetCFGDataStr(Content);
LeaveCriticalSection(CS_CFGFile);
End;
Procedure GetCFGFromFile();
Begin
EnterCriticalSection(CS_CFGFile);
SetCFGDataStr(LoadTextFromDisk(CFGFilename));
LeaveCriticalSection(CS_CFGFile);
End;
{$ENDREGION File access}
{$REGION Data access}
Procedure SetCFGDataStr(Content:String);
Begin
EnterCriticalSection(CS_CFGData);
NosoCFGString := Content;
LeaveCriticalSection(CS_CFGData);
SetCFGHash;
End;
Function GetCFGDataStr(LParam:integer=-1):String;
Begin
EnterCriticalSection(CS_CFGData);
if LParam<0 then Result := NosoCFGString
else Result := Parameter(NosoCFGString,LParam);
LeaveCriticalSection(CS_CFGData);
End;
{$ENDREGION Data access}
{$REGION Management}
Procedure AddCFGData(DataToAdd:String;CFGIndex:Integer);
var
LCFGstr : String;
LArrString : Array of string;
DataStr : String;
thisData : string;
Counter : integer = 0;
FinalStr : string = '';
Begin
if DataToAdd[Length(DataToAdd)] <> ':' then
DataToAdd := DataToAdd+':';
LCFGStr := GetCFGDataStr();
SetLength(LArrString,0);
Repeat
ThisData := Parameter(LCFGStr,counter);
if ThisData <> '' then
Insert(ThisData,LArrString,LEngth(LArrString));
Inc(Counter);
until thisData = '';
if CFGIndex+1 > LEngth(LArrString) then
begin
repeat
Insert('',LArrString,LEngth(LArrString));
until CFGIndex+1 = LEngth(LArrString);
end;
DataStr := LArrString[CFGIndex];
DataStr := DataStr+DataToAdd;
LArrString[CFGIndex] := DataStr;
For counter := 0 to length(LArrString)-1 do
FinalStr := FinalStr+' '+LArrString[counter];
If FinalStr[1] = ' ' then delete(FinalStr,1,1);
SaveCFGToFile(FinalStr);
LasTimeCFGRequest:= UTCTime+5;
End;
Procedure RemoveCFGData(DataToRemove:String;CFGIndex:Integer);
var
LCFGstr : String;
LArrString : Array of string;
DataStr : String;
thisData : string;
Counter : integer = 0;
FinalStr : string = '';
Begin
if ( (Length(DataToRemove)>0) and (DataToRemove[Length(DataToRemove)] <> ':') ) then
DataToRemove := DataToRemove+':';
LCFGStr := GetCFGDataStr();
SetLength(LArrString,0);
Repeat
ThisData := Parameter(LCFGStr,counter);
if ThisData <> '' then
begin
Insert(ThisData,LArrString,LEngth(LArrString));
end;
Inc(Counter);
until thisData = '';
DataStr := LArrString[CFGIndex];
DataStr := StringReplace(DataStr,DataToRemove,'',[rfReplaceAll, rfIgnoreCase]);
LArrString[CFGIndex] := DataStr;
For counter := 0 to length(LArrString)-1 do
FinalStr := FinalStr+' '+LArrString[counter];
FinalStr := Trim(FinalStr);
If FinalStr[1] = ' ' then delete(FinalStr,1,1);
LasTimeCFGRequest:= UTCTime+5;
SaveCFGToFile(FinalStr);
End;
Procedure SetCFGData(DataToSet:String;CFGIndex:Integer);
var
LCFGstr : String;
LArrString : Array of string;
DataStr : String;
thisData : string;
Counter : integer = 0;
FinalStr : string = '';
Begin
if ( (Length(DataToSet)>0) and (DataToSet[Length(DataToSet)] <> ':') and (CFGIndex>0) ) then
DataToSet := DataToSet+':';
if ((CFGIndex = 0) and (DatatoSet = '') ) then exit;
LCFGStr := GetCFGDataStr();
SetLength(LArrString,0);
Repeat
ThisData := Parameter(LCFGStr,counter);
if ThisData <> '' then
begin
Insert(ThisData,LArrString,LEngth(LArrString));
end;
Inc(Counter);
until thisData = '';
LArrString[CFGIndex] := DataToSet;
For counter := 0 to length(LArrString)-1 do
FinalStr := FinalStr+' '+LArrString[counter];
FinalStr := Trim(FinalStr);
LasTimeCFGRequest:= UTCTime+5;
SaveCFGToFile(FinalStr);
End;
Procedure RestoreCFGData();
Begin
LasTimeCFGRequest:= UTCTime+5;
SaveCFGToFile(DefaultNosoCFG);
End;
Procedure ClearCFGData(Index:string);
var
LIndex : integer;
Begin
LIndex := StrToIntDef(Index,-1);
If LIndex <= 0 then exit;
SetCFGData('null:',LIndex);
End;
{$ENDREGION Management}
{$REGION Information}
// If the specified IP a seed node
Function IsSeedNode(IP:String):boolean;
var
SeedNodesStr : string;
Begin
Result := false;
SeedNodesStr := ':'+GetCFGDataStr(1);
if AnsiContainsStr(SeedNodesStr,':'+ip+';') then result := true;
End;
{$REGION Information}
INITIALIZATION
InitCriticalSection(CS_CFGFile);
InitCriticalSection(CS_CFGData);
InitCriticalSection(CS_CFGHash);
FINALIZATION
DoneCriticalSection(CS_CFGFile);
DoneCriticalSection(CS_CFGData);
DoneCriticalSection(CS_CFGHash);
END.