Improve client port handling and fix IPv6 client port testing

This commit is contained in:
Daniel Carrillo 2021-11-21 12:49:13 +01:00
parent 1aa59f1b0b
commit 2807e2afc9
Signed by: dcarrillo
GPG Key ID: E4CD5C09DAED6E16
3 changed files with 15 additions and 12 deletions

View File

@ -7,7 +7,7 @@
- [Usage](#usage) - [Usage](#usage)
- [Examples](#examples) - [Examples](#examples)
- [Run a default TCP server](#run-a-default-tcp-server) - [Run a default TCP server](#run-a-default-tcp-server)
- [Run a TLS server only](#run-a-tls-server-only) - [Run a TLS (HTTP/2) server only](#run-a-tls-http2-server-only)
- [Run a default TCP server with a custom template and trust a custom header set by an upstream proxy](#run-a-default-tcp-server-with-a-custom-template-and-trust-a-custom-header-set-by-an-upstream-proxy) - [Run a default TCP server with a custom template and trust a custom header set by an upstream proxy](#run-a-default-tcp-server-with-a-custom-template-and-trust-a-custom-header-set-by-an-upstream-proxy)
- [Download](#download) - [Download](#download)
- [Docker](#docker) - [Docker](#docker)
@ -30,7 +30,7 @@ curl -6 ifconfig.es
## Features ## Features
- TLS is avaliable. - TLS and HTTP/2.
- Can run behind a proxy by trusting a custom header (usually `X-Real-IP`) to figure out the source IP address. - Can run behind a proxy by trusting a custom header (usually `X-Real-IP`) to figure out the source IP address.
- IPv4 and IPv6. - IPv4 and IPv6.
- Geolocation info including ASN. This feature is possible thanks to [maxmind](https://dev.maxmind.com/geoip/geolite2-free-geolocation-data?lang=en) GeoLite2 databases. In order to use these databases, a license key is needed. Please visit Maxmind site for further instructions and get a free license. - Geolocation info including ASN. This feature is possible thanks to [maxmind](https://dev.maxmind.com/geoip/geolite2-free-geolocation-data?lang=en) GeoLite2 databases. In order to use these databases, a license key is needed. Please visit Maxmind site for further instructions and get a free license.
@ -96,7 +96,7 @@ Usage of ./whatismyip:
./whatismyip -geoip2-city ./test/GeoIP2-City-Test.mmdb -geoip2-asn ./test/GeoLite2-ASN-Test.mmdb ./whatismyip -geoip2-city ./test/GeoIP2-City-Test.mmdb -geoip2-asn ./test/GeoLite2-ASN-Test.mmdb
``` ```
### Run a TLS server only ### Run a TLS (HTTP/2) server only
```bash ```bash
./whatismyip -geoip2-city ./test/GeoIP2-City-Test.mmdb -geoip2-asn ./test/GeoLite2-ASN-Test.mmdb -bind "" -tls-bind :8081 -tls-crt ./test/server.pem -tls-key ./test/server.key ./whatismyip -geoip2-city ./test/GeoIP2-City-Test.mmdb -geoip2-asn ./test/GeoLite2-ASN-Test.mmdb -bind "" -tls-bind :8081 -tls-crt ./test/server.pem -tls-key ./test/server.key

View File

@ -5,7 +5,6 @@ import (
"net/http" "net/http"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings"
"github.com/dcarrillo/whatismyip/internal/httputils" "github.com/dcarrillo/whatismyip/internal/httputils"
"github.com/dcarrillo/whatismyip/internal/setting" "github.com/dcarrillo/whatismyip/internal/setting"
@ -46,12 +45,14 @@ func getRoot(ctx *gin.Context) {
} }
func getClientPortAsString(ctx *gin.Context) { func getClientPortAsString(ctx *gin.Context) {
ctx.String(http.StatusOK, strings.Split(ctx.Request.RemoteAddr, ":")[1]+"\n") _, port, _ := net.SplitHostPort(ctx.Request.RemoteAddr)
ctx.String(http.StatusOK, port+"\n")
} }
func getAllAsString(ctx *gin.Context) { func getAllAsString(ctx *gin.Context) {
output := "IP: " + ctx.ClientIP() + "\n" output := "IP: " + ctx.ClientIP() + "\n"
output += "Client Port: " + strings.Split(ctx.Request.RemoteAddr, ":")[1] + "\n" _, port, _ := net.SplitHostPort(ctx.Request.RemoteAddr)
output += "Client Port: " + port + "\n"
r := service.Geo{IP: net.ParseIP(ctx.ClientIP())} r := service.Geo{IP: net.ParseIP(ctx.ClientIP())}
if record := r.LookUpCity(); record != nil { if record := r.LookUpCity(); record != nil {
@ -82,10 +83,11 @@ func jsonOutput(ctx *gin.Context) JSONResponse {
version = 6 version = 6
} }
_, port, _ := net.SplitHostPort(ctx.Request.RemoteAddr)
return JSONResponse{ return JSONResponse{
IP: ctx.ClientIP(), IP: ctx.ClientIP(),
IPVersion: version, IPVersion: version,
ClientPort: strings.Split(ctx.Request.RemoteAddr, ":")[1], ClientPort: port,
Country: cityRecord.Country.Names["en"], Country: cityRecord.Country.Names["en"],
CountryCode: cityRecord.Country.ISOCode, CountryCode: cityRecord.Country.ISOCode,
City: cityRecord.City.Names["en"], City: cityRecord.City.Names["en"],

View File

@ -1,6 +1,7 @@
package router package router
import ( import (
"net"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
@ -48,7 +49,7 @@ func TestHost(t *testing.T) {
func TestClientPort(t *testing.T) { func TestClientPort(t *testing.T) {
req, _ := http.NewRequest("GET", "/client-port", nil) req, _ := http.NewRequest("GET", "/client-port", nil)
req.RemoteAddr = testIP.ipv4 + ":" + "1000" req.RemoteAddr = net.JoinHostPort(testIP.ipv4 , "1000")
req.Header.Set("X-Real-IP", testIP.ipv4) req.Header.Set("X-Real-IP", testIP.ipv4)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -71,10 +72,10 @@ func TestNotFound(t *testing.T) {
func TestJSON(t *testing.T) { func TestJSON(t *testing.T) {
expectedIPv4 := `{"client_port":"1000","ip":"81.2.69.192","ip_version":4,"country":"United Kingdom","country_code":"GB","city":"London","latitude":51.5142,"longitude":-0.0931,"postal_code":"","time_zone":"Europe/London","asn":0,"asn_organization":"","host":"test","headers":{"X-Real-Ip":["81.2.69.192"]}}` expectedIPv4 := `{"client_port":"1000","ip":"81.2.69.192","ip_version":4,"country":"United Kingdom","country_code":"GB","city":"London","latitude":51.5142,"longitude":-0.0931,"postal_code":"","time_zone":"Europe/London","asn":0,"asn_organization":"","host":"test","headers":{"X-Real-Ip":["81.2.69.192"]}}`
expectedIPv6 := `{"asn":3352, "asn_organization":"TELEFONICA DE ESPANA", "city":"", "client_port":"9000", "country":"", "country_code":"", "headers":{"X-Real-Ip":["2a02:9000::1"]}, "host":"test", "ip":"2a02:9000::1", "ip_version":6, "latitude":0, "longitude":0, "postal_code":"", "time_zone":""}` expectedIPv6 := `{"asn":3352, "asn_organization":"TELEFONICA DE ESPANA", "city":"", "client_port":"1000", "country":"", "country_code":"", "headers":{"X-Real-Ip":["2a02:9000::1"]}, "host":"test", "ip":"2a02:9000::1", "ip_version":6, "latitude":0, "longitude":0, "postal_code":"", "time_zone":""}`
req, _ := http.NewRequest("GET", "/json", nil) req, _ := http.NewRequest("GET", "/json", nil)
req.RemoteAddr = testIP.ipv4 + ":" + "1000" req.RemoteAddr = net.JoinHostPort(testIP.ipv4, "1000")
req.Host = "test" req.Host = "test"
req.Header.Set("X-Real-IP", testIP.ipv4) req.Header.Set("X-Real-IP", testIP.ipv4)
@ -85,7 +86,7 @@ func TestJSON(t *testing.T) {
assert.Equal(t, contentType.json, w.Header().Get("Content-Type")) assert.Equal(t, contentType.json, w.Header().Get("Content-Type"))
assert.JSONEq(t, expectedIPv4, w.Body.String()) assert.JSONEq(t, expectedIPv4, w.Body.String())
req.RemoteAddr = testIP.ipv6 + ":" + "1000" req.RemoteAddr = net.JoinHostPort(testIP.ipv6, "1000")
req.Host = "test" req.Host = "test"
req.Header.Set("X-Real-IP", testIP.ipv6) req.Header.Set("X-Real-IP", testIP.ipv6)
@ -117,7 +118,7 @@ X-Real-Ip: 81.2.69.192
` `
req, _ := http.NewRequest("GET", "/all", nil) req, _ := http.NewRequest("GET", "/all", nil)
req.RemoteAddr = testIP.ipv4 + ":" + "1000" req.RemoteAddr = net.JoinHostPort(testIP.ipv4 , "1000")
req.Host = "test" req.Host = "test"
req.Header.Set("X-Real-IP", testIP.ipv4) req.Header.Set("X-Real-IP", testIP.ipv4)
req.Header.Set("Header1", "one") req.Header.Set("Header1", "one")