-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnginxtool_main.pas
1246 lines (1152 loc) · 36.4 KB
/
nginxtool_main.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
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 nginxtool_main;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
StdCtrls, JSONPropStorage, Spin, ComCtrls, EditBtn, UniqueInstance,
UExceptionLogger, uConfigParser;
type
{ TFormNginxtool }
TFormNginxtool = class(TForm)
ButtonShowError: TButton;
ButtonStart: TButton;
ButtonStop: TButton;
ButtonReload: TButton;
chkRecordUnique: TCheckBox;
CheckBox_interleave: TCheckBox;
CheckBox_publishnotify: TCheckBox;
CheckBox_sessionrelay: TCheckBox;
CheckBox_idlestm: TCheckBox;
CheckBox_waitkey: TCheckBox;
CheckBox_waitvideo: TCheckBox;
CheckBox_priority: TCheckBox;
CheckBoxModConf: TCheckBox;
ComboBox_Record: TComboBox;
ComboBox_meta: TComboBox;
edRecordSuffix: TEdit;
edRecordMaxSize: TEdit;
Label11: TLabel;
Label12: TLabel;
Record_path: TDirectoryEdit;
EdRtmp1: TEdit;
EdRtmp2: TEdit;
EdRtmp3: TEdit;
GroupBox1: TGroupBox;
JSONPropStorage1: TJSONPropStorage;
Label1: TLabel;
Label10: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
PageControl1: TPageControl;
Panel1: TPanel;
Panel2: TPanel;
SpinEdit_chunk: TSpinEdit;
SpinEdit_buflen: TSpinEdit;
SpinEdit_sync: TSpinEdit;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
TabSheet3: TTabSheet;
Timer1: TTimer;
TimerLog: TTimer;
UniqueInstance1: TUniqueInstance;
procedure ButtonShowErrorClick(Sender: TObject);
procedure ButtonStartClick(Sender: TObject);
procedure ButtonStopClick(Sender: TObject);
procedure ButtonReloadClick(Sender: TObject);
procedure CheckBoxModConfClick(Sender: TObject);
procedure CheckBox_ValueChange(Sender: TObject);
procedure ComboBoxChunkCloseUp(Sender: TObject);
procedure ComboBoxChunkKeyPress(Sender: TObject; var Key: char);
procedure ComboBox_ValueChange(Sender: TObject);
procedure EdRtmpExit(Sender: TObject);
procedure EdRtmpKeyPress(Sender: TObject; var Key: char);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure SpinEdit_syncChange(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure TimerLogTimer(Sender: TObject);
private
function CheckOnOff(item: TNginxItem; b: Boolean): Boolean;
public
procedure SaveOptions;
procedure VerboseNginxConfig(ForceUpdate: Boolean=False);
function CheckSettingChange:Boolean;
procedure NginxLogEndLine;
end;
var
FormNginxtool: TFormNginxtool;
implementation
uses
{$ifdef WINDOWS}windows,{$endif} loglistfpc, sockets, RegExpr, process {$ifdef WINDOWS}, JwaPsApi{$endif},
DefaultTranslator, LazFileUtils, DateUtils, frmError;
const
ngxLogFile = './logs/error.log';
resourcestring
rsError = '> Error: ';
rsNginxRestart = '> nginx restarted';
rsNginxIsNotRun = '> nginx is not running';
rsNginxStopped = '> nginx stopped';
rsNginxStarted = '> nginx started';
rsAlreadyRunning = '> Already running! Try restart.';
rsIPAddressS = '> IP Address: %s';
rsFailToWriteConfig = 'Fail to write file "conf/nginx.conf"';
rsNginxConfig = '----- nginx config -----';
rsCannotSetP = 'Cannot set process priority %d';
rsSetProcessP = 'Set process priority %d';
var
loglist:TLogListFPC;
{$ifdef WINDOWS}
UseAboveNormalProcess:Boolean;
{$endif}
checkflag:string = '';
ppval:DWORD;
nginx_process_find:Integer=0;
doUpdatePush: Boolean = False;
bGotNginxLog: Boolean = True;
runtime:TDateTime;
errorlog_path:string = ngxLogFile;
{$R *.lfm}
procedure GetIPAddr(var buf: array of char; const len: longint);
const
CN_GDNS_ADDR = '127.0.0.1';
CN_GDNS_PORT = 53;
var
s: string;
sock: longint;
err: longint;
HostAddr: TSockAddr;
l: Integer;
IPAddr: TInetSockAddr;
begin
err := 0;
Assert(len >= 16);
sock := fpsocket(AF_INET, SOCK_DGRAM, 0);
assert(sock <> -1);
IPAddr.sin_family := AF_INET;
IPAddr.sin_port := htons(CN_GDNS_PORT);
IPAddr.sin_addr.s_addr := StrToHostAddr(CN_GDNS_ADDR).s_addr;
if (fpConnect(sock,@IPAddr,SizeOf(IPAddr)) = 0) then
begin
try
l := SizeOf(HostAddr);
if (fpgetsockname(sock, @HostAddr, @l) = 0) then
begin
s := NetAddrToStr(HostAddr.sin_addr);
StrPCopy(PChar(Buf), s);
end
else
begin
err:=socketError;
end;
finally
if (CloseSocket(sock) <> 0) then
begin
err := socketError;
end;
end;
end
else
begin
err:=socketError;
end;
end;
function boolToInt(b:Boolean):Integer;
begin
if b then
Result:=1
else
Result:=0;
end;
{$ifdef WINDOWS}
function checkEnumProcess(const name:string):Boolean;
var
processids:array[0..1023] of DWord;
cbNeed, pCount, i:DWord;
ProcessName:array[0..MAX_PATH-1] of char;
function checkProcessName(pid:DWORD):Boolean;
var
ph:HANDLE;
pm:HMODULE;
cbval:DWORD;
begin
result:=false;
ph:=OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, false, processids[i]);
if ph<>0 then begin
if EnumProcessModules(ph,@pm,sizeof(pm),cbval) then begin
GetModuleBaseName(ph,pm,ProcessName,sizeof(ProcessName) div SizeOf(char));
result:=CompareText(name,strpas(ProcessName))=0;
end;
end;
CloseHandle(ph);
end;
begin
result:=false;
if EnumProcesses(@processids[0],sizeof(processids),cbNeed) then begin
pCount:=cbNeed div sizeof(DWord);
for i:=0 to pCount-1 do begin
if processids[i]<>0 then begin
result:=checkProcessName(processids[i]);
if result then
break;
end;
end;
end;
end;
procedure ProcessPriority(const name:string; priority:dword);
var
processids:array[0..1023] of DWord;
cbNeed, pCount, i:DWord;
ProcessName:array[0..MAX_PATH-1] of char;
function ProcessPriorityHandle(pid:DWORD):Boolean;
var
ph:HANDLE;
pm:HMODULE;
cbval:DWORD;
begin
result:=false;
ph:=OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ or PROCESS_SET_INFORMATION, false, processids[i]);
if ph<>0 then begin
if EnumProcessModules(ph,@pm,sizeof(pm),cbval) then begin
GetModuleBaseName(ph,pm,ProcessName,sizeof(ProcessName) div SizeOf(char));
if CompareText(name,strpas(ProcessName))=0 then begin
if not (GetPriorityClass(ph)=priority) then
if not SetPriorityClass(ph,priority) then begin
GetLastError;
loglist.AddLog(Format(rsCannotSetP, [ph]));
end else begin
Inc(nginx_process_find);
loglist.AddLog(Format(rsSetProcessP, [ph]));
end;
end;
end;
end;
CloseHandle(ph);
end;
begin
if EnumProcesses(@processids[0],sizeof(processids),cbNeed) then begin
pCount:=cbNeed div sizeof(DWord);
for i:=0 to pCount-1 do begin
if processids[i]<>0 then begin
ProcessPriorityHandle(processids[i]);
end;
end;
end;
end;
{$endif}
{ TFormNginxtool }
procedure TFormNginxtool.Timer1Timer(Sender: TObject);
begin
{$ifdef WINDOWS}
if CheckBox_priority.Checked then
ProcessPriority('nginx.exe',ppval);
if nginx_process_find>0 then
Timer1.Enabled:=False;
{$else}
Timer1.Enabled:=False;
{$endif}
end;
procedure TFormNginxtool.TimerLogTimer(Sender: TObject);
begin
NginxLogEndLine;
TimerLog.Enabled:=False;
end;
function checkConfigComment(const s:string):Boolean;
var
i,len:Integer;
begin
Result:=False;
i:=1;
len:=Length(s);
while i<=len do begin
if s[i]>#32 then
break;
Inc(i);
end;
while i<=len do begin
if s[i]='#' then begin
Result:=True;
break;
end;
Inc(i);
end;
end;
function boolToOnOff(b:Boolean):string;
begin
if b then
Result:='on'
else
Result:='off';
end;
function TFormNginxtool.CheckOnOff(item:TNginxItem;b:Boolean):Boolean;
begin
Result:=False;
if not SameText(item.Value,boolToOnOff(b)+';') then begin
item.Value:=boolToOnOff(b)+';';
Result:=True;
end;
end;
procedure TFormNginxtool.SaveOptions;
begin
// store values
JSONPropStorage1.WriteBoolean('priority', CheckBox_priority.Checked);
JSONPropStorage1.WriteBoolean('modify', CheckBoxModConf.Checked);
JSONPropStorage1.WriteInteger('chunk_size', SpinEdit_chunk.Value);
JSONPropStorage1.WriteInteger('meta', ComboBox_meta.ItemIndex);
JSONPropStorage1.WriteInteger('wait_video', boolToInt(
CheckBox_waitvideo.Checked));
JSONPropStorage1.WriteInteger('wait_key', boolToInt(
CheckBox_waitkey.Checked));
JSONPropStorage1.WriteString('record', ComboBox_Record.Text);
JSONPropStorage1.WriteBoolean('idlestreams', CheckBox_idlestm.Checked);
JSONPropStorage1.WriteBoolean('sessionrelay', CheckBox_sessionrelay.Checked);
JSONPropStorage1.WriteBoolean('publishnotify', CheckBox_publishnotify.Checked
);
JSONPropStorage1.WriteBoolean('interleave', CheckBox_interleave.Checked
);
JSONPropStorage1.WriteInteger('sync',SpinEdit_sync.Value);
JSONPropStorage1.WriteInteger('buflen',SpinEdit_buflen.Value);
JSONPropStorage1.WriteString('recordpath',SwitchPathDelims(Record_path.Directory,pdsSystem));
JSONPropStorage1.WriteString('recordmaxsize',edRecordMaxSize.Text);
JSONPropStorage1.WriteBoolean('recordunique',chkRecordUnique.Checked);
JSONPropStorage1.WriteString('recordsuffix',edRecordSuffix.Text);
end;
function StripInt(const s:string):Integer;
var
i, len:Integer;
buf:string;
ch:char;
begin
len:=Length(s);
i:=1;
buf:='';
while i<=len do begin
ch:=s[i];
if ch in ['0'..'9'] then
buf:=buf+ch
else
break;
Inc(i);
end;
Result:=StrToIntDef(buf,0);
end;
function GetFirstOption(const s:string):string;
var
i, l: Integer;
begin
Result:='';
l:=Length(s);
i:=1;
while i<=l do begin
if (s[i]<=#32) or (s[i]=';') then begin
Result:=Copy(s,1,i-1);
break;
end;
Inc(i);
end;
end;
procedure TFormNginxtool.VerboseNginxConfig(ForceUpdate: Boolean=False);
var
workercount : Integer;
chunk_modified : Boolean;
IPBuf:array[0..254] of char;
configpar:TNginxConfigParser;
item, itemprev:TNginxItem;
rtmpgrp, itemgrp, itemgrpPrev, firstitemgrp: TNginxItemGroup;
itemidx:Integer;
i,k:Integer;
dHandle: THANDLE;
stemp:string;
begin
chunk_modified:=ForceUpdate;
loglist.AddLog(rsNginxConfig);
configpar:=TNginxConfigParser.Create;
try
// create nil file
ForceDirectories('conf');
if not FileExists('conf/nginx.conf') then begin
dHandle:=FileCreate('conf/nginx.conf');
FileClose(dHandle);
end;
// open
configpar.Load('conf/nginx.conf');
item:=configpar.ItemList.FindItemName('error_log');
if item<>nil then begin
stemp:=GetFirstOption(item.Value);
if stemp<>'' then
errorlog_path:=stemp;
end;
// check 'worker_process 1;'
item:=configpar.ItemList.FindItemName('worker_processes');
if item<>nil then begin
workercount:=StripInt(item.Value);
if CheckBoxModConf.Checked then
if workercount<>1 then begin
item.Value:='1;';
chunk_modified:=True;
workercount:=1;
end;
end else
begin
workercount:=1;
if CheckBoxModConf.Checked then begin
item:=configpar.ItemList.InsertNameValue(0,0,'worker_processes','1;');
chunk_modified:=True;
end;
end;
if item<>nil then
loglist.AddLog(Format('%s %s',[item.NameItem,item.Value]));
// rtmp_auto_push - multiworker push
item:=configpar.ItemList.FindItemName('rtmp_auto_push');
if item=nil then begin
item:=configpar.ItemList.InsertNameValue(0,0,'rtmp_auto_push','off;');
chunk_modified:=True;
end;
if CheckBoxModConf.Checked then begin
if workercount<2 then
stemp:='off;'
else
stemp:='on;';
if not SameText(item.Value,stemp) then begin
item.Value:=stemp;
chunk_modified:=True;
end;
end;
loglist.AddLog(Format('%s %s',[item.NameItem,item.Value]));
// events
itemgrp:=configpar.ItemList.FindItemGroup('events');
if itemgrp=nil then begin
configpar.ItemList.AddNameGroup(0,'events','{');
itemgrp:=configpar.ItemList.FindItemGroup('events');
itemgrp.MarkClose;
chunk_modified:=True;
end;
// worker_connections
item:=itemgrp.FindItemName('worker_connections');
if item=nil then begin
itemgrp.AddNameValue(itemgrp.Level+1,'worker_connections','1024;');
chunk_modified:=True;
end;
// ----- http -----
rtmpgrp:=configpar.ItemList.FindItemGroup('http');
if rtmpgrp=nil then begin
configpar.ItemList.AddNameGroup(0,'http','{');
rtmpgrp:=configpar.ItemList.FindItemGroup('http');
rtmpgrp.MarkClose;
rtmpgrp.AddNameGroup(rtmpgrp.Level+1,'server','{');
rtmpgrp:=rtmpgrp.FindItemGroup('server');
rtmpgrp.MarkClose;
rtmpgrp.AddNameValue(rtmpgrp.Level+1,'listen','8080;');
rtmpgrp.AddNameGroup(rtmpgrp.Level+1,'location','/stat {');
rtmpgrp:=rtmpgrp.FindItemGroup('location');
rtmpgrp.MarkClose;
rtmpgrp.AddNameValue(rtmpgrp.Level+1,'rtmp_stat','all;');
chunk_modified:=True;
end;
// ----- rtmp -----
rtmpgrp:=configpar.ItemList.FindItemGroup('rtmp');
if rtmpgrp=nil then begin
configpar.ItemList.AddNameGroup(0,'rtmp','{');
rtmpgrp:=configpar.ItemList.FindItemGroup('rtmp');
rtmpgrp.MarkClose;
chunk_modified:=True;
end;
// ----- server -----
itemgrp:=rtmpgrp.FindItemGroup('server');
if itemgrp<>nil then begin
while itemgrp<>nil do begin
item:=itemgrp.FindItemName('listen');
if (item<>nil) and (NginxRemoveTrailValue(item.Value)='1935') then
break;
itemgrp:=rtmpgrp.FindItemGroupNext(itemgrp,'server');
end;
end;
if itemgrp=nil then begin
rtmpgrp.AddNameGroup(rtmpgrp.Level+1,'server','{');
itemgrp:=rtmpgrp.FindItemGroup('server');
itemgrp.AddNameValue(itemgrp.Level+1,'listen','1935;');
itemgrp.MarkClose;
chunk_modified:=True;
end;
// sync
item:=itemgrp.FindItemName('sync');
if item=nil then begin
itemgrp.AddNameValue(itemgrp.Level+1,'sync','300ms;');
chunk_modified:=True;
end else if CheckBoxModConf.Checked then
if SpinEdit_sync.Value<>StripInt(item.Value) then begin
item.Value:=IntToStr(SpinEdit_sync.Value)+'ms;';
chunk_modified:=True;
end;
if item<>nil then
loglist.AddLog(Format('%s %s',[item.NameItem,item.Value]));
// buflen
item:=itemgrp.FindItemName('buflen');
if item=nil then begin
itemgrp.AddNameValue(itemgrp.Level+1,'buflen','1000ms;');
chunk_modified:=True;
end else if CheckBoxModConf.Checked then
if SpinEdit_buflen.Value<>StripInt(item.Value) then begin
item.Value:=IntToStr(SpinEdit_buflen.Value)+'ms;';
chunk_modified:=True;
end;
if item<>nil then
loglist.AddLog(Format('%s %s',[item.NameItem,item.Value]));
// publish_notify
item:=itemgrp.FindItemName('publish_notify');
if item=nil then begin
itemgrp.AddNameValue(itemgrp.Level+1,'publish_notify',boolToOnOff(CheckBox_publishnotify.Checked)+';');
chunk_modified:=True;
end else if CheckBoxModConf.Checked then
if CheckOnOff(item,CheckBox_publishnotify.Checked) then
chunk_modified:=True;
// chunk_size 8192;
item:=itemgrp.FindItemName('chunk_size');
if item=nil then begin
item:=itemgrp.InsertNameValue(0,itemgrp.Level+1,'chunk_size',IntToStr(SpinEdit_chunk.Value)+';');
chunk_modified:=True;
end else if CheckBoxModConf.Checked then
if SpinEdit_chunk.Value<>StripInt(item.Value) then begin
item.Value:=IntToStr(SpinEdit_chunk.Value)+';';
chunk_modified:=True;
end;
if item<>nil then
loglist.AddLog(Format('%s %s',[item.NameItem,item.Value]));
// wait_key
item:=itemgrp.FindItemName('wait_key');
if CheckBoxModConf.Checked then begin
if item<>nil then begin
if CheckOnOff(item,CheckBox_waitkey.Checked) then begin
if CheckBox_waitkey.Checked then begin
CheckBox_waitvideo.OnChange:=nil;
CheckBox_waitvideo.Checked:=True;
CheckBox_waitvideo.OnChange:=@CheckBox_ValueChange;
end;
chunk_modified:=True;
end;
end else begin
if itemgrp<>nil then begin
item:=itemgrp.InsertNameValue(0,itemgrp.Level+1,'wait_key',boolToOnOff(CheckBox_waitkey.Checked)+';');
chunk_modified:=True;
end;
end;
end;
if item<>nil then
loglist.AddLog(Format('%s %s',[item.NameItem,item.Value]));
// wait_video
item:=itemgrp.FindItemName('wait_video');
if CheckBoxModConf.Checked then begin
if item<>nil then begin
if CheckOnOff(item,CheckBox_waitvideo.Checked) then
chunk_modified:=True;
end else begin
if itemgrp<>nil then begin
item:=itemgrp.InsertNameValue(0,itemgrp.Level+1,'wait_video',boolToOnOff(CheckBox_waitvideo.Checked)+';');
chunk_modified:=True;
end;
end;
end;
if item<>nil then
loglist.AddLog(Format('%s %s',[item.NameItem,item.Value]));
// insert meta copy
item:=itemgrp.FindItemName('meta');
if CheckBoxModConf.Checked then begin
if item<>nil then begin
if item.Value<>ComboBox_meta.Text+';' then begin
item.Value:=ComboBox_meta.Text+';';
chunk_modified:=True;
end;
end else begin
if itemgrp<>nil then begin
item:=itemgrp.InsertNameValue(0,itemgrp.Level+1,'meta',ComboBox_meta.Text+';');
chunk_modified:=True;
end;
end;
end;
if item<>nil then
loglist.AddLog(Format('%s %s',[item.NameItem,item.Value]));
// ----- application -----
firstitemgrp:=nil;
itemgrpPrev:=itemgrp;
itemgrp:=itemgrpPrev.FindItemGroup('application');
while itemgrp<>nil do begin
// check 'live' and 'hls' item
if ((itemgrp.FindItemName('live')<>nil) and
(NginxRemoveTrailValue(itemgrp.FindItemName('live').Value)='on')
)
and
((itemgrp.FindItemName('hls')=nil) or
((itemgrp.FindItemName('hls')<>nil) and
(NginxRemoveTrailValue(itemgrp.FindItemName('hls').Value)='off'))
) then
firstitemgrp:=itemgrp;
itemgrp:=itemgrpPrev.FindItemGroupNext(itemgrp,'application');
end;
// get first application live section.
itemgrp:=firstitemgrp;
// insert application live section
if itemgrp=nil then begin
itemgrpPrev.AddNameGroup(itemgrpPrev.Level+1,'application','live {');
itemgrp:=itemgrpPrev.FindItemGroup('application');
itemgrp.AddNameValue(itemgrp.Level+1,'live','on;');
itemgrp.AddNameValue(itemgrp.Level+1,'interleave','off;');
itemgrp.AddNameValue(itemgrp.Level+1,'allow','publish 127.0.0.1;');
itemgrp.AddNameValue(itemgrp.Level+1,'deny','publish all;');
//itemgrp.AddNameValue(itemgrp.Level+1,'allow','play all;');
itemgrp.MarkClose;
chunk_modified:=True;
end;
// interleave
item:=itemgrp.FindItemName('interleave');
if item=nil then begin
itemgrp.AddNameValue(itemgrp.Level+1,'interleave','off;');
chunk_modified:=True;
end else if CheckBoxModConf.Checked then begin
if CheckOnOff(item,CheckBox_interleave.Checked) then
chunk_modified:=True;
end;
if item<>nil then
loglist.AddLog(Format('%s %s',[item.NameItem,item.Value]));
// idle_streams
item:=itemgrp.FindItemName('idle_streams');
if item=nil then begin
itemgrp.AddNameValue(itemgrp.Level+1,'idle_streams','off;');
chunk_modified:=True;
end else if CheckBoxModConf.Checked then begin
if CheckOnOff(item,CheckBox_idlestm.Checked) then
chunk_modified:=True;
end;
if item<>nil then
loglist.AddLog(Format('%s %s',[item.NameItem,item.Value]));
// session_relay
item:=itemgrp.FindItemName('session_relay');
if item=nil then begin
itemgrp.AddNameValue(itemgrp.Level+1,'session_relay','off;');
chunk_modified:=True;
end else if CheckBoxModConf.Checked then begin
if CheckOnOff(item,CheckBox_sessionrelay.Checked) then
chunk_modified:=True;
end;
if item<>nil then
loglist.AddLog(Format('%s %s',[item.NameItem,item.Value]));
// push check and update
if itemgrp<>nil then begin
item:=itemgrp.FindItemName('push');
i:=1;
// insert nil value item
if item=nil then begin
itemgrp.AddNameValue(itemgrp.Level+1,'push','');
item:=itemgrp.FindItemName('push');
end;
while item<>nil do begin
if i<=3 then begin
// restore push
if not doUpdatePush then begin
(FindComponent('EdRtmp'+IntToStr(i)) as TEdit).Text:=NginxRemoveTrailValue(item.Value);
item:=itemgrp.FindItemNameNext(item,'push');
end else begin
// update push
if CheckBoxModConf.Checked then begin
chunk_modified:=True;
if (FindComponent('EdRtmp'+IntToStr(i)) as TEdit).Text<>'' then
item.value:=(FindComponent('EdRtmp'+IntToStr(i)) as TEdit).Text+';'
else
item.value:='';
itemprev:=item;
item:=itemgrp.FindItemNameNext(item,'push');
// insert nil value item
if item=nil then begin
itemgrp.InsertNameValue(itemgrp.IndexOfItem(itemprev),itemgrp.Level+1,'push','');
item:=itemgrp.FindItemNameNext(itemprev,'push');
end;
end;
end;
end else
break;
Inc(i);
end;
// remove nil value item
item:=itemgrp.FindItemName('push');
while item<>nil do begin
itemprev:=item;
item:=itemgrp.FindItemNameNext(item,'push');
if itemprev.Value='' then
itemgrp.DeleteItem(itemprev);
end;
doUpdatePush:=False;
end;
// record [off|all|audio|video|keyframes|manual]
item:=itemgrp.FindItemName('record');
if CheckBoxModConf.Checked then begin
if item<>nil then begin
if item.Value<>ComboBox_Record.Text+';' then begin
item.Value:=ComboBox_Record.Text+';';
chunk_modified:=True;
end;
end else begin
if itemgrp<>nil then begin
item:=itemgrp.InsertNameValue(0,itemgrp.Level+1,'record',ComboBox_Record.Text+';');
chunk_modified:=True;
end;
end;
end;
if item<>nil then
loglist.AddLog(Format('%s %s',[item.NameItem,item.Value]));
// record path
item:=itemgrp.FindItemName('record_path');
if CheckBoxModConf.Checked then begin
if item=nil then begin
if itemgrp<>nil then begin
item:=itemgrp.InsertNameValue(0,itemgrp.Level+1,'record_path',SwitchPathDelims(GetUserDir,pdsUnix)+';');
chunk_modified:=True;
end;
end else
if Record_path.Directory<>JSONPropStorage1.ReadString('recordpath',Record_path.Directory) then begin
item.Value:=SwitchPathDelims(Record_path.Directory,pdsUnix)+';';
chunk_modified:=True;
end;
end;
if item<>nil then
loglist.AddLog(Format('%s %s',[item.NameItem,item.Value]));
// record_max_size 128;
item:=itemgrp.FindItemName('record_max_size');
if CheckBoxModConf.Checked then begin
if item=nil then begin
if itemgrp<>nil then begin
item:=itemgrp.InsertNameValue(0,itemgrp.Level+1,'record_max_size','600M;');
chunk_modified:=True;
end;
end else
if edRecordMaxSize.Text<>JSONPropStorage1.ReadString('recordmaxsize',edRecordMaxSize.Text) then begin
item.Value:=edRecordMaxSize.Text+';';
chunk_modified:=True;
end;
end;
if item<>nil then
loglist.AddLog(Format('%s %s',[item.NameItem,item.Value]));
// record_suffix .flv;
item:=itemgrp.FindItemName('record_suffix');
if CheckBoxModConf.Checked then begin
if item=nil then begin
if itemgrp<>nil then begin
item:=itemgrp.InsertNameValue(0,itemgrp.Level+1,'record_suffix','-%y-%m-%d-%H-%M-%S.flv;');
chunk_modified:=True;
end;
end else
if edRecordSuffix.Text<>JSONPropStorage1.ReadString('recordsuffix',edRecordSuffix.Text) then begin
item.Value:=edRecordSuffix.Text+';';
chunk_modified:=True;
end;
end;
if item<>nil then
loglist.AddLog(Format('%s %s',[item.NameItem,item.Value]));
// record_unique off;
item:=itemgrp.FindItemName('record_unique');
if CheckBoxModConf.Checked then begin
if item=nil then begin
if itemgrp<>nil then begin
item:=itemgrp.InsertNameValue(0,itemgrp.Level+1,'record_unique','off;');
chunk_modified:=True;
end;
end else
if CheckOnOff(item,chkRecordUnique.Checked) then
chunk_modified:=True;
end;
if item<>nil then
loglist.AddLog(Format('%s %s',[item.NameItem,item.Value]));
if chunk_modified then begin
try
configpar.Save('conf/nginx.conf');
except
on e:exception do begin
loglist.AddLog(rsFailToWriteConfig);
end;
end;
end;
// show push values
item:=itemgrp.FindItemName('push');
repeat
if item<>nil then begin
loglist.AddLog(Format('%s %s',[item.NameItem,item.Value{Copy(item.Value,1,40)+'...'}]));
item:=itemgrp.FindItemNameNext(item,'push');
end;
until item=nil;
finally
configpar.Free;
end;
GetIPAddr(IPBuf,sizeof(IPBuf));
loglist.AddLog(Format(rsIPAddressS, [IPBuf]));
end;
function TFormNginxtool.CheckSettingChange: Boolean;
begin
Result:=False;
if not Result then
Result:=CheckBox_priority.Checked<>JSONPropStorage1.ReadBoolean('priority',False);
if not Result then
Result:=CheckBoxModConf.Checked<>JSONPropStorage1.ReadBoolean('modify',True);
if not Result then
Result:=SpinEdit_chunk.Value<>JSONPropStorage1.ReadInteger('chunk_size',SpinEdit_chunk.Value);
if not Result then
Result:=ComboBox_meta.ItemIndex<>JSONPropStorage1.ReadInteger('meta',ComboBox_meta.ItemIndex);
if not Result then
Result:=boolToInt(CheckBox_waitvideo.Checked)<>JSONPropStorage1.ReadInteger('wait_video',boolToInt(CheckBox_waitvideo.Checked));
if not Result then
Result:=boolToInt(CheckBox_waitkey.Checked)<>JSONPropStorage1.ReadInteger('wait_key',boolToInt(CheckBox_waitkey.Checked));
if not Result then
Result:=ComboBox_Record.Text<>JSONPropStorage1.ReadString('record',ComboBox_Record.Text);
if not Result then
Result:=CheckBox_idlestm.Checked<>JSONPropStorage1.ReadBoolean('idlestreams',CheckBox_idlestm.Checked);
if not Result then
Result:=CheckBox_sessionrelay.Checked<>JSONPropStorage1.ReadBoolean('sessionrelay',CheckBox_sessionrelay.Checked);
if not Result then
Result:=CheckBox_publishnotify.Checked<>JSONPropStorage1.ReadBoolean('publishnotify',CheckBox_publishnotify.Checked);
if not Result then
Result:=CheckBox_interleave.Checked<>JSONPropStorage1.ReadBoolean('interleave',CheckBox_interleave.Checked);
if not Result then
Result:=SpinEdit_sync.Value<>JSONPropStorage1.ReadInteger('sync',SpinEdit_sync.Value);
if not Result then
Result:=SpinEdit_buflen.Value<>JSONPropStorage1.ReadInteger('buflen',SpinEdit_buflen.Value);
if not Result then
Result:=Record_path.Directory<>JSONPropStorage1.ReadString('recordpath',Record_path.Directory);
if not Result then
Result:=edRecordMaxSize.Text<>JSONPropStorage1.ReadString('recordmaxsize',edRecordMaxSize.Text);
if not Result then
Result:=chkRecordUnique.Checked<>JSONPropStorage1.ReadBoolean('recordunique',chkRecordUnique.Checked);
if not Result then
Result:=edRecordSuffix.Text<>JSONPropStorage1.ReadString('recordsuffix',edRecordSuffix.Text);
end;
procedure TFormNginxtool.NginxLogEndLine;
var
fs : TFileStream;
l : int64;
i, j : Integer;
buf : array[0..1024] of char;
logtime : TDateTime;
begin
if bGotNginxLog or (not FileExistsUTF8(errorlog_path)) then
exit;
try
fs := TFileStream.Create(errorlog_path,fmOpenRead or fmShareDenyNone);
try
l:=fs.Size;
if l>1024 then
Dec(l,1024)
else
l:=0;
fs.Position:=l;
i:=fs.Read(buf[0],1024);
finally
fs.Free;
end;
if i>0 then begin
Dec(i);
j:=i;
while i>=0 do begin
if buf[i]>#32 then
break;
Dec(i);
end;
while i>=0 do begin
if buf[i] in [#10,#13] then begin
Inc(i);
break;
end;
Dec(i);
end;
logtime:=StrToDateTime(Copy(buf,i,20));
if WithinPastSeconds(logtime,runtime,1) then
loglist.AddLog('log: '+pchar(UTF8Encode(UnicodeString(Copy(buf,i,1024)))));
end;
bGotNginxLog:=True;
except
on e:exception do
loglist.AddLog('log: '+e.Message);
end;
end;
procedure TFormNginxtool.FormCreate(Sender: TObject);
begin
exceptionLogger.LogFileName:='bugreport.txt';
DateSeparator:='/';
loglist:=TLogListFPC.Create(Self);
loglist.Name:='loglist1';
loglist.Parent:=Panel1;
loglist.Align:=alClient;
loglist.Color:=clWhite;
loglist.LineLimit:=1000;
Application.SingleInstanceEnabled:=True;
doUpdatePush:=False;
end;
procedure TFormNginxtool.FormDestroy(Sender: TObject);
begin
if CheckSettingChange then begin
SaveOptions;
VerboseNginxConfig(True);
try
JSONPropStorage1.Save;
except
end;
end;
end;
procedure TFormNginxtool.ButtonStartClick(Sender: TObject);
var
myprocess:TProcess;
i: integer;
begin
bGotNginxLog:=False;
runtime:=Now;
{$ifdef WINDOWS}
if checkEnumProcess('nginx.exe') then begin
loglist.AddLog(rsAlreadyRunning);
ButtonReloadClick(Sender);
exit;
end;
{$endif}
VerboseNginxConfig;
myprocess:=TProcess.Create(nil);
try
myprocess.InheritHandles:=false;
myprocess.Options:=[];
myprocess.ShowWindow:=swoHIDE;
for i:=1 to GetEnvironmentVariableCount do
myprocess.Environment.Add(GetEnvironmentString(i));
myprocess.Executable:='nginx';
{$ifdef WINDOWS}
if CheckBox_priority.Checked then
if UseAboveNormalProcess then begin
myprocess.Priority:=ppAboveNormal;
ppval:=ABOVE_NORMAL_PRIORITY_CLASS;
end
else begin
myprocess.Priority:=ppHigh;
ppval:=HIGH_PRIORITY_CLASS;
end;