Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: Support to get http client ip #4495

Merged
merged 8 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions pkg/apiutil/apiutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"strconv"
"strings"

"github.com/pingcap/errcode"
"github.com/pingcap/errors"
Expand Down Expand Up @@ -136,6 +138,28 @@ func ErrorResp(rd *render.Render, w http.ResponseWriter, err error) {
}
}

// GetIPAddrFromHTTPRequest returns http client IP from context.
// Because `X-Forwarded-For ` header has been written into RFC 7239(Forwarded HTTP Extension),
// so `X-Forwarded-For` has the higher priority than `X-Real-IP`.
// And both of them have the higher priority than `RemoteAddr`
func GetIPAddrFromHTTPRequest(r *http.Request) string {
CabinfeverB marked this conversation as resolved.
Show resolved Hide resolved
ips := strings.Split(r.Header.Get("X-Forwarded-For"), ",")
if len(strings.Trim(ips[0], " ")) > 0 {
CabinfeverB marked this conversation as resolved.
Show resolved Hide resolved
return ips[0]
}

ip := r.Header.Get("X-Real-Ip")
if ip != "" {
CabinfeverB marked this conversation as resolved.
Show resolved Hide resolved
return ip
}

ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return ""
}
return ip
}

// GetComponentNameOnHTTP returns component name from Request Header
func GetComponentNameOnHTTP(r *http.Request) string {
componentName := r.Header.Get(componentSignatureKey)
Expand Down
5 changes: 4 additions & 1 deletion server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

. "github.com/pingcap/check"
"github.com/tikv/pd/pkg/apiutil"
"github.com/tikv/pd/pkg/assertutil"
"github.com/tikv/pd/pkg/etcdutil"
"github.com/tikv/pd/pkg/testutil"
Expand Down Expand Up @@ -204,6 +205,9 @@ func (s *testServerHandlerSuite) TestRegisterServerHandler(c *C) {
mux := http.NewServeMux()
mux.HandleFunc("/pd/apis/mok/v1/hello", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello World")
// test getting ip
clientIP := apiutil.GetIPAddrFromHTTPRequest(r)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to cover all cases in our test.

c.Assert(clientIP, Equals, "127.0.0.1")
})
info := ServiceGroup{
Name: "mok",
Expand All @@ -228,7 +232,6 @@ func (s *testServerHandlerSuite) TestRegisterServerHandler(c *C) {
resp, err := http.Get(fmt.Sprintf("%s/pd/apis/mok/v1/hello", svr.GetAddr()))
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
c.Assert(err, IsNil)
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
c.Assert(err, IsNil)
Expand Down