Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
b77356af
Commit
b77356af
authored
May 11, 2016
by
Maciej Szulik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SplitHostPort is needed since Request.RemoteAddr has the host:port format
parent
e0f7de94
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
12 additions
and
4 deletions
+12
-4
http.go
pkg/util/net/http.go
+8
-2
http_test.go
pkg/util/net/http_test.go
+4
-2
No files found.
pkg/util/net/http.go
View file @
b77356af
...
...
@@ -153,8 +153,14 @@ func GetClientIP(req *http.Request) net.IP {
}
// Fallback to Remote Address in request, which will give the correct client IP when there is no proxy.
ip
:=
net
.
ParseIP
(
req
.
RemoteAddr
)
return
ip
// Remote Address in Go's HTTP server is in the form host:port so we need to split that first.
host
,
_
,
err
:=
net
.
SplitHostPort
(
req
.
RemoteAddr
)
if
err
==
nil
{
return
net
.
ParseIP
(
host
)
}
// Fallback if Remote Address was just IP.
return
net
.
ParseIP
(
req
.
RemoteAddr
)
}
var
defaultProxyFuncPointer
=
fmt
.
Sprintf
(
"%p"
,
http
.
ProxyFromEnvironment
)
...
...
pkg/util/net/http_test.go
View file @
b77356af
...
...
@@ -76,7 +76,8 @@ func TestGetClientIP(t *testing.T) {
},
{
Request
:
http
.
Request
{
RemoteAddr
:
ipString
,
// RemoteAddr is in the form host:port
RemoteAddr
:
ipString
+
":1234"
,
},
ExpectedIP
:
ip
,
},
...
...
@@ -90,6 +91,7 @@ func TestGetClientIP(t *testing.T) {
Header
:
map
[
string
][]
string
{
"X-Forwarded-For"
:
{
invalidIPString
},
},
// RemoteAddr is in the form host:port
RemoteAddr
:
ipString
,
},
ExpectedIP
:
ip
,
...
...
@@ -98,7 +100,7 @@ func TestGetClientIP(t *testing.T) {
for
i
,
test
:=
range
testCases
{
if
a
,
e
:=
GetClientIP
(
&
test
.
Request
),
test
.
ExpectedIP
;
reflect
.
DeepEqual
(
e
,
a
)
!=
true
{
t
.
Fatalf
(
"test case %d failed. expected: %v, actual: %v"
,
i
+
1
,
e
,
a
)
t
.
Fatalf
(
"test case %d failed. expected: %v, actual: %v"
,
i
,
e
,
a
)
}
}
}
...
...
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