mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-29 08:59:58 +00:00
TUN-528: Move cloudflared into a separate repo
This commit is contained in:
54
vendor/github.com/mholt/caddy/caddyhttp/proxy/body.go
generated
vendored
Normal file
54
vendor/github.com/mholt/caddy/caddyhttp/proxy/body.go
generated
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
type bufferedBody struct {
|
||||
*bytes.Reader
|
||||
}
|
||||
|
||||
func (*bufferedBody) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// rewind allows bufferedBody to be read again.
|
||||
func (b *bufferedBody) rewind() error {
|
||||
if b == nil {
|
||||
return nil
|
||||
}
|
||||
_, err := b.Seek(0, io.SeekStart)
|
||||
return err
|
||||
}
|
||||
|
||||
// newBufferedBody returns *bufferedBody to use in place of src. Closes src
|
||||
// and returns Read error on src. All content from src is buffered.
|
||||
func newBufferedBody(src io.ReadCloser) (*bufferedBody, error) {
|
||||
if src == nil {
|
||||
return nil, nil
|
||||
}
|
||||
b, err := ioutil.ReadAll(src)
|
||||
src.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &bufferedBody{
|
||||
Reader: bytes.NewReader(b),
|
||||
}, nil
|
||||
}
|
83
vendor/github.com/mholt/caddy/caddyhttp/proxy/body_test.go
generated
vendored
Normal file
83
vendor/github.com/mholt/caddy/caddyhttp/proxy/body_test.go
generated
vendored
Normal file
@@ -0,0 +1,83 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBodyRetry(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
io.Copy(w, r.Body)
|
||||
r.Body.Close()
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
testcase := "test content"
|
||||
req, err := http.NewRequest(http.MethodPost, ts.URL, bytes.NewBufferString(testcase))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
body, err := newBufferedBody(req.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if body != nil {
|
||||
req.Body = body
|
||||
}
|
||||
|
||||
// simulate fail request
|
||||
host := req.URL.Host
|
||||
req.URL.Host = "example.com"
|
||||
body.rewind()
|
||||
_, _ = http.DefaultTransport.RoundTrip(req)
|
||||
|
||||
// retry request
|
||||
req.URL.Host = host
|
||||
body.rewind()
|
||||
resp, err := http.DefaultTransport.RoundTrip(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
if string(result) != testcase {
|
||||
t.Fatalf("result = %s, want %s", result, testcase)
|
||||
}
|
||||
|
||||
// try one more time for body reuse
|
||||
body.rewind()
|
||||
resp, err = http.DefaultTransport.RoundTrip(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err = ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
if string(result) != testcase {
|
||||
t.Fatalf("result = %s, want %s", result, testcase)
|
||||
}
|
||||
}
|
196
vendor/github.com/mholt/caddy/caddyhttp/proxy/policy.go
generated
vendored
Normal file
196
vendor/github.com/mholt/caddy/caddyhttp/proxy/policy.go
generated
vendored
Normal file
@@ -0,0 +1,196 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"hash/fnv"
|
||||
"math"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// HostPool is a collection of UpstreamHosts.
|
||||
type HostPool []*UpstreamHost
|
||||
|
||||
// Policy decides how a host will be selected from a pool.
|
||||
type Policy interface {
|
||||
Select(pool HostPool, r *http.Request) *UpstreamHost
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterPolicy("random", func(arg string) Policy { return &Random{} })
|
||||
RegisterPolicy("least_conn", func(arg string) Policy { return &LeastConn{} })
|
||||
RegisterPolicy("round_robin", func(arg string) Policy { return &RoundRobin{} })
|
||||
RegisterPolicy("ip_hash", func(arg string) Policy { return &IPHash{} })
|
||||
RegisterPolicy("first", func(arg string) Policy { return &First{} })
|
||||
RegisterPolicy("uri_hash", func(arg string) Policy { return &URIHash{} })
|
||||
RegisterPolicy("header", func(arg string) Policy { return &Header{arg} })
|
||||
}
|
||||
|
||||
// Random is a policy that selects up hosts from a pool at random.
|
||||
type Random struct{}
|
||||
|
||||
// Select selects an up host at random from the specified pool.
|
||||
func (r *Random) Select(pool HostPool, request *http.Request) *UpstreamHost {
|
||||
|
||||
// Because the number of available hosts isn't known
|
||||
// up front, the host is selected via reservoir sampling
|
||||
// https://en.wikipedia.org/wiki/Reservoir_sampling
|
||||
var randHost *UpstreamHost
|
||||
count := 0
|
||||
for _, host := range pool {
|
||||
if !host.Available() {
|
||||
continue
|
||||
}
|
||||
|
||||
// (n % 1 == 0) holds for all n, therefore randHost
|
||||
// will always get assigned a value if there is
|
||||
// at least 1 available host
|
||||
count++
|
||||
if (rand.Int() % count) == 0 {
|
||||
randHost = host
|
||||
}
|
||||
}
|
||||
return randHost
|
||||
}
|
||||
|
||||
// LeastConn is a policy that selects the host with the least connections.
|
||||
type LeastConn struct{}
|
||||
|
||||
// Select selects the up host with the least number of connections in the
|
||||
// pool. If more than one host has the same least number of connections,
|
||||
// one of the hosts is chosen at random.
|
||||
func (r *LeastConn) Select(pool HostPool, request *http.Request) *UpstreamHost {
|
||||
var bestHost *UpstreamHost
|
||||
count := 0
|
||||
leastConn := int64(math.MaxInt64)
|
||||
for _, host := range pool {
|
||||
if !host.Available() {
|
||||
continue
|
||||
}
|
||||
|
||||
if host.Conns < leastConn {
|
||||
leastConn = host.Conns
|
||||
count = 0
|
||||
}
|
||||
|
||||
// Among hosts with same least connections, perform a reservoir
|
||||
// sample: https://en.wikipedia.org/wiki/Reservoir_sampling
|
||||
if host.Conns == leastConn {
|
||||
count++
|
||||
if (rand.Int() % count) == 0 {
|
||||
bestHost = host
|
||||
}
|
||||
}
|
||||
}
|
||||
return bestHost
|
||||
}
|
||||
|
||||
// RoundRobin is a policy that selects hosts based on round-robin ordering.
|
||||
type RoundRobin struct {
|
||||
robin uint32
|
||||
mutex sync.Mutex
|
||||
}
|
||||
|
||||
// Select selects an up host from the pool using a round-robin ordering scheme.
|
||||
func (r *RoundRobin) Select(pool HostPool, request *http.Request) *UpstreamHost {
|
||||
poolLen := uint32(len(pool))
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
// Return next available host
|
||||
for i := uint32(0); i < poolLen; i++ {
|
||||
r.robin++
|
||||
host := pool[r.robin%poolLen]
|
||||
if host.Available() {
|
||||
return host
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// hostByHashing returns an available host from pool based on a hashable string
|
||||
func hostByHashing(pool HostPool, s string) *UpstreamHost {
|
||||
poolLen := uint32(len(pool))
|
||||
index := hash(s) % poolLen
|
||||
for i := uint32(0); i < poolLen; i++ {
|
||||
index += i
|
||||
host := pool[index%poolLen]
|
||||
if host.Available() {
|
||||
return host
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// hash calculates a hash based on string s
|
||||
func hash(s string) uint32 {
|
||||
h := fnv.New32a()
|
||||
h.Write([]byte(s))
|
||||
return h.Sum32()
|
||||
}
|
||||
|
||||
// IPHash is a policy that selects hosts based on hashing the request IP
|
||||
type IPHash struct{}
|
||||
|
||||
// Select selects an up host from the pool based on hashing the request IP
|
||||
func (r *IPHash) Select(pool HostPool, request *http.Request) *UpstreamHost {
|
||||
clientIP, _, err := net.SplitHostPort(request.RemoteAddr)
|
||||
if err != nil {
|
||||
clientIP = request.RemoteAddr
|
||||
}
|
||||
return hostByHashing(pool, clientIP)
|
||||
}
|
||||
|
||||
// URIHash is a policy that selects the host based on hashing the request URI
|
||||
type URIHash struct{}
|
||||
|
||||
// Select selects the host based on hashing the URI
|
||||
func (r *URIHash) Select(pool HostPool, request *http.Request) *UpstreamHost {
|
||||
return hostByHashing(pool, request.RequestURI)
|
||||
}
|
||||
|
||||
// First is a policy that selects the first available host
|
||||
type First struct{}
|
||||
|
||||
// Select selects the first available host from the pool
|
||||
func (r *First) Select(pool HostPool, request *http.Request) *UpstreamHost {
|
||||
for _, host := range pool {
|
||||
if host.Available() {
|
||||
return host
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Header is a policy that selects based on a hash of the given header
|
||||
type Header struct {
|
||||
// The name of the request header, the value of which will determine
|
||||
// how the request is routed
|
||||
Name string
|
||||
}
|
||||
|
||||
// Select selects the host based on hashing the header value
|
||||
func (r *Header) Select(pool HostPool, request *http.Request) *UpstreamHost {
|
||||
if r.Name == "" {
|
||||
return nil
|
||||
}
|
||||
val := request.Header.Get(r.Name)
|
||||
if val == "" {
|
||||
return nil
|
||||
}
|
||||
return hostByHashing(pool, val)
|
||||
}
|
357
vendor/github.com/mholt/caddy/caddyhttp/proxy/policy_test.go
generated
vendored
Normal file
357
vendor/github.com/mholt/caddy/caddyhttp/proxy/policy_test.go
generated
vendored
Normal file
@@ -0,0 +1,357 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var workableServer *httptest.Server
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
workableServer = httptest.NewServer(http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
// do nothing
|
||||
}))
|
||||
r := m.Run()
|
||||
workableServer.Close()
|
||||
os.Exit(r)
|
||||
}
|
||||
|
||||
type customPolicy struct{}
|
||||
|
||||
func (r *customPolicy) Select(pool HostPool, request *http.Request) *UpstreamHost {
|
||||
return pool[0]
|
||||
}
|
||||
|
||||
func testPool() HostPool {
|
||||
pool := []*UpstreamHost{
|
||||
{
|
||||
Name: workableServer.URL, // this should resolve (healthcheck test)
|
||||
},
|
||||
{
|
||||
Name: "http://localhost:99998", // this shouldn't
|
||||
},
|
||||
{
|
||||
Name: "http://C",
|
||||
},
|
||||
}
|
||||
return HostPool(pool)
|
||||
}
|
||||
|
||||
func TestRoundRobinPolicy(t *testing.T) {
|
||||
pool := testPool()
|
||||
rrPolicy := &RoundRobin{}
|
||||
request, _ := http.NewRequest("GET", "/", nil)
|
||||
|
||||
h := rrPolicy.Select(pool, request)
|
||||
// First selected host is 1, because counter starts at 0
|
||||
// and increments before host is selected
|
||||
if h != pool[1] {
|
||||
t.Error("Expected first round robin host to be second host in the pool.")
|
||||
}
|
||||
h = rrPolicy.Select(pool, request)
|
||||
if h != pool[2] {
|
||||
t.Error("Expected second round robin host to be third host in the pool.")
|
||||
}
|
||||
h = rrPolicy.Select(pool, request)
|
||||
if h != pool[0] {
|
||||
t.Error("Expected third round robin host to be first host in the pool.")
|
||||
}
|
||||
// mark host as down
|
||||
pool[1].Unhealthy = 1
|
||||
h = rrPolicy.Select(pool, request)
|
||||
if h != pool[2] {
|
||||
t.Error("Expected to skip down host.")
|
||||
}
|
||||
// mark host as up
|
||||
pool[1].Unhealthy = 0
|
||||
|
||||
h = rrPolicy.Select(pool, request)
|
||||
if h == pool[2] {
|
||||
t.Error("Expected to balance evenly among healthy hosts")
|
||||
}
|
||||
// mark host as full
|
||||
pool[1].Conns = 1
|
||||
pool[1].MaxConns = 1
|
||||
h = rrPolicy.Select(pool, request)
|
||||
if h != pool[2] {
|
||||
t.Error("Expected to skip full host.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLeastConnPolicy(t *testing.T) {
|
||||
pool := testPool()
|
||||
lcPolicy := &LeastConn{}
|
||||
request, _ := http.NewRequest("GET", "/", nil)
|
||||
|
||||
pool[0].Conns = 10
|
||||
pool[1].Conns = 10
|
||||
h := lcPolicy.Select(pool, request)
|
||||
if h != pool[2] {
|
||||
t.Error("Expected least connection host to be third host.")
|
||||
}
|
||||
pool[2].Conns = 100
|
||||
h = lcPolicy.Select(pool, request)
|
||||
if h != pool[0] && h != pool[1] {
|
||||
t.Error("Expected least connection host to be first or second host.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCustomPolicy(t *testing.T) {
|
||||
pool := testPool()
|
||||
customPolicy := &customPolicy{}
|
||||
request, _ := http.NewRequest("GET", "/", nil)
|
||||
|
||||
h := customPolicy.Select(pool, request)
|
||||
if h != pool[0] {
|
||||
t.Error("Expected custom policy host to be the first host.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIPHashPolicy(t *testing.T) {
|
||||
pool := testPool()
|
||||
ipHash := &IPHash{}
|
||||
request, _ := http.NewRequest("GET", "/", nil)
|
||||
// We should be able to predict where every request is routed.
|
||||
request.RemoteAddr = "172.0.0.1:80"
|
||||
h := ipHash.Select(pool, request)
|
||||
if h != pool[1] {
|
||||
t.Error("Expected ip hash policy host to be the second host.")
|
||||
}
|
||||
request.RemoteAddr = "172.0.0.2:80"
|
||||
h = ipHash.Select(pool, request)
|
||||
if h != pool[1] {
|
||||
t.Error("Expected ip hash policy host to be the second host.")
|
||||
}
|
||||
request.RemoteAddr = "172.0.0.3:80"
|
||||
h = ipHash.Select(pool, request)
|
||||
if h != pool[2] {
|
||||
t.Error("Expected ip hash policy host to be the third host.")
|
||||
}
|
||||
request.RemoteAddr = "172.0.0.4:80"
|
||||
h = ipHash.Select(pool, request)
|
||||
if h != pool[1] {
|
||||
t.Error("Expected ip hash policy host to be the second host.")
|
||||
}
|
||||
|
||||
// we should get the same results without a port
|
||||
request.RemoteAddr = "172.0.0.1"
|
||||
h = ipHash.Select(pool, request)
|
||||
if h != pool[1] {
|
||||
t.Error("Expected ip hash policy host to be the second host.")
|
||||
}
|
||||
request.RemoteAddr = "172.0.0.2"
|
||||
h = ipHash.Select(pool, request)
|
||||
if h != pool[1] {
|
||||
t.Error("Expected ip hash policy host to be the second host.")
|
||||
}
|
||||
request.RemoteAddr = "172.0.0.3"
|
||||
h = ipHash.Select(pool, request)
|
||||
if h != pool[2] {
|
||||
t.Error("Expected ip hash policy host to be the third host.")
|
||||
}
|
||||
request.RemoteAddr = "172.0.0.4"
|
||||
h = ipHash.Select(pool, request)
|
||||
if h != pool[1] {
|
||||
t.Error("Expected ip hash policy host to be the second host.")
|
||||
}
|
||||
|
||||
// we should get a healthy host if the original host is unhealthy and a
|
||||
// healthy host is available
|
||||
request.RemoteAddr = "172.0.0.1"
|
||||
pool[1].Unhealthy = 1
|
||||
h = ipHash.Select(pool, request)
|
||||
if h != pool[2] {
|
||||
t.Error("Expected ip hash policy host to be the third host.")
|
||||
}
|
||||
|
||||
request.RemoteAddr = "172.0.0.2"
|
||||
h = ipHash.Select(pool, request)
|
||||
if h != pool[2] {
|
||||
t.Error("Expected ip hash policy host to be the third host.")
|
||||
}
|
||||
pool[1].Unhealthy = 0
|
||||
|
||||
request.RemoteAddr = "172.0.0.3"
|
||||
pool[2].Unhealthy = 1
|
||||
h = ipHash.Select(pool, request)
|
||||
if h != pool[0] {
|
||||
t.Error("Expected ip hash policy host to be the first host.")
|
||||
}
|
||||
request.RemoteAddr = "172.0.0.4"
|
||||
h = ipHash.Select(pool, request)
|
||||
if h != pool[1] {
|
||||
t.Error("Expected ip hash policy host to be the second host.")
|
||||
}
|
||||
|
||||
// We should be able to resize the host pool and still be able to predict
|
||||
// where a request will be routed with the same IP's used above
|
||||
pool = []*UpstreamHost{
|
||||
{
|
||||
Name: workableServer.URL, // this should resolve (healthcheck test)
|
||||
},
|
||||
{
|
||||
Name: "http://localhost:99998", // this shouldn't
|
||||
},
|
||||
}
|
||||
pool = HostPool(pool)
|
||||
request.RemoteAddr = "172.0.0.1:80"
|
||||
h = ipHash.Select(pool, request)
|
||||
if h != pool[0] {
|
||||
t.Error("Expected ip hash policy host to be the first host.")
|
||||
}
|
||||
request.RemoteAddr = "172.0.0.2:80"
|
||||
h = ipHash.Select(pool, request)
|
||||
if h != pool[1] {
|
||||
t.Error("Expected ip hash policy host to be the second host.")
|
||||
}
|
||||
request.RemoteAddr = "172.0.0.3:80"
|
||||
h = ipHash.Select(pool, request)
|
||||
if h != pool[0] {
|
||||
t.Error("Expected ip hash policy host to be the first host.")
|
||||
}
|
||||
request.RemoteAddr = "172.0.0.4:80"
|
||||
h = ipHash.Select(pool, request)
|
||||
if h != pool[1] {
|
||||
t.Error("Expected ip hash policy host to be the second host.")
|
||||
}
|
||||
|
||||
// We should get nil when there are no healthy hosts
|
||||
pool[0].Unhealthy = 1
|
||||
pool[1].Unhealthy = 1
|
||||
h = ipHash.Select(pool, request)
|
||||
if h != nil {
|
||||
t.Error("Expected ip hash policy host to be nil.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFirstPolicy(t *testing.T) {
|
||||
pool := testPool()
|
||||
firstPolicy := &First{}
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
|
||||
h := firstPolicy.Select(pool, req)
|
||||
if h != pool[0] {
|
||||
t.Error("Expected first policy host to be the first host.")
|
||||
}
|
||||
|
||||
pool[0].Unhealthy = 1
|
||||
h = firstPolicy.Select(pool, req)
|
||||
if h != pool[1] {
|
||||
t.Error("Expected first policy host to be the second host.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUriPolicy(t *testing.T) {
|
||||
pool := testPool()
|
||||
uriPolicy := &URIHash{}
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||
h := uriPolicy.Select(pool, request)
|
||||
if h != pool[0] {
|
||||
t.Error("Expected uri policy host to be the first host.")
|
||||
}
|
||||
|
||||
pool[0].Unhealthy = 1
|
||||
h = uriPolicy.Select(pool, request)
|
||||
if h != pool[1] {
|
||||
t.Error("Expected uri policy host to be the first host.")
|
||||
}
|
||||
|
||||
request = httptest.NewRequest(http.MethodGet, "/test_2", nil)
|
||||
h = uriPolicy.Select(pool, request)
|
||||
if h != pool[1] {
|
||||
t.Error("Expected uri policy host to be the second host.")
|
||||
}
|
||||
|
||||
// We should be able to resize the host pool and still be able to predict
|
||||
// where a request will be routed with the same URI's used above
|
||||
pool = []*UpstreamHost{
|
||||
{
|
||||
Name: workableServer.URL, // this should resolve (healthcheck test)
|
||||
},
|
||||
{
|
||||
Name: "http://localhost:99998", // this shouldn't
|
||||
},
|
||||
}
|
||||
|
||||
request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||
h = uriPolicy.Select(pool, request)
|
||||
if h != pool[0] {
|
||||
t.Error("Expected uri policy host to be the first host.")
|
||||
}
|
||||
|
||||
pool[0].Unhealthy = 1
|
||||
h = uriPolicy.Select(pool, request)
|
||||
if h != pool[1] {
|
||||
t.Error("Expected uri policy host to be the first host.")
|
||||
}
|
||||
|
||||
request = httptest.NewRequest(http.MethodGet, "/test_2", nil)
|
||||
h = uriPolicy.Select(pool, request)
|
||||
if h != pool[1] {
|
||||
t.Error("Expected uri policy host to be the second host.")
|
||||
}
|
||||
|
||||
pool[0].Unhealthy = 1
|
||||
pool[1].Unhealthy = 1
|
||||
h = uriPolicy.Select(pool, request)
|
||||
if h != nil {
|
||||
t.Error("Expected uri policy policy host to be nil.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHeaderPolicy(t *testing.T) {
|
||||
pool := testPool()
|
||||
tests := []struct {
|
||||
Policy *Header
|
||||
RequestHeaderName string
|
||||
RequestHeaderValue string
|
||||
NilHost bool
|
||||
HostIndex int
|
||||
}{
|
||||
{&Header{""}, "", "", true, 0},
|
||||
{&Header{""}, "Affinity", "somevalue", true, 0},
|
||||
{&Header{""}, "Affinity", "", true, 0},
|
||||
|
||||
{&Header{"Affinity"}, "", "", true, 0},
|
||||
{&Header{"Affinity"}, "Affinity", "somevalue", false, 1},
|
||||
{&Header{"Affinity"}, "Affinity", "somevalue2", false, 0},
|
||||
{&Header{"Affinity"}, "Affinity", "somevalue3", false, 2},
|
||||
{&Header{"Affinity"}, "Affinity", "", true, 0},
|
||||
}
|
||||
|
||||
for idx, test := range tests {
|
||||
request, _ := http.NewRequest("GET", "/", nil)
|
||||
if test.RequestHeaderName != "" {
|
||||
request.Header.Add(test.RequestHeaderName, test.RequestHeaderValue)
|
||||
}
|
||||
|
||||
host := test.Policy.Select(pool, request)
|
||||
if test.NilHost && host != nil {
|
||||
t.Errorf("%d: Expected host to be nil", idx)
|
||||
}
|
||||
if !test.NilHost && host == nil {
|
||||
t.Errorf("%d: Did not expect host to be nil", idx)
|
||||
}
|
||||
if !test.NilHost && host != pool[test.HostIndex] {
|
||||
t.Errorf("%d: Expected Header policy to be host %d", idx, test.HostIndex)
|
||||
}
|
||||
}
|
||||
}
|
392
vendor/github.com/mholt/caddy/caddyhttp/proxy/proxy.go
generated
vendored
Normal file
392
vendor/github.com/mholt/caddy/caddyhttp/proxy/proxy.go
generated
vendored
Normal file
@@ -0,0 +1,392 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package proxy is middleware that proxies HTTP requests.
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
// Proxy represents a middleware instance that can proxy requests.
|
||||
type Proxy struct {
|
||||
Next httpserver.Handler
|
||||
Upstreams []Upstream
|
||||
}
|
||||
|
||||
// Upstream manages a pool of proxy upstream hosts.
|
||||
type Upstream interface {
|
||||
// The path this upstream host should be routed on
|
||||
From() string
|
||||
|
||||
// Selects an upstream host to be routed to. It
|
||||
// should return a suitable upstream host, or nil
|
||||
// if no such hosts are available.
|
||||
Select(*http.Request) *UpstreamHost
|
||||
|
||||
// Checks if subpath is not an ignored path
|
||||
AllowedPath(string) bool
|
||||
|
||||
// Gets how long to try selecting upstream hosts
|
||||
// in the case of cascading failures.
|
||||
GetTryDuration() time.Duration
|
||||
|
||||
// Gets how long to wait between selecting upstream
|
||||
// hosts in the case of cascading failures.
|
||||
GetTryInterval() time.Duration
|
||||
|
||||
// Gets the number of upstream hosts.
|
||||
GetHostCount() int
|
||||
|
||||
// Gets how long to wait before timing out
|
||||
// the request
|
||||
GetTimeout() time.Duration
|
||||
|
||||
// Stops the upstream from proxying requests to shutdown goroutines cleanly.
|
||||
Stop() error
|
||||
}
|
||||
|
||||
// UpstreamHostDownFunc can be used to customize how Down behaves.
|
||||
type UpstreamHostDownFunc func(*UpstreamHost) bool
|
||||
|
||||
// UpstreamHost represents a single proxy upstream
|
||||
type UpstreamHost struct {
|
||||
// This field is read & written to concurrently, so all access must use
|
||||
// atomic operations.
|
||||
Conns int64 // must be first field to be 64-bit aligned on 32-bit systems
|
||||
MaxConns int64
|
||||
Name string // hostname of this upstream host
|
||||
UpstreamHeaders http.Header
|
||||
DownstreamHeaders http.Header
|
||||
FailTimeout time.Duration
|
||||
CheckDown UpstreamHostDownFunc
|
||||
WithoutPathPrefix string
|
||||
ReverseProxy *ReverseProxy
|
||||
Fails int32
|
||||
// This is an int32 so that we can use atomic operations to do concurrent
|
||||
// reads & writes to this value. The default value of 0 indicates that it
|
||||
// is healthy and any non-zero value indicates unhealthy.
|
||||
Unhealthy int32
|
||||
HealthCheckResult atomic.Value
|
||||
}
|
||||
|
||||
// Down checks whether the upstream host is down or not.
|
||||
// Down will try to use uh.CheckDown first, and will fall
|
||||
// back to some default criteria if necessary.
|
||||
func (uh *UpstreamHost) Down() bool {
|
||||
if uh.CheckDown == nil {
|
||||
// Default settings
|
||||
return atomic.LoadInt32(&uh.Unhealthy) != 0 || atomic.LoadInt32(&uh.Fails) > 0
|
||||
}
|
||||
return uh.CheckDown(uh)
|
||||
}
|
||||
|
||||
// Full checks whether the upstream host has reached its maximum connections
|
||||
func (uh *UpstreamHost) Full() bool {
|
||||
return uh.MaxConns > 0 && atomic.LoadInt64(&uh.Conns) >= uh.MaxConns
|
||||
}
|
||||
|
||||
// Available checks whether the upstream host is available for proxying to
|
||||
func (uh *UpstreamHost) Available() bool {
|
||||
return !uh.Down() && !uh.Full()
|
||||
}
|
||||
|
||||
// ServeHTTP satisfies the httpserver.Handler interface.
|
||||
func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
// start by selecting most specific matching upstream config
|
||||
upstream := p.match(r)
|
||||
if upstream == nil {
|
||||
return p.Next.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// this replacer is used to fill in header field values
|
||||
replacer := httpserver.NewReplacer(r, nil, "")
|
||||
|
||||
// outreq is the request that makes a roundtrip to the backend
|
||||
outreq, cancel := createUpstreamRequest(w, r)
|
||||
defer cancel()
|
||||
|
||||
// If we have more than one upstream host defined and if retrying is enabled
|
||||
// by setting try_duration to a non-zero value, caddy will try to
|
||||
// retry the request at a different host if the first one failed.
|
||||
//
|
||||
// This requires us to possibly rewind and replay the request body though,
|
||||
// which in turn requires us to buffer the request body first.
|
||||
//
|
||||
// An unbuffered request is usually preferrable, because it reduces latency
|
||||
// as well as memory usage. Furthermore it enables different kinds of
|
||||
// HTTP streaming applications like gRPC for instance.
|
||||
requiresBuffering := upstream.GetHostCount() > 1 && upstream.GetTryDuration() != 0
|
||||
|
||||
if requiresBuffering {
|
||||
body, err := newBufferedBody(outreq.Body)
|
||||
if err != nil {
|
||||
return http.StatusBadRequest, errors.New("failed to read downstream request body")
|
||||
}
|
||||
if body != nil {
|
||||
outreq.Body = body
|
||||
}
|
||||
}
|
||||
|
||||
// The keepRetrying function will return true if we should
|
||||
// loop and try to select another host, or false if we
|
||||
// should break and stop retrying.
|
||||
start := time.Now()
|
||||
keepRetrying := func(backendErr error) bool {
|
||||
// if downstream has canceled the request, break
|
||||
if backendErr == context.Canceled {
|
||||
return false
|
||||
}
|
||||
// if we've tried long enough, break
|
||||
if time.Since(start) >= upstream.GetTryDuration() {
|
||||
return false
|
||||
}
|
||||
// otherwise, wait and try the next available host
|
||||
time.Sleep(upstream.GetTryInterval())
|
||||
return true
|
||||
}
|
||||
|
||||
var backendErr error
|
||||
for {
|
||||
// since Select() should give us "up" hosts, keep retrying
|
||||
// hosts until timeout (or until we get a nil host).
|
||||
host := upstream.Select(r)
|
||||
if host == nil {
|
||||
if backendErr == nil {
|
||||
backendErr = errors.New("no hosts available upstream")
|
||||
}
|
||||
if !keepRetrying(backendErr) {
|
||||
break
|
||||
}
|
||||
continue
|
||||
}
|
||||
if rr, ok := w.(*httpserver.ResponseRecorder); ok && rr.Replacer != nil {
|
||||
rr.Replacer.Set("upstream", host.Name)
|
||||
}
|
||||
|
||||
proxy := host.ReverseProxy
|
||||
|
||||
// a backend's name may contain more than just the host,
|
||||
// so we parse it as a URL to try to isolate the host.
|
||||
if nameURL, err := url.Parse(host.Name); err == nil {
|
||||
outreq.Host = nameURL.Host
|
||||
if proxy == nil {
|
||||
proxy = NewSingleHostReverseProxy(nameURL,
|
||||
host.WithoutPathPrefix,
|
||||
http.DefaultMaxIdleConnsPerHost,
|
||||
upstream.GetTimeout(),
|
||||
)
|
||||
}
|
||||
|
||||
// use upstream credentials by default
|
||||
if outreq.Header.Get("Authorization") == "" && nameURL.User != nil {
|
||||
pwd, _ := nameURL.User.Password()
|
||||
outreq.SetBasicAuth(nameURL.User.Username(), pwd)
|
||||
}
|
||||
} else {
|
||||
outreq.Host = host.Name
|
||||
}
|
||||
if proxy == nil {
|
||||
return http.StatusInternalServerError, errors.New("proxy for host '" + host.Name + "' is nil")
|
||||
}
|
||||
|
||||
// set headers for request going upstream
|
||||
if host.UpstreamHeaders != nil {
|
||||
// modify headers for request that will be sent to the upstream host
|
||||
mutateHeadersByRules(outreq.Header, host.UpstreamHeaders, replacer)
|
||||
if hostHeaders, ok := outreq.Header["Host"]; ok && len(hostHeaders) > 0 {
|
||||
outreq.Host = hostHeaders[len(hostHeaders)-1]
|
||||
}
|
||||
}
|
||||
|
||||
// prepare a function that will update response
|
||||
// headers coming back downstream
|
||||
var downHeaderUpdateFn respUpdateFn
|
||||
if host.DownstreamHeaders != nil {
|
||||
downHeaderUpdateFn = createRespHeaderUpdateFn(host.DownstreamHeaders, replacer)
|
||||
}
|
||||
|
||||
// Before we retry the request we have to make sure
|
||||
// that the body is rewound to it's beginning.
|
||||
if bb, ok := outreq.Body.(*bufferedBody); ok {
|
||||
if err := bb.rewind(); err != nil {
|
||||
return http.StatusInternalServerError, errors.New("unable to rewind downstream request body")
|
||||
}
|
||||
}
|
||||
|
||||
// tell the proxy to serve the request
|
||||
//
|
||||
// NOTE:
|
||||
// The call to proxy.ServeHTTP can theoretically panic.
|
||||
// To prevent host.Conns from getting out-of-sync we thus have to
|
||||
// make sure that it's _always_ correctly decremented afterwards.
|
||||
func() {
|
||||
atomic.AddInt64(&host.Conns, 1)
|
||||
defer atomic.AddInt64(&host.Conns, -1)
|
||||
backendErr = proxy.ServeHTTP(w, outreq, downHeaderUpdateFn)
|
||||
}()
|
||||
|
||||
// if no errors, we're done here
|
||||
if backendErr == nil {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
if backendErr == httpserver.ErrMaxBytesExceeded {
|
||||
return http.StatusRequestEntityTooLarge, backendErr
|
||||
}
|
||||
|
||||
// failover; remember this failure for some time if
|
||||
// request failure counting is enabled
|
||||
timeout := host.FailTimeout
|
||||
if timeout > 0 {
|
||||
atomic.AddInt32(&host.Fails, 1)
|
||||
go func(host *UpstreamHost, timeout time.Duration) {
|
||||
time.Sleep(timeout)
|
||||
atomic.AddInt32(&host.Fails, -1)
|
||||
}(host, timeout)
|
||||
}
|
||||
|
||||
// if we've tried long enough, break
|
||||
if !keepRetrying(backendErr) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return http.StatusBadGateway, backendErr
|
||||
}
|
||||
|
||||
// match finds the best match for a proxy config based on r.
|
||||
func (p Proxy) match(r *http.Request) Upstream {
|
||||
var u Upstream
|
||||
var longestMatch int
|
||||
for _, upstream := range p.Upstreams {
|
||||
basePath := upstream.From()
|
||||
if !httpserver.Path(r.URL.Path).Matches(basePath) || !upstream.AllowedPath(r.URL.Path) {
|
||||
continue
|
||||
}
|
||||
if len(basePath) > longestMatch {
|
||||
longestMatch = len(basePath)
|
||||
u = upstream
|
||||
}
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
// createUpstremRequest shallow-copies r into a new request
|
||||
// that can be sent upstream.
|
||||
//
|
||||
// Derived from reverseproxy.go in the standard Go httputil package.
|
||||
func createUpstreamRequest(rw http.ResponseWriter, r *http.Request) (*http.Request, context.CancelFunc) {
|
||||
// Original incoming server request may be canceled by the
|
||||
// user or by std lib(e.g. too many idle connections).
|
||||
ctx, cancel := context.WithCancel(r.Context())
|
||||
if cn, ok := rw.(http.CloseNotifier); ok {
|
||||
notifyChan := cn.CloseNotify()
|
||||
go func() {
|
||||
select {
|
||||
case <-notifyChan:
|
||||
cancel()
|
||||
case <-ctx.Done():
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
outreq := r.WithContext(ctx) // includes shallow copies of maps, but okay
|
||||
|
||||
// We should set body to nil explicitly if request body is empty.
|
||||
// For server requests the Request Body is always non-nil.
|
||||
if r.ContentLength == 0 {
|
||||
outreq.Body = nil
|
||||
}
|
||||
|
||||
// We are modifying the same underlying map from req (shallow
|
||||
// copied above) so we only copy it if necessary.
|
||||
copiedHeaders := false
|
||||
|
||||
// Remove hop-by-hop headers listed in the "Connection" header.
|
||||
// See RFC 2616, section 14.10.
|
||||
if c := outreq.Header.Get("Connection"); c != "" {
|
||||
for _, f := range strings.Split(c, ",") {
|
||||
if f = strings.TrimSpace(f); f != "" {
|
||||
if !copiedHeaders {
|
||||
outreq.Header = make(http.Header)
|
||||
copyHeader(outreq.Header, r.Header)
|
||||
copiedHeaders = true
|
||||
}
|
||||
outreq.Header.Del(f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove hop-by-hop headers to the backend. Especially
|
||||
// important is "Connection" because we want a persistent
|
||||
// connection, regardless of what the client sent to us.
|
||||
for _, h := range hopHeaders {
|
||||
if outreq.Header.Get(h) != "" {
|
||||
if !copiedHeaders {
|
||||
outreq.Header = make(http.Header)
|
||||
copyHeader(outreq.Header, r.Header)
|
||||
copiedHeaders = true
|
||||
}
|
||||
outreq.Header.Del(h)
|
||||
}
|
||||
}
|
||||
|
||||
if clientIP, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
|
||||
// If we aren't the first proxy, retain prior
|
||||
// X-Forwarded-For information as a comma+space
|
||||
// separated list and fold multiple headers into one.
|
||||
if prior, ok := outreq.Header["X-Forwarded-For"]; ok {
|
||||
clientIP = strings.Join(prior, ", ") + ", " + clientIP
|
||||
}
|
||||
outreq.Header.Set("X-Forwarded-For", clientIP)
|
||||
}
|
||||
|
||||
return outreq, cancel
|
||||
}
|
||||
|
||||
func createRespHeaderUpdateFn(rules http.Header, replacer httpserver.Replacer) respUpdateFn {
|
||||
return func(resp *http.Response) {
|
||||
mutateHeadersByRules(resp.Header, rules, replacer)
|
||||
}
|
||||
}
|
||||
|
||||
func mutateHeadersByRules(headers, rules http.Header, repl httpserver.Replacer) {
|
||||
for ruleField, ruleValues := range rules {
|
||||
if strings.HasPrefix(ruleField, "+") {
|
||||
for _, ruleValue := range ruleValues {
|
||||
replacement := repl.Replace(ruleValue)
|
||||
if len(replacement) > 0 {
|
||||
headers.Add(strings.TrimPrefix(ruleField, "+"), replacement)
|
||||
}
|
||||
}
|
||||
} else if strings.HasPrefix(ruleField, "-") {
|
||||
headers.Del(strings.TrimPrefix(ruleField, "-"))
|
||||
} else if len(ruleValues) > 0 {
|
||||
replacement := repl.Replace(ruleValues[len(ruleValues)-1])
|
||||
if len(replacement) > 0 {
|
||||
headers.Set(ruleField, replacement)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
1688
vendor/github.com/mholt/caddy/caddyhttp/proxy/proxy_test.go
generated
vendored
Normal file
1688
vendor/github.com/mholt/caddy/caddyhttp/proxy/proxy_test.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
741
vendor/github.com/mholt/caddy/caddyhttp/proxy/reverseproxy.go
generated
vendored
Normal file
741
vendor/github.com/mholt/caddy/caddyhttp/proxy/reverseproxy.go
generated
vendored
Normal file
@@ -0,0 +1,741 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// This file is adapted from code in the net/http/httputil
|
||||
// package of the Go standard library, which is by the
|
||||
// Go Authors, and bears this copyright and license info:
|
||||
//
|
||||
// Copyright 2011 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
//
|
||||
// This file has been modified from the standard lib to
|
||||
// meet the needs of the application.
|
||||
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/http2"
|
||||
|
||||
"github.com/lucas-clemente/quic-go"
|
||||
"github.com/lucas-clemente/quic-go/h2quic"
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultDialer = &net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}
|
||||
|
||||
bufferPool = sync.Pool{New: createBuffer}
|
||||
|
||||
defaultCryptoHandshakeTimeout = 10 * time.Second
|
||||
)
|
||||
|
||||
func createBuffer() interface{} {
|
||||
return make([]byte, 0, 32*1024)
|
||||
}
|
||||
|
||||
func pooledIoCopy(dst io.Writer, src io.Reader) {
|
||||
buf := bufferPool.Get().([]byte)
|
||||
defer bufferPool.Put(buf)
|
||||
|
||||
// CopyBuffer only uses buf up to its length and panics if it's 0.
|
||||
// Due to that we extend buf's length to its capacity here and
|
||||
// ensure it's always non-zero.
|
||||
bufCap := cap(buf)
|
||||
io.CopyBuffer(dst, src, buf[0:bufCap:bufCap])
|
||||
}
|
||||
|
||||
// onExitFlushLoop is a callback set by tests to detect the state of the
|
||||
// flushLoop() goroutine.
|
||||
var onExitFlushLoop func()
|
||||
|
||||
// ReverseProxy is an HTTP Handler that takes an incoming request and
|
||||
// sends it to another server, proxying the response back to the
|
||||
// client.
|
||||
type ReverseProxy struct {
|
||||
// Director must be a function which modifies
|
||||
// the request into a new request to be sent
|
||||
// using Transport. Its response is then copied
|
||||
// back to the original client unmodified.
|
||||
Director func(*http.Request)
|
||||
|
||||
// The transport used to perform proxy requests.
|
||||
Transport http.RoundTripper
|
||||
|
||||
// FlushInterval specifies the flush interval
|
||||
// to flush to the client while copying the
|
||||
// response body.
|
||||
// If zero, no periodic flushing is done.
|
||||
FlushInterval time.Duration
|
||||
|
||||
// dialer is used when values from the
|
||||
// defaultDialer need to be overridden per Proxy
|
||||
dialer *net.Dialer
|
||||
|
||||
srvResolver srvResolver
|
||||
}
|
||||
|
||||
// Though the relevant directive prefix is just "unix:", url.Parse
|
||||
// will - assuming the regular URL scheme - add additional slashes
|
||||
// as if "unix" was a request protocol.
|
||||
// What we need is just the path, so if "unix:/var/run/www.socket"
|
||||
// was the proxy directive, the parsed hostName would be
|
||||
// "unix:///var/run/www.socket", hence the ambiguous trimming.
|
||||
func socketDial(hostName string, timeout time.Duration) func(network, addr string) (conn net.Conn, err error) {
|
||||
return func(network, addr string) (conn net.Conn, err error) {
|
||||
return net.DialTimeout("unix", hostName[len("unix://"):], timeout)
|
||||
}
|
||||
}
|
||||
|
||||
func (rp *ReverseProxy) srvDialerFunc(locator string, timeout time.Duration) func(network, addr string) (conn net.Conn, err error) {
|
||||
service := locator
|
||||
if strings.HasPrefix(locator, "srv://") {
|
||||
service = locator[6:]
|
||||
} else if strings.HasPrefix(locator, "srv+https://") {
|
||||
service = locator[12:]
|
||||
}
|
||||
|
||||
return func(network, addr string) (conn net.Conn, err error) {
|
||||
_, addrs, err := rp.srvResolver.LookupSRV(context.Background(), "", "", service)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return net.DialTimeout("tcp", fmt.Sprintf("%s:%d", addrs[0].Target, addrs[0].Port), timeout)
|
||||
}
|
||||
}
|
||||
|
||||
func singleJoiningSlash(a, b string) string {
|
||||
aslash := strings.HasSuffix(a, "/")
|
||||
bslash := strings.HasPrefix(b, "/")
|
||||
switch {
|
||||
case aslash && bslash:
|
||||
return a + b[1:]
|
||||
case !aslash && !bslash && b != "":
|
||||
return a + "/" + b
|
||||
}
|
||||
return a + b
|
||||
}
|
||||
|
||||
// NewSingleHostReverseProxy returns a new ReverseProxy that rewrites
|
||||
// URLs to the scheme, host, and base path provided in target. If the
|
||||
// target's path is "/base" and the incoming request was for "/dir",
|
||||
// the target request will be for /base/dir.
|
||||
// Without logic: target's path is "/", incoming is "/api/messages",
|
||||
// without is "/api", then the target request will be for /messages.
|
||||
func NewSingleHostReverseProxy(target *url.URL, without string, keepalive int, timeout time.Duration) *ReverseProxy {
|
||||
targetQuery := target.RawQuery
|
||||
director := func(req *http.Request) {
|
||||
if target.Scheme == "unix" {
|
||||
// to make Dial work with unix URL,
|
||||
// scheme and host have to be faked
|
||||
req.URL.Scheme = "http"
|
||||
req.URL.Host = "socket"
|
||||
} else if target.Scheme == "srv" {
|
||||
req.URL.Scheme = "http"
|
||||
req.URL.Host = target.Host
|
||||
} else if target.Scheme == "srv+https" {
|
||||
req.URL.Scheme = "https"
|
||||
req.URL.Host = target.Host
|
||||
} else {
|
||||
req.URL.Scheme = target.Scheme
|
||||
req.URL.Host = target.Host
|
||||
}
|
||||
|
||||
// remove the `without` prefix
|
||||
if without != "" {
|
||||
req.URL.Path = strings.TrimPrefix(req.URL.Path, without)
|
||||
if req.URL.Opaque != "" {
|
||||
req.URL.Opaque = strings.TrimPrefix(req.URL.Opaque, without)
|
||||
}
|
||||
if req.URL.RawPath != "" {
|
||||
req.URL.RawPath = strings.TrimPrefix(req.URL.RawPath, without)
|
||||
}
|
||||
}
|
||||
|
||||
// prefer returns val if it isn't empty, otherwise def
|
||||
prefer := func(val, def string) string {
|
||||
if val != "" {
|
||||
return val
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
// Make up the final URL by concatenating the request and target URL.
|
||||
//
|
||||
// If there is encoded part in request or target URL,
|
||||
// the final URL should also be in encoded format.
|
||||
// Here, we concatenate their encoded parts which are stored
|
||||
// in URL.Opaque and URL.RawPath, if it is empty use
|
||||
// URL.Path instead.
|
||||
if req.URL.Opaque != "" || target.Opaque != "" {
|
||||
req.URL.Opaque = singleJoiningSlash(
|
||||
prefer(target.Opaque, target.Path),
|
||||
prefer(req.URL.Opaque, req.URL.Path))
|
||||
}
|
||||
if req.URL.RawPath != "" || target.RawPath != "" {
|
||||
req.URL.RawPath = singleJoiningSlash(
|
||||
prefer(target.RawPath, target.Path),
|
||||
prefer(req.URL.RawPath, req.URL.Path))
|
||||
}
|
||||
req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
|
||||
|
||||
// Trims the path of the socket from the URL path.
|
||||
// This is done because req.URL passed to your proxied service
|
||||
// will have the full path of the socket file prefixed to it.
|
||||
// Calling /test on a server that proxies requests to
|
||||
// unix:/var/run/www.socket will thus set the requested path
|
||||
// to /var/run/www.socket/test, rendering paths useless.
|
||||
if target.Scheme == "unix" {
|
||||
// See comment on socketDial for the trim
|
||||
socketPrefix := target.String()[len("unix://"):]
|
||||
req.URL.Path = strings.TrimPrefix(req.URL.Path, socketPrefix)
|
||||
if req.URL.Opaque != "" {
|
||||
req.URL.Opaque = strings.TrimPrefix(req.URL.Opaque, socketPrefix)
|
||||
}
|
||||
if req.URL.RawPath != "" {
|
||||
req.URL.RawPath = strings.TrimPrefix(req.URL.RawPath, socketPrefix)
|
||||
}
|
||||
}
|
||||
|
||||
if targetQuery == "" || req.URL.RawQuery == "" {
|
||||
req.URL.RawQuery = targetQuery + req.URL.RawQuery
|
||||
} else {
|
||||
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
|
||||
}
|
||||
}
|
||||
|
||||
dialer := *defaultDialer
|
||||
if timeout != defaultDialer.Timeout {
|
||||
dialer.Timeout = timeout
|
||||
}
|
||||
|
||||
rp := &ReverseProxy{
|
||||
Director: director,
|
||||
FlushInterval: 250 * time.Millisecond, // flushing good for streaming & server-sent events
|
||||
srvResolver: net.DefaultResolver,
|
||||
dialer: &dialer,
|
||||
}
|
||||
|
||||
if target.Scheme == "unix" {
|
||||
rp.Transport = &http.Transport{
|
||||
Dial: socketDial(target.String(), timeout),
|
||||
}
|
||||
} else if target.Scheme == "quic" {
|
||||
rp.Transport = &h2quic.RoundTripper{
|
||||
QuicConfig: &quic.Config{
|
||||
HandshakeTimeout: defaultCryptoHandshakeTimeout,
|
||||
KeepAlive: true,
|
||||
},
|
||||
}
|
||||
} else if keepalive != http.DefaultMaxIdleConnsPerHost || strings.HasPrefix(target.Scheme, "srv") {
|
||||
dialFunc := rp.dialer.Dial
|
||||
if strings.HasPrefix(target.Scheme, "srv") {
|
||||
dialFunc = rp.srvDialerFunc(target.String(), timeout)
|
||||
}
|
||||
|
||||
transport := &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
Dial: dialFunc,
|
||||
TLSHandshakeTimeout: defaultCryptoHandshakeTimeout,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
}
|
||||
if keepalive == 0 {
|
||||
transport.DisableKeepAlives = true
|
||||
} else {
|
||||
transport.MaxIdleConnsPerHost = keepalive
|
||||
}
|
||||
if httpserver.HTTP2 {
|
||||
http2.ConfigureTransport(transport)
|
||||
}
|
||||
rp.Transport = transport
|
||||
} else {
|
||||
transport := &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
Dial: rp.dialer.Dial,
|
||||
}
|
||||
if httpserver.HTTP2 {
|
||||
http2.ConfigureTransport(transport)
|
||||
}
|
||||
rp.Transport = transport
|
||||
}
|
||||
return rp
|
||||
}
|
||||
|
||||
// UseInsecureTransport is used to facilitate HTTPS proxying
|
||||
// when it is OK for upstream to be using a bad certificate,
|
||||
// since this transport skips verification.
|
||||
func (rp *ReverseProxy) UseInsecureTransport() {
|
||||
if transport, ok := rp.Transport.(*http.Transport); ok {
|
||||
if transport.TLSClientConfig == nil {
|
||||
transport.TLSClientConfig = &tls.Config{}
|
||||
}
|
||||
transport.TLSClientConfig.InsecureSkipVerify = true
|
||||
// No http2.ConfigureTransport() here.
|
||||
// For now this is only added in places where
|
||||
// an http.Transport is actually created.
|
||||
} else if transport, ok := rp.Transport.(*h2quic.RoundTripper); ok {
|
||||
if transport.TLSClientConfig == nil {
|
||||
transport.TLSClientConfig = &tls.Config{}
|
||||
}
|
||||
transport.TLSClientConfig.InsecureSkipVerify = true
|
||||
}
|
||||
}
|
||||
|
||||
// ServeHTTP serves the proxied request to the upstream by performing a roundtrip.
|
||||
// It is designed to handle websocket connection upgrades as well.
|
||||
func (rp *ReverseProxy) ServeHTTP(rw http.ResponseWriter, outreq *http.Request, respUpdateFn respUpdateFn) error {
|
||||
transport := rp.Transport
|
||||
if requestIsWebsocket(outreq) {
|
||||
transport = newConnHijackerTransport(transport)
|
||||
}
|
||||
|
||||
rp.Director(outreq)
|
||||
|
||||
if outreq.URL.Scheme == "quic" {
|
||||
outreq.URL.Scheme = "https" // Change scheme back to https for QUIC RoundTripper
|
||||
}
|
||||
|
||||
res, err := transport.RoundTrip(outreq)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
isWebsocket := res.StatusCode == http.StatusSwitchingProtocols && strings.ToLower(res.Header.Get("Upgrade")) == "websocket"
|
||||
|
||||
// Remove hop-by-hop headers listed in the
|
||||
// "Connection" header of the response.
|
||||
if c := res.Header.Get("Connection"); c != "" {
|
||||
for _, f := range strings.Split(c, ",") {
|
||||
if f = strings.TrimSpace(f); f != "" {
|
||||
res.Header.Del(f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, h := range hopHeaders {
|
||||
res.Header.Del(h)
|
||||
}
|
||||
|
||||
if respUpdateFn != nil {
|
||||
respUpdateFn(res)
|
||||
}
|
||||
|
||||
if isWebsocket {
|
||||
defer res.Body.Close()
|
||||
hj, ok := rw.(http.Hijacker)
|
||||
if !ok {
|
||||
panic(httpserver.NonHijackerError{Underlying: rw})
|
||||
}
|
||||
|
||||
conn, brw, err := hj.Hijack()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
var backendConn net.Conn
|
||||
if hj, ok := transport.(*connHijackerTransport); ok {
|
||||
backendConn = hj.Conn
|
||||
if _, err := conn.Write(hj.Replay); err != nil {
|
||||
return err
|
||||
}
|
||||
bufferPool.Put(hj.Replay)
|
||||
} else {
|
||||
backendConn, err = net.DialTimeout("tcp", outreq.URL.Host, rp.dialer.Timeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
outreq.Write(backendConn)
|
||||
}
|
||||
defer backendConn.Close()
|
||||
|
||||
proxyDone := make(chan struct{}, 2)
|
||||
|
||||
// Proxy backend -> frontend.
|
||||
go func() {
|
||||
pooledIoCopy(conn, backendConn)
|
||||
proxyDone <- struct{}{}
|
||||
}()
|
||||
|
||||
// Proxy frontend -> backend.
|
||||
//
|
||||
// NOTE: Hijack() sometimes returns buffered up bytes in brw which
|
||||
// would be lost if we didn't read them out manually below.
|
||||
if brw != nil {
|
||||
if n := brw.Reader.Buffered(); n > 0 {
|
||||
rbuf, err := brw.Reader.Peek(n)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
backendConn.Write(rbuf)
|
||||
}
|
||||
}
|
||||
go func() {
|
||||
pooledIoCopy(backendConn, conn)
|
||||
proxyDone <- struct{}{}
|
||||
}()
|
||||
|
||||
// If one side is done, we are done.
|
||||
<-proxyDone
|
||||
} else {
|
||||
// NOTE:
|
||||
// Closing the Body involves acquiring a mutex, which is a
|
||||
// unnecessarily heavy operation, considering that this defer will
|
||||
// pretty much never be executed with the Body still unclosed.
|
||||
bodyOpen := true
|
||||
closeBody := func() {
|
||||
if bodyOpen {
|
||||
res.Body.Close()
|
||||
bodyOpen = false
|
||||
}
|
||||
}
|
||||
defer closeBody()
|
||||
|
||||
// Copy all headers over.
|
||||
// res.Header does not include the "Trailer" header,
|
||||
// which means we will have to do that manually below.
|
||||
copyHeader(rw.Header(), res.Header)
|
||||
|
||||
// The "Trailer" header isn't included in res' Header map, which
|
||||
// is why we have to build one ourselves from res.Trailer.
|
||||
//
|
||||
// But res.Trailer does not necessarily contain all trailer keys at this
|
||||
// point yet. The HTTP spec allows one to send "unannounced trailers"
|
||||
// after a request and certain systems like gRPC make use of that.
|
||||
announcedTrailerKeyCount := len(res.Trailer)
|
||||
if announcedTrailerKeyCount > 0 {
|
||||
vv := make([]string, 0, announcedTrailerKeyCount)
|
||||
for k := range res.Trailer {
|
||||
vv = append(vv, k)
|
||||
}
|
||||
rw.Header()["Trailer"] = vv
|
||||
}
|
||||
|
||||
// Now copy over the status code as well as the response body.
|
||||
rw.WriteHeader(res.StatusCode)
|
||||
if announcedTrailerKeyCount > 0 {
|
||||
// Force chunking if we saw a response trailer.
|
||||
// This prevents net/http from calculating the length
|
||||
// for short bodies and adding a Content-Length.
|
||||
if fl, ok := rw.(http.Flusher); ok {
|
||||
fl.Flush()
|
||||
}
|
||||
}
|
||||
rp.copyResponse(rw, res.Body)
|
||||
|
||||
// Now close the body to fully populate res.Trailer.
|
||||
closeBody()
|
||||
|
||||
// Since Go does not remove keys from res.Trailer we
|
||||
// can safely do a length comparison to check wether
|
||||
// we received further, unannounced trailers.
|
||||
//
|
||||
// Most of the time forceSetTrailers should be false.
|
||||
forceSetTrailers := len(res.Trailer) != announcedTrailerKeyCount
|
||||
shallowCopyTrailers(rw.Header(), res.Trailer, forceSetTrailers)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rp *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) {
|
||||
if rp.FlushInterval != 0 {
|
||||
if wf, ok := dst.(writeFlusher); ok {
|
||||
mlw := &maxLatencyWriter{
|
||||
dst: wf,
|
||||
latency: rp.FlushInterval,
|
||||
done: make(chan bool),
|
||||
}
|
||||
go mlw.flushLoop()
|
||||
defer mlw.stop()
|
||||
dst = mlw
|
||||
}
|
||||
}
|
||||
pooledIoCopy(dst, src)
|
||||
}
|
||||
|
||||
// skip these headers if they already exist.
|
||||
// see https://github.com/mholt/caddy/pull/1112#discussion_r80092582
|
||||
var skipHeaders = map[string]struct{}{
|
||||
"Content-Type": {},
|
||||
"Content-Disposition": {},
|
||||
"Accept-Ranges": {},
|
||||
"Set-Cookie": {},
|
||||
"Cache-Control": {},
|
||||
"Expires": {},
|
||||
}
|
||||
|
||||
func copyHeader(dst, src http.Header) {
|
||||
for k, vv := range src {
|
||||
if _, ok := dst[k]; ok {
|
||||
// skip some predefined headers
|
||||
// see https://github.com/mholt/caddy/issues/1086
|
||||
if _, shouldSkip := skipHeaders[k]; shouldSkip {
|
||||
continue
|
||||
}
|
||||
// otherwise, overwrite to avoid duplicated fields that can be
|
||||
// problematic (see issue #1086) -- however, allow duplicate
|
||||
// Server fields so we can see the reality of the proxying.
|
||||
if k != "Server" {
|
||||
dst.Del(k)
|
||||
}
|
||||
}
|
||||
for _, v := range vv {
|
||||
dst.Add(k, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// shallowCopyTrailers copies all headers from srcTrailer to dstHeader.
|
||||
//
|
||||
// If forceSetTrailers is set to true, the http.TrailerPrefix will be added to
|
||||
// all srcTrailer key names. Otherwise the Go stdlib will ignore all keys
|
||||
// which weren't listed in the Trailer map before submitting the Response.
|
||||
//
|
||||
// WARNING: Only a shallow copy will be created!
|
||||
func shallowCopyTrailers(dstHeader, srcTrailer http.Header, forceSetTrailers bool) {
|
||||
for k, vv := range srcTrailer {
|
||||
if forceSetTrailers {
|
||||
k = http.TrailerPrefix + k
|
||||
}
|
||||
dstHeader[k] = vv
|
||||
}
|
||||
}
|
||||
|
||||
// Hop-by-hop headers. These are removed when sent to the backend.
|
||||
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
|
||||
var hopHeaders = []string{
|
||||
"Alt-Svc",
|
||||
"Alternate-Protocol",
|
||||
"Connection",
|
||||
"Keep-Alive",
|
||||
"Proxy-Authenticate",
|
||||
"Proxy-Authorization",
|
||||
"Proxy-Connection", // non-standard but still sent by libcurl and rejected by e.g. google
|
||||
"Te", // canonicalized version of "TE"
|
||||
"Trailer", // not Trailers per URL above; http://www.rfc-editor.org/errata_search.php?eid=4522
|
||||
"Transfer-Encoding",
|
||||
"Upgrade",
|
||||
}
|
||||
|
||||
type respUpdateFn func(resp *http.Response)
|
||||
|
||||
type hijackedConn struct {
|
||||
net.Conn
|
||||
hj *connHijackerTransport
|
||||
}
|
||||
|
||||
func (c *hijackedConn) Read(b []byte) (n int, err error) {
|
||||
n, err = c.Conn.Read(b)
|
||||
c.hj.Replay = append(c.hj.Replay, b[:n]...)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *hijackedConn) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type connHijackerTransport struct {
|
||||
*http.Transport
|
||||
Conn net.Conn
|
||||
Replay []byte
|
||||
}
|
||||
|
||||
func newConnHijackerTransport(base http.RoundTripper) *connHijackerTransport {
|
||||
t := &http.Transport{
|
||||
MaxIdleConnsPerHost: -1,
|
||||
}
|
||||
if b, _ := base.(*http.Transport); b != nil {
|
||||
tlsClientConfig := b.TLSClientConfig
|
||||
if tlsClientConfig != nil && tlsClientConfig.NextProtos != nil {
|
||||
tlsClientConfig = tlsClientConfig.Clone()
|
||||
tlsClientConfig.NextProtos = nil
|
||||
}
|
||||
|
||||
t.Proxy = b.Proxy
|
||||
t.TLSClientConfig = tlsClientConfig
|
||||
t.TLSHandshakeTimeout = b.TLSHandshakeTimeout
|
||||
t.Dial = b.Dial
|
||||
t.DialTLS = b.DialTLS
|
||||
} else {
|
||||
t.Proxy = http.ProxyFromEnvironment
|
||||
t.TLSHandshakeTimeout = 10 * time.Second
|
||||
}
|
||||
hj := &connHijackerTransport{t, nil, bufferPool.Get().([]byte)[:0]}
|
||||
|
||||
dial := getTransportDial(t)
|
||||
dialTLS := getTransportDialTLS(t)
|
||||
t.Dial = func(network, addr string) (net.Conn, error) {
|
||||
c, err := dial(network, addr)
|
||||
hj.Conn = c
|
||||
return &hijackedConn{c, hj}, err
|
||||
}
|
||||
t.DialTLS = func(network, addr string) (net.Conn, error) {
|
||||
c, err := dialTLS(network, addr)
|
||||
hj.Conn = c
|
||||
return &hijackedConn{c, hj}, err
|
||||
}
|
||||
|
||||
return hj
|
||||
}
|
||||
|
||||
// getTransportDial always returns a plain Dialer
|
||||
// and defaults to the existing t.Dial.
|
||||
func getTransportDial(t *http.Transport) func(network, addr string) (net.Conn, error) {
|
||||
if t.Dial != nil {
|
||||
return t.Dial
|
||||
}
|
||||
return defaultDialer.Dial
|
||||
}
|
||||
|
||||
// getTransportDial always returns a TLS Dialer
|
||||
// and defaults to the existing t.DialTLS.
|
||||
func getTransportDialTLS(t *http.Transport) func(network, addr string) (net.Conn, error) {
|
||||
if t.DialTLS != nil {
|
||||
return t.DialTLS
|
||||
}
|
||||
|
||||
// newConnHijackerTransport will modify t.Dial after calling this method
|
||||
// => Create a backup reference.
|
||||
plainDial := getTransportDial(t)
|
||||
|
||||
// The following DialTLS implementation stems from the Go stdlib and
|
||||
// is identical to what happens if DialTLS is not provided.
|
||||
// Source: https://github.com/golang/go/blob/230a376b5a67f0e9341e1fa47e670ff762213c83/src/net/http/transport.go#L1018-L1051
|
||||
return func(network, addr string) (net.Conn, error) {
|
||||
plainConn, err := plainDial(network, addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tlsClientConfig := t.TLSClientConfig
|
||||
if tlsClientConfig == nil {
|
||||
tlsClientConfig = &tls.Config{}
|
||||
}
|
||||
if !tlsClientConfig.InsecureSkipVerify && tlsClientConfig.ServerName == "" {
|
||||
tlsClientConfig.ServerName = stripPort(addr)
|
||||
}
|
||||
|
||||
tlsConn := tls.Client(plainConn, tlsClientConfig)
|
||||
errc := make(chan error, 2)
|
||||
var timer *time.Timer
|
||||
if d := t.TLSHandshakeTimeout; d != 0 {
|
||||
timer = time.AfterFunc(d, func() {
|
||||
errc <- tlsHandshakeTimeoutError{}
|
||||
})
|
||||
}
|
||||
go func() {
|
||||
err := tlsConn.Handshake()
|
||||
if timer != nil {
|
||||
timer.Stop()
|
||||
}
|
||||
errc <- err
|
||||
}()
|
||||
if err := <-errc; err != nil {
|
||||
plainConn.Close()
|
||||
return nil, err
|
||||
}
|
||||
if !tlsClientConfig.InsecureSkipVerify {
|
||||
hostname := tlsClientConfig.ServerName
|
||||
if hostname == "" {
|
||||
hostname = stripPort(addr)
|
||||
}
|
||||
if err := tlsConn.VerifyHostname(hostname); err != nil {
|
||||
plainConn.Close()
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return tlsConn, nil
|
||||
}
|
||||
}
|
||||
|
||||
// stripPort returns address without its port if it has one and
|
||||
// works with IP addresses as well as hostnames formatted as host:port.
|
||||
//
|
||||
// IPv6 addresses (excluding the port) must be enclosed in
|
||||
// square brackets similar to the requirements of Go's stdlib.
|
||||
func stripPort(address string) string {
|
||||
// Keep in mind that the address might be a IPv6 address
|
||||
// and thus contain a colon, but not have a port.
|
||||
portIdx := strings.LastIndex(address, ":")
|
||||
ipv6Idx := strings.LastIndex(address, "]")
|
||||
if portIdx > ipv6Idx {
|
||||
address = address[:portIdx]
|
||||
}
|
||||
return address
|
||||
}
|
||||
|
||||
type tlsHandshakeTimeoutError struct{}
|
||||
|
||||
func (tlsHandshakeTimeoutError) Timeout() bool { return true }
|
||||
func (tlsHandshakeTimeoutError) Temporary() bool { return true }
|
||||
func (tlsHandshakeTimeoutError) Error() string { return "net/http: TLS handshake timeout" }
|
||||
|
||||
func requestIsWebsocket(req *http.Request) bool {
|
||||
return strings.ToLower(req.Header.Get("Upgrade")) == "websocket" && strings.Contains(strings.ToLower(req.Header.Get("Connection")), "upgrade")
|
||||
}
|
||||
|
||||
type writeFlusher interface {
|
||||
io.Writer
|
||||
http.Flusher
|
||||
}
|
||||
|
||||
type maxLatencyWriter struct {
|
||||
dst writeFlusher
|
||||
latency time.Duration
|
||||
|
||||
lk sync.Mutex // protects Write + Flush
|
||||
done chan bool
|
||||
}
|
||||
|
||||
func (m *maxLatencyWriter) Write(p []byte) (int, error) {
|
||||
m.lk.Lock()
|
||||
defer m.lk.Unlock()
|
||||
return m.dst.Write(p)
|
||||
}
|
||||
|
||||
func (m *maxLatencyWriter) flushLoop() {
|
||||
t := time.NewTicker(m.latency)
|
||||
defer t.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-m.done:
|
||||
if onExitFlushLoop != nil {
|
||||
onExitFlushLoop()
|
||||
}
|
||||
return
|
||||
case <-t.C:
|
||||
m.lk.Lock()
|
||||
m.dst.Flush()
|
||||
m.lk.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *maxLatencyWriter) stop() { m.done <- true }
|
95
vendor/github.com/mholt/caddy/caddyhttp/proxy/reverseproxy_test.go
generated
vendored
Normal file
95
vendor/github.com/mholt/caddy/caddyhttp/proxy/reverseproxy_test.go
generated
vendored
Normal file
@@ -0,0 +1,95 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
expectedResponse = "response from request proxied to upstream"
|
||||
expectedStatus = http.StatusOK
|
||||
)
|
||||
|
||||
var upstreamHost *httptest.Server
|
||||
|
||||
func setupTest() {
|
||||
upstreamHost = httptest.NewServer(http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/test-path" {
|
||||
w.WriteHeader(expectedStatus)
|
||||
w.Write([]byte(expectedResponse))
|
||||
} else {
|
||||
w.WriteHeader(404)
|
||||
w.Write([]byte("Not found"))
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
func tearDownTest() {
|
||||
upstreamHost.Close()
|
||||
}
|
||||
|
||||
func TestSingleSRVHostReverseProxy(t *testing.T) {
|
||||
setupTest()
|
||||
defer tearDownTest()
|
||||
|
||||
target, err := url.Parse("srv://test.upstream.service")
|
||||
if err != nil {
|
||||
t.Errorf("Failed to parse target URL. %s", err.Error())
|
||||
}
|
||||
|
||||
upstream, err := url.Parse(upstreamHost.URL)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to parse test server URL [%s]. %s", upstreamHost.URL, err.Error())
|
||||
}
|
||||
pp, err := strconv.Atoi(upstream.Port())
|
||||
if err != nil {
|
||||
t.Errorf("Failed to parse upstream server port [%s]. %s", upstream.Port(), err.Error())
|
||||
}
|
||||
port := uint16(pp)
|
||||
|
||||
rp := NewSingleHostReverseProxy(target, "", http.DefaultMaxIdleConnsPerHost, 30*time.Second)
|
||||
rp.srvResolver = testResolver{
|
||||
result: []*net.SRV{
|
||||
{Target: upstream.Hostname(), Port: port, Priority: 1, Weight: 1},
|
||||
},
|
||||
}
|
||||
|
||||
resp := httptest.NewRecorder()
|
||||
req, err := http.NewRequest("GET", "http://test.host/test-path", nil)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to create new request. %s", err.Error())
|
||||
}
|
||||
|
||||
err = rp.ServeHTTP(resp, req, nil)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to perform reverse proxy to upstream host. %s", err.Error())
|
||||
}
|
||||
|
||||
if resp.Body.String() != expectedResponse {
|
||||
t.Errorf("Unexpected proxy response received. Expected: '%s', Got: '%s'", expectedResponse, resp.Body.String())
|
||||
}
|
||||
|
||||
if resp.Code != expectedStatus {
|
||||
t.Errorf("Unexpected proxy status. Expected: '%d', Got: '%d'", expectedStatus, resp.Code)
|
||||
}
|
||||
}
|
45
vendor/github.com/mholt/caddy/caddyhttp/proxy/setup.go
generated
vendored
Normal file
45
vendor/github.com/mholt/caddy/caddyhttp/proxy/setup.go
generated
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"github.com/mholt/caddy"
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterPlugin("proxy", caddy.Plugin{
|
||||
ServerType: "http",
|
||||
Action: setup,
|
||||
})
|
||||
}
|
||||
|
||||
// setup configures a new Proxy middleware instance.
|
||||
func setup(c *caddy.Controller) error {
|
||||
upstreams, err := NewStaticUpstreams(c.Dispenser, httpserver.GetConfig(c).Host())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
|
||||
return Proxy{Next: next, Upstreams: upstreams}
|
||||
})
|
||||
|
||||
// Register shutdown handlers.
|
||||
for _, upstream := range upstreams {
|
||||
c.OnShutdown(upstream.Stop)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
199
vendor/github.com/mholt/caddy/caddyhttp/proxy/setup_test.go
generated
vendored
Normal file
199
vendor/github.com/mholt/caddy/caddyhttp/proxy/setup_test.go
generated
vendored
Normal file
@@ -0,0 +1,199 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
func TestSetup(t *testing.T) {
|
||||
for i, test := range []struct {
|
||||
input string
|
||||
shouldErr bool
|
||||
expectedHosts map[string]struct{}
|
||||
}{
|
||||
// test #0 test usual to destination still works normally
|
||||
{
|
||||
"proxy / localhost:80",
|
||||
false,
|
||||
map[string]struct{}{
|
||||
"http://localhost:80": {},
|
||||
},
|
||||
},
|
||||
|
||||
// test #1 test usual to destination with port range
|
||||
{
|
||||
"proxy / localhost:8080-8082",
|
||||
false,
|
||||
map[string]struct{}{
|
||||
"http://localhost:8080": {},
|
||||
"http://localhost:8081": {},
|
||||
"http://localhost:8082": {},
|
||||
},
|
||||
},
|
||||
|
||||
// test #2 test upstream directive
|
||||
{
|
||||
"proxy / {\n upstream localhost:8080\n}",
|
||||
false,
|
||||
map[string]struct{}{
|
||||
"http://localhost:8080": {},
|
||||
},
|
||||
},
|
||||
|
||||
// test #3 test upstream directive with port range
|
||||
{
|
||||
"proxy / {\n upstream localhost:8080-8081\n}",
|
||||
false,
|
||||
map[string]struct{}{
|
||||
"http://localhost:8080": {},
|
||||
"http://localhost:8081": {},
|
||||
},
|
||||
},
|
||||
|
||||
// test #4 test to destination with upstream directive
|
||||
{
|
||||
"proxy / localhost:8080 {\n upstream localhost:8081-8082\n}",
|
||||
false,
|
||||
map[string]struct{}{
|
||||
"http://localhost:8080": {},
|
||||
"http://localhost:8081": {},
|
||||
"http://localhost:8082": {},
|
||||
},
|
||||
},
|
||||
|
||||
// test #5 test with unix sockets
|
||||
{
|
||||
"proxy / localhost:8080 {\n upstream unix:/var/foo\n}",
|
||||
false,
|
||||
map[string]struct{}{
|
||||
"http://localhost:8080": {},
|
||||
"unix:/var/foo": {},
|
||||
},
|
||||
},
|
||||
|
||||
// test #6 test fail on malformed port range
|
||||
{
|
||||
"proxy / localhost:8090-8080",
|
||||
true,
|
||||
nil,
|
||||
},
|
||||
|
||||
// test #7 test fail on malformed port range 2
|
||||
{
|
||||
"proxy / {\n upstream localhost:80-A\n}",
|
||||
true,
|
||||
nil,
|
||||
},
|
||||
|
||||
// test #8 test upstreams without ports work correctly
|
||||
{
|
||||
"proxy / http://localhost {\n upstream testendpoint\n}",
|
||||
false,
|
||||
map[string]struct{}{
|
||||
"http://localhost": {},
|
||||
"http://testendpoint": {},
|
||||
},
|
||||
},
|
||||
|
||||
// test #9 test several upstream directives
|
||||
{
|
||||
"proxy / localhost:8080 {\n upstream localhost:8081-8082\n upstream localhost:8083-8085\n}",
|
||||
false,
|
||||
map[string]struct{}{
|
||||
"http://localhost:8080": {},
|
||||
"http://localhost:8081": {},
|
||||
"http://localhost:8082": {},
|
||||
"http://localhost:8083": {},
|
||||
"http://localhost:8084": {},
|
||||
"http://localhost:8085": {},
|
||||
},
|
||||
},
|
||||
// test #10 test hyphen without port range
|
||||
{
|
||||
"proxy / http://localhost:8001/a--b",
|
||||
false,
|
||||
map[string]struct{}{
|
||||
"http://localhost:8001/a--b": {},
|
||||
},
|
||||
},
|
||||
// test #11 test hyphen with port range
|
||||
{
|
||||
"proxy / http://localhost:8001-8005/a--b",
|
||||
false,
|
||||
map[string]struct{}{
|
||||
"http://localhost:8001/a--b": {},
|
||||
"http://localhost:8002/a--b": {},
|
||||
"http://localhost:8003/a--b": {},
|
||||
"http://localhost:8004/a--b": {},
|
||||
"http://localhost:8005/a--b": {},
|
||||
},
|
||||
},
|
||||
// test #12 test value is optional when remove upstream header
|
||||
{
|
||||
"proxy / localhost:1984 {\n header_upstream -server \n}",
|
||||
false,
|
||||
map[string]struct{}{
|
||||
"http://localhost:1984": {},
|
||||
},
|
||||
},
|
||||
// test #13 test value is optional when remove downstream header
|
||||
{
|
||||
"proxy / localhost:1984 {\n header_downstream -server \n}",
|
||||
false,
|
||||
map[string]struct{}{
|
||||
"http://localhost:1984": {},
|
||||
},
|
||||
},
|
||||
// test #14 test QUIC
|
||||
{
|
||||
"proxy / quic://localhost:443",
|
||||
false,
|
||||
map[string]struct{}{
|
||||
"quic://localhost:443": {},
|
||||
},
|
||||
},
|
||||
} {
|
||||
c := caddy.NewTestController("http", test.input)
|
||||
err := setup(c)
|
||||
if err != nil && !test.shouldErr {
|
||||
t.Errorf("Test case #%d received an error of %v", i, err)
|
||||
} else if test.shouldErr {
|
||||
continue
|
||||
}
|
||||
|
||||
mids := httpserver.GetConfig(c).Middleware()
|
||||
mid := mids[len(mids)-1]
|
||||
|
||||
upstreams := mid(nil).(Proxy).Upstreams
|
||||
for _, upstream := range upstreams {
|
||||
val := reflect.ValueOf(upstream).Elem()
|
||||
hosts := val.FieldByName("Hosts").Interface().(HostPool)
|
||||
if len(hosts) != len(test.expectedHosts) {
|
||||
t.Errorf("Test case #%d expected %d hosts but received %d", i, len(test.expectedHosts), len(hosts))
|
||||
} else {
|
||||
for _, host := range hosts {
|
||||
if _, found := test.expectedHosts[host.Name]; !found {
|
||||
t.Errorf("Test case #%d has an unexpected host %s", i, host.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
56
vendor/github.com/mholt/caddy/caddyhttp/proxy/testdata/fullchain.pem
generated
vendored
Normal file
56
vendor/github.com/mholt/caddy/caddyhttp/proxy/testdata/fullchain.pem
generated
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIFAzCCA+ugAwIBAgISA7e2G9wJth5EaqaD5X0RiagYMA0GCSqGSIb3DQEBCwUA
|
||||
MEoxCzAJBgNVBAYTAlVTMRYwFAYDVQQKEw1MZXQncyBFbmNyeXB0MSMwIQYDVQQD
|
||||
ExpMZXQncyBFbmNyeXB0IEF1dGhvcml0eSBYMzAeFw0xNzA3MDMxODU3MDBaFw0x
|
||||
NzEwMDExODU3MDBaMBsxGTAXBgNVBAMTEHF1aWMuY2xlbWVudGUuaW8wggEiMA0G
|
||||
CSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC7UjonSCiB0tyHsenbXZw/QF028EmH
|
||||
tvdyMTOlz2DVLqi0K6brqVIh3KSl2gPlsizLHmkoTLVINuGCnDXc4jXu6yCVHrPr
|
||||
KOf+ip8SUcqQEmLXmHw5Y+L4/6ZKUE5mFpfqmlMCEb1t86J7FI9z+QA9LGdbziYv
|
||||
qQBW8GytX16OJ4h/S1fiPCQ5GfWtkoVgYzgz8Vn4o51lLG2YXAl451BR8+XhGlYS
|
||||
OjS6x7RA0F5wqCeGgro7wKbFuyfxrkWkVzn5hNdEkBAABiub6obNBMZ6v+u84bQk
|
||||
1rH0oZB5rn3uEPycLmrQF2cYR5b+2F+BymKC0ElkFi3iWQoO98SZZQinAgMBAAGj
|
||||
ggIQMIICDDAOBgNVHQ8BAf8EBAMCBaAwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsG
|
||||
AQUFBwMCMAwGA1UdEwEB/wQCMAAwHQYDVR0OBBYEFNjHumfJ0g905MebRnAvNfQh
|
||||
3AvEMB8GA1UdIwQYMBaAFKhKamMEfd265tE5t6ZFZe/zqOyhMG8GCCsGAQUFBwEB
|
||||
BGMwYTAuBggrBgEFBQcwAYYiaHR0cDovL29jc3AuaW50LXgzLmxldHNlbmNyeXB0
|
||||
Lm9yZzAvBggrBgEFBQcwAoYjaHR0cDovL2NlcnQuaW50LXgzLmxldHNlbmNyeXB0
|
||||
Lm9yZy8wGwYDVR0RBBQwEoIQcXVpYy5jbGVtZW50ZS5pbzCB/gYDVR0gBIH2MIHz
|
||||
MAgGBmeBDAECATCB5gYLKwYBBAGC3xMBAQEwgdYwJgYIKwYBBQUHAgEWGmh0dHA6
|
||||
Ly9jcHMubGV0c2VuY3J5cHQub3JnMIGrBggrBgEFBQcCAjCBngyBm1RoaXMgQ2Vy
|
||||
dGlmaWNhdGUgbWF5IG9ubHkgYmUgcmVsaWVkIHVwb24gYnkgUmVseWluZyBQYXJ0
|
||||
aWVzIGFuZCBvbmx5IGluIGFjY29yZGFuY2Ugd2l0aCB0aGUgQ2VydGlmaWNhdGUg
|
||||
UG9saWN5IGZvdW5kIGF0IGh0dHBzOi8vbGV0c2VuY3J5cHQub3JnL3JlcG9zaXRv
|
||||
cnkvMA0GCSqGSIb3DQEBCwUAA4IBAQAqs3Mrr/Erqp1rOFkLwKbStWZniCvqhl58
|
||||
VnScP2CjiBsaLJUuBlWqC215FtX5CrdkIwYrMMkkOZHZI4mPxN64UVqMY5UJRonL
|
||||
GvkeHC5QYsCV09bBHjCei6JDItNH2PCec9+mV9EIQiVzd8xliE3t0eTbjNsa9zf1
|
||||
Qwp64THbiyTIXuh4xgFTxU2u58+RkIRbKGRM1X4jgIv8xjNV4P1c0jUVqaEFkCjR
|
||||
A03becsSv3wqWvPCNQRdVRdoMMghHenDEAGD621McnaXDoNz8pgn/ss1vzrO36gX
|
||||
WZ7CmbgIFdYeMgqQop/252bN2wrNjnxAjLAHo/X1MPEabjoL1C0g
|
||||
-----END CERTIFICATE-----
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/
|
||||
MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT
|
||||
DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow
|
||||
SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT
|
||||
GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC
|
||||
AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF
|
||||
q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8
|
||||
SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0
|
||||
Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA
|
||||
a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj
|
||||
/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T
|
||||
AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG
|
||||
CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv
|
||||
bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k
|
||||
c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw
|
||||
VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC
|
||||
ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz
|
||||
MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu
|
||||
Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF
|
||||
AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo
|
||||
uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/
|
||||
wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu
|
||||
X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG
|
||||
PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6
|
||||
KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==
|
||||
-----END CERTIFICATE-----
|
28
vendor/github.com/mholt/caddy/caddyhttp/proxy/testdata/privkey.pem
generated
vendored
Normal file
28
vendor/github.com/mholt/caddy/caddyhttp/proxy/testdata/privkey.pem
generated
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
-----BEGIN PRIVATE KEY-----
|
||||
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC7UjonSCiB0tyH
|
||||
senbXZw/QF028EmHtvdyMTOlz2DVLqi0K6brqVIh3KSl2gPlsizLHmkoTLVINuGC
|
||||
nDXc4jXu6yCVHrPrKOf+ip8SUcqQEmLXmHw5Y+L4/6ZKUE5mFpfqmlMCEb1t86J7
|
||||
FI9z+QA9LGdbziYvqQBW8GytX16OJ4h/S1fiPCQ5GfWtkoVgYzgz8Vn4o51lLG2Y
|
||||
XAl451BR8+XhGlYSOjS6x7RA0F5wqCeGgro7wKbFuyfxrkWkVzn5hNdEkBAABiub
|
||||
6obNBMZ6v+u84bQk1rH0oZB5rn3uEPycLmrQF2cYR5b+2F+BymKC0ElkFi3iWQoO
|
||||
98SZZQinAgMBAAECggEAbO8EopNz+wuE8+Si+s8VbjMgAjL6j9H3VJEIWASha1gX
|
||||
A6/fAm0VNlv54/lFCu7y3axxut3hDn3b5viw2iMy+h4CdLXGK5s+TuiOWTj3c5E9
|
||||
qeMjWryb4fHJ4q2Q6g15ixTz8OAgKTDl7G2ofujvGqQX92uLCWxepjBrAufTNRcJ
|
||||
OZ3ngqHlKsRXX3nXkAMYrypK7ALF2kuavAGNrDQvPWUZKp3vuvd3Hx/stw0s3Th1
|
||||
XrHZnaAMZlxZg32IiVxs3vR2sACJ0YyOBpERBjjBsIaeyNXfZVrEmNzvo6iVhdhN
|
||||
ZNxrKSnPEfTdFk5pldFbTzNpvCvjbFAlE0aHXNRJAQKBgQDpAzWGkOTE+wmcWJNk
|
||||
oRi4ZJHhK/kckvNg1OZMXAqqZJOPxvwatEFgQ1GZo8rhSzdf64kB9b9I3OjEhd8r
|
||||
M90pt57BqRSq5rbytZBdR2BcbNnKkYF204AS2pkEVvkOVnWz5zSVhd8a0gMx3EdE
|
||||
LKN0r+DLKune8cnAS0BDvBjf3QKBgQDNzRJe9pI29mxUyuLQuKngaa8KmPy1EpbW
|
||||
+d21ET4MjZbH6uPOAe1Q+7aCEA7rjvFoOqGk0w1WIN0i8EaIOuwM2W0jw4VS7AVI
|
||||
rWXTYy9uSnUuLWL6gHNbehqLs6JaADEvytWdXdqiR/XWxCDn7qg2CrjxwmDB/OUm
|
||||
RopmnlkEUwKBgQCreZ4ZUmXYhDmVYiXN5zPO9svYHkkr+wS6HNMCHLYIoQ1qwG/k
|
||||
owR9d+0EGOKDm5u7rhTcaWIEl/WAMliCbZ9zRNrC/8/i2PiHcpAz5QQH4F8CUMQq
|
||||
kwjsVwxGgk60e3IRG7O52ZPPJAAP4GBdzk/X3lqaiREk7WCgb4BymGjhzQKBgEMF
|
||||
mQkCJeXuZKNMm4c7zF8AK/g4kHvrvOHv56sTHXD7H3Kl5WBusjmgb/R1hFZka+v0
|
||||
xDWoYfx9oWbCd0XgYoVgvbFa+G1j3eioR7QK5iR17SmHsGdCM89DuadrbeD/lQUq
|
||||
elzQduZIpyA1KT4/M9q9rTNWiSpD0OChMmtvADBvAoGAAXF3cARv5w0fSZGSRCOw
|
||||
U3LdFNIhBgVdROj2C4ym+uJFErKTkB5kghdUER7UsFH8fVn3JLAb35cQRYGrysYz
|
||||
XF5eK0akNhkO9GLNrK0GbSHKZm9vQxixm5W05aVoUofRHqkkKL1ceC2rhwzp3Q5P
|
||||
1jLabOA4K0DkhNga0YPKJLQ=
|
||||
-----END PRIVATE KEY-----
|
669
vendor/github.com/mholt/caddy/caddyhttp/proxy/upstream.go
generated
vendored
Normal file
669
vendor/github.com/mholt/caddy/caddyhttp/proxy/upstream.go
generated
vendored
Normal file
@@ -0,0 +1,669 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"crypto/tls"
|
||||
|
||||
"github.com/mholt/caddy/caddyfile"
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
var (
|
||||
supportedPolicies = make(map[string]func(string) Policy)
|
||||
)
|
||||
|
||||
type staticUpstream struct {
|
||||
from string
|
||||
upstreamHeaders http.Header
|
||||
downstreamHeaders http.Header
|
||||
stop chan struct{} // Signals running goroutines to stop.
|
||||
wg sync.WaitGroup // Used to wait for running goroutines to stop.
|
||||
Hosts HostPool
|
||||
Policy Policy
|
||||
KeepAlive int
|
||||
Timeout time.Duration
|
||||
FailTimeout time.Duration
|
||||
TryDuration time.Duration
|
||||
TryInterval time.Duration
|
||||
MaxConns int64
|
||||
HealthCheck struct {
|
||||
Client http.Client
|
||||
Path string
|
||||
Interval time.Duration
|
||||
Timeout time.Duration
|
||||
Host string
|
||||
Port string
|
||||
ContentString string
|
||||
}
|
||||
WithoutPathPrefix string
|
||||
IgnoredSubPaths []string
|
||||
insecureSkipVerify bool
|
||||
MaxFails int32
|
||||
resolver srvResolver
|
||||
}
|
||||
|
||||
type srvResolver interface {
|
||||
LookupSRV(context.Context, string, string, string) (string, []*net.SRV, error)
|
||||
}
|
||||
|
||||
// NewStaticUpstreams parses the configuration input and sets up
|
||||
// static upstreams for the proxy middleware. The host string parameter,
|
||||
// if not empty, is used for setting the upstream Host header for the
|
||||
// health checks if the upstream header config requires it.
|
||||
func NewStaticUpstreams(c caddyfile.Dispenser, host string) ([]Upstream, error) {
|
||||
var upstreams []Upstream
|
||||
for c.Next() {
|
||||
|
||||
upstream := &staticUpstream{
|
||||
from: "",
|
||||
stop: make(chan struct{}),
|
||||
upstreamHeaders: make(http.Header),
|
||||
downstreamHeaders: make(http.Header),
|
||||
Hosts: nil,
|
||||
Policy: &Random{},
|
||||
MaxFails: 1,
|
||||
TryInterval: 250 * time.Millisecond,
|
||||
MaxConns: 0,
|
||||
KeepAlive: http.DefaultMaxIdleConnsPerHost,
|
||||
Timeout: 30 * time.Second,
|
||||
resolver: net.DefaultResolver,
|
||||
}
|
||||
|
||||
if !c.Args(&upstream.from) {
|
||||
return upstreams, c.ArgErr()
|
||||
}
|
||||
|
||||
var to []string
|
||||
hasSrv := false
|
||||
|
||||
for _, t := range c.RemainingArgs() {
|
||||
if len(to) > 0 && hasSrv {
|
||||
return upstreams, c.Err("only one upstream is supported when using SRV locator")
|
||||
}
|
||||
|
||||
if strings.HasPrefix(t, "srv://") || strings.HasPrefix(t, "srv+https://") {
|
||||
if len(to) > 0 {
|
||||
return upstreams, c.Err("service locator upstreams can not be mixed with host names")
|
||||
}
|
||||
|
||||
hasSrv = true
|
||||
}
|
||||
|
||||
parsed, err := parseUpstream(t)
|
||||
if err != nil {
|
||||
return upstreams, err
|
||||
}
|
||||
to = append(to, parsed...)
|
||||
}
|
||||
|
||||
for c.NextBlock() {
|
||||
switch c.Val() {
|
||||
case "upstream":
|
||||
if !c.NextArg() {
|
||||
return upstreams, c.ArgErr()
|
||||
}
|
||||
|
||||
if hasSrv {
|
||||
return upstreams, c.Err("upstream directive is not supported when backend is service locator")
|
||||
}
|
||||
|
||||
parsed, err := parseUpstream(c.Val())
|
||||
if err != nil {
|
||||
return upstreams, err
|
||||
}
|
||||
to = append(to, parsed...)
|
||||
default:
|
||||
if err := parseBlock(&c, upstream, hasSrv); err != nil {
|
||||
return upstreams, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(to) == 0 {
|
||||
return upstreams, c.ArgErr()
|
||||
}
|
||||
|
||||
upstream.Hosts = make([]*UpstreamHost, len(to))
|
||||
for i, host := range to {
|
||||
uh, err := upstream.NewHost(host)
|
||||
if err != nil {
|
||||
return upstreams, err
|
||||
}
|
||||
upstream.Hosts[i] = uh
|
||||
}
|
||||
|
||||
if upstream.HealthCheck.Path != "" {
|
||||
upstream.HealthCheck.Client = http.Client{
|
||||
Timeout: upstream.HealthCheck.Timeout,
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: upstream.insecureSkipVerify},
|
||||
},
|
||||
}
|
||||
|
||||
// set up health check upstream host if we have one
|
||||
if host != "" {
|
||||
hostHeader := upstream.upstreamHeaders.Get("Host")
|
||||
if strings.Contains(hostHeader, "{host}") {
|
||||
upstream.HealthCheck.Host = strings.Replace(hostHeader, "{host}", host, -1)
|
||||
}
|
||||
}
|
||||
upstream.wg.Add(1)
|
||||
go func() {
|
||||
defer upstream.wg.Done()
|
||||
upstream.HealthCheckWorker(upstream.stop)
|
||||
}()
|
||||
}
|
||||
upstreams = append(upstreams, upstream)
|
||||
}
|
||||
return upstreams, nil
|
||||
}
|
||||
|
||||
func (u *staticUpstream) From() string {
|
||||
return u.from
|
||||
}
|
||||
|
||||
func (u *staticUpstream) NewHost(host string) (*UpstreamHost, error) {
|
||||
if !strings.HasPrefix(host, "http") &&
|
||||
!strings.HasPrefix(host, "unix:") &&
|
||||
!strings.HasPrefix(host, "quic:") &&
|
||||
!strings.HasPrefix(host, "srv://") &&
|
||||
!strings.HasPrefix(host, "srv+https://") {
|
||||
host = "http://" + host
|
||||
}
|
||||
uh := &UpstreamHost{
|
||||
Name: host,
|
||||
Conns: 0,
|
||||
Fails: 0,
|
||||
FailTimeout: u.FailTimeout,
|
||||
Unhealthy: 0,
|
||||
UpstreamHeaders: u.upstreamHeaders,
|
||||
DownstreamHeaders: u.downstreamHeaders,
|
||||
CheckDown: func(u *staticUpstream) UpstreamHostDownFunc {
|
||||
return func(uh *UpstreamHost) bool {
|
||||
if atomic.LoadInt32(&uh.Unhealthy) != 0 {
|
||||
return true
|
||||
}
|
||||
if atomic.LoadInt32(&uh.Fails) >= u.MaxFails {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}(u),
|
||||
WithoutPathPrefix: u.WithoutPathPrefix,
|
||||
MaxConns: u.MaxConns,
|
||||
HealthCheckResult: atomic.Value{},
|
||||
}
|
||||
|
||||
baseURL, err := url.Parse(uh.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
uh.ReverseProxy = NewSingleHostReverseProxy(baseURL, uh.WithoutPathPrefix, u.KeepAlive, u.Timeout)
|
||||
if u.insecureSkipVerify {
|
||||
uh.ReverseProxy.UseInsecureTransport()
|
||||
}
|
||||
|
||||
return uh, nil
|
||||
}
|
||||
|
||||
func parseUpstream(u string) ([]string, error) {
|
||||
if strings.HasPrefix(u, "unix:") {
|
||||
return []string{u}, nil
|
||||
}
|
||||
|
||||
isSrv := strings.HasPrefix(u, "srv://") || strings.HasPrefix(u, "srv+https://")
|
||||
colonIdx := strings.LastIndex(u, ":")
|
||||
protoIdx := strings.Index(u, "://")
|
||||
|
||||
if colonIdx == -1 || colonIdx == protoIdx {
|
||||
return []string{u}, nil
|
||||
}
|
||||
|
||||
if isSrv {
|
||||
return nil, fmt.Errorf("service locator %s can not have port specified", u)
|
||||
}
|
||||
|
||||
us := u[:colonIdx]
|
||||
ue := ""
|
||||
portsEnd := len(u)
|
||||
if nextSlash := strings.Index(u[colonIdx:], "/"); nextSlash != -1 {
|
||||
portsEnd = colonIdx + nextSlash
|
||||
ue = u[portsEnd:]
|
||||
}
|
||||
|
||||
ports := u[len(us)+1 : portsEnd]
|
||||
separators := strings.Count(ports, "-")
|
||||
|
||||
if separators == 0 {
|
||||
return []string{u}, nil
|
||||
}
|
||||
|
||||
if separators > 1 {
|
||||
return nil, fmt.Errorf("port range [%s] has %d separators", ports, separators)
|
||||
}
|
||||
|
||||
portsStr := strings.Split(ports, "-")
|
||||
pIni, err := strconv.Atoi(portsStr[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pEnd, err := strconv.Atoi(portsStr[1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if pEnd <= pIni {
|
||||
return nil, fmt.Errorf("port range [%s] is invalid", ports)
|
||||
}
|
||||
|
||||
hosts := []string{}
|
||||
for p := pIni; p <= pEnd; p++ {
|
||||
hosts = append(hosts, fmt.Sprintf("%s:%d%s", us, p, ue))
|
||||
}
|
||||
|
||||
return hosts, nil
|
||||
}
|
||||
|
||||
func parseBlock(c *caddyfile.Dispenser, u *staticUpstream, hasSrv bool) error {
|
||||
switch c.Val() {
|
||||
case "policy":
|
||||
if !c.NextArg() {
|
||||
return c.ArgErr()
|
||||
}
|
||||
policyCreateFunc, ok := supportedPolicies[c.Val()]
|
||||
if !ok {
|
||||
return c.ArgErr()
|
||||
}
|
||||
arg := ""
|
||||
if c.NextArg() {
|
||||
arg = c.Val()
|
||||
}
|
||||
u.Policy = policyCreateFunc(arg)
|
||||
case "fail_timeout":
|
||||
if !c.NextArg() {
|
||||
return c.ArgErr()
|
||||
}
|
||||
dur, err := time.ParseDuration(c.Val())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
u.FailTimeout = dur
|
||||
case "max_fails":
|
||||
if !c.NextArg() {
|
||||
return c.ArgErr()
|
||||
}
|
||||
n, err := strconv.Atoi(c.Val())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n < 1 {
|
||||
return c.Err("max_fails must be at least 1")
|
||||
}
|
||||
u.MaxFails = int32(n)
|
||||
case "try_duration":
|
||||
if !c.NextArg() {
|
||||
return c.ArgErr()
|
||||
}
|
||||
dur, err := time.ParseDuration(c.Val())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
u.TryDuration = dur
|
||||
case "try_interval":
|
||||
if !c.NextArg() {
|
||||
return c.ArgErr()
|
||||
}
|
||||
interval, err := time.ParseDuration(c.Val())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
u.TryInterval = interval
|
||||
case "max_conns":
|
||||
if !c.NextArg() {
|
||||
return c.ArgErr()
|
||||
}
|
||||
n, err := strconv.ParseInt(c.Val(), 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
u.MaxConns = n
|
||||
case "health_check":
|
||||
if !c.NextArg() {
|
||||
return c.ArgErr()
|
||||
}
|
||||
u.HealthCheck.Path = c.Val()
|
||||
|
||||
// Set defaults
|
||||
if u.HealthCheck.Interval == 0 {
|
||||
u.HealthCheck.Interval = 30 * time.Second
|
||||
}
|
||||
if u.HealthCheck.Timeout == 0 {
|
||||
u.HealthCheck.Timeout = 60 * time.Second
|
||||
}
|
||||
case "health_check_interval":
|
||||
var interval string
|
||||
if !c.Args(&interval) {
|
||||
return c.ArgErr()
|
||||
}
|
||||
dur, err := time.ParseDuration(interval)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
u.HealthCheck.Interval = dur
|
||||
case "health_check_timeout":
|
||||
var interval string
|
||||
if !c.Args(&interval) {
|
||||
return c.ArgErr()
|
||||
}
|
||||
dur, err := time.ParseDuration(interval)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
u.HealthCheck.Timeout = dur
|
||||
case "health_check_port":
|
||||
if !c.NextArg() {
|
||||
return c.ArgErr()
|
||||
}
|
||||
|
||||
if hasSrv {
|
||||
return c.Err("health_check_port directive is not allowed when upstream is SRV locator")
|
||||
}
|
||||
|
||||
port := c.Val()
|
||||
n, err := strconv.Atoi(port)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if n < 0 {
|
||||
return c.Errf("invalid health_check_port '%s'", port)
|
||||
}
|
||||
u.HealthCheck.Port = port
|
||||
case "health_check_contains":
|
||||
if !c.NextArg() {
|
||||
return c.ArgErr()
|
||||
}
|
||||
u.HealthCheck.ContentString = c.Val()
|
||||
case "header_upstream":
|
||||
var header, value string
|
||||
if !c.Args(&header, &value) {
|
||||
// When removing a header, the value can be optional.
|
||||
if !strings.HasPrefix(header, "-") {
|
||||
return c.ArgErr()
|
||||
}
|
||||
}
|
||||
u.upstreamHeaders.Add(header, value)
|
||||
case "header_downstream":
|
||||
var header, value string
|
||||
if !c.Args(&header, &value) {
|
||||
// When removing a header, the value can be optional.
|
||||
if !strings.HasPrefix(header, "-") {
|
||||
return c.ArgErr()
|
||||
}
|
||||
}
|
||||
u.downstreamHeaders.Add(header, value)
|
||||
case "transparent":
|
||||
// Note: X-Forwarded-For header is always being appended for proxy connections
|
||||
// See implementation of createUpstreamRequest in proxy.go
|
||||
u.upstreamHeaders.Add("Host", "{host}")
|
||||
u.upstreamHeaders.Add("X-Real-IP", "{remote}")
|
||||
u.upstreamHeaders.Add("X-Forwarded-Proto", "{scheme}")
|
||||
case "websocket":
|
||||
u.upstreamHeaders.Add("Connection", "{>Connection}")
|
||||
u.upstreamHeaders.Add("Upgrade", "{>Upgrade}")
|
||||
case "without":
|
||||
if !c.NextArg() {
|
||||
return c.ArgErr()
|
||||
}
|
||||
u.WithoutPathPrefix = c.Val()
|
||||
case "except":
|
||||
ignoredPaths := c.RemainingArgs()
|
||||
if len(ignoredPaths) == 0 {
|
||||
return c.ArgErr()
|
||||
}
|
||||
u.IgnoredSubPaths = ignoredPaths
|
||||
case "insecure_skip_verify":
|
||||
u.insecureSkipVerify = true
|
||||
case "keepalive":
|
||||
if !c.NextArg() {
|
||||
return c.ArgErr()
|
||||
}
|
||||
n, err := strconv.Atoi(c.Val())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n < 0 {
|
||||
return c.ArgErr()
|
||||
}
|
||||
u.KeepAlive = n
|
||||
case "timeout":
|
||||
if !c.NextArg() {
|
||||
return c.ArgErr()
|
||||
}
|
||||
dur, err := time.ParseDuration(c.Val())
|
||||
if err != nil {
|
||||
return c.Errf("unable to parse timeout duration '%s'", c.Val())
|
||||
}
|
||||
u.Timeout = dur
|
||||
default:
|
||||
return c.Errf("unknown property '%s'", c.Val())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *staticUpstream) resolveHost(h string) ([]string, bool, error) {
|
||||
names := []string{}
|
||||
proto := "http"
|
||||
if !strings.HasPrefix(h, "srv://") && !strings.HasPrefix(h, "srv+https://") {
|
||||
return []string{h}, false, nil
|
||||
}
|
||||
|
||||
if strings.HasPrefix(h, "srv+https://") {
|
||||
proto = "https"
|
||||
}
|
||||
|
||||
_, addrs, err := u.resolver.LookupSRV(context.Background(), "", "", h)
|
||||
if err != nil {
|
||||
return names, true, err
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
names = append(names, fmt.Sprintf("%s://%s:%d", proto, addr.Target, addr.Port))
|
||||
}
|
||||
|
||||
return names, true, nil
|
||||
}
|
||||
|
||||
func (u *staticUpstream) healthCheck() {
|
||||
for _, host := range u.Hosts {
|
||||
candidates, isSrv, err := u.resolveHost(host.Name)
|
||||
if err != nil {
|
||||
host.HealthCheckResult.Store(err.Error())
|
||||
atomic.StoreInt32(&host.Unhealthy, 1)
|
||||
continue
|
||||
}
|
||||
|
||||
unhealthyCount := 0
|
||||
for _, addr := range candidates {
|
||||
hostURL := addr
|
||||
if !isSrv && u.HealthCheck.Port != "" {
|
||||
hostURL = replacePort(hostURL, u.HealthCheck.Port)
|
||||
}
|
||||
hostURL += u.HealthCheck.Path
|
||||
|
||||
unhealthy := func() bool {
|
||||
// set up request, needed to be able to modify headers
|
||||
// possible errors are bad HTTP methods or un-parsable urls
|
||||
req, err := http.NewRequest("GET", hostURL, nil)
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
// set host for request going upstream
|
||||
if u.HealthCheck.Host != "" {
|
||||
req.Host = u.HealthCheck.Host
|
||||
}
|
||||
r, err := u.HealthCheck.Client.Do(req)
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
defer func() {
|
||||
io.Copy(ioutil.Discard, r.Body)
|
||||
r.Body.Close()
|
||||
}()
|
||||
if r.StatusCode < 200 || r.StatusCode >= 400 {
|
||||
return true
|
||||
}
|
||||
if u.HealthCheck.ContentString == "" { // don't check for content string
|
||||
return false
|
||||
}
|
||||
// TODO ReadAll will be replaced if deemed necessary
|
||||
// See https://github.com/mholt/caddy/pull/1691
|
||||
buf, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
if bytes.Contains(buf, []byte(u.HealthCheck.ContentString)) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}()
|
||||
|
||||
if unhealthy {
|
||||
unhealthyCount++
|
||||
}
|
||||
}
|
||||
|
||||
if unhealthyCount == len(candidates) {
|
||||
atomic.StoreInt32(&host.Unhealthy, 1)
|
||||
host.HealthCheckResult.Store("Failed")
|
||||
} else {
|
||||
atomic.StoreInt32(&host.Unhealthy, 0)
|
||||
host.HealthCheckResult.Store("OK")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (u *staticUpstream) HealthCheckWorker(stop chan struct{}) {
|
||||
ticker := time.NewTicker(u.HealthCheck.Interval)
|
||||
u.healthCheck()
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
u.healthCheck()
|
||||
case <-stop:
|
||||
ticker.Stop()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (u *staticUpstream) Select(r *http.Request) *UpstreamHost {
|
||||
pool := u.Hosts
|
||||
if len(pool) == 1 {
|
||||
if !pool[0].Available() {
|
||||
return nil
|
||||
}
|
||||
return pool[0]
|
||||
}
|
||||
allUnavailable := true
|
||||
for _, host := range pool {
|
||||
if host.Available() {
|
||||
allUnavailable = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if allUnavailable {
|
||||
return nil
|
||||
}
|
||||
if u.Policy == nil {
|
||||
return (&Random{}).Select(pool, r)
|
||||
}
|
||||
return u.Policy.Select(pool, r)
|
||||
}
|
||||
|
||||
func (u *staticUpstream) AllowedPath(requestPath string) bool {
|
||||
for _, ignoredSubPath := range u.IgnoredSubPaths {
|
||||
if httpserver.Path(path.Clean(requestPath)).Matches(path.Join(u.From(), ignoredSubPath)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// GetTryDuration returns u.TryDuration.
|
||||
func (u *staticUpstream) GetTryDuration() time.Duration {
|
||||
return u.TryDuration
|
||||
}
|
||||
|
||||
// GetTryInterval returns u.TryInterval.
|
||||
func (u *staticUpstream) GetTryInterval() time.Duration {
|
||||
return u.TryInterval
|
||||
}
|
||||
|
||||
// GetTimeout returns u.Timeout.
|
||||
func (u *staticUpstream) GetTimeout() time.Duration {
|
||||
return u.Timeout
|
||||
}
|
||||
|
||||
func (u *staticUpstream) GetHostCount() int {
|
||||
return len(u.Hosts)
|
||||
}
|
||||
|
||||
// Stop sends a signal to all goroutines started by this staticUpstream to exit
|
||||
// and waits for them to finish before returning.
|
||||
func (u *staticUpstream) Stop() error {
|
||||
close(u.stop)
|
||||
u.wg.Wait()
|
||||
return nil
|
||||
}
|
||||
|
||||
// RegisterPolicy adds a custom policy to the proxy.
|
||||
func RegisterPolicy(name string, policy func(string) Policy) {
|
||||
supportedPolicies[name] = policy
|
||||
}
|
||||
|
||||
func replacePort(originalURL string, newPort string) string {
|
||||
parsedURL, err := url.Parse(originalURL)
|
||||
if err != nil {
|
||||
return originalURL
|
||||
}
|
||||
|
||||
// handles 'localhost' and 'localhost:8080'
|
||||
parsedHost, _, err := net.SplitHostPort(parsedURL.Host)
|
||||
if err != nil {
|
||||
parsedHost = parsedURL.Host
|
||||
}
|
||||
|
||||
parsedURL.Host = net.JoinHostPort(parsedHost, newPort)
|
||||
return parsedURL.String()
|
||||
}
|
776
vendor/github.com/mholt/caddy/caddyhttp/proxy/upstream_test.go
generated
vendored
Normal file
776
vendor/github.com/mholt/caddy/caddyhttp/proxy/upstream_test.go
generated
vendored
Normal file
@@ -0,0 +1,776 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/lucas-clemente/quic-go/h2quic"
|
||||
"github.com/mholt/caddy/caddyfile"
|
||||
)
|
||||
|
||||
func TestNewHost(t *testing.T) {
|
||||
upstream := &staticUpstream{
|
||||
FailTimeout: 10 * time.Second,
|
||||
MaxConns: 1,
|
||||
MaxFails: 1,
|
||||
}
|
||||
|
||||
uh, err := upstream.NewHost("example.com")
|
||||
if err != nil {
|
||||
t.Error("Expected no error")
|
||||
}
|
||||
if uh.Name != "http://example.com" {
|
||||
t.Error("Expected default schema to be added to Name.")
|
||||
}
|
||||
if uh.FailTimeout != upstream.FailTimeout {
|
||||
t.Error("Expected default FailTimeout to be set.")
|
||||
}
|
||||
if uh.MaxConns != upstream.MaxConns {
|
||||
t.Error("Expected default MaxConns to be set.")
|
||||
}
|
||||
if uh.CheckDown == nil {
|
||||
t.Error("Expected default CheckDown to be set.")
|
||||
}
|
||||
if uh.CheckDown(uh) {
|
||||
t.Error("Expected new host not to be down.")
|
||||
}
|
||||
// mark Unhealthy
|
||||
uh.Unhealthy = 1
|
||||
if !uh.CheckDown(uh) {
|
||||
t.Error("Expected unhealthy host to be down.")
|
||||
}
|
||||
// mark with Fails
|
||||
uh.Unhealthy = 0
|
||||
uh.Fails = 1
|
||||
if !uh.CheckDown(uh) {
|
||||
t.Error("Expected failed host to be down.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthCheck(t *testing.T) {
|
||||
upstream := &staticUpstream{
|
||||
from: "",
|
||||
Hosts: testPool(),
|
||||
Policy: &Random{},
|
||||
FailTimeout: 10 * time.Second,
|
||||
MaxFails: 1,
|
||||
}
|
||||
upstream.healthCheck()
|
||||
if upstream.Hosts[0].Down() {
|
||||
t.Error("Expected first host in testpool to not fail healthcheck.")
|
||||
}
|
||||
if !upstream.Hosts[1].Down() {
|
||||
t.Error("Expected second host in testpool to fail healthcheck.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelect(t *testing.T) {
|
||||
upstream := &staticUpstream{
|
||||
from: "",
|
||||
Hosts: testPool()[:3],
|
||||
Policy: &Random{},
|
||||
FailTimeout: 10 * time.Second,
|
||||
MaxFails: 1,
|
||||
}
|
||||
r, _ := http.NewRequest("GET", "/", nil)
|
||||
upstream.Hosts[0].Unhealthy = 1
|
||||
upstream.Hosts[1].Unhealthy = 1
|
||||
upstream.Hosts[2].Unhealthy = 1
|
||||
if h := upstream.Select(r); h != nil {
|
||||
t.Error("Expected select to return nil as all host are down")
|
||||
}
|
||||
upstream.Hosts[2].Unhealthy = 0
|
||||
if h := upstream.Select(r); h == nil {
|
||||
t.Error("Expected select to not return nil")
|
||||
}
|
||||
upstream.Hosts[0].Conns = 1
|
||||
upstream.Hosts[0].MaxConns = 1
|
||||
upstream.Hosts[1].Conns = 1
|
||||
upstream.Hosts[1].MaxConns = 1
|
||||
upstream.Hosts[2].Conns = 1
|
||||
upstream.Hosts[2].MaxConns = 1
|
||||
if h := upstream.Select(r); h != nil {
|
||||
t.Error("Expected select to return nil as all hosts are full")
|
||||
}
|
||||
upstream.Hosts[2].Conns = 0
|
||||
if h := upstream.Select(r); h == nil {
|
||||
t.Error("Expected select to not return nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterPolicy(t *testing.T) {
|
||||
name := "custom"
|
||||
customPolicy := &customPolicy{}
|
||||
RegisterPolicy(name, func(string) Policy { return customPolicy })
|
||||
if _, ok := supportedPolicies[name]; !ok {
|
||||
t.Error("Expected supportedPolicies to have a custom policy.")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestAllowedPaths(t *testing.T) {
|
||||
upstream := &staticUpstream{
|
||||
from: "/proxy",
|
||||
IgnoredSubPaths: []string{"/download", "/static"},
|
||||
}
|
||||
tests := []struct {
|
||||
url string
|
||||
expected bool
|
||||
}{
|
||||
{"/proxy", true},
|
||||
{"/proxy/dl", true},
|
||||
{"/proxy/download", false},
|
||||
{"/proxy/download/static", false},
|
||||
{"/proxy/static", false},
|
||||
{"/proxy/static/download", false},
|
||||
{"/proxy/something/download", true},
|
||||
{"/proxy/something/static", true},
|
||||
{"/proxy//static", false},
|
||||
{"/proxy//static//download", false},
|
||||
{"/proxy//download", false},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
allowed := upstream.AllowedPath(test.url)
|
||||
if test.expected != allowed {
|
||||
t.Errorf("Test %d: expected %v found %v", i+1, test.expected, allowed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseBlockHealthCheck(t *testing.T) {
|
||||
tests := []struct {
|
||||
config string
|
||||
interval string
|
||||
timeout string
|
||||
}{
|
||||
// Test #1: Both options set correct time
|
||||
{"health_check /health\n health_check_interval 10s\n health_check_timeout 20s", "10s", "20s"},
|
||||
|
||||
// Test #2: Health check options flipped around. Making sure health_check doesn't overwrite it
|
||||
{"health_check_interval 10s\n health_check_timeout 20s\n health_check /health", "10s", "20s"},
|
||||
|
||||
// Test #3: No health_check options. So default.
|
||||
{"health_check /health", "30s", "1m0s"},
|
||||
|
||||
// Test #4: Interval sets it to 15s and timeout defaults
|
||||
{"health_check /health\n health_check_interval 15s", "15s", "1m0s"},
|
||||
|
||||
// Test #5: Timeout sets it to 15s and interval defaults
|
||||
{"health_check /health\n health_check_timeout 15s", "30s", "15s"},
|
||||
|
||||
// Test #6: Some funky spelling to make sure it still defaults
|
||||
{"health_check /health health_check_time 15s", "30s", "1m0s"},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
u := staticUpstream{}
|
||||
c := caddyfile.NewDispenser("Testfile", strings.NewReader(test.config))
|
||||
for c.Next() {
|
||||
parseBlock(&c, &u, false)
|
||||
}
|
||||
if u.HealthCheck.Interval.String() != test.interval {
|
||||
t.Errorf(
|
||||
"Test %d: HealthCheck interval not the same from config. Got %v. Expected: %v",
|
||||
i+1,
|
||||
u.HealthCheck.Interval,
|
||||
test.interval,
|
||||
)
|
||||
}
|
||||
if u.HealthCheck.Timeout.String() != test.timeout {
|
||||
t.Errorf(
|
||||
"Test %d: HealthCheck timeout not the same from config. Got %v. Expected: %v",
|
||||
i+1,
|
||||
u.HealthCheck.Timeout,
|
||||
test.timeout,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStop(t *testing.T) {
|
||||
config := "proxy / %s {\n health_check /healthcheck \nhealth_check_interval %dms \n}"
|
||||
tests := []struct {
|
||||
name string
|
||||
intervalInMilliseconds int
|
||||
numHealthcheckIntervals int
|
||||
}{
|
||||
{
|
||||
"No Healthchecks After Stop - 5ms, 1 intervals",
|
||||
5,
|
||||
1,
|
||||
},
|
||||
{
|
||||
"No Healthchecks After Stop - 5ms, 2 intervals",
|
||||
5,
|
||||
2,
|
||||
},
|
||||
{
|
||||
"No Healthchecks After Stop - 5ms, 3 intervals",
|
||||
5,
|
||||
3,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
// Set up proxy.
|
||||
var counter int64
|
||||
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Body.Close()
|
||||
atomic.AddInt64(&counter, 1)
|
||||
}))
|
||||
|
||||
defer backend.Close()
|
||||
|
||||
upstreams, err := NewStaticUpstreams(caddyfile.NewDispenser("Testfile", strings.NewReader(fmt.Sprintf(config, backend.URL, test.intervalInMilliseconds))), "")
|
||||
if err != nil {
|
||||
t.Error("Expected no error. Got:", err.Error())
|
||||
}
|
||||
|
||||
// Give some time for healthchecks to hit the server.
|
||||
time.Sleep(time.Duration(test.intervalInMilliseconds*test.numHealthcheckIntervals) * time.Millisecond)
|
||||
|
||||
for _, upstream := range upstreams {
|
||||
if err := upstream.Stop(); err != nil {
|
||||
t.Error("Expected no error stopping upstream. Got: ", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
counterValueAfterShutdown := atomic.LoadInt64(&counter)
|
||||
|
||||
// Give some time to see if healthchecks are still hitting the server.
|
||||
time.Sleep(time.Duration(test.intervalInMilliseconds*test.numHealthcheckIntervals) * time.Millisecond)
|
||||
|
||||
if counterValueAfterShutdown == 0 {
|
||||
t.Error("Expected healthchecks to hit test server. Got no healthchecks.")
|
||||
}
|
||||
|
||||
counterValueAfterWaiting := atomic.LoadInt64(&counter)
|
||||
if counterValueAfterWaiting != counterValueAfterShutdown {
|
||||
t.Errorf("Expected no more healthchecks after shutdown. Got: %d healthchecks after shutdown", counterValueAfterWaiting-counterValueAfterShutdown)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseBlockTransparent(t *testing.T) {
|
||||
// tests for transparent proxy presets
|
||||
r, _ := http.NewRequest("GET", "/", nil)
|
||||
tests := []struct {
|
||||
config string
|
||||
}{
|
||||
// Test #1: transparent preset
|
||||
{"proxy / localhost:8080 {\n transparent \n}"},
|
||||
|
||||
// Test #2: transparent preset with another param
|
||||
{"proxy / localhost:8080 {\n transparent \nheader_upstream X-Test Tester \n}"},
|
||||
|
||||
// Test #3: transparent preset on multiple sites
|
||||
{"proxy / localhost:8080 {\n transparent \n} \nproxy /api localhost:8081 { \ntransparent \n}"},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
upstreams, err := NewStaticUpstreams(caddyfile.NewDispenser("Testfile", strings.NewReader(test.config)), "")
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error. Got: %s", err.Error())
|
||||
}
|
||||
for _, upstream := range upstreams {
|
||||
headers := upstream.Select(r).UpstreamHeaders
|
||||
|
||||
if _, ok := headers["Host"]; !ok {
|
||||
t.Errorf("Test %d: Could not find the Host header", i+1)
|
||||
}
|
||||
|
||||
if _, ok := headers["X-Real-Ip"]; !ok {
|
||||
t.Errorf("Test %d: Could not find the X-Real-Ip header", i+1)
|
||||
}
|
||||
|
||||
if _, ok := headers["X-Forwarded-Proto"]; !ok {
|
||||
t.Errorf("Test %d: Could not find the X-Forwarded-Proto header", i+1)
|
||||
}
|
||||
|
||||
if _, ok := headers["X-Forwarded-For"]; ok {
|
||||
t.Errorf("Test %d: Found unexpected X-Forwarded-For header", i+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthSetUp(t *testing.T) {
|
||||
// tests for insecure skip verify
|
||||
tests := []struct {
|
||||
config string
|
||||
flag bool
|
||||
}{
|
||||
// Test #1: without flag
|
||||
{"proxy / localhost:8080 {\n health_check / \n}", false},
|
||||
|
||||
// Test #2: with flag
|
||||
{"proxy / localhost:8080 {\n health_check / \n insecure_skip_verify \n}", true},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
upstreams, err := NewStaticUpstreams(caddyfile.NewDispenser("Testfile", strings.NewReader(test.config)), "")
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error. Got: %s", err.Error())
|
||||
}
|
||||
for _, upstream := range upstreams {
|
||||
staticUpstream, ok := upstream.(*staticUpstream)
|
||||
if !ok {
|
||||
t.Errorf("Type mismatch: %#v", upstream)
|
||||
continue
|
||||
}
|
||||
transport, ok := staticUpstream.HealthCheck.Client.Transport.(*http.Transport)
|
||||
if !ok {
|
||||
t.Errorf("Type mismatch: %#v", staticUpstream.HealthCheck.Client.Transport)
|
||||
continue
|
||||
}
|
||||
if test.flag != transport.TLSClientConfig.InsecureSkipVerify {
|
||||
t.Errorf("Test %d: expected transport.TLSClientCnfig.InsecureSkipVerify=%v, got %v", i, test.flag, transport.TLSClientConfig.InsecureSkipVerify)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthCheckHost(t *testing.T) {
|
||||
// tests for upstream host on health checks
|
||||
tests := []struct {
|
||||
config string
|
||||
flag bool
|
||||
host string
|
||||
}{
|
||||
// Test #1: without upstream header
|
||||
{"proxy / localhost:8080 {\n health_check / \n}", false, "example.com"},
|
||||
|
||||
// Test #2: without upstream header, missing host
|
||||
{"proxy / localhost:8080 {\n health_check / \n}", true, ""},
|
||||
|
||||
// Test #3: with upstream header (via transparent preset)
|
||||
{"proxy / localhost:8080 {\n health_check / \n transparent \n}", true, "foo.example.com"},
|
||||
|
||||
// Test #4: with upstream header (explicit header)
|
||||
{"proxy / localhost:8080 {\n health_check / \n header_upstream Host {host} \n}", true, "example.com"},
|
||||
|
||||
// Test #5: with upstream header, missing host
|
||||
{"proxy / localhost:8080 {\n health_check / \n transparent \n}", true, ""},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
upstreams, err := NewStaticUpstreams(caddyfile.NewDispenser("Testfile", strings.NewReader(test.config)), test.host)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error. Got: %s", err.Error())
|
||||
}
|
||||
for _, upstream := range upstreams {
|
||||
staticUpstream, ok := upstream.(*staticUpstream)
|
||||
if !ok {
|
||||
t.Errorf("Type mismatch: %#v", upstream)
|
||||
continue
|
||||
}
|
||||
if test.flag != (staticUpstream.HealthCheck.Host == test.host) {
|
||||
t.Errorf("Test %d: expected staticUpstream.HealthCheck.Host=%v, got %v", i, test.host, staticUpstream.HealthCheck.Host)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthCheckPort(t *testing.T) {
|
||||
var counter int64
|
||||
|
||||
healthCounter := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Body.Close()
|
||||
atomic.AddInt64(&counter, 1)
|
||||
}))
|
||||
|
||||
_, healthPort, err := net.SplitHostPort(healthCounter.Listener.Addr().String())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer healthCounter.Close()
|
||||
|
||||
tests := []struct {
|
||||
config string
|
||||
}{
|
||||
// Test #1: upstream with port
|
||||
{"proxy / localhost:8080 {\n health_check / health_check_port " + healthPort + "\n}"},
|
||||
|
||||
// Test #2: upstream without port (default to 80)
|
||||
{"proxy / localhost {\n health_check / health_check_port " + healthPort + "\n}"},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
counterValueAtStart := atomic.LoadInt64(&counter)
|
||||
upstreams, err := NewStaticUpstreams(caddyfile.NewDispenser("Testfile", strings.NewReader(test.config)), "")
|
||||
if err != nil {
|
||||
t.Error("Expected no error. Got:", err.Error())
|
||||
}
|
||||
|
||||
// Give some time for healthchecks to hit the server.
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
|
||||
for _, upstream := range upstreams {
|
||||
if err := upstream.Stop(); err != nil {
|
||||
t.Errorf("Test %d: Expected no error stopping upstream. Got: %v", i, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
counterValueAfterShutdown := atomic.LoadInt64(&counter)
|
||||
|
||||
if counterValueAfterShutdown == counterValueAtStart {
|
||||
t.Errorf("Test %d: Expected healthchecks to hit test server. Got no healthchecks.", i)
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("valid_port", func(t *testing.T) {
|
||||
tests := []struct {
|
||||
config string
|
||||
}{
|
||||
// Test #1: invalid port (nil)
|
||||
{"proxy / localhost {\n health_check / health_check_port\n}"},
|
||||
|
||||
// Test #2: invalid port (string)
|
||||
{"proxy / localhost {\n health_check / health_check_port abc\n}"},
|
||||
|
||||
// Test #3: invalid port (negative)
|
||||
{"proxy / localhost {\n health_check / health_check_port -1\n}"},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
_, err := NewStaticUpstreams(caddyfile.NewDispenser("Testfile", strings.NewReader(test.config)), "")
|
||||
if err == nil {
|
||||
t.Errorf("Test %d accepted invalid config", i)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestHealthCheckContentString(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "blablabla good blablabla")
|
||||
r.Body.Close()
|
||||
}))
|
||||
_, port, err := net.SplitHostPort(server.Listener.Addr().String())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer server.Close()
|
||||
|
||||
tests := []struct {
|
||||
config string
|
||||
shouldContain bool
|
||||
}{
|
||||
{"proxy / localhost:" + port +
|
||||
" { health_check /testhealth " +
|
||||
" health_check_contains good\n}",
|
||||
true,
|
||||
},
|
||||
{"proxy / localhost:" + port + " {\n health_check /testhealth health_check_port " + port +
|
||||
" \n health_check_contains bad\n}",
|
||||
false,
|
||||
},
|
||||
}
|
||||
for i, test := range tests {
|
||||
u, err := NewStaticUpstreams(caddyfile.NewDispenser("Testfile", strings.NewReader(test.config)), "")
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error. Test %d Got: %s", i, err.Error())
|
||||
}
|
||||
for _, upstream := range u {
|
||||
staticUpstream, ok := upstream.(*staticUpstream)
|
||||
if !ok {
|
||||
t.Errorf("Type mismatch: %#v", upstream)
|
||||
continue
|
||||
}
|
||||
staticUpstream.healthCheck()
|
||||
for _, host := range staticUpstream.Hosts {
|
||||
if test.shouldContain && atomic.LoadInt32(&host.Unhealthy) == 0 {
|
||||
// healthcheck url was hit and the required test string was found
|
||||
continue
|
||||
}
|
||||
if !test.shouldContain && atomic.LoadInt32(&host.Unhealthy) != 0 {
|
||||
// healthcheck url was hit and the required string was not found
|
||||
continue
|
||||
}
|
||||
t.Errorf("Health check bad response")
|
||||
}
|
||||
upstream.Stop()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestQuicHost(t *testing.T) {
|
||||
// tests for QUIC proxy
|
||||
tests := []struct {
|
||||
config string
|
||||
flag bool
|
||||
}{
|
||||
// Test #1: without flag
|
||||
{"proxy / quic://localhost:8080", false},
|
||||
|
||||
// Test #2: with flag
|
||||
{"proxy / quic://localhost:8080 {\n insecure_skip_verify \n}", true},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
upstreams, err := NewStaticUpstreams(caddyfile.NewDispenser("Testfile", strings.NewReader(test.config)), "")
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error. Got: %s", err.Error())
|
||||
}
|
||||
for _, upstream := range upstreams {
|
||||
staticUpstream, ok := upstream.(*staticUpstream)
|
||||
if !ok {
|
||||
t.Errorf("Type mismatch: %#v", upstream)
|
||||
continue
|
||||
}
|
||||
for _, host := range staticUpstream.Hosts {
|
||||
_, ok := host.ReverseProxy.Transport.(*h2quic.RoundTripper)
|
||||
if !ok {
|
||||
t.Errorf("Type mismatch: %#v", host.ReverseProxy.Transport)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseSRVBlock(t *testing.T) {
|
||||
tests := []struct {
|
||||
config string
|
||||
shouldErr bool
|
||||
}{
|
||||
{"proxy / srv://bogus.service", false},
|
||||
{"proxy / srv://bogus.service:80", true},
|
||||
{"proxy / srv://bogus.service srv://bogus.service.fallback", true},
|
||||
{"proxy / srv://bogus.service http://bogus.service.fallback", true},
|
||||
{"proxy / http://bogus.service srv://bogus.service.fallback", true},
|
||||
{"proxy / srv://bogus.service bogus.service.fallback", true},
|
||||
{`proxy / srv://bogus.service {
|
||||
upstream srv://bogus.service
|
||||
}`, true},
|
||||
{"proxy / srv+https://bogus.service", false},
|
||||
{"proxy / srv+https://bogus.service:80", true},
|
||||
{"proxy / srv+https://bogus.service srv://bogus.service.fallback", true},
|
||||
{"proxy / srv+https://bogus.service http://bogus.service.fallback", true},
|
||||
{"proxy / http://bogus.service srv+https://bogus.service.fallback", true},
|
||||
{"proxy / srv+https://bogus.service bogus.service.fallback", true},
|
||||
{`proxy / srv+https://bogus.service {
|
||||
upstream srv://bogus.service
|
||||
}`, true},
|
||||
{`proxy / srv+https://bogus.service {
|
||||
health_check_port 96
|
||||
}`, true},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
_, err := NewStaticUpstreams(caddyfile.NewDispenser("Testfile", strings.NewReader(test.config)), "")
|
||||
if err == nil && test.shouldErr {
|
||||
t.Errorf("Case %d - Expected an error. got nothing", i)
|
||||
}
|
||||
|
||||
if err != nil && !test.shouldErr {
|
||||
t.Errorf("Case %d - Expected no error. got %s", i, err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type testResolver struct {
|
||||
errOn string
|
||||
result []*net.SRV
|
||||
}
|
||||
|
||||
func (r testResolver) LookupSRV(ctx context.Context, _, _, service string) (string, []*net.SRV, error) {
|
||||
if service == r.errOn {
|
||||
return "", nil, errors.New("an error occurred")
|
||||
}
|
||||
|
||||
return "", r.result, nil
|
||||
}
|
||||
|
||||
func TestResolveHost(t *testing.T) {
|
||||
upstream := &staticUpstream{
|
||||
resolver: testResolver{
|
||||
errOn: "srv://problematic.service.name",
|
||||
result: []*net.SRV{
|
||||
{Target: "target-1.fqdn", Port: 85, Priority: 1, Weight: 1},
|
||||
{Target: "target-2.fqdn", Port: 33, Priority: 1, Weight: 1},
|
||||
{Target: "target-3.fqdn", Port: 94, Priority: 1, Weight: 1},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
host string
|
||||
expect []string
|
||||
isSrv bool
|
||||
shouldErr bool
|
||||
}{
|
||||
// Static DNS records
|
||||
{"http://subdomain.domain.service",
|
||||
[]string{"http://subdomain.domain.service"},
|
||||
false,
|
||||
false},
|
||||
{"https://subdomain.domain.service",
|
||||
[]string{"https://subdomain.domain.service"},
|
||||
false,
|
||||
false},
|
||||
{"http://subdomain.domain.service:76",
|
||||
[]string{"http://subdomain.domain.service:76"},
|
||||
false,
|
||||
false},
|
||||
{"https://subdomain.domain.service:65",
|
||||
[]string{"https://subdomain.domain.service:65"},
|
||||
false,
|
||||
false},
|
||||
|
||||
// SRV lookups
|
||||
{"srv://service.name", []string{
|
||||
"http://target-1.fqdn:85",
|
||||
"http://target-2.fqdn:33",
|
||||
"http://target-3.fqdn:94",
|
||||
}, true, false},
|
||||
{"srv+https://service.name", []string{
|
||||
"https://target-1.fqdn:85",
|
||||
"https://target-2.fqdn:33",
|
||||
"https://target-3.fqdn:94",
|
||||
}, true, false},
|
||||
{"srv://problematic.service.name", []string{}, true, true},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
results, isSrv, err := upstream.resolveHost(test.host)
|
||||
if err == nil && test.shouldErr {
|
||||
t.Errorf("Test %d - expected an error, got none", i)
|
||||
}
|
||||
|
||||
if err != nil && !test.shouldErr {
|
||||
t.Errorf("Test %d - unexpected error %s", i, err.Error())
|
||||
}
|
||||
|
||||
if test.isSrv && !isSrv {
|
||||
t.Errorf("Test %d - expecting resolution to be SRV lookup but it isn't", i)
|
||||
}
|
||||
|
||||
if isSrv && !test.isSrv {
|
||||
t.Errorf("Test %d - expecting resolution to be normal lookup, got SRV", i)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(results, test.expect) {
|
||||
t.Errorf("Test %d - resolution result %#v does not match expected value %#v", i, results, test.expect)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSRVHealthCheck(t *testing.T) {
|
||||
serverURL, err := url.Parse(workableServer.URL)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to parse test server URL: %s", err.Error())
|
||||
}
|
||||
|
||||
pp, err := strconv.Atoi(serverURL.Port())
|
||||
if err != nil {
|
||||
t.Errorf("Failed to parse test server port [%s]: %s", serverURL.Port(), err.Error())
|
||||
}
|
||||
|
||||
port := uint16(pp)
|
||||
|
||||
allGoodResolver := testResolver{
|
||||
result: []*net.SRV{
|
||||
{Target: serverURL.Hostname(), Port: port, Priority: 1, Weight: 1},
|
||||
},
|
||||
}
|
||||
|
||||
partialFailureResolver := testResolver{
|
||||
result: []*net.SRV{
|
||||
{Target: serverURL.Hostname(), Port: port, Priority: 1, Weight: 1},
|
||||
{Target: "target-2.fqdn", Port: 33, Priority: 1, Weight: 1},
|
||||
{Target: "target-3.fqdn", Port: 94, Priority: 1, Weight: 1},
|
||||
},
|
||||
}
|
||||
|
||||
fullFailureResolver := testResolver{
|
||||
result: []*net.SRV{
|
||||
{Target: "target-1.fqdn", Port: 876, Priority: 1, Weight: 1},
|
||||
{Target: "target-2.fqdn", Port: 33, Priority: 1, Weight: 1},
|
||||
{Target: "target-3.fqdn", Port: 94, Priority: 1, Weight: 1},
|
||||
},
|
||||
}
|
||||
|
||||
resolutionErrorResolver := testResolver{
|
||||
errOn: "srv://tag.service.consul",
|
||||
result: []*net.SRV{},
|
||||
}
|
||||
|
||||
upstream := &staticUpstream{
|
||||
Hosts: []*UpstreamHost{
|
||||
{Name: "srv://tag.service.consul"},
|
||||
},
|
||||
FailTimeout: 10 * time.Second,
|
||||
MaxFails: 1,
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
resolver testResolver
|
||||
shouldFail bool
|
||||
shouldErr bool
|
||||
}{
|
||||
{allGoodResolver, false, false},
|
||||
{partialFailureResolver, false, false},
|
||||
{fullFailureResolver, true, false},
|
||||
{resolutionErrorResolver, true, true},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
upstream.resolver = test.resolver
|
||||
upstream.healthCheck()
|
||||
if upstream.Hosts[0].Down() && !test.shouldFail {
|
||||
t.Errorf("Test %d - expected all healthchecks to pass, all failing", i)
|
||||
}
|
||||
|
||||
if test.shouldFail && !upstream.Hosts[0].Down() {
|
||||
t.Errorf("Test %d - expected all healthchecks to fail, all passing", i)
|
||||
}
|
||||
|
||||
status := fmt.Sprintf("%s", upstream.Hosts[0].HealthCheckResult.Load())
|
||||
|
||||
if test.shouldFail && !test.shouldErr && status != "Failed" {
|
||||
t.Errorf("Test %d - Expected health check result to be 'Failed', got '%s'", i, status)
|
||||
}
|
||||
|
||||
if !test.shouldFail && status != "OK" {
|
||||
t.Errorf("Test %d - Expected health check result to be 'OK', got '%s'", i, status)
|
||||
}
|
||||
|
||||
if test.shouldErr && status != "an error occurred" {
|
||||
t.Errorf("Test %d - Expected health check result to be 'an error occured', got '%s'", i, status)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user