/
flashlight.go
210 lines (180 loc) · 4.34 KB
/
flashlight.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
// flashlight is a lightweight chained proxy that can run in client or server mode.
package main
import (
"flag"
"math/rand"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"time"
"github.com/getlantern/flashlight/client"
"github.com/getlantern/flashlight/config"
"github.com/getlantern/flashlight/server"
"github.com/getlantern/flashlight/statreporter"
"github.com/getlantern/flashlight/statserver"
"github.com/getlantern/golog"
)
const (
// Exit Statuses
ConfigError = 1
Interrupted = 2
PortmapFailure = 50
)
var (
log = golog.LoggerFor("flashlight")
version string
buildDate string
// Command-line Flags
help = flag.Bool("help", false, "Get usage help")
parentPID = flag.Int("parentpid", 0, "the parent process's PID, used on Windows for killing flashlight when the parent disappears")
configUpdates = make(chan *config.Config)
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
displayVersion()
flag.Parse()
configUpdates = make(chan *config.Config)
cfg, err := config.Start(func(updated *config.Config) {
configUpdates <- updated
})
if err != nil {
log.Fatalf("Unable to start configuration: %s", err)
}
if *help || cfg.Addr == "" || (cfg.Role != "server" && cfg.Role != "client") {
flag.Usage()
os.Exit(ConfigError)
}
if cfg.CpuProfile != "" {
startCPUProfiling(cfg.CpuProfile)
defer stopCPUProfiling(cfg.CpuProfile)
}
if cfg.MemProfile != "" {
defer saveMemProfile(cfg.MemProfile)
}
saveProfilingOnSigINT(cfg)
// Configure stats initially
configureStats(cfg, true)
log.Debugf("Running proxy")
if cfg.IsDownstream() {
runClientProxy(cfg)
} else {
runServerProxy(cfg)
}
}
func displayVersion() {
if version == "" {
version = "development"
}
if buildDate == "" {
buildDate = "now"
}
log.Debugf("---- flashlight version %s (%s) ----", version, buildDate)
}
func configureStats(cfg *config.Config, failOnError bool) {
err := statreporter.Configure(cfg.Stats)
if err != nil {
log.Error(err)
if failOnError {
flag.Usage()
os.Exit(ConfigError)
}
}
}
// Runs the client-side proxy
func runClientProxy(cfg *config.Config) {
client := &client.Client{
Addr: cfg.Addr,
ReadTimeout: 0, // don't timeout
WriteTimeout: 0,
}
// Configure client initially
client.Configure(cfg.Client, nil)
// Continually poll for config updates and update client accordingly
go func() {
for {
cfg := <-configUpdates
configureStats(cfg, false)
client.Configure(cfg.Client, nil)
}
}()
err := client.ListenAndServe()
if err != nil {
log.Fatalf("Unable to run client proxy: %s", err)
}
}
// Runs the server-side proxy
func runServerProxy(cfg *config.Config) {
useAllCores()
srv := &server.Server{
Addr: cfg.Addr,
ReadTimeout: 0, // don't timeout
WriteTimeout: 0,
CertContext: &server.CertContext{
PKFile: config.InConfigDir("proxypk.pem"),
ServerCertFile: config.InConfigDir("servercert.pem"),
},
}
if cfg.StatsAddr != "" {
// Serve stats
srv.StatServer = &statserver.Server{
Addr: cfg.StatsAddr,
}
}
srv.Configure(cfg.Server)
// Continually poll for config updates and update server accordingly
go func() {
for {
cfg := <-configUpdates
configureStats(cfg, false)
srv.Configure(cfg.Server)
}
}()
err := srv.ListenAndServe()
if err != nil {
log.Fatalf("Unable to run server proxy: %s", err)
}
}
func useAllCores() {
numcores := runtime.NumCPU()
log.Debugf("Using all %d cores on machine", numcores)
runtime.GOMAXPROCS(numcores)
}
func startCPUProfiling(filename string) {
f, err := os.Create(filename)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
log.Debugf("Process will save cpu profile to %s after terminating", filename)
}
func stopCPUProfiling(filename string) {
log.Debugf("Saving CPU profile to: %s", filename)
pprof.StopCPUProfile()
}
func saveMemProfile(filename string) {
f, err := os.Create(filename)
if err != nil {
log.Errorf("Unable to create file to save memprofile: %s", err)
return
}
log.Debugf("Saving heap profile to: %s", filename)
pprof.WriteHeapProfile(f)
f.Close()
}
func saveProfilingOnSigINT(cfg *config.Config) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
if cfg.CpuProfile != "" {
stopCPUProfiling(cfg.CpuProfile)
}
if cfg.MemProfile != "" {
saveMemProfile(cfg.MemProfile)
}
os.Exit(Interrupted)
}()
}