/
handlers.go
173 lines (157 loc) · 3.69 KB
/
handlers.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package server
import (
"fmt"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
"github.com/julienschmidt/httprouter"
"github.com/kahlys/proxy"
"gitlab.com/bobbae/kuberp/api"
"gitlab.com/bobbae/q"
"golang.org/x/net/context"
)
var router *httprouter.Router
var proxyMap = make(map[string]string)
type Server struct{}
func (srv *Server) Echo(ctx context.Context, req *api.EchoRequest) (res *api.EchoResponse, err error) {
res = &api.EchoResponse{
Message: req.Message,
}
return res, nil
}
func (srv *Server) Proxy(ctx context.Context, req *api.ProxyRequest) (*api.ProxyResponse, error) {
res := &api.ProxyResponse{
Message: req.Message,
Status: "ok",
}
var err error
switch req.Message {
case "add":
if err = addProxy(req); err != nil {
res.Status = fmt.Sprintf("error, failed to add, %v", err)
}
case "delete":
if err = deleteProxy(req); err != nil {
res.Status = fmt.Sprintf("error, failed to delete, %v", err)
}
case "list":
res.Status = fmt.Sprintf("list, %v", proxyMap)
default:
res.Status = "error, invalid message"
}
return res, nil
}
func addProxy(req *api.ProxyRequest) error {
_, ok := proxyMap[req.Path]
if ok {
q.Q("proxy path already exists, will replace", req)
}
switch req.Kind {
case "http":
return addHTTPProxy(req)
case "tcp":
return addTCPProxy(req)
case "udp":
return addUDPProxy(req)
}
return fmt.Errorf("unsupported type")
}
func addHTTPProxy(req *api.ProxyRequest) error {
origin, err := url.Parse(req.Origin)
if err != nil {
return err
}
rp := httputil.NewSingleHostReverseProxy(origin)
if err != nil {
return fmt.Errorf("cannot parse origin %s, %v", origin, err)
}
if router == nil {
return fmt.Errorf("no http router")
}
rp.Director = func(ireq *http.Request) {
p, found := proxyMap[req.Path]
if !found {
return
}
if p == "" {
return
}
if strings.HasPrefix(p, "Deleted-") {
return
}
ireq.Header.Add("X-Forwarded-Host", ireq.Host)
ireq.Header.Add("X-Origin-Host", origin.Host)
ireq.URL.Scheme = origin.Scheme
ireq.URL.Host = origin.Host
wildcard := strings.Index(req.Path, "*")
proxyPath := singleJoiningSlash(origin.Path, ireq.URL.Path[wildcard:])
if strings.HasSuffix(proxyPath, "/") && len(proxyPath) > 1 {
proxyPath = proxyPath[:len(proxyPath)-1]
}
ireq.URL.Path = proxyPath
}
_, ok := proxyMap[req.Path]
if !ok {
for _, method := range []string{"GET", "POST"} {
router.Handle(method, req.Path, func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
rp.ServeHTTP(w, r)
})
}
}
proxyMap[req.Path] = req.Origin
return nil
}
var defaultCert, defaultKey string
func CheckCerts(certDir string) error {
defaultCert = certDir + "/cert.pem"
_, err := os.Stat(defaultCert)
if err != nil {
return err
}
defaultKey = certDir + "/key.pem"
_, err = os.Stat(defaultKey)
if err != nil {
return err
}
return nil
}
func addTCPProxy(req *api.ProxyRequest) error {
laddr, err := net.ResolveTCPAddr("tcp", req.Path)
if err != nil {
return err
}
raddr, err := net.ResolveTCPAddr("tcp", req.Origin)
if err != nil {
return err
}
go func() {
proxy.NewServer(raddr, nil, nil).ListenAndServeTLS(laddr, defaultCert, defaultKey)
}()
return nil
}
func addUDPProxy(req *api.ProxyRequest) error {
return nil
}
func deleteProxy(req *api.ProxyRequest) error {
proxyMap[req.Path] = "Deleted-" + req.Origin
//XXX no delete for tcp,udp
return nil
}
func NewRouter() *httprouter.Router {
router = httprouter.New()
return router
}
func singleJoiningSlash(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}