mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-22 10:25:46 +03:00
fixed transparent host and added test case
This commit is contained in:
parent
8d7136fc06
commit
b58872925a
2 changed files with 24 additions and 14 deletions
|
@ -26,6 +26,7 @@ type staticUpstream struct {
|
||||||
Hosts HostPool
|
Hosts HostPool
|
||||||
Policy Policy
|
Policy Policy
|
||||||
insecureSkipVerify bool
|
insecureSkipVerify bool
|
||||||
|
transparent bool
|
||||||
|
|
||||||
FailTimeout time.Duration
|
FailTimeout time.Duration
|
||||||
MaxFails int32
|
MaxFails int32
|
||||||
|
@ -54,6 +55,7 @@ func NewStaticUpstreams(c caddyfile.Dispenser) ([]Upstream, error) {
|
||||||
FailTimeout: 10 * time.Second,
|
FailTimeout: 10 * time.Second,
|
||||||
MaxFails: 1,
|
MaxFails: 1,
|
||||||
MaxConns: 0,
|
MaxConns: 0,
|
||||||
|
transparent: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
if !c.Args(&upstream.from) {
|
if !c.Args(&upstream.from) {
|
||||||
|
@ -69,19 +71,6 @@ func NewStaticUpstreams(c caddyfile.Dispenser) ([]Upstream, error) {
|
||||||
to = append(to, parsed...)
|
to = append(to, parsed...)
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
for c.NextBlock() {
|
for c.NextBlock() {
|
||||||
switch c.Val() {
|
switch c.Val() {
|
||||||
case "upstream":
|
case "upstream":
|
||||||
|
@ -100,6 +89,23 @@ func NewStaticUpstreams(c caddyfile.Dispenser) ([]Upstream, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.transparent {
|
||||||
|
upstream.upstreamHeaders.Add("Host", upstream.Hosts[0].Name)
|
||||||
|
}
|
||||||
|
|
||||||
if upstream.HealthCheck.Path != "" {
|
if upstream.HealthCheck.Path != "" {
|
||||||
upstream.HealthCheck.Client = http.Client{
|
upstream.HealthCheck.Client = http.Client{
|
||||||
Timeout: upstream.HealthCheck.Timeout,
|
Timeout: upstream.HealthCheck.Timeout,
|
||||||
|
@ -287,7 +293,7 @@ func parseBlock(c *caddyfile.Dispenser, u *staticUpstream) error {
|
||||||
}
|
}
|
||||||
u.downstreamHeaders.Add(header, value)
|
u.downstreamHeaders.Add(header, value)
|
||||||
case "transparent":
|
case "transparent":
|
||||||
u.upstreamHeaders.Add("Host", u.Hosts[0].Name)
|
u.transparent = true
|
||||||
u.upstreamHeaders.Add("X-Real-IP", "{remote}")
|
u.upstreamHeaders.Add("X-Real-IP", "{remote}")
|
||||||
u.upstreamHeaders.Add("X-Forwarded-For", "{remote}")
|
u.upstreamHeaders.Add("X-Forwarded-For", "{remote}")
|
||||||
u.upstreamHeaders.Add("X-Forwarded-Proto", "{scheme}")
|
u.upstreamHeaders.Add("X-Forwarded-Proto", "{scheme}")
|
||||||
|
|
|
@ -213,6 +213,10 @@ func TestParseBlock(t *testing.T) {
|
||||||
t.Errorf("Test %d: Could not find the Host header", i+1)
|
t.Errorf("Test %d: Could not find the Host header", i+1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if v, _ := headers["Host"]; v[0] != upstream.Select().Name {
|
||||||
|
t.Errorf("Test %d: Host not match first hostname in upstream", i+1)
|
||||||
|
}
|
||||||
|
|
||||||
if _, ok := headers["X-Real-Ip"]; !ok {
|
if _, ok := headers["X-Real-Ip"]; !ok {
|
||||||
t.Errorf("Test %d: Could not find the X-Real-Ip header", i+1)
|
t.Errorf("Test %d: Could not find the X-Real-Ip header", i+1)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue