-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (84 loc) · 2.19 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
package main
import (
"log"
"net/http"
"os"
"crypto/tls"
"github.com/codegangsta/negroni"
"github.com/wunderlist/moxy"
"github.com/codegangsta/cli"
"github.com/zpatrick/go-config"
)
func AddSecurityHeaders(request *http.Request, response *http.Response) {
//response.Header.Del("X-Powered-By")
//response.Header.Set("X-Super-Secure", "Yes!!")
}
func main() {
app := cli.NewApp()
app.Name = "Reverse Proxy to terminate SSL for arbitrary http service"
app.Usage = "gosslterm [options]"
app.Version = "0.0.0"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "cert,c",
Value: "cert.pem",
Usage: "Path to SSL certificate.",
EnvVar: "GOSSLTERM_CERT",
},
cli.StringFlag{
Name: "key,k",
Value: "key.pem",
Usage: "Path to SSL key.",
EnvVar: "GOSSLTERM_KEY",
},
cli.StringFlag{
Name: "backend-addr",
Value: "127.0.0.1:80",
Usage: "Address to proxy to",
EnvVar: "GOSSLTERM_BACKEND_ADDR",
},
cli.StringFlag{
Name: "frontend-addr",
Value: ":8080",
Usage: "Address to service proxy at",
EnvVar: "GOSSLTERM_FRONTEND_ADDR",
},
cli.StringFlag{
Name: "log-level",
Value: "warn",
Usage: "Log level (warn: silent)",
EnvVar: "LOG_LEVEL",
},
}
app.Action = Run
app.Run(os.Args)
}
func getTLSConfig(cPath, kPath string) tls.Config {
log.Printf("Load cert '%s' and key '%s'", cPath, kPath)
cert, err := tls.LoadX509KeyPair(cPath, kPath)
if err != nil {
log.Fatalf("error in tls.LoadX509KeyPair: %s", err)
}
config := tls.Config{Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true}
return config
}
func Run(ctx *cli.Context) {
cfg := config.NewConfig([]config.Provider{config.NewCLI(ctx, true)})
cPath, _ := cfg.String("cert")
kPath, _ := cfg.String("key")
tlsConfig := getTLSConfig(cPath, kPath)
bAddr, _ := cfg.String("backend-addr")
hosts := []string{bAddr}
filters := []moxy.FilterFunc{}
proxy := moxy.NewReverseProxy(hosts, filters)
app := negroni.New(negroni.NewLogger())
app.UseHandler(proxy)
fAddr, _ := cfg.String("frontend-addr")
log.Printf("Create http.Server on '%s'", fAddr)
srv := &http.Server{
Addr: fAddr,
Handler: app,
TLSConfig: &tlsConfig,
}
log.Fatal(srv.ListenAndServeTLS(cPath, kPath))
}