-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
113 lines (90 loc) · 2.2 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
package main
import (
"crypto/tls"
"encoding/json"
"flag"
"net"
"net/http"
"net/http/httputil"
"os"
"strconv"
"time"
"github.com/microwaves/go-utils/logger"
"rsc.io/letsencrypt"
)
var (
confFile = flag.String("conf", "", "Configuration file")
httpAddr = flag.String("http", ":http", "HTTP listen address")
letsEncryptCacheFile = flag.String("letsencrypt-cache", "", "Let's Encrypt cache file (default HTTPS disabled)")
logFile = flag.String("log", "/var/log/horsefly.log", "Log file")
pollInterval = flag.Duration("poll", time.Second*10, "Configuration file poll interval")
)
type Conf struct {
Host string
Forward string
Serve string
handler http.Handler
}
func makeHandler(c *Conf) http.Handler {
if h := c.Forward; h != "" {
return &httputil.ReverseProxy{
Director: func(req *http.Request) {
req.URL.Scheme = "http"
req.URL.Host = h
},
}
}
if d := c.Serve; d != "" {
return http.FileServer(http.Dir(d))
}
return nil
}
func parseConf(file string) ([]*Conf, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
var conf []*Conf
if err := json.NewDecoder(f).Decode(&conf); err != nil {
return nil, err
}
for _, c := range conf {
c.handler = makeHandler(c)
if c.handler == nil {
logger.Error.Printf("bad configuration: %#v", c)
}
}
return conf, nil
}
func listen(fwd int, addr string) (l net.Listener) {
var err error
if fwd >= 3 {
l, err = net.FileListener(os.NewFile(uintptr(fwd), "https"))
} else {
l, err = net.Listen("tcp", addr)
}
handleError(err)
return
}
func main() {
flag.Parse()
setupLogging()
s, err := NewServer(*confFile, *pollInterval)
handleError(err)
httpFWD, _ := strconv.Atoi(os.Getenv("RUNSIT_PORTFD_http"))
httpsFWD, _ := strconv.Atoi(os.Getenv("RUNSIT_PORTFD_https"))
if *letsEncryptCacheFile != "" {
var m letsencrypt.Manager
err := m.CacheFile(*letsEncryptCacheFile)
handleError(err)
cfg := tls.Config{GetCertificate: m.GetCertificate}
l := tls.NewListener(listen(httpsFWD, ":https"), &cfg)
go func() {
err := http.Serve(l, s)
handleError(err)
}()
}
err = http.Serve(listen(httpFWD, *httpAddr), s)
handleError(err)
}