forked from imperviousinc/fingertip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
355 lines (286 loc) · 7.32 KB
/
main.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
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
package main
import (
"errors"
"fingertip/internal/config"
"fingertip/internal/config/auto"
"fingertip/internal/resolvers"
"fingertip/internal/ui"
"fmt"
"log"
"net/http"
"os"
"path"
"path/filepath"
"time"
"github.com/emersion/go-autostart"
"github.com/pkg/browser"
"github.com/randomlogin/sane"
"github.com/randomlogin/sane/debuglog"
"github.com/randomlogin/sane/resolver"
"github.com/randomlogin/sane/sync"
)
const Version = "0.0.3"
type App struct {
server *http.Server
config *config.App
usrConfig *config.User
proxyURL string
autostart *autostart.App
autostartEnabled bool
}
var (
appPath string
fileLogger *log.Logger
fileLoggerHandle *os.File
)
func setupApp() *App {
c, err := config.NewConfig()
if err != nil {
log.Fatal(err)
}
c.Version = Version
app, err := NewApp(c)
if err != nil {
log.Fatal(err)
}
return app
}
func onBoardingSeen(name string) bool {
if _, err := os.Stat(name); err == nil {
return true
}
return false
}
func autoConfigure(app *App, checked, onBoarded bool) bool {
// TODO: delete once linux is supported
if !auto.Supported() {
if !onBoarded {
browser.OpenURL(app.proxyURL + "/setup")
}
return false
}
autoURL := app.proxyURL + "/proxy.pac"
if checked {
confirm := ui.ShowYesNoDlg("Remove Fingertip configuration settings?")
if confirm {
auto.UninstallAutoProxy(autoURL)
auto.UndoFirefoxConfiguration()
_ = auto.UninstallCert(app.config.CertPath)
return false
}
return checked
}
confirm := ui.ShowYesNoDlg("Would you like to automatically configure Fingertip?")
if !confirm {
// if this is the first time show
// manual setup instructions instead
if !onBoarded {
browser.OpenURL(app.proxyURL + "/setup")
}
return false
}
if err := auto.InstallAutoProxy(autoURL); err != nil {
ui.ShowErrorDlg(err.Error())
return false
}
_ = auto.ConfigureFirefox()
if err := auto.InstallCert(app.config.CertPath); err != nil {
// revert proxy settings
auto.UninstallAutoProxy(autoURL)
auto.UndoFirefoxConfiguration()
ui.ShowErrorDlg(err.Error())
return false
}
// Enable open at login
if !ui.Data.OpenAtLogin() {
enable := ui.OnAutostart(false)
ui.Data.SetOpenAtLogin(enable)
}
if time.Since(app.config.Debug.GetLastPing()) > 5*time.Second {
browser.OpenURL(app.proxyURL)
}
return true
}
func main() {
if os.Getenv("VERBOSE") != "" {
debuglog.Logger.Verbose = true
}
var err error
app := setupApp()
if fileLoggerHandle, err = os.OpenFile(path.Join(app.config.Path, "fingertip.logs"),
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err != nil {
log.Fatal(err)
}
defer fileLoggerHandle.Close()
fileLogger = log.New(fileLoggerHandle, "", log.LstdFlags|log.Lshortfile)
appPath, err = os.Executable()
if err != nil {
log.Fatalf("error reading app path: %v", err)
}
app.autostart.Exec = []string{appPath}
if app.autostart.IsEnabled() {
app.autostartEnabled = true
}
serverErrCh := make(chan error)
onBoardingFilename := path.Join(app.config.Path, "init")
onBoarded := onBoardingSeen(onBoardingFilename)
hnsdPath, err := getProcPath()
if err != nil {
log.Fatal(err)
}
hnsdPath = path.Join(hnsdPath, "/hnsd")
start := func() {
ui.Data.SetOptionsEnabled(true)
ui.Data.SetStarted(true)
go func() {
serverErrCh <- app.listen()
}()
go func() {
if onBoarded {
return
}
autoConf := autoConfigure(app, false, false)
ui.Data.SetAutoConfig(autoConf)
app.config.Store.AutoConfig = autoConf
go app.config.Store.Save()
onBoarded = true
}()
app.config.Debug.SetCheckSynced(func() bool { return false }) //perhaps not the best way to show syncing status correctly
sync.GetRoots(hnsdPath, app.config.Path, app.config.Path)
go func() {
for {
time.Sleep(24 * time.Hour)
sync.GetRoots(hnsdPath, app.config.Path, app.config.Path)
}
}()
app.config.Debug.SetCheckSynced(func() bool { return true })
}
ui.OnStart = start
ui.OnConfigureOS = func(checked bool) bool {
res := autoConfigure(app, checked, onBoarded)
app.config.Store.AutoConfig = res
go app.config.Store.Save()
return res
}
ui.OnOpenHelp = func() {
browser.OpenURL(app.proxyURL)
}
ui.OnAutostart = func(checked bool) bool {
if checked {
if err := app.autostart.Disable(); err != nil {
ui.ShowErrorDlg(fmt.Sprintf("error disabling open at login: %v", err))
return checked
}
return false
}
if err = app.autostart.Enable(); err != nil {
ui.ShowErrorDlg(fmt.Sprintf("error enabling open at login: %v", err))
return false
}
return true
}
ui.OnStop = func() {
app.stop()
ui.Data.SetOptionsEnabled(false)
ui.Data.SetStarted(false)
}
ui.OnReady = func() {
ui.Data.SetAutoConfigEnabled(auto.Supported())
ui.Data.SetOptionsEnabled(false)
app.config.Debug.SetCheckCert(func() bool {
return auto.VerifyCert(app.config.CertPath) == nil
})
// update initial state
ui.Data.SetOpenAtLogin(app.autostartEnabled || ui.Data.OpenAtLogin())
autoConfig := auto.Supported() &&
app.config.Store.AutoConfig
ui.Data.SetAutoConfig(autoConfig)
// start fingertip
start()
}
ui.OnExit = func() {
if fileLoggerHandle != nil {
fileLoggerHandle.Close()
}
app.stop()
}
ui.Loop()
}
func NewApp(appConfig *config.App) (*App, error) {
var err error
app := &App{
autostart: &autostart.App{
Name: config.AppId,
DisplayName: config.AppName,
Icon: "",
},
}
app.config = appConfig
usrConfig, err := config.ReadUserConfig(appConfig.Path)
if err != nil && !errors.Is(err, config.ErrUserConfigNotFound) {
return nil, err
}
app.proxyURL = config.GetProxyURL(usrConfig.ProxyAddr)
app.usrConfig = &usrConfig
app.server, err = app.newProxyServer()
if err != nil {
return nil, err
}
return app, nil
}
func (a *App) NewResolver() (resolver.Resolver, error) {
rs, err := resolver.NewStub(a.usrConfig.RecursiveAddr)
if err != nil {
return nil, err
}
hip5 := resolvers.NewHIP5Resolver(rs, a.usrConfig.RootAddr, func() bool { return true })
ethExt, err := resolvers.NewEthereum(a.usrConfig.EthereumEndpoint)
if err != nil {
return nil, err
}
// Register HIP-5 handlers
hip5.RegisterHandler("_eth", ethExt.Handler)
hip5.SetQueryMiddleware(a.config.Debug.GetDNSProbeMiddleware())
return hip5, nil
}
func (a *App) listen() error {
return a.server.ListenAndServe()
}
func (a *App) stop() {
a.server.Close()
// on stop create a new server
// to reset any state like old cache ... etc.
var err error
if a.server, err = a.newProxyServer(); err != nil {
log.Fatalf("app: error creating a new proxy server: %v", err)
}
}
func (a *App) newProxyServer() (*http.Server, error) {
var err error
// add a new resolver to the proxy config
if a.config.Proxy.Resolver, err = a.NewResolver(); err != nil {
return nil, err
}
// initialize a new handler
h, err := a.config.Proxy.NewHandler()
if err != nil {
return nil, err
}
// copy proxy address from user specified config
a.config.ProxyAddr = a.usrConfig.ProxyAddr
server := &http.Server{Addr: a.config.ProxyAddr, Handler: h}
return server, nil
}
func getProcPath() (string, error) {
exe, err := os.Executable()
if err != nil {
return "", err
}
exePath := filepath.Dir(exe)
return exePath, nil
}
func init() {
// in the footer on errors
// 0.6.1 is the version used in go.mod
sane.Version = fmt.Sprintf("fingertip (v%s)", Version)
}