mirror of
https://github.com/dcarrillo/whatismyip.git
synced 2025-07-06 12:29:25 +00:00
Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
8b5cccd744 | |||
2571e22843
|
|||
2807e2afc9
|
|||
1aa59f1b0b |
6
Makefile
6
Makefile
@ -22,8 +22,14 @@ install-tools:
|
||||
@command $(GOPATH)/shadow > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||
go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@v0.1.7; \
|
||||
fi
|
||||
|
||||
@command $(GOPATH)/golines > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||
go install github.com/segmentio/golines@latest; \
|
||||
fi
|
||||
.PHONY: lint
|
||||
lint: install-tools
|
||||
gofmt -l . && test -z $$(gofmt -l .)
|
||||
golines -l . && test -z $$(golines -l .)
|
||||
golangci-lint run
|
||||
shadow ./...
|
||||
|
||||
|
@ -7,7 +7,7 @@
|
||||
- [Usage](#usage)
|
||||
- [Examples](#examples)
|
||||
- [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)
|
||||
- [Download](#download)
|
||||
- [Docker](#docker)
|
||||
@ -30,7 +30,7 @@ curl -6 ifconfig.es
|
||||
|
||||
## 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.
|
||||
- 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.
|
||||
@ -96,7 +96,7 @@ Usage of ./whatismyip:
|
||||
./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
|
||||
./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
|
||||
|
@ -3,6 +3,8 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
@ -24,7 +26,15 @@ var (
|
||||
)
|
||||
|
||||
func main() {
|
||||
setting.Setup()
|
||||
o, err := setting.Setup(os.Args[1:])
|
||||
if err == flag.ErrHelp || err == setting.ErrVersion {
|
||||
fmt.Print(o)
|
||||
os.Exit(0)
|
||||
} else if err != nil {
|
||||
fmt.Print(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
models.Setup(setting.App.GeodbPath.City, setting.App.GeodbPath.ASN)
|
||||
setupEngine()
|
||||
router.SetupTemplate(engine)
|
||||
@ -112,7 +122,8 @@ func runTLSServer() {
|
||||
|
||||
go func() {
|
||||
log.Printf("Starting TLS server listening on %s", setting.App.TLSAddress)
|
||||
if err := tlsServer.ListenAndServeTLS(setting.App.TLSCrtPath, setting.App.TLSKeyPath); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
if err := tlsServer.ListenAndServeTLS(setting.App.TLSCrtPath, setting.App.TLSKeyPath); err != nil &&
|
||||
!errors.Is(err, http.ErrServerClosed) {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Printf("Stopping TLS server...")
|
||||
|
@ -1,6 +1,8 @@
|
||||
package setting
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
@ -26,62 +28,75 @@ type settings struct {
|
||||
TLSKeyPath string
|
||||
TrustedHeader string
|
||||
Server serverSettings
|
||||
version bool
|
||||
}
|
||||
|
||||
const defaultAddress = ":8080"
|
||||
|
||||
var App *settings
|
||||
var ErrVersion = errors.New("setting: version requested")
|
||||
var App = settings{
|
||||
// hard-coded for the time being
|
||||
Server: serverSettings{
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
},
|
||||
}
|
||||
|
||||
func Setup() {
|
||||
city := flag.String("geoip2-city", "", "Path to GeoIP2 city database")
|
||||
asn := flag.String("geoip2-asn", "", "Path to GeoIP2 ASN database")
|
||||
template := flag.String("template", "", "Path to template file")
|
||||
address := flag.String("bind", defaultAddress, "Listening address (see https://pkg.go.dev/net?#Listen)")
|
||||
addressTLS := flag.String("tls-bind", "", "Listening address for TLS (see https://pkg.go.dev/net?#Listen)")
|
||||
tlsCrtPath := flag.String("tls-crt", "", "When using TLS, path to certificate file")
|
||||
tlsKeyPath := flag.String("tls-key", "", "When using TLS, path to private key file")
|
||||
trustedHeader := flag.String("trusted-header", "", "Trusted request header for remote IP (e.g. X-Real-IP)")
|
||||
ver := flag.Bool("version", false, "Output version information and exit")
|
||||
func Setup(args []string) (output string, err error) {
|
||||
flags := flag.NewFlagSet("whatismyip", flag.ContinueOnError)
|
||||
var buf bytes.Buffer
|
||||
flags.SetOutput(&buf)
|
||||
|
||||
flag.Parse()
|
||||
flags.StringVar(&App.GeodbPath.City, "geoip2-city", "", "Path to GeoIP2 city database")
|
||||
flags.StringVar(&App.GeodbPath.ASN, "geoip2-asn", "", "Path to GeoIP2 ASN database")
|
||||
flags.StringVar(&App.TemplatePath, "template", "", "Path to template file")
|
||||
flags.StringVar(
|
||||
&App.BindAddress,
|
||||
"bind",
|
||||
defaultAddress,
|
||||
"Listening address (see https://pkg.go.dev/net?#Listen)",
|
||||
)
|
||||
flags.StringVar(
|
||||
&App.TLSAddress,
|
||||
"tls-bind",
|
||||
"",
|
||||
"Listening address for TLS (see https://pkg.go.dev/net?#Listen)",
|
||||
)
|
||||
flags.StringVar(&App.TLSCrtPath, "tls-crt", "", "When using TLS, path to certificate file")
|
||||
flags.StringVar(&App.TLSKeyPath, "tls-key", "", "When using TLS, path to private key file")
|
||||
flags.StringVar(&App.TrustedHeader,
|
||||
"trusted-header",
|
||||
"",
|
||||
"Trusted request header for remote IP (e.g. X-Real-IP)",
|
||||
)
|
||||
flags.BoolVar(&App.version, "version", false, "Output version information and exit")
|
||||
|
||||
if *ver {
|
||||
fmt.Printf("whatismyip version %s", core.Version)
|
||||
os.Exit(0)
|
||||
err = flags.Parse(args)
|
||||
if err != nil {
|
||||
return buf.String(), err
|
||||
}
|
||||
|
||||
if *city == "" || *asn == "" {
|
||||
exitWithError("geoip2-city and geoip2-asn parameters are mandatory")
|
||||
if App.version {
|
||||
return fmt.Sprintf("whatismyip version %s", core.Version), ErrVersion
|
||||
}
|
||||
|
||||
if (*addressTLS != "") && (*tlsCrtPath == "" || *tlsKeyPath == "") {
|
||||
exitWithError("In order to use TLS -tls-crt and -tls-key flags are mandatory")
|
||||
if App.GeodbPath.City == "" || App.GeodbPath.ASN == "" {
|
||||
return "", fmt.Errorf("geoip2-city and geoip2-asn parameters are mandatory")
|
||||
}
|
||||
|
||||
if *template != "" {
|
||||
info, err := os.Stat(*template)
|
||||
if os.IsNotExist(err) || info.IsDir() {
|
||||
exitWithError(*template + " doesn't exist or it's not a file")
|
||||
if (App.TLSAddress != "") && (App.TLSCrtPath == "" || App.TLSKeyPath == "") {
|
||||
return "", fmt.Errorf("In order to use TLS -tls-crt and -tls-key flags are mandatory")
|
||||
}
|
||||
|
||||
if App.TemplatePath != "" {
|
||||
info, err := os.Stat(App.TemplatePath)
|
||||
if os.IsNotExist(err) {
|
||||
return "", fmt.Errorf("%s no such file or directory", App.TemplatePath)
|
||||
}
|
||||
if info.IsDir() {
|
||||
return "", fmt.Errorf("%s must be a file", App.TemplatePath)
|
||||
}
|
||||
}
|
||||
|
||||
App = &settings{
|
||||
GeodbPath: geodbPath{City: *city, ASN: *asn},
|
||||
TemplatePath: *template,
|
||||
BindAddress: *address,
|
||||
TLSAddress: *addressTLS,
|
||||
TLSCrtPath: *tlsCrtPath,
|
||||
TLSKeyPath: *tlsKeyPath,
|
||||
TrustedHeader: *trustedHeader,
|
||||
Server: serverSettings{
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func exitWithError(error string) {
|
||||
fmt.Printf("%s\n\n", error)
|
||||
flag.Usage()
|
||||
os.Exit(1)
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
193
internal/setting/app_test.go
Normal file
193
internal/setting/app_test.go
Normal file
@ -0,0 +1,193 @@
|
||||
package setting
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParseMandatoryFlags(t *testing.T) {
|
||||
var mandatoryFlags = []struct {
|
||||
args []string
|
||||
conf settings
|
||||
}{
|
||||
{
|
||||
[]string{},
|
||||
settings{},
|
||||
},
|
||||
{
|
||||
[]string{"-geoip2-city", "/city-path"},
|
||||
settings{},
|
||||
},
|
||||
{
|
||||
[]string{"-geoip2-asn", "/asn-path"},
|
||||
settings{},
|
||||
},
|
||||
{
|
||||
[]string{
|
||||
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path", "-tls-bind", ":9000",
|
||||
},
|
||||
settings{},
|
||||
},
|
||||
{
|
||||
[]string{
|
||||
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path", "-tls-bind", ":9000",
|
||||
"-tls-crt", "/crt-path",
|
||||
},
|
||||
settings{},
|
||||
},
|
||||
{
|
||||
[]string{
|
||||
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path", "-tls-bind", ":9000",
|
||||
"-tls-key", "/key-path",
|
||||
},
|
||||
settings{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range mandatoryFlags {
|
||||
t.Run(strings.Join(tt.args, " "), func(t *testing.T) {
|
||||
_, err := Setup(tt.args)
|
||||
assert.NotNil(t, err)
|
||||
assert.Contains(t, err.Error(), "mandatory")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFlags(t *testing.T) {
|
||||
var flags = []struct {
|
||||
args []string
|
||||
conf settings
|
||||
}{
|
||||
{
|
||||
[]string{"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path"},
|
||||
settings{
|
||||
GeodbPath: geodbPath{
|
||||
City: "/city-path",
|
||||
ASN: "/asn-path",
|
||||
},
|
||||
TemplatePath: "",
|
||||
BindAddress: ":8080",
|
||||
TLSAddress: "",
|
||||
TLSCrtPath: "",
|
||||
TLSKeyPath: "",
|
||||
TrustedHeader: "",
|
||||
Server: serverSettings{
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
[]string{"-bind", ":8001", "-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path"},
|
||||
settings{
|
||||
GeodbPath: geodbPath{
|
||||
City: "/city-path",
|
||||
ASN: "/asn-path",
|
||||
},
|
||||
TemplatePath: "",
|
||||
BindAddress: ":8001",
|
||||
TLSAddress: "",
|
||||
TLSCrtPath: "",
|
||||
TLSKeyPath: "",
|
||||
TrustedHeader: "",
|
||||
Server: serverSettings{
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
[]string{
|
||||
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path", "-tls-bind", ":9000",
|
||||
"-tls-crt", "/crt-path", "-tls-key", "/key-path",
|
||||
},
|
||||
settings{
|
||||
GeodbPath: geodbPath{
|
||||
City: "/city-path",
|
||||
ASN: "/asn-path",
|
||||
},
|
||||
TemplatePath: "",
|
||||
BindAddress: ":8080",
|
||||
TLSAddress: ":9000",
|
||||
TLSCrtPath: "/crt-path",
|
||||
TLSKeyPath: "/key-path",
|
||||
TrustedHeader: "",
|
||||
Server: serverSettings{
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
[]string{
|
||||
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path",
|
||||
"-trusted-header", "header",
|
||||
},
|
||||
settings{
|
||||
GeodbPath: geodbPath{
|
||||
City: "/city-path",
|
||||
ASN: "/asn-path",
|
||||
},
|
||||
TemplatePath: "",
|
||||
BindAddress: ":8080",
|
||||
TLSAddress: "",
|
||||
TLSCrtPath: "",
|
||||
TLSKeyPath: "",
|
||||
TrustedHeader: "header",
|
||||
Server: serverSettings{
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range flags {
|
||||
t.Run(strings.Join(tt.args, " "), func(t *testing.T) {
|
||||
_, err := Setup(tt.args)
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, reflect.DeepEqual(App, tt.conf))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFlagsUsage(t *testing.T) {
|
||||
var usageArgs = []string{"-help", "-h", "--help"}
|
||||
|
||||
for _, arg := range usageArgs {
|
||||
t.Run(arg, func(t *testing.T) {
|
||||
output, err := Setup([]string{arg})
|
||||
assert.ErrorIs(t, err, flag.ErrHelp)
|
||||
assert.Contains(t, output, "Usage of")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFlagVersion(t *testing.T) {
|
||||
output, err := Setup([]string{"-version"})
|
||||
assert.ErrorIs(t, err, ErrVersion)
|
||||
assert.Contains(t, output, "whatismyip version")
|
||||
}
|
||||
|
||||
func TestParseFlagTemplate(t *testing.T) {
|
||||
flags := []string{
|
||||
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path",
|
||||
"-template", "/template-path",
|
||||
}
|
||||
_, err := Setup(flags)
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "no such file or directory")
|
||||
|
||||
flags = []string{
|
||||
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path",
|
||||
"-template", "/",
|
||||
}
|
||||
_, err = Setup(flags)
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "must be a file")
|
||||
}
|
@ -5,7 +5,6 @@ import (
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/dcarrillo/whatismyip/internal/httputils"
|
||||
"github.com/dcarrillo/whatismyip/internal/setting"
|
||||
@ -46,12 +45,14 @@ func getRoot(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) {
|
||||
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())}
|
||||
if record := r.LookUpCity(); record != nil {
|
||||
@ -82,10 +83,11 @@ func jsonOutput(ctx *gin.Context) JSONResponse {
|
||||
version = 6
|
||||
}
|
||||
|
||||
_, port, _ := net.SplitHostPort(ctx.Request.RemoteAddr)
|
||||
return JSONResponse{
|
||||
IP: ctx.ClientIP(),
|
||||
IPVersion: version,
|
||||
ClientPort: strings.Split(ctx.Request.RemoteAddr, ":")[1],
|
||||
ClientPort: port,
|
||||
Country: cityRecord.Country.Names["en"],
|
||||
CountryCode: cityRecord.Country.ISOCode,
|
||||
City: cityRecord.City.Names["en"],
|
||||
|
@ -1,6 +1,7 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
@ -9,7 +10,18 @@ import (
|
||||
)
|
||||
|
||||
func TestIP4RootFromCli(t *testing.T) {
|
||||
uas := []string{"", "curl", "wget", "libwww-perl", "python", "ansible-httpget", "HTTPie", "WindowsPowerShell", "http_request", "Go-http-client"}
|
||||
uas := []string{
|
||||
"",
|
||||
"curl",
|
||||
"wget",
|
||||
"libwww-perl",
|
||||
"python",
|
||||
"ansible-httpget",
|
||||
"HTTPie",
|
||||
"WindowsPowerShell",
|
||||
"http_request",
|
||||
"Go-http-client",
|
||||
}
|
||||
|
||||
req, _ := http.NewRequest("GET", "/", nil)
|
||||
req.Header.Set("X-Real-IP", testIP.ipv4)
|
||||
@ -37,7 +49,7 @@ func TestHost(t *testing.T) {
|
||||
|
||||
func TestClientPort(t *testing.T) {
|
||||
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)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
@ -60,10 +72,10 @@ func TestNotFound(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"]}}`
|
||||
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.RemoteAddr = testIP.ipv4 + ":" + "1000"
|
||||
req.RemoteAddr = net.JoinHostPort(testIP.ipv4, "1000")
|
||||
req.Host = "test"
|
||||
req.Header.Set("X-Real-IP", testIP.ipv4)
|
||||
|
||||
@ -74,7 +86,7 @@ func TestJSON(t *testing.T) {
|
||||
assert.Equal(t, contentType.json, w.Header().Get("Content-Type"))
|
||||
assert.JSONEq(t, expectedIPv4, w.Body.String())
|
||||
|
||||
req.RemoteAddr = testIP.ipv6 + ":" + "1000"
|
||||
req.RemoteAddr = net.JoinHostPort(testIP.ipv6, "1000")
|
||||
req.Host = "test"
|
||||
req.Header.Set("X-Real-IP", testIP.ipv6)
|
||||
|
||||
@ -106,7 +118,7 @@ X-Real-Ip: 81.2.69.192
|
||||
`
|
||||
|
||||
req, _ := http.NewRequest("GET", "/all", nil)
|
||||
req.RemoteAddr = testIP.ipv4 + ":" + "1000"
|
||||
req.RemoteAddr = net.JoinHostPort(testIP.ipv4, "1000")
|
||||
req.Host = "test"
|
||||
req.Header.Set("X-Real-IP", testIP.ipv4)
|
||||
req.Header.Set("Header1", "one")
|
||||
|
Reference in New Issue
Block a user