Skip to content

Commit

Permalink
contrib/internal/httptrace: code review fix
Browse files Browse the repository at this point in the history
- Improve code wrt style guidelines
- Rework error checking in parseIP
- Use net.SplitHostPort instead of local function
- Minor nits
  • Loading branch information
Hellzy committed Jun 10, 2022
1 parent 93c31da commit 8b67d9e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 34 deletions.
54 changes: 21 additions & 33 deletions contrib/internal/httptrace/httptrace.go
Expand Up @@ -10,6 +10,7 @@ package httptrace
import (
"context"
"fmt"
"net"
"net/http"
"os"
"strconv"
Expand Down Expand Up @@ -96,8 +97,8 @@ func getClientIP(remoteAddr string, headers http.Header, clientIPHeader string)
if len(clientIPHeader) > 0 {
ipHeaders = []string{clientIPHeader}
}
check := func(value string) netaddr.IP {
for _, ipstr := range strings.Split(value, ",") {
check := func(s string) netaddr.IP {
for _, ipstr := range strings.Split(s, ",") {
ip := parseIP(strings.TrimSpace(ipstr))
if !ip.IsValid() {
continue
Expand All @@ -109,53 +110,40 @@ func getClientIP(remoteAddr string, headers http.Header, clientIPHeader string)
return netaddr.IP{}
}
for _, hdr := range ipHeaders {
if value := headers.Get(hdr); value != "" {
if ip := check(value); ip.IsValid() {
if v := headers.Get(hdr); v != "" {
if ip := check(v); ip.IsValid() {
return ip
}
}
}

if remoteIP := parseIP(remoteAddr); remoteIP.IsValid() && isGlobal(remoteIP) {
return remoteIP
}
return netaddr.IP{}
}

func parseIP(s string) netaddr.IP {
ip, err := netaddr.ParseIP(s)
if err != nil {
h, _ := splitHostPort(s)
ip, err = netaddr.ParseIP(h)
if ip, err := netaddr.ParseIP(s); err == nil {
return ip
}
return ip
}

func isGlobal(ip netaddr.IP) bool {
if ip.Is6() {
for _, network := range ipv6SpecialNetworks {
if network.Contains(ip) {
return false
}
if h, _, err := net.SplitHostPort(s); err == nil {
if ip, err := netaddr.ParseIP(h); err == nil {
return ip
}
}
//IsPrivate also checks for ipv6 ULA
return !ip.IsPrivate() && !ip.IsLoopback() && !ip.IsLinkLocalUnicast()
return netaddr.IP{}
}

// SplitHostPort splits a network address of the form `host:port` or
// `[host]:port` into `host` and `port`.
func splitHostPort(addr string) (host string, port string) {
i := strings.LastIndex(addr, "]:")
if i != -1 {
// ipv6
return strings.Trim(addr[:i+1], "[]"), addr[i+2:]
func isGlobal(ip netaddr.IP) bool {
//IsPrivate also checks for ipv6 ULA
globalCheck := !ip.IsPrivate() && !ip.IsLoopback() && !ip.IsLinkLocalUnicast()
if !globalCheck || !ip.Is6() {
return globalCheck
}

i = strings.LastIndex(addr, ":")
if i == -1 {
// not an address with a port number
return addr, ""
for _, n := range ipv6SpecialNetworks {
if n.Contains(ip) {
return false
}
}
return addr[:i], addr[i+1:]
return globalCheck
}
1 change: 0 additions & 1 deletion contrib/internal/httptrace/httptrace_test.go
Expand Up @@ -174,7 +174,6 @@ func TestIPHeaders(t *testing.T) {
require.Equal(t, tc.expectedIP.String(), getClientIP(tc.remoteAddr, header, tc.userIPHeader).String())
})
}

}

func randIPv4() netaddr.IP {
Expand Down

0 comments on commit 8b67d9e

Please sign in to comment.