RDPConf: Update firewall rule on port change (fix #86)

This commit is contained in:
binarymaster 2016-07-23 19:56:58 +03:00
parent 11be8c6833
commit 2d482b29be

View File

@ -1,5 +1,5 @@
{ {
Copyright 2015 Stas'M Corp. Copyright 2016 Stas'M Corp.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -61,6 +61,7 @@ type
{ Private declarations } { Private declarations }
public public
{ Public declarations } { Public declarations }
function ExecWait(Cmdline: String): Boolean;
procedure ReadSettings; procedure ReadSettings;
procedure WriteSettings; procedure WriteSettings;
end; end;
@ -89,6 +90,7 @@ var
Ready: Boolean = False; Ready: Boolean = False;
Arch: Byte; Arch: Byte;
OldWow64RedirectionValue: LongBool; OldWow64RedirectionValue: LongBool;
OldPort: Word;
INI: String; INI: String;
function WinStationEnumerateW(hServer: THandle; function WinStationEnumerateW(hServer: THandle;
@ -329,6 +331,29 @@ begin
Str.Free; Str.Free;
end; end;
function TMainForm.ExecWait(Cmdline: String): Boolean;
var
si: STARTUPINFO;
pi: PROCESS_INFORMATION;
begin
Result := False;
ZeroMemory(@si, sizeof(si));
si.cb := sizeof(si);
si.dwFlags := STARTF_USESHOWWINDOW;
si.wShowWindow := SW_HIDE;
UniqueString(Cmdline);
if not CreateProcess(nil, PWideChar(Cmdline), nil, nil, True, 0, nil, nil, si, pi) then begin
MessageBox(Handle,
PWideChar('CreateProcess error (code: ' + IntToStr(GetLastError) + ').'),
'Error', MB_ICONERROR or MB_OK);
Exit;
end;
CloseHandle(pi.hThread);
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
Result := True;
end;
procedure TMainForm.ReadSettings; procedure TMainForm.ReadSettings;
var var
Reg: TRegistry; Reg: TRegistry;
@ -356,6 +381,7 @@ begin
except except
end; end;
OldPort := seRDPPort.Value;
SecurityLayer := 0; SecurityLayer := 0;
UserAuthentication := 0; UserAuthentication := 0;
try try
@ -404,6 +430,11 @@ begin
Reg.WriteInteger('PortNumber', seRDPPort.Value); Reg.WriteInteger('PortNumber', seRDPPort.Value);
except except
end;
if OldPort <> seRDPPort.Value then
begin
OldPort := seRDPPort.Value;
ExecWait('netsh advfirewall firewall set rule name="Remote Desktop" new localport=' + IntToStr(OldPort));
end; end;
case rgNLA.ItemIndex of case rgNLA.ItemIndex of
0: begin 0: begin