24 Commits

Author SHA1 Message Date
6b7fc0bc6a Bump gin and testify to latest versions 2022-07-03 11:39:51 +02:00
c5a659ff64 Create codeql-analysis.yml 2022-07-03 11:29:23 +02:00
bd06da7b2b Bump gin-gonic to 1.8.0 2022-06-03 18:37:22 +02:00
b61d64a755 Moving secure again, this time to github.com/gin-contrib/secure 2022-05-07 15:56:21 +02:00
3df794ecc4 Bump testcontainers-go tp 0.13.0 2022-05-07 13:06:19 +02:00
ca1d002974 Use gin-gonic/contrib/secure instead of deprecated module 2022-05-07 12:51:13 +02:00
1ee7256506 Remove headers set by a trusted proxy from outputs 2022-05-02 18:07:40 +02:00
7c70abf07f Add feature to get the right client port when using a trusted proxy 2022-05-02 17:56:57 +02:00
9070e9a2c2 Use Accept request header instead of user agent to figure out non-browser clients. Funny fact, I borrowed the idea from a fork ('8a3e142cf3/router/generic.go (L33)') 2022-04-30 17:35:12 +02:00
12da27ddab Add optional secure headers to responses 2022-04-02 18:12:31 +02:00
aae2e08240 Bump maxminddb-golang to 1.9.0 2022-04-02 18:07:12 +02:00
e4667bda05 Remove timestamp snapshot from log when releasing 2022-03-19 19:08:02 +01:00
49b28b6536 Update golangci-lint and shadow 2022-03-19 19:02:41 +01:00
edd0713c21 Update modules and bump go version to 1.18 2022-03-19 18:59:15 +01:00
ab46275990 Tidy up the house 2022-01-27 13:17:51 +01:00
f7d0a679c9 Add test for geo model 2021-12-01 18:09:13 +01:00
ac32dc240f Add release badge to README 2021-12-01 13:18:32 +01:00
2d330a99b7 Add CI and go-report badges. Add comments to make golint happy 2021-11-30 17:49:02 +01:00
9b10052cd1 refactor on some tests 2021-11-30 16:32:52 +01:00
def2b0f2f0 Update Gin and testcontainers to latest version 2021-11-30 16:07:31 +01:00
8b5cccd744 Refactor setting module and add tests (#4) 2021-11-29 18:16:27 +01:00
2571e22843 Fix format errors and add gofmt and golines to Makefile 2021-11-21 18:23:36 +01:00
2807e2afc9 Improve client port handling and fix IPv6 client port testing 2021-11-21 12:49:13 +01:00
1aa59f1b0b Fix long lines (#2)
Using [golines](https://github.com/segmentio/golines):
`golines -w .`
2021-11-18 19:26:53 +01:00
23 changed files with 1270 additions and 249 deletions

50
.github/workflows/codeql-analysis.yml vendored Normal file
View File

@ -0,0 +1,50 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"
on:
push:
branches: [ "main" ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ "main" ]
schedule:
- cron: '21 21 * * 0'
jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write
strategy:
fail-fast: false
steps:
- name: Checkout repository
uses: actions/checkout@v3
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: go
- run: |
echo "Build"
make build
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2

View File

@ -17,7 +17,7 @@ jobs:
- name: install go - name: install go
uses: actions/setup-go@v2 uses: actions/setup-go@v2
with: with:
go-version: "^1.17" go-version: "^1.18"
- name: Lint - name: Lint
run: make lint run: make lint
@ -51,7 +51,7 @@ jobs:
- name: Prepare release - name: Prepare release
run: | run: |
git log $(git describe HEAD~ --tags --abbrev=0)..HEAD --pretty='format:%h - %s (%cr) <%an>' --no-merges > changelog.txt git log $(git describe HEAD~ --tags --abbrev=0)..HEAD --pretty='format:%h - %s <%an>' --no-merges > changelog.txt
tar zcvf whatismyip-$RELEASE_VERSION-${{matrix.goosarch}}.tar.gz whatismyip LICENSE README.md tar zcvf whatismyip-$RELEASE_VERSION-${{matrix.goosarch}}.tar.gz whatismyip LICENSE README.md
sha256sum whatismyip-$RELEASE_VERSION-${{matrix.goosarch}}.tar.gz > whatismyip-$RELEASE_VERSION-${{matrix.goosarch}}.tar.gz.sha256 sha256sum whatismyip-$RELEASE_VERSION-${{matrix.goosarch}}.tar.gz > whatismyip-$RELEASE_VERSION-${{matrix.goosarch}}.tar.gz.sha256

View File

@ -1,4 +1,4 @@
FROM golang:1.17-alpine as builder FROM golang:1.18-alpine as builder
ARG ARG_VERSION ARG ARG_VERSION
ENV VERSION $ARG_VERSION ENV VERSION $ARG_VERSION
@ -7,7 +7,7 @@ WORKDIR /app
COPY . . COPY . .
RUN apk add make && make build VERSION=$VERSION RUN apk add make git && make build VERSION=$VERSION
# Build final image # Build final image
FROM scratch FROM scratch

View File

@ -16,14 +16,20 @@ integration-test:
.PHONY: install-tools .PHONY: install-tools
install-tools: install-tools:
@command golangci-lint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ @command golangci-lint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.43.0; \ curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.45.2; \
fi fi
@command $(GOPATH)/shadow > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ @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; \ go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@v0.1.10; \
fi
@command $(GOPATH)/golines > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
go install github.com/segmentio/golines@latest; \
fi fi
.PHONY: lint .PHONY: lint
lint: install-tools lint: install-tools
gofmt -l . && test -z $$(gofmt -l .)
golines -l . && test -z $$(golines -l .)
golangci-lint run golangci-lint run
shadow ./... shadow ./...

View File

@ -1,5 +1,10 @@
# What is my IP address # What is my IP address
[![CI](https://github.com/dcarrillo/whatismyip/workflows/CI/badge.svg)](https://github.com/dcarrillo/whatismyip/actions)
[![Go Report Card](https://goreportcard.com/badge/github.com/dcarrillo/whatismyip)](https://goreportcard.com/report/github.com/dcarrillo/whatismyip)
[![GitHub release](https://img.shields.io/github/release/dcarrillo/whatismyip.svg)](https://github.com/dcarrillo/whatismyip/releases/)
[![License Apache 2.0](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](./LICENSE)
- [What is my IP address](#what-is-my-ip-address) - [What is my IP address](#what-is-my-ip-address)
- [Features](#features) - [Features](#features)
- [Endpoints](#endpoints) - [Endpoints](#endpoints)
@ -7,11 +12,11 @@
- [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 pair of custom headers set by an upstream proxy](#run-a-default-tcp-server-with-a-custom-template-and-trust-a-pair-of-custom-headers-set-by-an-upstream-proxy)
- [Download](#download) - [Download](#download)
- [Docker](#docker) - [Docker](#docker)
- [Running a container locally using test databases](#running-a-container-locally-using-test-databases) - [Run a container locally using test databases](#run-a-container-locally-using-test-databases)
- [From Docker Hub](#from-docker-hub) - [From Docker Hub](#from-docker-hub)
Just another "what is my IP address" service, including geolocation and headers information, written in go with high performance in mind, it uses [gin](https://github.com/gin-gonic/gin) which uses [httprouter](https://github.com/julienschmidt/httprouter) a lightweight high performance HTTP multiplexer. Just another "what is my IP address" service, including geolocation and headers information, written in go with high performance in mind, it uses [gin](https://github.com/gin-gonic/gin) which uses [httprouter](https://github.com/julienschmidt/httprouter) a lightweight high performance HTTP multiplexer.
@ -30,8 +35,8 @@ 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. It also supports a custom header to resolve the client port, if the proxy can only add a header for the IP (for example a fixed header from CDNs) the client port is shown as unknown.
- 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.
- High performance. - High performance.
@ -42,7 +47,8 @@ curl -6 ifconfig.es
## Endpoints ## Endpoints
- https://ifconfig.es/ - https://ifconfig.es/
- https://ifconfig.es/json - https://ifconfig.es/client-port
- https://ifconfig.es/json (this is the same as `curl -H "Accept: application/json" https://ifconfig.es/`)
- https://ifconfig.es/geo - https://ifconfig.es/geo
- https://ifconfig.es/geo/city - https://ifconfig.es/geo/city
- https://ifconfig.es/geo/country - https://ifconfig.es/geo/country
@ -67,9 +73,11 @@ Golang >= 1.17 is required. Previous versions may work.
## Usage ## Usage
```text ```text
Usage of ./whatismyip: Usage of whatismyip:
-bind string -bind string
Listening address (see https://pkg.go.dev/net?#Listen) (default ":8080") Listening address (see https://pkg.go.dev/net?#Listen) (default ":8080")
-enable-secure-headers
Add sane security-related headers to every response
-geoip2-asn string -geoip2-asn string
Path to GeoIP2 ASN database Path to GeoIP2 ASN database
-geoip2-city string -geoip2-city string
@ -83,7 +91,9 @@ Usage of ./whatismyip:
-tls-key string -tls-key string
When using TLS, path to private key file When using TLS, path to private key file
-trusted-header string -trusted-header string
Trusted request header for remote IP (e.g. X-Real-IP) Trusted request header for remote IP (e.g. X-Real-IP). When using this feature if -trusted-port-header is not set the client port is shown as 'unknown'
-trusted-port-header string
Trusted request header for remote client port (e.g. X-Real-Port). When this parameter is set -trusted-header becomes mandatory
-version -version
Output version information and exit Output version information and exit
``` ```
@ -96,16 +106,18 @@ 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
``` ```
### 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 pair of custom headers set by an upstream proxy
```bash ```bash
./whatismyip -geoip2-city ./test/GeoIP2-City-Test.mmdb -geoip2-asn ./test/GeoLite2-ASN-Test.mmdb -trusted-header X-Real-IP -template mytemplate.tmpl ./whatismyip -geoip2-city ./test/GeoIP2-City-Test.mmdb -geoip2-asn ./test/GeoLite2-ASN-Test.mmdb \
-trusted-header X-Real-IP -trusted-port-header X-Real-Port -template mytemplate.tmpl
``` ```
## Download ## Download
@ -114,9 +126,9 @@ Download latest version from https://github.com/dcarrillo/whatismyip/releases
## Docker ## Docker
An ultra-light (~9MB) image is available. An ultra-light (~10MB) image is available at [docker hub](https://hub.docker.com/r/dcarrillo/whatismyip).
### Running a container locally using test databases ### Run a container locally using test databases
`make docker-run` `make docker-run`

View File

@ -3,6 +3,8 @@ package main
import ( import (
"context" "context"
"errors" "errors"
"flag"
"fmt"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -11,6 +13,8 @@ import (
"github.com/dcarrillo/whatismyip/internal/httputils" "github.com/dcarrillo/whatismyip/internal/httputils"
"github.com/dcarrillo/whatismyip/internal/setting" "github.com/dcarrillo/whatismyip/internal/setting"
"github.com/gin-contrib/secure"
"github.com/dcarrillo/whatismyip/models" "github.com/dcarrillo/whatismyip/models"
"github.com/dcarrillo/whatismyip/router" "github.com/dcarrillo/whatismyip/router"
@ -24,7 +28,15 @@ var (
) )
func main() { 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) models.Setup(setting.App.GeodbPath.City, setting.App.GeodbPath.ASN)
setupEngine() setupEngine()
router.SetupTemplate(engine) router.SetupTemplate(engine)
@ -112,7 +124,8 @@ func runTLSServer() {
go func() { go func() {
log.Printf("Starting TLS server listening on %s", setting.App.TLSAddress) 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.Fatal(err)
} }
log.Printf("Stopping TLS server...") log.Printf("Stopping TLS server...")
@ -127,6 +140,13 @@ func setupEngine() {
engine = gin.New() engine = gin.New()
engine.Use(gin.LoggerWithFormatter(httputils.GetLogFormatter)) engine.Use(gin.LoggerWithFormatter(httputils.GetLogFormatter))
engine.Use(gin.Recovery()) engine.Use(gin.Recovery())
if setting.App.EnableSecureHeaders {
engine.Use(secure.New(secure.Config{
BrowserXssFilter: true,
ContentTypeNosniff: true,
FrameDeny: true,
}))
}
_ = engine.SetTrustedProxies(nil) _ = engine.SetTrustedProxies(nil)
engine.TrustedPlatform = setting.App.TrustedHeader engine.TrustedPlatform = setting.App.TrustedHeader
} }

99
go.mod
View File

@ -1,59 +1,90 @@
module github.com/dcarrillo/whatismyip module github.com/dcarrillo/whatismyip
go 1.17 go 1.18
require ( require (
github.com/gin-gonic/gin v1.7.2-0.20211103141324-89a159bdd92c github.com/gin-contrib/secure v0.0.1
github.com/oschwald/maxminddb-golang v1.8.0 github.com/gin-gonic/gin v1.8.1
github.com/stretchr/testify v1.7.0 github.com/oschwald/maxminddb-golang v1.9.0
github.com/testcontainers/testcontainers-go v0.11.1 github.com/stretchr/testify v1.8.0
github.com/testcontainers/testcontainers-go v0.13.0
) )
require ( require (
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.4.17-0.20210211115548-6eac466e5fa3 // indirect github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/Microsoft/hcsshim v0.8.16 // indirect github.com/Microsoft/hcsshim v0.9.3 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible // indirect github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/containerd/cgroups v0.0.0-20210114181951-8a68de567b68 // indirect github.com/checkpoint-restore/go-criu/v5 v5.3.0 // indirect
github.com/containerd/containerd v1.5.0-beta.4 // indirect github.com/cilium/ebpf v0.9.0 // indirect
github.com/containerd/cgroups v1.0.4 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/containerd/containerd v1.6.6 // indirect
github.com/containerd/continuity v0.3.0 // indirect
github.com/containerd/fifo v1.0.0 // indirect
github.com/containerd/ttrpc v1.1.0 // indirect
github.com/containerd/typeurl v1.0.2 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/docker v20.10.7+incompatible // indirect github.com/docker/docker v20.10.17+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect
github.com/docker/go-units v0.4.0 // indirect github.com/docker/go-units v0.4.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.9.0 // indirect github.com/go-playground/validator/v10 v10.11.0 // indirect
github.com/goccy/go-json v0.7.10 // indirect github.com/goccy/go-json v0.9.8 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.0 // indirect github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.2.0 // indirect github.com/google/uuid v1.3.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.7 // indirect
github.com/leodido/go-urn v1.2.1 // indirect github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-isatty v0.0.14 // indirect
github.com/moby/sys/mount v0.2.0 // indirect github.com/moby/locker v1.0.1 // indirect
github.com/moby/sys/mountinfo v0.4.1 // indirect github.com/moby/sys/mount v0.3.3 // indirect
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635 // indirect github.com/moby/sys/mountinfo v0.6.2 // indirect
github.com/moby/sys/signal v0.7.0 // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/morikuni/aec v0.0.0-20170113033406-39771216ff4c // indirect github.com/morikuni/aec v1.0.0 // indirect
github.com/mrunalp/fileutils v0.5.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 // indirect
github.com/opencontainers/runc v1.0.0-rc93 // indirect github.com/opencontainers/runc v1.1.3 // indirect
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 // indirect
github.com/opencontainers/selinux v1.10.1 // indirect
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.7.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/ugorji/go/codec v1.2.6 // indirect github.com/seccomp/libseccomp-golang v0.10.0 // indirect
go.opencensus.io v0.22.3 // indirect github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect github.com/sirupsen/logrus v1.8.1 // indirect
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect github.com/stretchr/objx v0.4.0 // indirect
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359 // indirect github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/urfave/cli v1.22.9 // indirect
github.com/vishvananda/netlink v1.2.1-beta.2 // indirect
github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/net v0.0.0-20220630215102-69896b714898 // indirect
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
golang.org/x/sys v0.0.0-20220702020025-31831981b65f // indirect
golang.org/x/text v0.3.7 // indirect golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20201110150050-8816d57aaa9a // indirect google.golang.org/genproto v0.0.0-20220630174209-ad1d48641aa7 // indirect
google.golang.org/grpc v1.33.2 // indirect google.golang.org/grpc v1.47.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )

472
go.sum

File diff suppressed because it is too large Load Diff

View File

@ -19,7 +19,7 @@ import (
func buildContainer() testcontainers.ContainerRequest { func buildContainer() testcontainers.ContainerRequest {
_, filename, _, _ := runtime.Caller(0) _, filename, _, _ := runtime.Caller(0)
dirname := filepath.Dir(filename) dir := filepath.Dir(filename)
req := testcontainers.ContainerRequest{ req := testcontainers.ContainerRequest{
FromDockerfile: testcontainers.FromDockerfile{ FromDockerfile: testcontainers.FromDockerfile{
@ -34,15 +34,22 @@ func buildContainer() testcontainers.ContainerRequest {
"-tls-crt", "/tmp/server.pem", "-tls-crt", "/tmp/server.pem",
"-tls-key", "/tmp/server.key", "-tls-key", "/tmp/server.key",
"-trusted-header", "X-Real-IP", "-trusted-header", "X-Real-IP",
"-enable-secure-headers",
}, },
ExposedPorts: []string{"8000:8000", "8001:8001"}, ExposedPorts: []string{"8000:8000", "8001:8001"},
WaitingFor: wait.ForLog("Starting TLS server listening on :8001"), WaitingFor: wait.ForLog("Starting TLS server listening on :8001"),
BindMounts: map[string]string{ Mounts: testcontainers.Mounts(
filepath.Join(dirname, "/../test/GeoIP2-City-Test.mmdb"): "/tmp/GeoIP2-City-Test.mmdb", testcontainers.BindMount(
filepath.Join(dirname, "/../test/GeoLite2-ASN-Test.mmdb"): "/tmp/GeoLite2-ASN-Test.mmdb", filepath.Join(dir, "/../test/GeoIP2-City-Test.mmdb"),
filepath.Join(dirname, "/../test/server.pem"): "/tmp/server.pem", "/tmp/GeoIP2-City-Test.mmdb",
filepath.Join(dirname, "/../test/server.key"): "/tmp/server.key", ),
}, testcontainers.BindMount(
filepath.Join(dir, "/../test/GeoLite2-ASN-Test.mmdb"),
"/tmp/GeoLite2-ASN-Test.mmdb",
),
testcontainers.BindMount(filepath.Join(dir, "/../test/server.pem"), "/tmp/server.pem"),
testcontainers.BindMount(filepath.Join(dir, "/../test/server.key"), "/tmp/server.key"),
),
} }
return req return req
@ -69,16 +76,21 @@ func TestContainerIntegration(t *testing.T) {
}() }()
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
for _, url := range []string{"http://localhost:8000/json", "https://localhost:8001/json"} { for _, url := range []string{"http://localhost:8000", "https://localhost:8001"} {
resp, _ := http.Get(url) client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Accept", "application/json")
resp, _ := client.Do(req)
assert.Equal(t, 200, resp.StatusCode) assert.Equal(t, 200, resp.StatusCode)
var dat router.JSONResponse
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
assert.NoError(t, json.Unmarshal(body, &dat)) assert.NoError(t, json.Unmarshal(body, &router.JSONResponse{}))
assert.Equal(t, "DENY", resp.Header.Get("X-Frame-Options"))
assert.Equal(t, "nosniff", resp.Header.Get("X-Content-Type-Options"))
assert.Equal(t, "1; mode=block", resp.Header.Get("X-Xss-Protection"))
} }
} }

View File

@ -1,3 +1,4 @@
package core package core
// Version to be defined at build time
var Version = "tobedefined" var Version = "tobedefined"

View File

@ -3,12 +3,15 @@ package httputils
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"net/textproto"
"sort" "sort"
"strings" "strings"
"github.com/dcarrillo/whatismyip/internal/setting"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// HeadersToSortedString shorts and dumps http.Header to a string separated by \n
func HeadersToSortedString(headers http.Header) string { func HeadersToSortedString(headers http.Header) string {
var output string var output string
@ -31,6 +34,18 @@ func HeadersToSortedString(headers http.Header) string {
return output return output
} }
// GetHeadersWithoutTrustedHeaders return a http.Heade object with the original headers except trusted headers
func GetHeadersWithoutTrustedHeaders(ctx *gin.Context) http.Header {
h := ctx.Request.Header
for _, k := range []string{setting.App.TrustedHeader, setting.App.TrustedPortHeader} {
delete(h, textproto.CanonicalMIMEHeaderKey(k))
}
return h
}
// GetLogFormatter returns our custom log format
func GetLogFormatter(param gin.LogFormatterParams) string { func GetLogFormatter(param gin.LogFormatterParams) string {
return fmt.Sprintf("%s - [%s] \"%s %s %s\" %d %d %d %s \"%s\" \"%s\" \"%s\"\n", return fmt.Sprintf("%s - [%s] \"%s %s %s\" %d %d %d %s \"%s\" \"%s\" \"%s\"\n",
param.ClientIP, param.ClientIP,
@ -57,9 +72,8 @@ func normalizeLog(log interface{}) interface{} {
case []string: case []string:
if len(v) == 0 { if len(v) == 0 {
return "-" return "-"
} else {
return strings.Join(v, ", ")
} }
return strings.Join(v, ", ")
} }
return log return log

View File

@ -1,6 +1,8 @@
package setting package setting
import ( import (
"bytes"
"errors"
"flag" "flag"
"fmt" "fmt"
"os" "os"
@ -25,63 +27,99 @@ type settings struct {
TLSCrtPath string TLSCrtPath string
TLSKeyPath string TLSKeyPath string
TrustedHeader string TrustedHeader string
TrustedPortHeader string
EnableSecureHeaders bool
Server serverSettings Server serverSettings
version bool
} }
const defaultAddress = ":8080" const defaultAddress = ":8080"
var App *settings // ErrVersion is the custom error triggered when -version flag is passed
var ErrVersion = errors.New("setting: version requested")
func Setup() { // App is the var with the parsed settings
city := flag.String("geoip2-city", "", "Path to GeoIP2 city database") var App = settings{
asn := flag.String("geoip2-asn", "", "Path to GeoIP2 ASN database") // hard-coded for the time being
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")
flag.Parse()
if *ver {
fmt.Printf("whatismyip version %s", core.Version)
os.Exit(0)
}
if *city == "" || *asn == "" {
exitWithError("geoip2-city and geoip2-asn parameters are mandatory")
}
if (*addressTLS != "") && (*tlsCrtPath == "" || *tlsKeyPath == "") {
exitWithError("In order to use TLS -tls-crt and -tls-key flags 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")
}
}
App = &settings{
GeodbPath: geodbPath{City: *city, ASN: *asn},
TemplatePath: *template,
BindAddress: *address,
TLSAddress: *addressTLS,
TLSCrtPath: *tlsCrtPath,
TLSKeyPath: *tlsKeyPath,
TrustedHeader: *trustedHeader,
Server: serverSettings{ Server: serverSettings{
ReadTimeout: 10 * time.Second, ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second,
}, },
}
} }
func exitWithError(error string) { // Setup initializes the App object parsing the flags
fmt.Printf("%s\n\n", error) func Setup(args []string) (output string, err error) {
flag.Usage() flags := flag.NewFlagSet("whatismyip", flag.ContinueOnError)
os.Exit(1) var buf bytes.Buffer
flags.SetOutput(&buf)
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). When using this feature if -trusted-port-header is not set the client port is shown as 'unknown'",
)
flags.StringVar(
&App.TrustedPortHeader,
"trusted-port-header",
"",
"Trusted request header for remote client port (e.g. X-Real-Port). When this parameter is set -trusted-header becomes mandatory",
)
flags.BoolVar(&App.version, "version", false, "Output version information and exit")
flags.BoolVar(
&App.EnableSecureHeaders,
"enable-secure-headers",
false,
"Add sane security-related headers to every response",
)
err = flags.Parse(args)
if err != nil {
return buf.String(), err
}
if App.version {
return fmt.Sprintf("whatismyip version %s", core.Version), ErrVersion
}
if App.TrustedPortHeader != "" && App.TrustedHeader == "" {
return "", fmt.Errorf("truster-header is mandatory when truster-port-header is set\n")
}
if App.GeodbPath.City == "" || App.GeodbPath.ASN == "" {
return "", fmt.Errorf("geoip2-city and geoip2-asn parameters are mandatory\n")
}
if (App.TLSAddress != "") && (App.TLSCrtPath == "" || App.TLSKeyPath == "") {
return "", fmt.Errorf("In order to use TLS -tls-crt and -tls-key flags are mandatory\n")
}
if App.TemplatePath != "" {
info, err := os.Stat(App.TemplatePath)
if os.IsNotExist(err) {
return "", fmt.Errorf("%s no such file or directory\n", App.TemplatePath)
}
if info.IsDir() {
return "", fmt.Errorf("%s must be a file\n", App.TemplatePath)
}
}
return buf.String(), nil
} }

View File

@ -0,0 +1,197 @@
package setting
import (
"flag"
"reflect"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParseMandatoryFlags(t *testing.T) {
var mandatoryFlags = []struct {
args []string
}{
{
[]string{},
},
{
[]string{"-geoip2-city", "/city-path"},
},
{
[]string{"-geoip2-asn", "/asn-path"},
},
{
[]string{
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path", "-tls-bind", ":9000",
},
},
{
[]string{
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path", "-tls-bind", ":9000",
"-tls-crt", "/crt-path",
},
},
{
[]string{
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path", "-tls-bind", ":9000",
"-tls-key", "/key-path",
},
},
{
[]string{
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path", "-bind", ":8000",
"-trusted-port-header", "port-header",
},
},
}
for _, tt := range mandatoryFlags {
t.Run(strings.Join(tt.args, " "), func(t *testing.T) {
_, err := Setup(tt.args)
require.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",
},
BindAddress: ":8080",
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",
},
BindAddress: ":8001",
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",
},
BindAddress: ":8080",
TLSAddress: ":9000",
TLSCrtPath: "/crt-path",
TLSKeyPath: "/key-path",
Server: serverSettings{
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
},
},
},
{
[]string{
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path",
"-trusted-header", "header", "-trusted-port-header", "port-header",
},
settings{
GeodbPath: geodbPath{
City: "/city-path",
ASN: "/asn-path",
},
BindAddress: ":8080",
TrustedHeader: "header",
TrustedPortHeader: "port-header",
Server: serverSettings{
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
},
},
},
{
[]string{
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path",
"-trusted-header", "header", "-enable-secure-headers",
},
settings{
GeodbPath: geodbPath{
City: "/city-path",
ASN: "/asn-path",
},
BindAddress: ":8080",
TrustedHeader: "header",
EnableSecureHeaders: true,
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)
require.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)
require.Error(t, err)
assert.Contains(t, err.Error(), "must be a file")
}

View File

@ -7,10 +7,7 @@ import (
"github.com/oschwald/maxminddb-golang" "github.com/oschwald/maxminddb-golang"
) )
type Record interface { // GeoRecord is the model for City database
LookUp(ip net.IP)
}
type GeoRecord struct { type GeoRecord struct {
Country struct { Country struct {
ISOCode string `maxminddb:"iso_code"` ISOCode string `maxminddb:"iso_code"`
@ -29,6 +26,7 @@ type GeoRecord struct {
} `maxminddb:"postal"` } `maxminddb:"postal"`
} }
// ASNRecord is the model for ASN database
type ASNRecord struct { type ASNRecord struct {
AutonomousSystemNumber uint `maxminddb:"autonomous_system_number"` AutonomousSystemNumber uint `maxminddb:"autonomous_system_number"`
AutonomousSystemOrganization string `maxminddb:"autonomous_system_organization"` AutonomousSystemOrganization string `maxminddb:"autonomous_system_organization"`
@ -51,11 +49,13 @@ func openMMDB(path string) *maxminddb.Reader {
return db return db
} }
// Setup opens all Geolite2 databases
func Setup(cityPath string, asnPath string) { func Setup(cityPath string, asnPath string) {
db.city = openMMDB(cityPath) db.city = openMMDB(cityPath)
db.asn = openMMDB(asnPath) db.asn = openMMDB(asnPath)
} }
// CloseDBs unmaps from memory and frees resources to the filesystem
func CloseDBs() { func CloseDBs() {
log.Printf("Closing dbs...") log.Printf("Closing dbs...")
if err := db.city.Close(); err != nil { if err := db.city.Close(); err != nil {
@ -66,18 +66,18 @@ func CloseDBs() {
} }
} }
// LookUp an IP and get city data
func (record *GeoRecord) LookUp(ip net.IP) error { func (record *GeoRecord) LookUp(ip net.IP) error {
err := db.city.Lookup(ip, record) if err := db.city.Lookup(ip, record); err != nil {
if err != nil {
return err return err
} }
return nil return nil
} }
// LookUp an IP and get ASN data
func (record *ASNRecord) LookUp(ip net.IP) error { func (record *ASNRecord) LookUp(ip net.IP) error {
err := db.asn.Lookup(ip, record) if err := db.asn.Lookup(ip, record); err != nil {
if err != nil {
return err return err
} }

77
models/geo_test.go Normal file
View File

@ -0,0 +1,77 @@
package models
import (
"net"
"testing"
"github.com/stretchr/testify/assert"
)
func TestModels(t *testing.T) {
expectedCity := &GeoRecord{
Country: struct {
ISOCode string "maxminddb:\"iso_code\""
Names map[string]string "maxminddb:\"names\""
}{
ISOCode: "GB",
Names: map[string]string{
"de": "Vereinigtes Königreich",
"en": "United Kingdom",
"es": "Reino Unido",
"fr": "Royaume-Uni",
"ja": "イギリス",
"pt-BR": "Reino Unido",
"ru": "Великобритания",
"zh-CN": "英国",
},
},
City: struct {
Names map[string]string "maxminddb:\"names\""
}{
Names: map[string]string{
"de": "London",
"en": "London",
"es": "Londres",
"fr": "Londres",
"ja": "ロンドン",
"pt-BR": "Londres",
"ru": "Лондон",
},
},
Location: struct {
Latitude float64 "maxminddb:\"latitude\""
Longitude float64 "maxminddb:\"longitude\""
TimeZone string "maxminddb:\"time_zone\""
}{
Latitude: 51.5142,
Longitude: -0.0931,
TimeZone: "Europe/London",
},
Postal: struct {
Code string "maxminddb:\"code\""
}{
Code: "",
},
}
expectedASN := &ASNRecord{
AutonomousSystemNumber: 12552,
AutonomousSystemOrganization: "IP-Only",
}
Setup("../test/GeoIP2-City-Test.mmdb", "../test/GeoLite2-ASN-Test.mmdb")
defer CloseDBs()
assert.NotNil(t, db.asn)
assert.NotNil(t, db.city)
cityRecord := &GeoRecord{}
assert.Nil(t, cityRecord.LookUp(net.ParseIP("81.2.69.192")))
assert.Equal(t, expectedCity, cityRecord)
assert.Error(t, cityRecord.LookUp(net.ParseIP("error")))
asnRecord := &ASNRecord{}
assert.Nil(t, asnRecord.LookUp(net.ParseIP("82.99.17.64")))
assert.Equal(t, expectedASN, asnRecord)
assert.Error(t, asnRecord.LookUp(net.ParseIP("error")))
}

View File

@ -4,8 +4,6 @@ import (
"net" "net"
"net/http" "net/http"
"path/filepath" "path/filepath"
"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"
@ -13,8 +11,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
const userAgentPattern = `curl|wget|libwww-perl|python|ansible-httpget|HTTPie|WindowsPowerShell|http_request|Go-http-client|^$` // JSONResponse maps data as json
type JSONResponse struct { type JSONResponse struct {
IP string `json:"ip"` IP string `json:"ip"`
IPVersion byte `json:"ip_version"` IPVersion byte `json:"ip_version"`
@ -33,25 +30,45 @@ type JSONResponse struct {
} }
func getRoot(ctx *gin.Context) { func getRoot(ctx *gin.Context) {
reg := regexp.MustCompile(userAgentPattern) switch ctx.NegotiateFormat(gin.MIMEPlain, gin.MIMEHTML, gin.MIMEJSON) {
if reg.Match([]byte(ctx.Request.UserAgent())) { case gin.MIMEHTML:
ctx.String(http.StatusOK, ctx.ClientIP())
} else {
name := "home" name := "home"
if setting.App.TemplatePath != "" { if setting.App.TemplatePath != "" {
name = filepath.Base(setting.App.TemplatePath) name = filepath.Base(setting.App.TemplatePath)
} }
ctx.HTML(http.StatusOK, name, jsonOutput(ctx)) ctx.HTML(http.StatusOK, name, jsonOutput(ctx))
case gin.MIMEJSON:
getJSON(ctx)
default:
ctx.String(http.StatusOK, ctx.ClientIP()+"\n")
} }
} }
func getClientPort(ctx *gin.Context) string {
var port string
if setting.App.TrustedPortHeader == "" {
if setting.App.TrustedHeader != "" {
port = "unknown"
} else {
_, port, _ = net.SplitHostPort(ctx.Request.RemoteAddr)
}
} else {
port = ctx.GetHeader(setting.App.TrustedPortHeader)
if port == "" {
port = "unknown"
}
}
return port
}
func getClientPortAsString(ctx *gin.Context) { func getClientPortAsString(ctx *gin.Context) {
ctx.String(http.StatusOK, strings.Split(ctx.Request.RemoteAddr, ":")[1]+"\n") ctx.String(http.StatusOK, getClientPort(ctx)+"\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" output += "Client Port: " + getClientPort(ctx) + "\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 {
@ -62,8 +79,8 @@ func getAllAsString(ctx *gin.Context) {
output += geoASNRecordToString(record) + "\n" output += geoASNRecordToString(record) + "\n"
} }
h := ctx.Request.Header h := httputils.GetHeadersWithoutTrustedHeaders(ctx)
h["Host"] = []string{ctx.Request.Host} h.Set("Host", ctx.Request.Host)
output += httputils.HeadersToSortedString(h) output += httputils.HeadersToSortedString(h)
ctx.String(http.StatusOK, output) ctx.String(http.StatusOK, output)
@ -85,7 +102,7 @@ func jsonOutput(ctx *gin.Context) JSONResponse {
return JSONResponse{ return JSONResponse{
IP: ctx.ClientIP(), IP: ctx.ClientIP(),
IPVersion: version, IPVersion: version,
ClientPort: strings.Split(ctx.Request.RemoteAddr, ":")[1], ClientPort: getClientPort(ctx),
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"],
@ -96,6 +113,6 @@ func jsonOutput(ctx *gin.Context) JSONResponse {
ASN: asnRecord.AutonomousSystemNumber, ASN: asnRecord.AutonomousSystemNumber,
ASNOrganization: asnRecord.AutonomousSystemOrganization, ASNOrganization: asnRecord.AutonomousSystemOrganization,
Host: ctx.Request.Host, Host: ctx.Request.Host,
Headers: ctx.Request.Header, Headers: httputils.GetHeadersWithoutTrustedHeaders(ctx),
} }
} }

View File

@ -1,27 +1,89 @@
package router package router
import ( import (
"net"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"github.com/dcarrillo/whatismyip/internal/setting"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestIP4RootFromCli(t *testing.T) { func TestRootContentType(t *testing.T) {
uas := []string{"", "curl", "wget", "libwww-perl", "python", "ansible-httpget", "HTTPie", "WindowsPowerShell", "http_request", "Go-http-client"} tests := []struct {
name string
accepted string
expected string
}{
{
name: "Accept wildcard",
accepted: "*/*",
expected: contentType.text,
},
{
name: "Bogus accept",
accepted: "bogus/plain",
expected: contentType.text,
},
{
name: "Accept plain text",
accepted: "text/plain",
expected: contentType.text,
},
{
name: "Accept json",
accepted: "application/json",
expected: contentType.json,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil) req, _ := http.NewRequest("GET", "/", nil)
req.Header.Set("X-Real-IP", testIP.ipv4) req.Header.Set(trustedHeader, testIP.ipv4)
req.Header.Set("Accept", tt.accepted)
for _, ua := range uas {
req.Header.Set("User-Agent", ua)
w := httptest.NewRecorder() w := httptest.NewRecorder()
app.ServeHTTP(w, req) app.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code) assert.Equal(t, 200, w.Code)
assert.Equal(t, testIP.ipv4, w.Body.String()) assert.Equal(t, tt.expected, w.Header().Get("Content-Type"))
})
}
}
func TestGetIP(t *testing.T) {
expected := testIP.ipv4 + "\n"
tests := []struct {
name string
accepted string
}{
{
name: "No browser",
accepted: "*/*",
},
{
name: "Bogus accept",
accepted: "bogus/plain",
},
{
name: "Plain accept",
accepted: "text/plain",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Set(trustedHeader, testIP.ipv4)
req.Header.Set("Accept", tt.accepted)
w := httptest.NewRecorder()
app.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
assert.Equal(t, expected, w.Body.String())
assert.Equal(t, contentType.text, w.Header().Get("Content-Type"))
})
} }
} }
@ -36,16 +98,76 @@ func TestHost(t *testing.T) {
} }
func TestClientPort(t *testing.T) { func TestClientPort(t *testing.T) {
type args struct {
params []string
headers map[string][]string
}
tests := []struct {
name string
args args
expected string
}{
{
name: "No trusted headers set",
expected: "1000\n",
},
{
name: "Trusted header only set",
args: args{
params: []string{
"-geoip2-city", "city",
"-geoip2-asn", "asn",
"-trusted-header", trustedHeader,
},
},
expected: "unknown\n",
},
{
name: "Trusted and port header set but not included in headers",
args: args{
params: []string{
"-geoip2-city", "city",
"-geoip2-asn", "asn",
"-trusted-header", trustedHeader,
"-trusted-port-header", trustedPortHeader,
},
},
expected: "unknown\n",
},
{
name: "Trusted and port header set and included in headers",
args: args{
params: []string{
"-geoip2-city", "city",
"-geoip2-asn", "asn",
"-trusted-header", trustedHeader,
"-trusted-port-header", trustedPortHeader,
},
headers: map[string][]string{
trustedHeader: {testIP.ipv4},
trustedPortHeader: {"1001"},
},
},
expected: "1001\n",
},
}
for _, tt := range tests {
_, _ = setting.Setup(tt.args.params)
t.Run(tt.name, func(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 = tt.args.headers
w := httptest.NewRecorder() w := httptest.NewRecorder()
app.ServeHTTP(w, req) app.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code) assert.Equal(t, 200, w.Code)
assert.Equal(t, contentType.text, w.Header().Get("Content-Type")) assert.Equal(t, contentType.text, w.Header().Get("Content-Type"))
assert.Equal(t, "1000\n", w.Body.String()) assert.Equal(t, tt.expected, w.Body.String())
t.Log(w.Header())
})
}
} }
func TestNotFound(t *testing.T) { func TestNotFound(t *testing.T) {
@ -59,36 +181,59 @@ 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"]}}` _, _ = setting.Setup(
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":""}` []string{
"-geoip2-city", "city",
"-geoip2-asn", "asn",
"-trusted-header", trustedHeader,
"-trusted-port-header", trustedPortHeader,
},
)
type args struct {
ip string
}
tests := []struct {
name string
args args
expected string
}{
{
name: "IPv4",
args: args{
ip: testIP.ipv4,
},
expected: jsonIPv4,
},
{
name: "IPv6",
args: args{
ip: testIP.ipv6,
},
expected: jsonIPv6,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, _ := http.NewRequest("GET", "/json", nil) req, _ := http.NewRequest("GET", "/json", nil)
req.RemoteAddr = testIP.ipv4 + ":" + "1000" req.RemoteAddr = net.JoinHostPort(tt.args.ip, "1000")
req.Host = "test" req.Host = "test"
req.Header.Set("X-Real-IP", testIP.ipv4) req.Header.Set(trustedHeader, tt.args.ip)
req.Header.Set(trustedPortHeader, "1001")
w := httptest.NewRecorder() w := httptest.NewRecorder()
app.ServeHTTP(w, req) app.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code) assert.Equal(t, 200, w.Code)
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, tt.expected, w.Body.String())
})
req.RemoteAddr = testIP.ipv6 + ":" + "1000" }
req.Host = "test"
req.Header.Set("X-Real-IP", testIP.ipv6)
w = httptest.NewRecorder()
app.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
assert.Equal(t, contentType.json, w.Header().Get("Content-Type"))
assert.JSONEq(t, expectedIPv6, w.Body.String())
} }
func TestAll(t *testing.T) { func TestAll(t *testing.T) {
expected := `IP: 81.2.69.192 expected := `IP: 81.2.69.192
Client Port: 1000 Client Port: 1001
City: London City: London
Country: United Kingdom Country: United Kingdom
Country Code: GB Country Code: GB
@ -102,13 +247,21 @@ ASN Organization:
Header1: one Header1: one
Host: test Host: test
X-Real-Ip: 81.2.69.192
` `
_, _ = setting.Setup(
[]string{
"-geoip2-city", "city",
"-geoip2-asn", "asn",
"-trusted-header", trustedHeader,
"-trusted-port-header", trustedPortHeader,
},
)
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(trustedHeader, testIP.ipv4)
req.Header.Set(trustedPortHeader, "1001")
req.Header.Set("Header1", "one") req.Header.Set("Header1", "one")
w := httptest.NewRecorder() w := httptest.NewRecorder()

View File

@ -10,15 +10,17 @@ import (
) )
func getHeadersAsSortedString(ctx *gin.Context) { func getHeadersAsSortedString(ctx *gin.Context) {
h := ctx.Request.Header h := httputils.GetHeadersWithoutTrustedHeaders(ctx)
h["Host"] = []string{ctx.Request.Host} h.Set("Host", ctx.Request.Host)
ctx.String(http.StatusOK, httputils.HeadersToSortedString(h)) ctx.String(http.StatusOK, httputils.HeadersToSortedString(h))
} }
func getHeaderAsString(ctx *gin.Context) { func getHeaderAsString(ctx *gin.Context) {
headers := httputils.GetHeadersWithoutTrustedHeaders(ctx)
h := ctx.Params.ByName("header") h := ctx.Params.ByName("header")
if v := ctx.GetHeader(h); v != "" { if v := headers.Get(ctx.Params.ByName("header")); v != "" {
ctx.String(http.StatusOK, template.HTMLEscapeString(v)) ctx.String(http.StatusOK, template.HTMLEscapeString(v))
} else if strings.ToLower(h) == "host" { } else if strings.ToLower(h) == "host" {
ctx.String(http.StatusOK, template.HTMLEscapeString(ctx.Request.Host)) ctx.String(http.StatusOK, template.HTMLEscapeString(ctx.Request.Host))

View File

@ -5,6 +5,7 @@ import (
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"github.com/dcarrillo/whatismyip/internal/setting"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -26,11 +27,20 @@ Header2: value22
Header3: value3 Header3: value3
Host: Host:
` `
_, _ = setting.Setup([]string{
"-geoip2-city", "city",
"-geoip2-asn", "asn",
"-trusted-header", trustedHeader,
"-trusted-port-header", trustedPortHeader,
})
req, _ := http.NewRequest("GET", "/headers", nil) req, _ := http.NewRequest("GET", "/headers", nil)
req.Header["Header1"] = []string{"value1"} req.Header = map[string][]string{
req.Header["Header2"] = []string{"value21", "value22"} "Header1": {"value1"},
req.Header["Header3"] = []string{"value3"} "Header2": {"value21", "value22"},
"Header3": {"value3"},
}
req.Header.Set(trustedHeader, "1.1.1.1")
req.Header.Set(trustedPortHeader, "1025")
w := httptest.NewRecorder() w := httptest.NewRecorder()
app.ServeHTTP(w, req) app.ServeHTTP(w, req)

View File

@ -8,6 +8,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// SetupTemplate reads and parses a template from file
func SetupTemplate(r *gin.Engine) { func SetupTemplate(r *gin.Engine) {
if setting.App.TemplatePath == "" { if setting.App.TemplatePath == "" {
t, _ := template.New("home").Parse(home) t, _ := template.New("home").Parse(home)
@ -18,6 +19,7 @@ func SetupTemplate(r *gin.Engine) {
} }
} }
// Setup defines the endpoints
func Setup(r *gin.Engine) { func Setup(r *gin.Engine) {
r.GET("/", getRoot) r.GET("/", getRoot)
r.GET("/client-port", getClientPortAsString) r.GET("/client-port", getClientPortAsString)

View File

@ -16,6 +16,7 @@ type testIPs struct {
} }
type contentTypes struct { type contentTypes struct {
html string
text string text string
json string json string
} }
@ -29,12 +30,16 @@ var (
ipv6ASN: "2a02:a800::1", ipv6ASN: "2a02:a800::1",
} }
contentType = contentTypes{ contentType = contentTypes{
html: "content-type: text/html; charset=utf-8",
text: "text/plain; charset=utf-8", text: "text/plain; charset=utf-8",
json: "application/json; charset=utf-8", json: "application/json; charset=utf-8",
} }
jsonIPv4 = `{"client_port":"1001","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": {}}`
jsonIPv6 = `{"asn":3352, "asn_organization":"TELEFONICA DE ESPANA", "city":"", "client_port":"1001", "country":"", "country_code":"", "host":"test", "ip":"2a02:9000::1", "ip_version":6, "latitude":0, "longitude":0, "postal_code":"", "time_zone":"", "headers": {}}`
) )
const trustedHeader = "X-Real-IP" const trustedHeader = "X-Real-IP"
const trustedPortHeader = "X-Real-Port"
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
app = gin.Default() app = gin.Default()

View File

@ -54,10 +54,11 @@ const expectedHome = `
func TestDefaultTemplate(t *testing.T) { func TestDefaultTemplate(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil) req, _ := http.NewRequest("GET", "/", nil)
req.Header = map[string][]string{
req.Header["Header1"] = []string{"value1"} "Header1": {"value1"},
req.Header["Header2"] = []string{"value21", "value22"} "Header2": {"value21", "value22"},
req.Header["Header3"] = []string{"value3"} "Header3": {"value3"},
}
tmpl, _ := template.New("home").Parse(home) tmpl, _ := template.New("home").Parse(home)
response := JSONResponse{ response := JSONResponse{

View File

@ -7,10 +7,12 @@ import (
"github.com/dcarrillo/whatismyip/models" "github.com/dcarrillo/whatismyip/models"
) )
// Geo defines a base type for lookups
type Geo struct { type Geo struct {
IP net.IP IP net.IP
} }
// LookUpCity queries the database for city data related to the given IP
func (g *Geo) LookUpCity() *models.GeoRecord { func (g *Geo) LookUpCity() *models.GeoRecord {
record := &models.GeoRecord{} record := &models.GeoRecord{}
err := record.LookUp(g.IP) err := record.LookUp(g.IP)
@ -22,6 +24,7 @@ func (g *Geo) LookUpCity() *models.GeoRecord {
return record return record
} }
// LookUpASN queries the database for ASN data related to the given IP
func (g *Geo) LookUpASN() *models.ASNRecord { func (g *Geo) LookUpASN() *models.ASNRecord {
record := &models.ASNRecord{} record := &models.ASNRecord{}
err := record.LookUp(g.IP) err := record.LookUp(g.IP)