Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Łukasz Nowak
caddy
Commits
6bb1e0c6
Commit
6bb1e0c6
authored
Jul 14, 2016
by
Pedro Nasser
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improve transparent mode
parent
b5887292
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
21 additions
and
10 deletions
+21
-10
caddyhttp/proxy/proxy.go
caddyhttp/proxy/proxy.go
+6
-0
caddyhttp/proxy/proxy_test.go
caddyhttp/proxy/proxy_test.go
+8
-0
caddyhttp/proxy/upstream.go
caddyhttp/proxy/upstream.go
+5
-4
caddyhttp/proxy/upstream_test.go
caddyhttp/proxy/upstream_test.go
+2
-6
No files found.
caddyhttp/proxy/proxy.go
View file @
6bb1e0c6
...
@@ -30,6 +30,8 @@ type Upstream interface {
...
@@ -30,6 +30,8 @@ type Upstream interface {
Select
()
*
UpstreamHost
Select
()
*
UpstreamHost
// Checks if subpath is not an ignored path
// Checks if subpath is not an ignored path
AllowedPath
(
string
)
bool
AllowedPath
(
string
)
bool
// Is Upstream in transparent mode?
Transparent
()
bool
}
}
// UpstreamHostDownFunc can be used to customize how Down behaves.
// UpstreamHostDownFunc can be used to customize how Down behaves.
...
@@ -94,6 +96,7 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
...
@@ -94,6 +96,7 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
start
:=
time
.
Now
()
start
:=
time
.
Now
()
for
time
.
Now
()
.
Sub
(
start
)
<
tryDuration
{
for
time
.
Now
()
.
Sub
(
start
)
<
tryDuration
{
host
:=
upstream
.
Select
()
host
:=
upstream
.
Select
()
if
host
==
nil
{
if
host
==
nil
{
return
http
.
StatusBadGateway
,
errUnreachable
return
http
.
StatusBadGateway
,
errUnreachable
}
}
...
@@ -125,6 +128,9 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
...
@@ -125,6 +128,9 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
if
v
,
ok
:=
host
.
UpstreamHeaders
[
"Host"
];
ok
{
if
v
,
ok
:=
host
.
UpstreamHeaders
[
"Host"
];
ok
{
outreq
.
Host
=
replacer
.
Replace
(
v
[
len
(
v
)
-
1
])
outreq
.
Host
=
replacer
.
Replace
(
v
[
len
(
v
)
-
1
])
}
}
if
upstream
.
Transparent
()
{
host
.
UpstreamHeaders
.
Set
(
"Host"
,
host
.
Name
)
}
// modify headers for request that will be sent to the upstream host
// modify headers for request that will be sent to the upstream host
upHeaders
:=
createHeadersByRules
(
host
.
UpstreamHeaders
,
r
.
Header
,
replacer
)
upHeaders
:=
createHeadersByRules
(
host
.
UpstreamHeaders
,
r
.
Header
,
replacer
)
for
k
,
v
:=
range
upHeaders
{
for
k
,
v
:=
range
upHeaders
{
...
...
caddyhttp/proxy/proxy_test.go
View file @
6bb1e0c6
...
@@ -687,6 +687,10 @@ func (u *fakeUpstream) AllowedPath(requestPath string) bool {
...
@@ -687,6 +687,10 @@ func (u *fakeUpstream) AllowedPath(requestPath string) bool {
return
true
return
true
}
}
func
(
u
*
fakeUpstream
)
Transparent
()
bool
{
return
true
}
// newWebSocketTestProxy returns a test proxy that will
// newWebSocketTestProxy returns a test proxy that will
// redirect to the specified backendAddr. The function
// redirect to the specified backendAddr. The function
// also sets up the rules/environment for testing WebSocket
// also sets up the rules/environment for testing WebSocket
...
@@ -729,6 +733,10 @@ func (u *fakeWsUpstream) AllowedPath(requestPath string) bool {
...
@@ -729,6 +733,10 @@ func (u *fakeWsUpstream) AllowedPath(requestPath string) bool {
return
true
return
true
}
}
func
(
u
*
fakeWsUpstream
)
Transparent
()
bool
{
return
true
}
// recorderHijacker is a ResponseRecorder that can
// recorderHijacker is a ResponseRecorder that can
// be hijacked.
// be hijacked.
type
recorderHijacker
struct
{
type
recorderHijacker
struct
{
...
...
caddyhttp/proxy/upstream.go
View file @
6bb1e0c6
...
@@ -102,10 +102,6 @@ func NewStaticUpstreams(c caddyfile.Dispenser) ([]Upstream, error) {
...
@@ -102,10 +102,6 @@ func NewStaticUpstreams(c caddyfile.Dispenser) ([]Upstream, error) {
upstream
.
Hosts
[
i
]
=
uh
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
,
...
@@ -372,6 +368,11 @@ func (u *staticUpstream) Select() *UpstreamHost {
...
@@ -372,6 +368,11 @@ func (u *staticUpstream) Select() *UpstreamHost {
return
u
.
Policy
.
Select
(
pool
)
return
u
.
Policy
.
Select
(
pool
)
}
}
// Transparent returns true if upstream in transparent mode
func
(
u
*
staticUpstream
)
Transparent
()
bool
{
return
u
.
transparent
}
func
(
u
*
staticUpstream
)
AllowedPath
(
requestPath
string
)
bool
{
func
(
u
*
staticUpstream
)
AllowedPath
(
requestPath
string
)
bool
{
for
_
,
ignoredSubPath
:=
range
u
.
IgnoredSubPaths
{
for
_
,
ignoredSubPath
:=
range
u
.
IgnoredSubPaths
{
if
httpserver
.
Path
(
path
.
Clean
(
requestPath
))
.
Matches
(
path
.
Join
(
u
.
From
(),
ignoredSubPath
))
{
if
httpserver
.
Path
(
path
.
Clean
(
requestPath
))
.
Matches
(
path
.
Join
(
u
.
From
(),
ignoredSubPath
))
{
...
...
caddyhttp/proxy/upstream_test.go
View file @
6bb1e0c6
...
@@ -209,12 +209,8 @@ func TestParseBlock(t *testing.T) {
...
@@ -209,12 +209,8 @@ func TestParseBlock(t *testing.T) {
for
_
,
upstream
:=
range
upstreams
{
for
_
,
upstream
:=
range
upstreams
{
headers
:=
upstream
.
Select
()
.
UpstreamHeaders
headers
:=
upstream
.
Select
()
.
UpstreamHeaders
if
_
,
ok
:=
headers
[
"Host"
];
!
ok
{
if
!
upstream
.
Transparent
()
{
t
.
Errorf
(
"Test %d: Could not find the Host header"
,
i
+
1
)
t
.
Errorf
(
"Test %d: Upstream should be in transparent mode"
,
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
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment