-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
154 lines (132 loc) · 3.02 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path/filepath"
"time"
"github.com/BurntSushi/toml"
"github.com/fsnotify/fsnotify"
)
type config struct {
Port string `toml:"port"`
TLSPort string `toml:"tls_port"`
Cert string `toml:"cert"`
Key string `toml:"key"`
Domains map[string]string `toml:"domains"`
}
func (c config) isZero() bool {
return c.Port == "" && c.TLSPort == "" && c.Cert == "" && c.Key == "" && len(c.Domains) == 0
}
func (c *config) override(n config) {
if n.Port != "" {
c.Port = n.Port
}
if n.TLSPort != "" {
c.TLSPort = n.TLSPort
}
if n.Cert != "" {
c.Cert = n.Cert
}
if n.Key != "" {
c.Cert = n.Cert
}
if n.Domains != nil {
c.Domains = n.Domains
}
}
var cfg config
func redirect(w http.ResponseWriter, r *http.Request) {
target, ok := cfg.Domains[r.Host]
if !ok {
fmt.Fprintf(w, "error: not found")
return
}
origin, err := url.Parse(target)
if err != nil {
fmt.Fprintf(w, "error: failed to parse url")
return
}
proxy := httputil.NewSingleHostReverseProxy(origin)
proxy.ServeHTTP(w, r)
}
func watch(path string, cfgptr *config) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal("watch", "fsnotify.NewWatcher", err)
}
defer watcher.Close()
if err := watcher.Add(filepath.Dir(path)); err != nil {
log.Println("watch", "watcher.Add", err)
return
}
loop:
for {
select {
case event, ok := <-watcher.Events:
if !ok {
continue loop
}
if event.Has(fsnotify.Write) && event.Name == path {
if _, err := toml.DecodeFile(path, cfgptr); err != nil {
log.Println("watch", "toml.DecodeFile", err)
}
}
case err, ok := <-watcher.Errors:
if !ok {
break
}
log.Println("watch", err)
}
}
}
func parseFlags() (cpath string, c config) {
flag.StringVar(&cpath, "cfg", "", "Path to the configuration file")
flag.StringVar(&c.Port, "port", "", "The port that onering will listen to")
flag.StringVar(&c.TLSPort, "tlsport", "", "The TLS port that onering will listen to")
flag.StringVar(&c.Cert, "cert", "", "Path to the TLS certificate")
flag.StringVar(&c.Key, "key", "", "Path to the TLS key")
flag.Parse()
return
}
func getConfig(path string) (c config) {
cpath, fcfg := parseFlags()
if cpath != "" {
path = cpath
}
_, err := toml.DecodeFile(path, &c)
if err != nil {
log.Println("getConfig", "toml.DecodeFile", err)
}
c.override(fcfg)
if c.isZero() {
log.Fatal("error: no configuration provided")
}
return
}
func retry(d time.Duration, fn func()) {
for {
fn()
time.Sleep(d)
}
}
func main() {
ucfg, err := os.UserConfigDir()
if err != nil {
log.Fatal("main", "os.UserConfigDir", err)
}
cfgpath := filepath.Join(ucfg, "onering.toml")
cfg = getConfig(cfgpath)
go watch(cfgpath, &cfg)
http.HandleFunc("/", redirect)
go retry(time.Second*5, func() {
log.Println(http.ListenAndServe(cfg.Port, nil))
})
retry(time.Second*5, func() {
log.Println(http.ListenAndServeTLS(cfg.TLSPort, cfg.Cert, cfg.Key, nil))
})
}