-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
68 lines (55 loc) · 1.25 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"github.com/weaming/proxyany/reverseproxy"
)
var (
bind = ":20443"
https = false
cfgPath = "config.json"
version = "version 1.2"
mg *reverseproxy.MapGroup
)
func init() {
fmt.Println(version)
flag.StringVar(&cfgPath, "config", cfgPath, "file path domain mapping config in json format")
flag.StringVar(&bind, "bind", bind, "local bind [<host>]:<port>")
flag.BoolVar(&https, "https", https, "HTTPS mode, auto certification from let's encrypt")
flag.Parse()
mg = reverseproxy.LoadMapGroupFromJson(cfgPath)
}
func main() {
srv := newProxyServer()
if https {
config := NewConfig(srv)
config.IsHostAllowed = isHostAllowed
fmt.Printf("listening :443\n")
config.ListenAndServeTLS()
} else {
srv.Addr = bind
fmt.Printf("listening %v\n", bind)
err := srv.ListenAndServe()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}
func newProxyServer() *http.Server {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
proxy := reverseproxy.NewReverseProxy(mg)
proxy.ServeHTTP(w, r)
})
srv := NewHTTPServer()
srv.Handler = handler
return srv
}
func isHostAllowed(host string) bool {
if mg.GetMapping(host) != nil {
return true
}
return false
}