forked from phelian/go-vhostd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (88 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
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
"time"
"github.com/phelian/log"
)
type vhost struct {
Vhost string `json:"vhost"`
Host string `json:"host"`
}
type config struct {
Host string `json:"host"`
Port int `json:"port"`
Timeout time.Duration `json:"timeout"`
Log log.Config `json:"log"`
Vhosts []vhost `json:"vhosts"`
}
func main() {
if len(os.Args) != 2 {
usage()
os.Exit(1)
}
cfg, err := readConfig(os.Args[1])
if err != nil {
log.Println(err.Error())
os.Exit(1)
}
logger, err := log.New(cfg.Log)
if err != nil {
log.Println(err.Error())
}
run(cfg, logger)
}
func usage() {
fmt.Println("Usage go-vhostd <config>")
}
func readConfig(path string) (*config, error) {
file, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
cfg := &config{}
err = json.Unmarshal(file, cfg)
return cfg, err
}
func proxyHandler(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
r.Header.Add("Proxy-RemoteIP", strings.Split(r.RemoteAddr, ":")[0])
p.ServeHTTP(w, r)
}
}
func run(cfg *config, logger *log.Handle) {
mux := http.NewServeMux()
for _, vhost := range cfg.Vhosts {
urlString, err := url.Parse("http://" + vhost.Host)
if err != nil {
panic(err)
}
proxy := httputil.NewSingleHostReverseProxy(urlString)
log.Printf("Setting up redirection for %s to %s\n", vhost.Vhost, vhost.Host)
mux.HandleFunc(vhost.Vhost+"/", proxyHandler(proxy))
}
// Server setup
address := fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
server := &http.Server{
Addr: address,
Handler: logHTTP(mux, logger),
ReadTimeout: cfg.Timeout * time.Second,
WriteTimeout: cfg.Timeout * time.Second,
MaxHeaderBytes: 1 << 20,
}
// Start server
log.Printf("Listening to http://%s\n", address)
panic(server.ListenAndServe())
}
func logHTTP(handler http.Handler, logger *log.Handle) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger.Infof("Request: %s %s %s", r.RemoteAddr, r.Method, r.Host+r.URL.String())
handler.ServeHTTP(w, r)
})
}