-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
94 lines (83 loc) · 2.97 KB
/
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
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
// Package header_pattern_proxy
package multiplexer_proxy
import (
"context"
"fmt"
"log"
"strings"
"net/http"
"net/http/httputil"
"net/url"
"regexp"
"github.com/patrickmn/go-cache"
"time"
)
// Config the plugin configuration.
type Config struct {
Header string `json:"header,omitempty" yaml:"Header" mapstructure:"Header" default:"X-Forward-User"`
TargetMatch string `json:"target_match,omitempty" yaml:"Target_match" mapstructure:"Target_match" default:"^(.*)$"`
TargetReplace string `json:"target_replace,omitempty" yaml:"Target_replace" mapstructure:"Target_replace" default:"test.$1"`
}
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{
Header: "",
TargetMatch: "^(.*)$",
TargetReplace: "${header}.$1",
}
}
type SiteProxy struct {
config *Config
proxyCache *cache.Cache
headerPattern *regexp.Regexp
targetPattern *regexp.Regexp
dotPattern *regexp.Regexp
next http.Handler
name string
}
// New created a new SiteProxy plugin.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
if len(config.Header) == 0 {
log.Printf("Plugin multiplexer-proxy Header zero-length")
return nil, fmt.Errorf("header cannot be empty")
}
if len(config.TargetMatch) == 0 {
log.Printf("Plugin multiplexer-proxy TargetMatch zero-length")
return nil, fmt.Errorf("target_match cannot be empty")
}
if len(config.TargetReplace) == 0 {
log.Printf("Plugin multiplexer-proxy TargetReplace zero-length")
return nil, fmt.Errorf("target_replace cannot be empty")
}
return &SiteProxy{
config: config,
proxyCache: cache.New(5*time.Minute, 10*time.Minute),
headerPattern : regexp.MustCompile(`\${header}`),
targetPattern : regexp.MustCompile(config.TargetMatch),
dotPattern : regexp.MustCompile(`\.`),
next: next,
name: name,
}, nil
}
func (a *SiteProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if req.Header.Get("X-Multiplexer-Proxy") == "true" {
a.next.ServeHTTP(rw, req)
return
}
destTemplate := a.headerPattern.ReplaceAllString(a.config.TargetReplace,a.dotPattern.ReplaceAllString(url.QueryEscape(strings.Replace(req.Header.Get(a.config.Header),"@","-at-",-1)),"-"))
originalDest := req.Header.Get("X-Forwarded-Proto") + "://" + req.Host + req.URL.String()
destination := a.targetPattern.ReplaceAllString(originalDest, destTemplate)
destinationUrl, err := url.Parse(destination)
if err != nil {
a.next.ServeHTTP(rw, req)
return
}
proxy, found := a.proxyCache.Get(destinationUrl.String())
if !found {
proxy = httputil.NewSingleHostReverseProxy(destinationUrl)
a.proxyCache.Add(destinationUrl.String(),proxy,cache.DefaultExpiration)
}
req.Header["X-Multiplexer-Proxy"] = []string{"true"}
req.Host = ""
proxy.(*httputil.ReverseProxy).ServeHTTP(rw, req)
}