-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
181 lines (155 loc) · 3.98 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
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
//go:generate go run generate/versioninfo.go
// secure is a super simple TLS termination proxy
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"syscall"
"gopkg.in/yaml.v3"
)
type Config struct {
CertFile string `yaml:"cert"`
KeyFile string `yaml:"key"`
Upstream string `yaml:"upstream"`
Addr string `yaml:"addr"`
}
var (
version bool
configFile string
)
func getConfig() (config *Config, err error) {
cfg := Config{}
flag.StringVar(&configFile, "c", "", "config file")
flag.StringVar(&cfg.Addr, "addr", ":443", "listen address")
flag.StringVar(&cfg.CertFile, "cert", "", "path to cert file")
flag.StringVar(&cfg.KeyFile, "key", "", "path to key file")
flag.BoolVar(&version, "version", false, "print version string and exit")
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(),
"usage: %s -c [config.yml] [-addr host:port] -cert certfile -key keyfile [-version] upstream\n",
filepath.Base(os.Args[0]))
flag.PrintDefaults()
fmt.Fprintln(flag.CommandLine.Output(), " upstream string\n \tupstream url")
}
flag.Parse()
if version {
fmt.Fprintln(flag.CommandLine.Output(), Version)
os.Exit(0)
}
if configFile != "" {
var data []byte
data, err = os.ReadFile(configFile)
if err != nil {
return
}
err = yaml.Unmarshal(data, &cfg)
if err != nil {
return nil, fmt.Errorf("cant parse config, %v", err)
}
if _, err := strconv.Atoi(cfg.Addr); err == nil {
cfg.Addr = ":" + cfg.Addr
}
return &cfg, nil
}
if flag.NArg() == 1 {
cfg.Upstream = flag.Arg(0)
} else {
flag.Usage()
os.Exit(2)
}
return &cfg, nil
}
func singleJoiningSlash(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}
func joinURLPath(a, b *url.URL) (path, rawpath string) {
if a.RawPath == "" && b.RawPath == "" {
return singleJoiningSlash(a.Path, b.Path), ""
}
// Same as singleJoiningSlash, but uses EscapedPath to determine
// whether a slash should be added
apath := a.EscapedPath()
bpath := b.EscapedPath()
aslash := strings.HasSuffix(apath, "/")
bslash := strings.HasPrefix(bpath, "/")
switch {
case aslash && bslash:
return a.Path + b.Path[1:], apath + bpath[1:]
case !aslash && !bslash:
return a.Path + "/" + b.Path, apath + "/" + bpath
}
return a.Path + b.Path, apath + bpath
}
func _main() error {
cfg, err := getConfig()
if err != nil {
return err
}
upstream, err := url.Parse(cfg.Upstream)
if err != nil {
return fmt.Errorf("invalid upstream address: %v", err)
}
upstreamQuery := upstream.RawQuery
director := func(req *http.Request) {
req.URL.Scheme = upstream.Scheme
req.Host = upstream.Host
req.URL.Host = upstream.Host
req.URL.Path, req.URL.RawPath = joinURLPath(upstream, req.URL)
if upstreamQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = upstreamQuery + req.URL.RawQuery
} else {
req.URL.RawQuery = upstreamQuery + "&" + req.URL.RawQuery
}
if _, ok := req.Header["User-Agent"]; !ok {
// explicitly disable User-Agent so it's not set to default value
req.Header.Set("User-Agent", "")
}
}
srv := http.Server{
Handler: &httputil.ReverseProxy{
Director: director,
},
Addr: cfg.Addr,
}
done := make(chan struct{})
go func() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
fmt.Println(<-sig)
if err := srv.Shutdown(context.Background()); err != nil {
fmt.Printf("Shutdown: %v", err)
}
close(done)
}()
log.Printf("cert-file=%s key-file=%s listen-addr=%s upstream-url=%s", cfg.CertFile, cfg.KeyFile, srv.Addr, upstream.String())
if err := srv.ListenAndServeTLS(cfg.CertFile, cfg.KeyFile); err != http.ErrServerClosed {
return fmt.Errorf("ListenAndServeTLS: %v", err)
}
<-done
return nil
}
func main() {
err := _main()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}