/
main.go
146 lines (122 loc) · 3.18 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
package main
import (
"crypto/tls"
"encoding/json"
"encoding/pem"
"flag"
"fmt"
"io/ioutil"
_ "net/http/pprof"
"os"
"os/exec"
"os/signal"
"runtime"
"runtime/pprof"
"github.com/nccgroup/tracy/api/rest"
"github.com/nccgroup/tracy/api/store"
"github.com/nccgroup/tracy/configure"
"github.com/nccgroup/tracy/log"
"github.com/nccgroup/tracy/proxy"
)
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
func main() {
if *cpuprofile != "" {
defer pprof.StopCPUProfile()
}
go func() {
log.Error.Fatal(rest.Server.ListenAndServe())
}()
fmt.Printf("Tracer server:\t%s%s",
configure.Current.TracyServer.Addr(), log.NewLine)
if configure.Current.AutoLaunch {
processAutoLaunch()
}
// Wait for the user to close the program.
signalChan := make(chan os.Signal, 1)
cleanupDone := make(chan bool)
signal.Notify(signalChan, os.Interrupt)
go func() {
for range signalChan {
fmt.Println("Ctrl+C pressed. Shutting down...")
store.DB.Close()
cleanupDone <- true
}
}()
<-cleanupDone
}
func init() {
// Parse the flags. Have to parse them hear since other package
// initialize command line.
flag.Parse()
// Set up the logging based on the user command line flags.
log.Configure()
// Set up the configuration.
configure.Setup()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Error.Fatal(err)
}
pprof.StartCPUProfile(f)
}
// Open the database.
if err := store.Open(configure.Current.DatabasePath, log.Verbose); err != nil {
log.Error.Fatal(err.Error())
}
// Initialize the HTTP routes.
rest.Configure()
// Instantiate the certificate cache.
certsJSON, err := ioutil.ReadFile(configure.Current.CertCachePath)
if err != nil {
certsJSON = []byte("[]")
// Can recover from this. Simply make a cache file and
// instantiate an empty cache.
ioutil.WriteFile(configure.Current.CertCachePath, certsJSON, os.ModePerm)
}
var certs []proxy.CertCacheEntry
if err := json.Unmarshal(certsJSON, &certs); err != nil {
log.Error.Print(err)
return
}
cache := make(map[string]tls.Certificate)
for _, cert := range certs {
keyPEM := cert.Certs.KeyPEM
certPEM := cert.Certs.CertPEM
cachedCert, err := tls.X509KeyPair(
pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: certPEM}),
pem.EncodeToMemory(&pem.Block{
Type: "EC PRIVATE KEY",
Bytes: keyPEM}))
if err != nil {
log.Error.Println(err)
continue
}
cache[cert.Host] = cachedCert
}
proxy.SetCertCache(cache)
configure.Certificates()
}
// processAutoLaunch launchs whatever browser they have configured.
func processAutoLaunch() {
openbrowser(fmt.Sprintf("%s", configure.Current.TracyServer.Addr()))
}
// openBrowser opens the default browser the user has configured.
// Taken from here https://gist.github.com/hyg/9c4afcd91fe24316cbf0
func openbrowser(url string) {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
log.Error.Print(err)
}
}