-
Notifications
You must be signed in to change notification settings - Fork 7
/
proxy.go
44 lines (38 loc) · 911 Bytes
/
proxy.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
package proxy
import (
"errors"
"net/http"
"net/http/httputil"
"net/url"
"github.com/gin-gonic/gin"
"github.com/harryzcy/mailbox-browser/bff/config"
"github.com/harryzcy/mailbox-browser/bff/transport/rest/ginutil"
)
func Proxy(ctx *gin.Context) {
if !config.ProxyEnable {
ginutil.Forbidden(ctx, errors.New("proxy disabled"))
return
}
target, err := url.QueryUnescape(ctx.Query("l"))
if err != nil {
ginutil.InternalError(ctx, err)
return
}
remote, err := url.Parse(target)
if err != nil {
ginutil.InternalError(ctx, err)
return
}
proxy := httputil.NewSingleHostReverseProxy(&url.URL{
Scheme: remote.Scheme,
Host: remote.Host,
})
proxy.Director = func(req *http.Request) {
req.Header = ctx.Request.Header
req.Host = remote.Host
req.URL.Scheme = remote.Scheme
req.URL.Host = remote.Host
req.URL.Path = remote.Path
}
proxy.ServeHTTP(ctx.Writer, ctx.Request)
}