-
Notifications
You must be signed in to change notification settings - Fork 7
/
ProxyServerModuleUnit.pas
827 lines (718 loc) · 24.2 KB
/
ProxyServerModuleUnit.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
unit ProxyServerModuleUnit;
interface
uses
System.SysUtils,
System.Classes,
System.Diagnostics,
System.Generics.Collections,
System.DateUtils,
Winapi.ActiveX,
FMX.Types,
FMX.Dialogs,
REST.Client,
REST.Types,
IdHTTPWebBrokerBridge,
IdBaseComponent,
IdScheduler,
IdSchedulerOfThread,
IdSchedulerOfThreadDefault,
IdThread,
IdContext,
IdUDPServer,
IdIPMCastServer,
IdIPMCastClient,
IdTCPServer,
IdSocketHandle,
IdGlobal,
IdUDPClient,
ProxyServiceModuleUnit,
Ceton,
HDHR,
LogUtils,
SocketUtils;
const
SSDP_MULTICAST_GROUP = '239.255.255.250';
SSDP_PORT = 1900;
cSSDPAliveIntervalSec = 1800;
type
TIdCOMThread = class(TIdThreadWithTask)
protected
procedure AfterExecute; override;
procedure BeforeExecute; override;
end;
TProxyServerModule = class(TDataModule, IServiceConfigEvents)
IdScheduler: TIdSchedulerOfThreadDefault;
RestartServersTimer: TTimer;
procedure DataModuleCreate(Sender: TObject);
procedure DataModuleDestroy(Sender: TObject);
procedure RestartServersTimerTimer(Sender: TObject);
private
{ Private declarations }
fConfigManager: IServiceConfigManager;
fClient: TCetonClient;
fServer: TIdHTTPWebBrokerBridge;
fDiscoveryServer: TIdUDPServer;
fSSDPClient: TIdIPMCastClient;
fSSDPServer: TIdUDPServer;
// fControlServer: TIdTCPServer;
procedure DiscoveryUDPRead(AThread: TIdUDPListenerThread; const AData: TIdBytes; ABinding: TIdSocketHandle);
procedure SSDPClientRead(Sender: TObject; const AData: TIdBytes; ABinding: TIdSocketHandle);
procedure SSDPServerRead(AThread: TIdUDPListenerThread; const AData: TIdBytes; ABinding: TIdSocketHandle);
procedure ControlTCPConnect(aContext: TIdContext);
procedure ControlTCPExecute(aContext: TIdContext);
procedure ServerException(AContext: TIdContext; AException: Exception);
function CreateSSDPDiscoverPacket: String;
function TryCreateSSDPResponsePacket(const aRequestHost: String; out aPacket: String): Boolean;
function TryCreateSSDPAlivePacket(const aRequestHost: String; out aPacket: String): Boolean;
procedure DiscoverCetonDevices;
function GetActive: Boolean;
property ConfigManager: IServiceConfigManager read fConfigManager;
property Client: TCetonClient read fClient;
protected
// IServiceConfigEvents
procedure Changed(const aSender: TObject; const aSections: TServiceConfigSections);
procedure Log(const aLogName: String; const aMessage: String);
procedure LogError(const aLogName: String; const aMessage: String);
procedure DiscoveredCetonDevicesChanged;
public
{ Public declarations }
procedure StartServer;
procedure StopServer;
function TryGetAddress(const aRequestLocalIP: String; out aAddress: String): Boolean;
property Active: Boolean read GetActive;
property DiscoveryServer: TIdUDPServer read fDiscoveryServer;
end;
var
ProxyServerModule: TProxyServerModule;
implementation
{%CLASSGROUP 'FMX.Controls.TControl'}
{$R *.dfm}
{ TIdCOMThread }
procedure TIdCOMThread.AfterExecute;
begin
inherited;
CoUninitialize;
end;
procedure TIdCOMThread.BeforeExecute;
begin
Coinitialize(nil);
inherited;
end;
{ TProxyServerModule }
procedure TProxyServerModule.DataModuleCreate(Sender: TObject);
begin
IdScheduler.ThreadClass := TIdCOMThread;
fConfigManager := ProxyServiceModule.ConfigManager;
fConfigManager.AddListener(Self);
fClient := ProxyServiceModule.Client;
fServer := TIdHTTPWebBrokerBridge.Create(Self);
fServer.Scheduler := IdScheduler;
fServer.OnException := ServerException;
fDiscoveryServer := TIdUDPServer.Create(Self);
fDiscoveryServer.ThreadedEvent := True;
fDiscoveryServer.OnUDPRead := DiscoveryUDPRead;
// Used to respond to UPnP discovery requests for HDHomeRun and listen
// for "alive" messages
fSSDPClient := TIdIPMCastClient.Create(Self);
fSSDPClient.MulticastGroup := SSDP_MULTICAST_GROUP;
fSSDPClient.OnIPMCastRead := SSDPClientRead;
fSSDPClient.ThreadedEvent := True;
// Used to discover Ceton devices
fSSDPServer := TIdUDPServer.Create(Self);
fSSDPServer.ThreadedEvent := True;
fSSDPServer.OnUDPRead := SSDPServerRead;
// fControlServer := TIdTCPServer.Create(Self);
// fControlServer.OnConnect := ControlTCPConnect;
// fControlServer.OnExecute := ControlTCPExecute;
end;
procedure TProxyServerModule.DataModuleDestroy(Sender: TObject);
begin
fConfigManager.RemoveListener(Self);
fSSDPClient.Active := False;
end;
function TProxyServerModule.GetActive: Boolean;
begin
Result := fServer.Active;
end;
procedure TProxyServerModule.StartServer;
var
lConfig: TServiceConfig;
lListenIP: String;
lHTTPPort: Integer;
lLocalIPs: TLocalIPInfoArray;
lLocalIPInfo: TLocalIPInfo;
begin
if not fServer.Active then
begin
ConfigManager.LockConfig(lConfig);
try
lListenIP := lConfig.ListenIP;
lHTTPPort := lConfig.HTTPPort;
finally
ConfigManager.UnlockConfig(lConfig);
end;
lLocalIPs := TSocketUtils.GetLocalIPs;
// FastMM detected memory corruption unless separated into 2 assignments
lLocalIPs := lLocalIPs.Keep(4);
try
fServer.Bindings.Clear;
if FindCmdLineSwitch('port80') then
begin
// Previously needed by NextPVR, but does not appear to be the case any more.
with fServer.Bindings.Add do
begin
IP := lListenIP;
Port := 80;
end;
end;
with fServer.Bindings.Add do
begin
IP := lListenIP;
Port := lHTTPPort;
end;
fServer.Active := True;
except
TLogger.Log(cLogDefault, 'Unable to bind HTTP server listening port');
end;
try
fDiscoveryServer.Bindings.Clear;
if lListenIP = '' then
begin
// If listening on all IPs, have to explicitly create a binding for each local IP
// so that we can properly read which IP that UDP packets come in on
for lLocalIPInfo in lLocalIPs do
with fDiscoveryServer.Bindings.Add do
begin
IP := lLocalIPInfo.IP;
Port := HDHR_DISCOVERY_PORT;
end;
end
else
begin
with fDiscoveryServer.Bindings.Add do
begin
IP := lListenIP;
Port := HDHR_DISCOVERY_PORT;
end;
end;
fDiscoveryServer.Active := True;
except
TLogger.Log(cLogDefault, 'Unable to bind discovery listening port');
end;
{ try
fControlServer.Bindings.Clear;
with fControlServer.Bindings.Add do
begin
IP := lListenIP;
Port := HDHR_DISCOVERY_PORT;
end;
fControlServer.Active := True;
except
TLogger.Log(cLogDefault, 'Unable to bind control listening port');
end;}
try
fSSDPClient.Bindings.Clear;
if lListenIP = '' then
begin
// If listening on all IPs, have to explicitly create a binding for each local IP
// so that we can properly read which IP that UDP packets come in on
for lLocalIPInfo in lLocalIPs do
with fSSDPClient.Bindings.Add do
begin
IP := lLocalIPInfo.IP;
Port := SSDP_PORT;
end;
end
else
begin
with fSSDPClient.Bindings.Add do
begin
IP := lListenIP;
Port := SSDP_PORT;
end;
end;
fSSDPClient.ReuseSocket := rsTrue;
fSSDPClient.Active := True;
except
TLogger.Log(cLogDefault, 'Unable to bind SSDP listening port');
end;
try
fSSDPServer.Bindings.Clear;
for lLocalIPInfo in lLocalIPs do
with fSSDPServer.Bindings.Add do
begin
IP := lLocalIPInfo.IP;
Port := 0;
end;
fSSDPServer.Active := True;
except
TLogger.Log(cLogDefault, 'Unable to create SSDP server');
end;
// Send broadcast for UPnP devices
DiscoverCetonDevices;
end;
end;
procedure TProxyServerModule.StopServer;
begin
try
fServer.Active := False;
fServer.Bindings.Clear;
except
// Ignore
end;
try
fDiscoveryServer.Active := False;
fDiscoveryServer.Bindings.Clear;
except
// Ignore
end;
{ try
fControlServer.Active := False;
fControlServer.Bindings.Clear;
except
// Ignore
end;}
try
fSSDPClient.Active := False;
fSSDPClient.Bindings.Clear;
except
// Ignore
end;
try
fSSDPServer.Active := False;
fSSDPServer.Bindings.Clear;
except
// Ignore
end;
end;
procedure TProxyServerModule.ServerException(AContext: TIdContext;
AException: Exception);
begin
// Do nothing
end;
procedure TProxyServerModule.DiscoveryUDPRead(AThread: TIdUDPListenerThread;
const AData: TIdBytes; ABinding: TIdSocketHandle);
var
lHex: AnsiString;
lPacket: TPacket;
lDiscovery: TDiscoveryData;
lConfig: TServiceConfig;
lBytes: TBytes;
lAddress: String;
lDeviceID: UInt32;
lTunerCount: Integer;
begin
if Length(AData) > 0 then
begin
SetLength(lHex, Length(AData)*2);
BinToHex(AData, PAnsiChar(lHex), Length(AData));
TLogger.LogFmt(cLogDiscovery,'Received control data from %s on %s: %s', [ABinding.PeerIP, ABinding.IP, lHex]);
if TPacket.TryFromBytes(TBytes(AData), lPacket) then
begin
if lPacket.IsValid then
begin
case lPacket._Type of
HDHOMERUN_TYPE_DISCOVER_REQ: begin
lDiscovery := lPacket.ToDiscovery;
TLogger.LogFmt(cLogDiscovery,'Received discovery request: Device type: %d, Device ID: %s', [lDiscovery.DeviceType, IntToHex(lDiscovery.DeviceID, 8)]);
ConfigManager.LockConfig(lConfig);
try
lDeviceID := lConfig.DeviceID;
finally
ConfigManager.UnlockConfig(lConfig);
end;
if ((lDiscovery.DeviceType = HDHOMERUN_DEVICE_TYPE_TUNER) or (lDiscovery.DeviceType = HDHOMERUN_DEVICE_TYPE_WILDCARD)) and ((lDiscovery.DeviceID = HDHOMERUN_DEVICE_ID_WILDCARD) or (lDiscovery.DeviceID = lDeviceID)) then
begin
if TryGetAddress(ABinding.IP, lAddress) then
begin
lTunerCount := Client.EnabledTunerCount;
ConfigManager.LockConfig(lConfig);
try
lDiscovery.DeviceID := lConfig.DeviceID;
lDiscovery.TunerCount := lTunerCount;
lDiscovery.DeviceAuthStr := 'abcdefg';
lDiscovery.BaseURL := AnsiString(Format('http://%s:%d', [lAddress, lConfig.ExternalHTTPPort]));
lDiscovery.LineupURL := AnsiString(Format('%s/lineup.json', [lDiscovery.BaseURL]));
finally
ConfigManager.UnlockConfig(lConfig);
end;
lBytes := TPacket.FromDiscovery(False, lDiscovery).ToBytes;
SetLength(lHex, Length(lBytes)*2);
BinToHex(lBytes, PAnsiChar(lHex), Length(lBytes));
TLogger.LogFmt(cLogDiscovery,'Sending discovery response: Device ID: %s, Base URL: %s, %s', [IntToHex(lDiscovery.DeviceID, 8), lDiscovery.BaseURL, lHex]);
AThread.Server.SendBuffer(ABinding.PeerIP, ABinding.PeerPort, TIdBytes(lBytes));
end;
end;
end;
HDHOMERUN_TYPE_DISCOVER_RPY: begin
lDiscovery := lPacket.ToDiscovery;
TLogger.LogFmt(cLogDiscovery,'Received discovery reply: Device type: %d, Device ID: %s', [lDiscovery.DeviceType, IntToHex(lDiscovery.DeviceID, 8)]);
end;
end;
end;
end;
end;
end;
procedure TProxyServerModule.Changed(const aSender: TObject;
const aSections: TServiceConfigSections);
begin
TThread.ForceQueue(nil,
procedure()
begin
if TServiceConfigSection.Server in aSections then
begin
RestartServersTimer.Enabled := True;
end;
end);
end;
procedure TProxyServerModule.RestartServersTimerTimer(Sender: TObject);
begin
RestartServersTimer.Enabled := False;
StopServer;
StartServer;
end;
function TProxyServerModule.TryGetAddress(const aRequestLocalIP: String; out aAddress: String): Boolean;
var
lConfig: TServiceConfig;
lAddresses: TLocalIPInfoArray;
lModel: TCetonModel;
begin
// The goal here is to return an IP address that the remote end will be able to reach
// us on as an HDHomeRun. The easiest thing to do is take whatever IP the remote already
// used to reach us as the IP address it should continue to use, but of course it's not
// that easy.
ConfigManager.LockConfig(lConfig);
try
aAddress := lConfig.ExternalAddress;
if aAddress <> '' then
Exit(True);
aAddress := lConfig.ListenIP;
if aAddress <> '' then
Exit(True);
finally
ConfigManager.UnlockConfig(lConfig);
end;
// Normally we would respond to UPnP or discovery requests with the
// local IP that we received the request on, because we know for sure
// the remote host is able to reach us on that.
// However, if the request is coming from an app on the same computer
// as this app, we may get multiple requests from them on different local
// IPs. To avoid confusing the app, we must respond with the same
// BaseURL to all of those requests. e.g. NextPVR gets confused if it receives
// different discovery responses with the same DeviceID, but different
// BaseURLs.
// We can detect if the request is coming from the same computer as this
// app by checking if the ARequestLocalIP is found in the local address list.
lAddresses := TSocketUtils.GetLocalIPs;
if (lAddresses.IndexOf(ARequestLocalIP) > -1) then
begin
if ProxyServiceModule.Client.EnabledTunerCount = 0 then
Exit(False);
// If it's a USB/PCI device, exclude the local ip that was created by
// the Ceton device
// TODO: This is not a good assumption to make if it's a USB/PCI device
// and set to bridged mode. If there is only one local IP address,
// don't remove it.
lModel := ProxyServiceModule.Client.Model;
if (lModel <> TCetonModel.Ethernet) and (Length(lAddresses) > 1) then
lAddresses := lAddresses.Remove(ProxyServiceModule.Client.ListenIP);
// Choose a local IP that we should respond to
aAddress := lAddresses.LowestMetric(4).IP;
// Return whether the local ip that the request came in on is equal to that chosen IP
Result := SameText(aRequestLocalIP, aAddress);
end
else
begin
aAddress := aRequestLocalIP;
Result := aAddress <> '';
end;
end;
procedure TProxyServerModule.ControlTCPConnect(aContext: TIdContext);
begin
TLogger.Log(cLogDefault, 'Control connect');
end;
procedure TProxyServerModule.ControlTCPExecute(aContext: TIdContext);
begin
TLogger.Log(cLogDefault, 'Control execute');
end;
{Description: Ceton InfiniTV MOCUR (00-00-22-00-00-XX-XX-XX)
DeviceType: urn:schemas-cetoncorp-com:device:SecureContainer:1
FriendlyName: Ceton InfiniTV Ethernet (00-XX-XX-XX)
Manufacturer: Ceton Corporation
ManufacturerUrl: http://www.cetoncorp.com/
ModelName: Ceton InfiniTV MOCUR (00-00-22-00-00-XX-XX-XX)
ModelNumber: 0.0.0.9
PresentationUrl: http://192.168.1.132/Services/System.html
SerialNumber: 00-00-22-00-00-XX-XX-XX
UDN: uuid:XXX-XXX-XXX
}
{Process cetonproxy.exe (25300)
Debug Output:
[2020-03-08 14:29:28.305] Received NOTIFY from 192.168.1.132 on 192.168.1.8: NOTIFY * HTTP/1.1
HOST: 239.255.255.250:1900
CACHE-CONTROL: max-age=1800
LOCATION: http://192.168.1.132/description.xml
NT: uuid:89333102-EBE5-11D8-AC9A-000008098100
NTS: ssdp:alive
SERVER: Linux/3.0.1+, UPnP/1.0
USN: uuid:XXX-XXX-XXX
}
{Process cetonproxy.exe (25300)
Debug Output:
[2020-03-08 14:29:28.302] Received NOTIFY from 192.168.1.132 on 192.168.1.8: NOTIFY * HTTP/1.1
HOST: 239.255.255.250:1900
CACHE-CONTROL: max-age=1800
LOCATION: http://192.168.1.132/description.xml
NT: upnp:rootdevice
NTS: ssdp:alive
SERVER: Linux/3.0.1+, UPnP/1.0
USN: uuid:XXX-XXX-XXX::upnp:rootdevice
}
{Debug Output:
[2020-03-08 14:29:28.309] Received NOTIFY from 192.168.1.132 on 192.168.1.116: NOTIFY * HTTP/1.1
HOST: 239.255.255.250:1900
CACHE-CONTROL: max-age=1800
LOCATION: http://192.168.1.132/description.xml
NT: upnp:rootdevice
NTS: ssdp:alive
SERVER: Linux/3.0.1+, UPnP/1.0
USN: uuid:XXX-XXX-XXX::upnp:rootdevice
}
{[2020-03-08 14:29:28.313] Received NOTIFY from 192.168.1.132 on 192.168.1.8: NOTIFY * HTTP/1.1
HOST: 239.255.255.250:1900
CACHE-CONTROL: max-age=1800
LOCATION: http://192.168.1.132/description.xml
NT: urn:schemas-cetoncorp-com:device:SecureContainer:1
NTS: ssdp:alive
SERVER: Linux/3.0.1+, UPnP/1.0
USN: uuid:XXX-XXX-XXX::urn:schemas-cetoncorp-com:device:SecureContainer:1
}
{Debug Output:
[2020-03-08 14:51:25.922] Received M-SEARCH from 192.168.1.10 on 192.168.1.8: M-SEARCH * HTTP/1.1
Host: 239.255.255.250:1900
ST: urn:schemas-cetoncorp-com:device:SecureContainer:1
Man: "ssdp:discover"
MX: 3
}
{Debug Output:
[2020-03-08 14:51:26.021] Received M-SEARCH from 192.168.1.10 on 192.168.1.8: M-SEARCH * HTTP/1.1
Host: 239.255.255.250:1900
ST: uuid:89333102-EBE5-11D8-AC9A-000008085F10
Man: "ssdp:discover"
MX: 3
}
function TProxyServerModule.CreateSSDPDiscoverPacket: String;
const
cSSDPDiscover =
'M-SEARCH * HTTP/1.1'#13#10+
'Host: 239.255.255.250:1900'#13#10+
// 'ST: ssdp:all'#13#10+
'ST: urn:schemas-cetoncorp-com:device:SecureContainer:1'#13#10+
// 'ST: upnp:rootdevice'#13#10+
'Man: "ssdp:discover"'#13#10+
'MX: 3'#13#10#13#10;
begin
Result := Format(cSSDPDiscover, []);
end;
procedure TProxyServerModule.SSDPClientRead(Sender: TObject;
const AData: TIdBytes; ABinding: TIdSocketHandle);
var
lData: UTF8String;
lPacket: String;
lValues: TStringList;
lLocation: String;
begin
if Length(AData) > 0 then
begin
SetLength(lData, Length(AData));
Move(AData[0], lData[Low(lData)], Length(AData));
// TODO: Make more robust
if String(lData).StartsWith('M-SEARCH', True) then
begin
if String(lData).Contains('ST: upnp:rootdevice') then
begin
TLogger.LogFmt(cLogDiscovery, 'Received M-SEARCH for rootdevice from %s on %s', [ABinding.PeerIP, ABinding.IP]);
if TryCreateSSDPResponsePacket(ABinding.IP, lPacket) then
fDiscoveryServer.Send(ABinding.PeerIP, ABinding.PeerPort, lPacket);
end;
end
else if String(lData).StartsWith('NOTIFY', True) then
begin
if String(lData).ToLower.Contains('cetoncorp') then
begin
TLogger.LogFmt(cLogDiscovery, 'Received NOTIFY from ceton device %s on %s: %s', [ABinding.PeerIP, ABinding.IP, String(lData)]);
lValues := TStringList.Create;
try
lValues.CaseSensitive := False;
lValues.NameValueSeparator := ':';
lValues.Text := String(lData);
lLocation := Trim(lValues.Values['LOCATION']);
if lLocation <> '' then
ProxyServiceModule.CetonDeviceDiscovered(ABinding.PeerIP, lLocation);
finally
lValues.Free;
end;
end;
end;
end;
end;
// Received SSDP data from 192.168.1.43: NOTIFY * HTTP/1.1
// Host: 239.255.255.250:1900
// NT: upnp:rootdevice
// NTS: ssdp:alive
// Server: HDHomeRun/1.0 UPnP/1.0
// Location: http://192.168.1.43:80/dri/device.xml
// Cache-Control: max-age=1800
// USN: uuid:473366D2-A765-3D61-B466-XXXXX::upnp:rootdevice
function TProxyServerModule.TryCreateSSDPAlivePacket(const aRequestHost: String; out aPacket: String): Boolean;
const
cSSDPAlive =
'NOTIFY * HTTP/1.1'#13#10+
'Host: 239.255.255.250:1900'#13#10+
'NT: upnp:rootdevice'#13#10+
'NTS: ssdp:alive'#13#10+
'Server: HDHomeRun/1.0 UPnP/1.0'#13#10+
'Location: http://%s:%d/device.xml'#13#10+
'Cache-Control: max-age=1800'#13#10+
'USN: uuid:%s::upnp:rootdevice'#13#10#13#10;
var
lAddress: String;
lPort: Integer;
lDeviceUUID: String;
lConfig: TServiceConfig;
begin
if TryGetAddress(ARequestHost, lAddress) then
begin
ConfigManager.LockConfig(lConfig);
try
lPort := lConfig.ExternalHTTPPort;
lDeviceUUID := lConfig.DeviceUUID;
finally
ConfigManager.UnlockConfig(lConfig);
end;
aPacket := Format(cSSDPAlive, [lAddress, lPort, lDeviceUUID]);
Result := True;
end
else
Result := False;
end;
procedure TProxyServerModule.Log(const aLogName: String; const aMessage: String);
begin
// Nothing
end;
procedure TProxyServerModule.DiscoverCetonDevices;
var
i: Integer;
begin
if fSSDPServer.Active then
begin
ProxyServiceModule.DiscoveredCetonDeviceList.DiscoveryStarted;
for i := 0 to fSSDPServer.Bindings.Count-1 do
fSSDPServer.Bindings[i].SendTo(SSDP_MULTICAST_GROUP, SSDP_PORT, CreateSSDPDiscoverPacket);
end;
end;
{Debug Output:
[2020-03-08 21:09:18.964] Ceton Incoming from 192.168.1.116: HTTP/1.1 200 OK
CACHE-CONTROL: max-age=300
DATE: Thu, 15 Jan 1970 03:19:09 GMT
EXT:
LOCATION: http://192.168.1.132/description.xml
SERVER: Linux/3.0.1+, UPnP/1.0
ST: urn:schemas-cetoncorp-com:device:SecureContainer:1
USN: uuid:XXX-XXX-XXX::urn:schemas-cetoncorp-com:device:SecureContainer:1}
procedure TProxyServerModule.SSDPServerRead(AThread: TIdUDPListenerThread; const AData: TIdBytes; ABinding: TIdSocketHandle);
var
lData: UTF8String;
lValues: TStringList;
lLocation: String;
begin
if Length(AData) > 0 then
begin
SetLength(lData, Length(AData));
Move(AData[0], lData[Low(lData)], Length(AData));
TLogger.LogFmt(cLogDiscovery, 'Received SSDP discovery response from %s on %s: %s', [ABinding.PeerIP, ABinding.IP, String(lData)]);
lValues := TStringList.Create;
try
lValues.CaseSensitive := False;
lValues.NameValueSeparator := ':';
lValues.Text := String(lData);
if Trim(lValues.Values['ST']).ToLower.Contains('cetoncorp') then
begin
lLocation := Trim(lValues.Values['LOCATION']);
ProxyServiceModule.CetonDeviceDiscovered(ABinding.PeerIP, lLocation);
end;
finally
lValues.Free;
end;
end;
end;
procedure TProxyServerModule.DiscoveredCetonDevicesChanged;
begin
// Nothing
end;
function DateTimeToRFC1123(aDate: TDateTime): string;
const
StrWeekDay: string = 'MonTueWedThuFriSatSun';
StrMonth: string = 'JanFebMarAprMayJunJulAugSepOctNovDec';
var
Year, Month, Day: Word;
Hour, Min, Sec, MSec: Word;
DayOfWeek: Word;
begin
DecodeDate(aDate, Year, Month, Day);
DecodeTime(aDate, Hour, Min, Sec, MSec);
DayOfWeek := ((Trunc(aDate) - 2) mod 7);
Result := Copy(StrWeekDay, 1 + DayOfWeek * 3, 3) + ', ' +
Format('%2.2d %s %4.4d %2.2d:%2.2d:%2.2d',
[Day, Copy(StrMonth, 1 + 3 * (Month - 1), 3),
Year, Hour, Min, Sec]);
end;
{
HTTP/1.1 200 OK
Server: HDHomeRun/1.0 UPnP/1.0
ST: upnp:rootdevice
Location: http://192.168.1.43:80/dri/device.xml
Cache-Control: max-age=1800
USN: uuid:473366D2-A765-3D61-B466-E73B254C632B::upnp:rootdevice
Ext:
Content-Length: 0
Date: Wed, 11 Mar 2020 21:34:52 GMT
}
function TProxyServerModule.TryCreateSSDPResponsePacket(
const aRequestHost: String; out aPacket: String): Boolean;
const
cSSDPResponse =
'Server: HDHomeRun/1.0 UPnP/1.0'#13#10+
'ST: upnp:rootdevice'#13#10+
'Location: http://%s:%d/device.xml'#13#10+
'Cache-Control: max-age=%d'#13#10+
'USN: uuid:%s::upnp:rootdevice'#13#10+
'Ext:'#13#10+
'Content-Length: 0'#13#10+
'Date: %s'#13#10#13#10;
var
lAddress: String;
lPort: Integer;
lDeviceUUID: String;
lConfig: TServiceConfig;
begin
if TryGetAddress(ARequestHost, lAddress) then
begin
ConfigManager.LockConfig(lConfig);
try
lPort := lConfig.ExternalHTTPPort;
lDeviceUUID := lConfig.DeviceUUID;
finally
ConfigManager.UnlockConfig(lConfig);
end;
aPacket := Format(cSSDPResponse, [lAddress, lPort, cSSDPAliveIntervalSec+30, lDeviceUUID, DateTimeToRFC1123(TTimeZone.Local.ToUniversalTime(Now))+' GMT']);
Result := True;
end
else
Result := False;
end;
procedure TProxyServerModule.LogError(const aLogName, aMessage: String);
begin
// Nothing
end;
end.