-
Notifications
You must be signed in to change notification settings - Fork 0
/
rproxyd.go
63 lines (56 loc) · 1.19 KB
/
rproxyd.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
package main
import (
"bufio"
"net/http"
"net/http/httputil"
"net/url"
"os"
"runtime"
"strings"
"github.com/kardianos/osext"
)
type RProxy struct {
Pre string
Proxy *httputil.ReverseProxy
}
type RPServer struct {
Addr string
RPs []RProxy
}
func (s *RPServer) ReadCfg(file string) {
cfg, _ := os.Open(file)
defer cfg.Close()
scanner := bufio.NewScanner(cfg)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "listen") {
s.Addr = strings.Split(line, " ")[1]
} else if strings.HasPrefix(line, "proxy") {
tmp := strings.Split(line, " ")
remote, _ := url.Parse(tmp[2])
proxy := httputil.NewSingleHostReverseProxy(remote)
s.RPs = append(s.RPs, RProxy{Pre: tmp[1], Proxy: proxy})
}
}
}
func (s *RPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
handled := false
for _, rp := range s.RPs {
if strings.HasPrefix(r.RequestURI, rp.Pre) {
handled = true
rp.Proxy.ServeHTTP(w, r)
break
}
}
if !handled {
w.WriteHeader(404)
w.Write([]byte("Bad Proxy\n"))
}
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
s := &RPServer{}
path, _ := osext.ExecutableFolder()
s.ReadCfg(path + "\\rproxyd")
http.ListenAndServe(s.Addr, s)
}