forked from nccgroup/tracy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
183 lines (157 loc) · 3.98 KB
/
main.go
File metadata and controls
183 lines (157 loc) · 3.98 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
package main
import (
"crypto/tls"
"encoding/json"
"encoding/pem"
"flag"
"fmt"
"io/ioutil"
_ "net/http/pprof"
"os"
"os/exec"
"os/signal"
"runtime"
"runtime/pprof"
"strings"
"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()
}
// Open a TCP listener.
ln := configure.ProxyServer()
// Create the proxy
p := proxy.New(ln)
// Start the proxy that will run forever
go p.Accept()
ps, err := configure.ReadConfig("proxy-server")
if err != nil {
log.Error.Fatal(err)
}
fmt.Printf("Proxy server:\t%s%s", ps.(string), log.NewLine)
go func() {
log.Error.Fatal(rest.RestServer.ListenAndServe())
}()
ts, err := configure.ReadConfig("tracer-server")
if err != nil {
log.Error.Fatal(err)
}
fmt.Printf("Tracer server:\t%s%s", ts.(string), log.NewLine)
autoLaunch, err := configure.ReadConfig("auto-launch")
if err != nil {
log.Error.Fatal(err.Error())
}
processAutoLaunch(autoLaunch.(string))
// 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()
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.DatabaseFile, log.Verbose); err != nil {
log.Error.Fatal(err.Error())
}
// Initialize the HTTP routes.
rest.Configure()
// Instantiate the certificate cache.
certsJSON, err := ioutil.ReadFile(configure.CertCacheFile)
if err != nil {
certsJSON = []byte("[]")
// Can recover from this. Simply make a cache file and
// instantiate an empty cache.
ioutil.WriteFile(configure.CertCacheFile, 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(option string) {
switch option {
case "default":
s, err := configure.ReadConfig("tracer-server")
if err != nil {
log.Error.Print(err)
}
openbrowser(fmt.Sprintf("http://%s", s))
case "off":
return
default:
var cmd *exec.Cmd
optionArray := strings.Split(option, " ")
if len(optionArray) == 1 {
cmd = exec.Command(optionArray[0])
} else if len(optionArray) > 1 {
cmd = exec.Command(optionArray[0], optionArray[1:]...)
} else {
return
}
cmd.Run()
}
}
// 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)
}
}