-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadm.go
271 lines (227 loc) · 5.4 KB
/
adm.go
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
// Froxy - HTTP over SSH proxy
//
// Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
// See LICENSE for license terms and conditions
//
// Froxy administration (install/uninstall etc)
package main
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"unicode"
"github.com/alexpevzner/froxy/internal/pages"
"github.com/alexpevzner/froxy/internal/sysdep"
)
// Adm represents Environment for Froxy administration
// actions (install/uninstall etc)
type Adm struct {
*Env // Environment
port int // -p port
OsExecutable string // Path to executable file
FroxyIsRunning bool // Froxy is running
}
// NewAdm creates new administrative environment
func NewAdm(env *Env, port int) (*Adm, error) {
// ----- Create Adm structure -----
adm := &Adm{Env: env, port: port}
exe, err := os.Executable()
if err != nil {
return nil, err
}
adm.OsExecutable = exe
// ----- Attempt to acquire froxy.lock -----
err = adm.FroxyLockAcquire()
if err != nil && err != ErrFroxyRunning {
return nil, err
}
adm.FroxyIsRunning = err != nil
return adm, nil
}
// Install performs Froxy installation
func (adm *Adm) Install(flags OptFlags) error {
// Kill Froxy if it is running
err := adm.Kill()
if err != nil {
return err
}
// Fetch icon from resources
_, path := filepath.Split(adm.PathUserIconFile)
path = "icons/" + path
var icon []byte
iconfile, err := pages.AssetFS.Open(path)
if err == nil {
icon, err = ioutil.ReadAll(iconfile)
iconfile.Close()
}
if err != nil {
return fmt.Errorf("Resource %q: %s", path, err)
}
// Save icon to disk
err = ioutil.WriteFile(adm.PathUserIconFile, icon, 0644)
if err != nil {
return err
}
// Create desktop entry
if !flags.Test(OptFlgNoShortcut) {
err = sysdep.CreateDesktopShortcut(
adm.PathUserDesktopFile,
adm.OsExecutable,
"-open",
adm.PathUserIconFile,
PROGRAM_ICON_NAME,
"Open Froxy configuration page in a web browser",
false,
)
}
if !flags.Test(OptFlgNoAutostart) && err == nil {
err = sysdep.CreateDesktopShortcut(
adm.PathUserStartupFile,
adm.OsExecutable,
"-r",
adm.PathUserIconFile,
PROGRAM_NAME,
"Start Froxy service",
true,
)
}
// Run the program
if !flags.Test(OptFlgNoRun) && err == nil {
err = adm.Run()
}
// Undo changes in a case of errors
if err != nil {
adm.Uninstall()
}
return err
}
// Uninstall performs Froxy uninstallation
func (adm *Adm) Uninstall() error {
// Kill Froxy if it is running
err := adm.Kill()
if err != nil {
return err
}
// Remove installed files
os.Remove(adm.PathUserDesktopFile)
os.Remove(adm.PathUserStartupFile)
os.Remove(adm.PathUserIconFile)
return nil
}
// Run starts Froxy in background
func (adm *Adm) Run() error {
if adm.FroxyIsRunning {
return ErrFroxyRunning
}
adm.FroxyLockRelease()
// Create stdout/stderr pipes
rstdout, wstdout, err := os.Pipe()
if err != nil {
return err
}
rstderr, wstderr, err := os.Pipe()
if err != nil {
return err
}
devnull, err := os.Open(os.DevNull)
if err != nil {
return err
}
// Initialize process attributes
attr := sysdep.ProcAttrBackground()
attr.Files = []*os.File{devnull, wstdout, wstderr}
// Initialize process arguments
argv := []string{
adm.OsExecutable,
"-p", fmt.Sprintf("%d", adm.port),
"-fg",
}
// Force CGO resolver
os.Setenv("GODEBUG", "netdns=cgo")
// Start new process
proc, err := os.StartProcess(adm.OsExecutable, argv, attr)
if err != nil {
return err
}
// Collect its initialization output
wstdout.Close()
wstderr.Close()
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
io.Copy(stdout, rstdout)
io.Copy(stderr, rstderr)
if stdout.Len() != 0 {
os.Stdout.Write(stdout.Bytes())
}
// Check for an error
if stderr.Len() > 0 {
s := strings.TrimFunc(stderr.String(), unicode.IsSpace)
proc.Kill() // Just in case
return errors.New(s)
}
proc.Release()
return nil
}
// Kill terminates running Froxy
func (adm *Adm) Kill() error {
// Froxy not running? Perfect, nothing to do
if !adm.FroxyIsRunning {
return nil
}
// Create shutdown request
url := fmt.Sprintf("http://localhost:%d", adm.GetPort())
url += "/api/shutdown"
rq, err := http.NewRequest("FROXY", url, nil)
if err != nil {
return err
}
// Send request and wait until connection is closed
// Don't worry about errors too much here -- if Froxy
// leave, we will get an error but its not a problem
rsp, err := http.DefaultClient.Do(rq)
if err == nil {
io.Copy(ioutil.Discard, rsp.Body)
rsp.Body.Close()
}
// And reacquire the froxy.lock
//
// FIXME
//
// Sometimes exiting Froxy closes the connection, but
// still continues to hold a run lock. It needs a further
// investigation. Looks like Linux doesn't atomically release
// resources held by an exiting process
//
// We will try to fix it later, but for now we have a busy-wait
// as a temporary workaround
for i := 0; i < 20; i++ {
err = adm.FroxyLockAcquire()
if err != ErrFroxyRunning {
break
}
time.Sleep(time.Millisecond * 50)
}
if err == ErrFroxyRunning {
err = ErrCantKillFroxy
}
if err == nil {
adm.FroxyIsRunning = false
}
return err
}
// Open opens Froxy configuration window in the default web brauser
func (adm *Adm) Open() error {
err := adm.Run()
if err != nil && err != ErrFroxyRunning {
return err
}
url := fmt.Sprintf("http://localhost:%d", adm.GetPort())
return sysdep.OpenURL(url)
}