-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleHttpServer.iss
More file actions
220 lines (191 loc) · 7.04 KB
/
SimpleHttpServer.iss
File metadata and controls
220 lines (191 loc) · 7.04 KB
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
#define MyAppName "SimpleHttpServer"
#define MyAppPublisher "Enycs"
#define MyAppURL "http://www.enycs.com/"
#define MyAppExeName "SimpleHttpServer.exe"
#define MyAppPath "dist\" + MyAppExeName
#define MyAppVersion GetVersionNumbersString(MyAppPath)
#define MyWebFolder "public"
#define MyLogFolder "log"
#define MyAppDescription "Simple Http Server"
#define MyServiceName "SimpleHttpService"
[Languages]
Name: "italian"; MessagesFile: "compiler:Languages\Italian.isl"
[Setup]
AppId={{A07D07FA-5F30-4604-B8CE-D319E3FB1A8E}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
WizardStyle=modern
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={pf64}\{#MyAppPublisher}\{#MyAppName}
DefaultGroupName={#MyAppPublisher}\{#MyAppName}
Compression=lzma2
SolidCompression=yes
VersionInfoVersion={#MyAppVersion}
VersionInfoCompany={#MyAppPublisher}
VersionInfoDescription={#MyAppDescription}
OutputBaseFilename={#MyAppName}-Setup_{#MyAppVersion}
OutputDir=dist
PrivilegesRequired=poweruser
[Files]
Source: "dist/{#MyAppExeName}"; DestDir: "{app}"; DestName: "{#MyAppExeName}"; Flags: replacesameversion
Source: "config.ini"; DestDir: "{app}"
Source: "{#MyWebFolder}/sdc.xml"; DestDir: "{app}/{#MyWebFolder}"
Source: "Enycs_512.ico"; DestDir: "{app}"
;[UninstallRun]
;Filename: "{app}\{#MyAppExeName}"; Parameters: "stop"; Flags: waituntilterminated
;Filename: "{app}\{#MyAppExeName}"; Parameters: "remove"; Flags: waituntilterminated
[Run]
Filename: "{app}\{#MyAppExeName}"; Parameters: "--startup delayed install"; Flags: runascurrentuser waituntilterminated; Description: "Installa il servizio in autoavvio"; Tasks: AvvioAutomatico
Filename: "{app}\{#MyAppExeName}"; Parameters: "start"; Flags: runascurrentuser waituntilterminated; Description: "Avvia ora il servizio"; Tasks: AvvioAutomatico
Filename: "{app}\{#MyAppExeName}"; Parameters: "install"; Flags: runascurrentuser waituntilterminated; Description: "Installa il servizio in autoavvio"; Tasks: AvvioManuale
[Icons]
Name: "{group}\config.ini"; Filename: "{app}\config.ini"
Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}"
Name: "{group}\Start {#MyAppDescription}"; Filename: "{app}\{#MyAppExeName}"; Parameters: "start"; IconFilename: "{app}\Enycs_512.ico"; AfterInstall: SetElevationBit('{group}\Start {#MyAppDescription}.lnk')
Name: "{group}\Stop {#MyAppDescription}"; Filename: "{app}\{#MyAppExeName}"; Parameters: "stop"; IconFilename: "{app}\Enycs_512.ico"; AfterInstall: SetElevationBit('{group}\Stop {#MyAppDescription}.lnk')
[UninstallDelete]
Type: filesandordirs; Name: "{app}"
Type: dirifempty; Name: "{#MyAppPublisher}"
[Tasks]
Name: "AvvioManuale"; Description: "Il servizio dovr� essere avviato manualmente"; Flags: exclusive
Name: "AvvioAutomatico"; Description: "Il servizio verr� avviato automaticamente al boot"; Flags: exclusive unchecked
[Code]
const
ServiceName = '{#MyServiceName}';
NET_FW_SCOPE_ALL = 0;
NET_FW_IP_VERSION_ANY = 2;
procedure SetFirewallException(AppName,FileName:string);
var
FirewallObject: Variant;
FirewallManager: Variant;
FirewallProfile: Variant;
begin
try
FirewallObject := CreateOleObject('HNetCfg.FwAuthorizedApplication');
FirewallObject.ProcessImageFileName := FileName;
FirewallObject.Name := AppName;
FirewallObject.Scope := NET_FW_SCOPE_ALL;
FirewallObject.IpVersion := NET_FW_IP_VERSION_ANY;
FirewallObject.Enabled := True;
FirewallManager := CreateOleObject('HNetCfg.FwMgr');
FirewallProfile := FirewallManager.LocalPolicy.CurrentProfile;
FirewallProfile.AuthorizedApplications.Add(FirewallObject);
except
end;
end;
procedure RemoveFirewallException( FileName:string );
var
FirewallManager: Variant;
FirewallProfile: Variant;
begin
try
FirewallManager := CreateOleObject('HNetCfg.FwMgr');
FirewallProfile := FirewallManager.LocalPolicy.CurrentProfile;
FireWallProfile.AuthorizedApplications.Remove(FileName);
except
end;
end;
function ExecAndCapture(const CmdLine: string; var Output: TExecOutput): Boolean;
var
ResultCode: Integer;
begin
Result := ExecAndCaptureOutput('cmd.exe', '/C ' + CmdLine, '', SW_HIDE, ewWaitUntilTerminated, ResultCode, Output);
end;
function IsServiceStopped(): Boolean;
var
Output: TExecOutput;
begin
Result := False;
if ExecAndCapture('sc query "' + ServiceName + '"', Output) then
Result := (Pos('STOPPED', StringJoin(' ', Output.StdOut)) > 0 );
end;
function WaitForServiceToStop(MaxSeconds: Integer): Boolean;
var
i: Integer;
begin
for i := 0 to MaxSeconds do
begin
if IsServiceStopped() then
begin
Result := True;
Exit;
end;
Sleep(1000);
end;
Result := False;
end;
function IsServiceDeleted(): Boolean;
var
ResultCode: Integer;
begin
Result := False;
// Esegui il comando e cattura il codice di ritorno
// Non � necessario catturare l'output testuale, ma il formato della funzione lo richiede
Exec('sc.exe', 'query "' + ServiceName + '"', '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
// Il servizio non esiste se il codice di errore � 1060
if ResultCode = 1060 then
begin
Result := True;
end;
end;
function StopAndDeleteService(): Boolean;
var
ResultCode: Integer;
Success: Boolean;
begin
Result := True
if not IsServiceDeleted() then
begin
// Prova a fermare il servizio
Exec('sc.exe', 'stop "' + ServiceName + '"', '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
// Attendi che sia fermo (max 20 secondi)
Success := WaitForServiceToStop(20);
// Prova a eliminarlo
Success := Exec('sc.exe', 'delete "' + ServiceName + '"', '', SW_HIDE, ewWaitUntilTerminated, ResultCode) and Success;
// Verifica che sia stato rimosso
Success := IsServiceDeleted() and Success;
Result := Success;
end;
end;
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
if CurUninstallStep = usUninstall then
begin
if not StopAndDeleteService() then
MsgBox('Impossibile fermare o rimuovere il servizio "' + ServiceName + '". Assicurati di avere i permessi necessari.', mbError, MB_OK);
RemoveFirewallException(ExpandConstant('{app}\')+'{#MyAppExeName}');
end;
end;
procedure CurStepChanged(CurStep: TSetupStep);
begin
if CurStep = ssInstall then
begin
if not StopAndDeleteService() then
MsgBox('Impossibile fermare il servizio. Assicurati di avere i permessi necessari.', mbError, MB_OK);
end;
if CurStep=ssPostInstall then
SetFirewallException('{#MyAppName}', ExpandConstant('{app}\')+'{#MyAppExeName}');
end;
// SetElevationBit procedure link https://stackoverflow.com/a/44082068/1145281
procedure SetElevationBit(Filename: string);
var
Buffer: string;
Stream: TStream;
begin
Filename := ExpandConstant(Filename);
Log('Setting elevation bit for ' + Filename);
Stream := TFileStream.Create(FileName, fmOpenReadWrite);
try
Stream.Seek(21, soFromBeginning);
SetLength(Buffer, 1);
Stream.ReadBuffer(Buffer, 1);
Buffer[1] := Chr(Ord(Buffer[1]) or $20);
Stream.Seek(-1, soFromCurrent);
Stream.WriteBuffer(Buffer, 1);
finally
Stream.Free;
end;
end;