-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.go
More file actions
64 lines (57 loc) · 1.67 KB
/
proxy.go
File metadata and controls
64 lines (57 loc) · 1.67 KB
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
package proxy
import (
"crypto/tls"
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
type ReverseProxy struct {
Proxy *httputil.ReverseProxy
Target *url.URL
}
func NewReverseProxy(targetDomain string) (*ReverseProxy, error) {
target, err := url.Parse(targetDomain)
if err != nil {
return nil, fmt.Errorf("error parsing target URL: %v", err)
}
proxy := httputil.NewSingleHostReverseProxy(target)
// Skip https cert
proxy.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true, // don't set true in prod environment
},
}
return &ReverseProxy{Proxy: proxy, Target: target}, nil
}
func (rp *ReverseProxy) HandleRequest(w http.ResponseWriter, r *http.Request) {
proto := r.Header.Get("X-Forwarded-Proto")
if proto == "" {
proto = "http"
}
log.Println("Received request:", r.Method, r.URL.Path, r.Host, proto)
host := r.Host
// ModifyResponse modify Header
rp.Proxy.ModifyResponse = func(resp *http.Response) error {
// Modify header path: v2/
if resp.Header.Get("Www-Authenticate") != "" {
// clear Header
resp.Header.Del("Www-Authenticate")
// setting new Header
resp.Header.Set("Www-Authenticate", fmt.Sprintf(`Bearer realm="%s://%s/service/token",service="harbor-registry"`, proto, host))
}
// Modify Location Header
// for path: /uploads/ /manifests/
if location := resp.Header.Get("Location"); location != "" {
newLocation := strings.Replace(location, rp.Target.String(), fmt.Sprintf("%s://%s", proto, host), 1)
resp.Header.Set("Location", newLocation)
}
return nil
}
r.URL.Scheme = rp.Target.Scheme
r.URL.Host = rp.Target.Host
r.Host = rp.Target.Host
rp.Proxy.ServeHTTP(w, r)
}