Skip to content

Commit

Permalink
Merge e359948 into 753d757
Browse files Browse the repository at this point in the history
  • Loading branch information
buger committed Apr 6, 2018
2 parents 753d757 + e359948 commit d216a26
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions apidef/api_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ type APIDefinition struct {
Transport struct {
SSLCipherSuites []string `bson:"ssl_ciphers" json:"ssl_ciphers"`
SSLMinVersion uint16 `bson:"ssl_min_version" json:"ssl_min_version"`
ProxyURL string `bson:"proxy_url" json:"proxy_url"`
} `bson:"transport" json:"transport"`
} `bson:"proxy" json:"proxy"`
DisableRateLimit bool `bson:"disable_rate_limit" json:"disable_rate_limit"`
Expand Down
2 changes: 2 additions & 0 deletions batch_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func (b *BatchRequestHandler) doRequest(req *http.Request, relURL string) BatchR

tr.DialTLS = dialTLSPinnedCheck(b.API, tr.TLSClientConfig)

tr.Proxy = proxyFromAPI(b.API)

client := &http.Client{Transport: tr}

resp, err := client.Do(req)
Expand Down
15 changes: 14 additions & 1 deletion cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ func TestCipherSuites(t *testing.T) {
})
}

func TestProxyCipherSuites(t *testing.T) {
func TestProxyTransport(t *testing.T) {
upstream := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("test"))
}))
Expand Down Expand Up @@ -760,4 +760,17 @@ func TestProxyCipherSuites(t *testing.T) {

ts.Run(t, test.TestCase{Path: "/", Code: 500})
})

t.Run("API: Proxy", func(t *testing.T) {
config.Global.ProxySSLMinVersion = 771
buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.Proxy.TargetURL = upstream.URL
spec.Proxy.Transport.SSLCipherSuites = []string{"TLS_RSA_WITH_AES_128_CBC_SHA"}
// Invalid proxy
spec.Proxy.Transport.ProxyURL = upstream.URL
})

ts.Run(t, test.TestCase{Path: "/", Code: 500})
})
}
2 changes: 2 additions & 0 deletions mw_js_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,8 @@ func (j *JSVM) LoadTykJSApi() {

tr.DialTLS = dialTLSPinnedCheck(j.Spec, tr.TLSClientConfig)

tr.Proxy = proxyFromAPI(j.Spec)

// using new Client each time should be ok, since we closing connection every time
client := &http.Client{Transport: tr}
resp, err := client.Do(r)
Expand Down
11 changes: 10 additions & 1 deletion reverse_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@ func defaultTransport() *http.Transport {
// allocate a new one every time for now, to avoid modifying a
// global variable for each request's needs (e.g. timeouts).
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
Expand Down Expand Up @@ -421,9 +420,19 @@ func (p *ReverseProxy) CheckCircuitBreakerEnforced(spec *APISpec, req *http.Requ
return false, nil
}

func proxyFromAPI(api *APISpec) func(*http.Request) (*url.URL, error) {
return func(req *http.Request) (*url.URL, error) {
if api != nil && api.Proxy.Transport.ProxyURL != "" {
return url.Parse(api.Proxy.Transport.ProxyURL)
}
return http.ProxyFromEnvironment(req)
}
}

func httpTransport(timeOut int, rw http.ResponseWriter, req *http.Request, p *ReverseProxy) http.RoundTripper {
transport := defaultTransport() // modifies a newly created transport
transport.TLSClientConfig = &tls.Config{}
transport.Proxy = proxyFromAPI(p.TykAPISpec)

if config.Global.ProxySSLInsecureSkipVerify {
transport.TLSClientConfig.InsecureSkipVerify = true
Expand Down

0 comments on commit d216a26

Please sign in to comment.