| 1 |
package server |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"compress/gzip" |
| 6 |
"encoding/json" |
| 7 |
"io" |
| 8 |
"net/http" |
| 9 |
"net/http/httputil" |
| 10 |
"net/url" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"github.com/gin-gonic/gin" |
| 14 |
"github.com/henrybear327/go-proton-api" |
| 15 |
) |
| 16 |
|
| 17 |
func newProxy(proxyOrigin, base, path string, transport http.RoundTripper) http.HandlerFunc { |
| 18 |
origin, err := url.Parse(proxyOrigin) |
| 19 |
if err != nil { |
| 20 |
panic(err) |
| 21 |
} |
| 22 |
|
| 23 |
return (&httputil.ReverseProxy{ |
| 24 |
Director: func(req *http.Request) { |
| 25 |
req.URL.Scheme = origin.Scheme |
| 26 |
req.URL.Host = origin.Host |
| 27 |
req.URL.Path = origin.Path + strings.TrimPrefix(path, base) |
| 28 |
req.Host = origin.Host |
| 29 |
}, |
| 30 |
|
| 31 |
Transport: transport, |
| 32 |
}).ServeHTTP |
| 33 |
} |
| 34 |
|
| 35 |
func (s *Server) handleProxy(base string) gin.HandlerFunc { |
| 36 |
return func(c *gin.Context) { |
| 37 |
proxy := newProxyServer(s.proxyOrigin, base, s.proxyTransport) |
| 38 |
|
| 39 |
proxy.handle("/", s.handleProxyAll) |
| 40 |
|
| 41 |
if s.authCacher != nil { |
| 42 |
proxy.handle("/auth/v4", s.handleProxyAuth) |
| 43 |
proxy.handle("/auth/v4/info", s.handleProxyAuthInfo) |
| 44 |
} |
| 45 |
|
| 46 |
proxy.ServeHTTP(c.Writer, c.Request) |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
func (s *Server) handleProxyAll(proxier func(string) HandlerFunc) http.HandlerFunc { |
| 51 |
return func(w http.ResponseWriter, r *http.Request) { |
| 52 |
if _, err := proxier(r.URL.Path)(w, r); err != nil { |
| 53 |
http.Error(w, err.Error(), http.StatusBadRequest) |
| 54 |
return |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
func (s *Server) handleProxyAuth(proxier func(string) HandlerFunc) http.HandlerFunc { |
| 60 |
return func(w http.ResponseWriter, r *http.Request) { |
| 61 |
switch r.Method { |
| 62 |
case http.MethodPost: |
| 63 |
s.handleProxyAuthPost(w, r, proxier(r.URL.Path)) |
| 64 |
|
| 65 |
case http.MethodDelete: |
| 66 |
s.handleProxyAuthDelete(w, r, proxier(r.URL.Path)) |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
func (s *Server) handleProxyAuthPost(w http.ResponseWriter, r *http.Request, proxier HandlerFunc) { |
| 72 |
req, err := readFromBody[proton.AuthReq](r) |
| 73 |
if err != nil { |
| 74 |
http.Error(w, err.Error(), http.StatusBadRequest) |
| 75 |
return |
| 76 |
} |
| 77 |
|
| 78 |
if info, ok := s.authCacher.GetAuth(req.Username); ok { |
| 79 |
if err := writeBody(w, info); err != nil { |
| 80 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 81 |
return |
| 82 |
} |
| 83 |
} else { |
| 84 |
b, err := proxier(w, r) |
| 85 |
if err != nil { |
| 86 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 87 |
return |
| 88 |
} |
| 89 |
|
| 90 |
res, err := readFrom[proton.Auth](b) |
| 91 |
if err != nil { |
| 92 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 93 |
return |
| 94 |
} |
| 95 |
|
| 96 |
s.authCacher.SetAuth(req.Username, res) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
func (s *Server) handleProxyAuthDelete(w http.ResponseWriter, r *http.Request, proxier HandlerFunc) { |
| 101 |
// When caching, we don't need to do anything here. |
| 102 |
} |
| 103 |
|
| 104 |
func (s *Server) handleProxyAuthInfo(proxier func(string) HandlerFunc) http.HandlerFunc { |
| 105 |
return func(w http.ResponseWriter, r *http.Request) { |
| 106 |
req, err := readFromBody[proton.AuthInfoReq](r) |
| 107 |
if err != nil { |
| 108 |
http.Error(w, err.Error(), http.StatusBadRequest) |
| 109 |
return |
| 110 |
} |
| 111 |
|
| 112 |
if info, ok := s.authCacher.GetAuthInfo(req.Username); ok { |
| 113 |
if err := writeBody(w, info); err != nil { |
| 114 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 115 |
return |
| 116 |
} |
| 117 |
} else { |
| 118 |
b, err := proxier(r.URL.Path)(w, r) |
| 119 |
if err != nil { |
| 120 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 121 |
return |
| 122 |
} |
| 123 |
|
| 124 |
res, err := readFrom[proton.AuthInfo](b) |
| 125 |
if err != nil { |
| 126 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 127 |
return |
| 128 |
} |
| 129 |
|
| 130 |
s.authCacher.SetAuthInfo(req.Username, res) |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
type HandlerFunc func(http.ResponseWriter, *http.Request) ([]byte, error) |
| 136 |
|
| 137 |
type proxyServer struct { |
| 138 |
mux *http.ServeMux |
| 139 |
|
| 140 |
origin, base string |
| 141 |
|
| 142 |
transport http.RoundTripper |
| 143 |
} |
| 144 |
|
| 145 |
func newProxyServer(origin, base string, transport http.RoundTripper) *proxyServer { |
| 146 |
return &proxyServer{ |
| 147 |
mux: http.NewServeMux(), |
| 148 |
origin: origin, |
| 149 |
base: base, |
| 150 |
transport: transport, |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
func (s *proxyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 155 |
s.mux.ServeHTTP(w, r) |
| 156 |
} |
| 157 |
|
| 158 |
func (s *proxyServer) handle(path string, h func(func(string) HandlerFunc) http.HandlerFunc) { |
| 159 |
s.mux.Handle(s.base+path, h(func(path string) HandlerFunc { |
| 160 |
return func(w http.ResponseWriter, r *http.Request) ([]byte, error) { |
| 161 |
buf := new(bytes.Buffer) |
| 162 |
|
| 163 |
// Call the proxy, capturing whatever data it writes. |
| 164 |
newProxy(s.origin, s.base, path, s.transport)(&writerWrapper{w, buf}, r) |
| 165 |
|
| 166 |
// If there is a gzip header entry, decode it. |
| 167 |
if strings.Contains(w.Header().Get("Content-Encoding"), "gzip") { |
| 168 |
return gzipDecode(buf.Bytes()) |
| 169 |
} |
| 170 |
|
| 171 |
// Otherwise, return the original written data. |
| 172 |
return buf.Bytes(), nil |
| 173 |
} |
| 174 |
})) |
| 175 |
} |
| 176 |
|
| 177 |
type writerWrapper struct { |
| 178 |
http.ResponseWriter |
| 179 |
|
| 180 |
buf *bytes.Buffer |
| 181 |
} |
| 182 |
|
| 183 |
func (w *writerWrapper) Write(b []byte) (int, error) { |
| 184 |
if _, err := w.buf.Write(b); err != nil { |
| 185 |
return 0, err |
| 186 |
} |
| 187 |
|
| 188 |
return w.ResponseWriter.Write(b) |
| 189 |
} |
| 190 |
|
| 191 |
func readFrom[T any](b []byte) (T, error) { |
| 192 |
var v T |
| 193 |
|
| 194 |
if err := json.Unmarshal(b, &v); err != nil { |
| 195 |
return *new(T), err |
| 196 |
} |
| 197 |
|
| 198 |
return v, nil |
| 199 |
} |
| 200 |
|
| 201 |
func readFromBody[T any](r *http.Request) (T, error) { |
| 202 |
b, err := io.ReadAll(r.Body) |
| 203 |
if err != nil { |
| 204 |
return *new(T), err |
| 205 |
} |
| 206 |
defer r.Body.Close() |
| 207 |
|
| 208 |
v, err := readFrom[T](b) |
| 209 |
if err != nil { |
| 210 |
return *new(T), err |
| 211 |
} |
| 212 |
|
| 213 |
r.Body = io.NopCloser(bytes.NewReader(b)) |
| 214 |
|
| 215 |
return v, nil |
| 216 |
} |
| 217 |
|
| 218 |
func writeBody[T any](w http.ResponseWriter, v T) error { |
| 219 |
b, err := json.Marshal(v) |
| 220 |
if err != nil { |
| 221 |
return err |
| 222 |
} |
| 223 |
|
| 224 |
w.Header().Set("Content-Type", "application/json") |
| 225 |
|
| 226 |
if _, err := w.Write(b); err != nil { |
| 227 |
return err |
| 228 |
} |
| 229 |
|
| 230 |
return nil |
| 231 |
} |
| 232 |
|
| 233 |
func gzipDecode(b []byte) ([]byte, error) { |
| 234 |
r, err := gzip.NewReader(bytes.NewReader(b)) |
| 235 |
if err != nil { |
| 236 |
return nil, err |
| 237 |
} |
| 238 |
defer r.Close() |
| 239 |
|
| 240 |
return io.ReadAll(r) |
| 241 |
} |