-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
134 lines (113 loc) · 3.46 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
// Copyright 2019 tawalaya
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"crypto/tls"
"flag"
"fmt"
"net/http"
"net/url"
"path/filepath"
"github.com/kabukky/httpscerts"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/vulcand/oxy/forward"
"golang.org/x/crypto/acme/autocert"
)
var logger = logrus.New()
var log = logrus.NewEntry(logger)
func cliSetup() {
viper.SetDefault("port", 8000)
viper.SetDefault("author", "admin@localhost")
viper.SetDefault("self", false)
viper.SetDefault("certs", ".")
flag.Int("port", 8000, "set the port to listen to")
flag.String("author", "admin@localhost", "the mail used by acme to register this service")
flag.String("domain", "localhost", "the domain to use for acme")
flag.Bool("self", false, "use self signed instead")
flag.String("certs", ".", "location of the certificates")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
}
func main() {
cliSetup()
pxy := SSLProxy{port: viper.GetInt("port")}
pxy.setupServer()
}
type SSLProxy struct {
address *url.URL
port int
oxy *forward.Forwarder
}
func (p *SSLProxy) setupServer() {
//initialize proxy
oxy, err := forward.New(
forward.Stream(true),
forward.PassHostHeader(true),
)
if err != nil {
log.Fatalf("failed to create proxy %+v", err)
}
p.oxy = oxy
p.address, err = url.Parse(fmt.Sprintf("http://localhost:%d", p.port))
if viper.GetBool("self") {
log.Info("Using Self Signed")
cert := filepath.Join(viper.GetString("certs"), "cert.pem")
key := filepath.Join(viper.GetString("certs"), "key.pem")
err := httpscerts.Check(cert, key)
if err != nil {
log.Info("could not load self signed keys - generationg some")
err = httpscerts.Generate(cert, key, "127.0.0.1:443")
if err != nil {
log.Fatal("Error: Couldn't create https certs.")
}
}
httpsServer := &http.Server{
Addr: ":443",
Handler: http.HandlerFunc(p.serve),
}
err = httpsServer.ListenAndServeTLS(cert, key)
if err != nil {
log.Errorf("httpsSrv.ListendAndServeTLS() failed with %s", err)
}
} else {
log.Infof("Using autocert for %+v", viper.GetStringSlice("domain"))
m := &autocert.Manager{
Email: viper.GetString("author"),
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(viper.GetStringSlice("domain")...),
Cache: autocert.DirCache("."),
}
httpsServer := &http.Server{
Addr: ":443",
Handler: http.HandlerFunc(p.serve),
TLSConfig: &tls.Config{GetCertificate: m.GetCertificate},
}
go func() {
// serve HTTP, which will redirect automatically to HTTPS
h := m.HTTPHandler(nil)
log.Fatal(http.ListenAndServe(":http", h))
}()
err = httpsServer.ListenAndServeTLS("", "")
if err != nil {
log.Errorf("httpsSrv.ListendAndServeTLS() failed with %s", err)
}
}
}
func (p *SSLProxy) serve(w http.ResponseWriter, req *http.Request) {
req.URL = p.address
p.oxy.ServeHTTP(w, req)
}