mirror of
https://github.com/dcarrillo/whatismyip.git
synced 2025-07-06 22:39:25 +00:00
Compare commits
21 Commits
1.0.0
...
e4667bda05
Author | SHA1 | Date | |
---|---|---|---|
e4667bda05
|
|||
49b28b6536
|
|||
edd0713c21
|
|||
ab46275990
|
|||
f7d0a679c9
|
|||
ac32dc240f
|
|||
2d330a99b7
|
|||
9b10052cd1
|
|||
def2b0f2f0
|
|||
8b5cccd744 | |||
2571e22843
|
|||
2807e2afc9
|
|||
1aa59f1b0b | |||
126ae833b5
|
|||
97b3245278
|
|||
7bc95f98c8
|
|||
66f3436371
|
|||
7d2209d390
|
|||
255660374f
|
|||
36f82eb6a7 | |||
c0d1143de6
|
66
.github/workflows/main.yml
vendored
Normal file
66
.github/workflows/main.yml
vendored
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
name: CI
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
tags:
|
||||||
|
- '*'
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
tests:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2.4.0
|
||||||
|
|
||||||
|
- name: install go
|
||||||
|
uses: actions/setup-go@v2
|
||||||
|
with:
|
||||||
|
go-version: "^1.18"
|
||||||
|
|
||||||
|
- name: Lint
|
||||||
|
run: make lint
|
||||||
|
|
||||||
|
- name: Tests
|
||||||
|
run: make test
|
||||||
|
|
||||||
|
deploy:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: tests
|
||||||
|
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
goosarch: [linux-amd64]
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2.4.0
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Set env
|
||||||
|
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
- name: Sign in to dockerhub
|
||||||
|
run: echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
|
||||||
|
|
||||||
|
- name: Deploy image
|
||||||
|
run: make docker-push VERSION=$RELEASE_VERSION
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: make build VERSION=$RELEASE_VERSION
|
||||||
|
|
||||||
|
- name: Prepare release
|
||||||
|
run: |
|
||||||
|
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
|
||||||
|
sha256sum whatismyip-$RELEASE_VERSION-${{matrix.goosarch}}.tar.gz > whatismyip-$RELEASE_VERSION-${{matrix.goosarch}}.tar.gz.sha256
|
||||||
|
|
||||||
|
- name: Release
|
||||||
|
uses: softprops/action-gh-release@v0.1.14
|
||||||
|
with:
|
||||||
|
body_path: changelog.txt
|
||||||
|
files: |
|
||||||
|
whatismyip-${{env.RELEASE_VERSION}}-${{matrix.goosarch}}.tar.gz
|
||||||
|
whatismyip-${{env.RELEASE_VERSION}}-${{matrix.goosarch}}.tar.gz.sha256
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
@ -1,10 +1,13 @@
|
|||||||
FROM golang:1.17-alpine as builder
|
FROM golang:1.18-alpine as builder
|
||||||
|
|
||||||
|
ARG ARG_VERSION
|
||||||
|
ENV VERSION $ARG_VERSION
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
RUN apk add make && make build
|
RUN apk add make git && make build VERSION=$VERSION
|
||||||
|
|
||||||
# Build final image
|
# Build final image
|
||||||
FROM scratch
|
FROM scratch
|
||||||
|
36
Makefile
36
Makefile
@ -1,3 +1,4 @@
|
|||||||
|
GOPATH ?= $(shell go env GOPATH)
|
||||||
VERSION ?= devel-$(shell git rev-parse --short HEAD)
|
VERSION ?= devel-$(shell git rev-parse --short HEAD)
|
||||||
DOCKER_URL ?= dcarrillo/whatismyip
|
DOCKER_URL ?= dcarrillo/whatismyip
|
||||||
|
|
||||||
@ -13,32 +14,49 @@ integration-test:
|
|||||||
go test ./integration-tests -v
|
go test ./integration-tests -v
|
||||||
|
|
||||||
.PHONY: install-tools
|
.PHONY: install-tools
|
||||||
install-int:
|
install-tools:
|
||||||
@hash 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 $(go env GOPATH)/bin v1.43.0
|
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.45.0; \
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@hash 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 ./...
|
||||||
|
|
||||||
.PHONY: build
|
.PHONY: build
|
||||||
build:
|
build:
|
||||||
CGO_ENABLED=0 go build -ldflags="-X 'github.com/dcarrillo/whatismyip/internal/core.Version=${VERSION}'" -o whatismyip ./cmd
|
CGO_ENABLED=0 go build -ldflags="-s -w -X 'github.com/dcarrillo/whatismyip/internal/core.Version=${VERSION}'" -o whatismyip ./cmd
|
||||||
|
|
||||||
.PHONY: docker-build
|
.PHONY: docker-build
|
||||||
docker-build:
|
docker-build:
|
||||||
docker build --tag ${DOCKER_URL}:${VERSION} .
|
docker build --build-arg=ARG_VERSION="${VERSION}" --tag ${DOCKER_URL}:${VERSION} .
|
||||||
|
|
||||||
|
.PHONY: docker-push
|
||||||
|
docker-push: docker-build
|
||||||
|
ifneq (,$(findstring devel-,$(VERSION)))
|
||||||
|
@echo "VERSION is set to ${VERSION}, I can't push devel builds"
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
docker push ${DOCKER_URL}:${VERSION}
|
||||||
|
docker tag ${DOCKER_URL}:${VERSION} ${DOCKER_URL}:latest
|
||||||
|
docker push ${DOCKER_URL}:latest
|
||||||
|
endif
|
||||||
|
|
||||||
.PHONY: docker-run
|
.PHONY: docker-run
|
||||||
docker-run: docker-build
|
docker-run: docker-build
|
||||||
docker run --tty --interactive --rm \
|
docker run --tty --interactive --rm \
|
||||||
-v $$PWD/test/GeoIP2-City-Test.mmdb:/tmp/GeoIP2-City-Test.mmdb:ro \
|
-v ${PWD}/test/GeoIP2-City-Test.mmdb:/tmp/GeoIP2-City-Test.mmdb:ro \
|
||||||
-v $$PWD/test/GeoLite2-ASN-Test.mmdb:/tmp/GeoLite2-ASN-Test.mmdb:ro -p 8080:8080 \
|
-v ${PWD}/test/GeoLite2-ASN-Test.mmdb:/tmp/GeoLite2-ASN-Test.mmdb:ro -p 8080:8080 \
|
||||||
${DOCKER_URL}:${VERSION} \
|
${DOCKER_URL}:${VERSION} \
|
||||||
-geoip2-city /tmp/GeoIP2-City-Test.mmdb \
|
-geoip2-city /tmp/GeoIP2-City-Test.mmdb \
|
||||||
-geoip2-asn /tmp/GeoLite2-ASN-Test.mmdb \
|
-geoip2-asn /tmp/GeoLite2-ASN-Test.mmdb \
|
||||||
|
56
README.md
56
README.md
@ -1,12 +1,22 @@
|
|||||||
# What is my IP address
|
# What is my IP address
|
||||||
|
|
||||||
|
[](https://github.com/dcarrillo/whatismyip/actions)
|
||||||
|
[](https://goreportcard.com/report/github.com/dcarrillo/whatismyip)
|
||||||
|
[](https://github.com/dcarrillo/whatismyip/releases/)
|
||||||
|
[](./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)
|
||||||
- [Build](#build)
|
- [Build](#build)
|
||||||
- [Usage](#usage)
|
- [Usage](#usage)
|
||||||
|
- [Examples](#examples)
|
||||||
|
- [Run a default TCP server](#run-a-default-tcp-server)
|
||||||
|
- [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)
|
- [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.
|
||||||
@ -25,12 +35,14 @@ curl -6 ifconfig.es
|
|||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- TLS available
|
- 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 -icense 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.
|
||||||
- HTML with templates, text plain and JSON output.
|
- Self-contained server what can reload GeoLite2 databases and/or SSL certificates without stop/start. The `hup` signal is honored.
|
||||||
|
- HTML templates for the landing page.
|
||||||
|
- Text plain and JSON output.
|
||||||
|
|
||||||
## Endpoints
|
## Endpoints
|
||||||
|
|
||||||
@ -81,11 +93,37 @@ Usage of ./whatismyip:
|
|||||||
Output version information and exit
|
Output version information and exit
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
### Run a default TCP server
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./whatismyip -geoip2-city ./test/GeoIP2-City-Test.mmdb -geoip2-asn ./test/GeoLite2-ASN-Test.mmdb
|
||||||
|
```
|
||||||
|
|
||||||
|
### 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
|
||||||
|
```
|
||||||
|
|
||||||
|
### Run a default TCP server with a custom template and trust a custom header set by an upstream proxy
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./whatismyip -geoip2-city ./test/GeoIP2-City-Test.mmdb -geoip2-asn ./test/GeoLite2-ASN-Test.mmdb \
|
||||||
|
-trusted-header X-Real-IP -template mytemplate.tmpl
|
||||||
|
```
|
||||||
|
|
||||||
|
## Download
|
||||||
|
|
||||||
|
Download latest version from https://github.com/dcarrillo/whatismyip/releases
|
||||||
|
|
||||||
## Docker
|
## Docker
|
||||||
|
|
||||||
An ultra-light (13MB) image is available.
|
An ultra-light (~9MB) image is available.
|
||||||
|
|
||||||
### Running a container locally using test databases
|
### Run a container locally using test databases
|
||||||
|
|
||||||
`make docker-run`
|
`make docker-run`
|
||||||
|
|
||||||
@ -96,6 +134,6 @@ docker run --tty --interactive --rm \
|
|||||||
-v $PWD/<path to city database>:/tmp/GeoIP2-City-Test.mmdb:ro \
|
-v $PWD/<path to city database>:/tmp/GeoIP2-City-Test.mmdb:ro \
|
||||||
-v $PWD/<path to ASN database>:/tmp/GeoLite2-ASN-Test.mmdb:ro -p 8080:8080 \
|
-v $PWD/<path to ASN database>:/tmp/GeoLite2-ASN-Test.mmdb:ro -p 8080:8080 \
|
||||||
dcarrillo/whatismyip:latest \
|
dcarrillo/whatismyip:latest \
|
||||||
-geoip2-city /tmp/GeoIP2-City-Test.mmdb \
|
-geoip2-city /tmp/GeoIP2-City-Test.mmdb \
|
||||||
-geoip2-asn /tmp/GeoLite2-ASN-Test.mmdb
|
-geoip2-asn /tmp/GeoLite2-ASN-Test.mmdb
|
||||||
```
|
```
|
||||||
|
@ -3,6 +3,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -24,7 +26,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 +122,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...")
|
||||||
|
60
go.mod
60
go.mod
@ -1,58 +1,58 @@
|
|||||||
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-gonic/gin v1.7.7
|
||||||
github.com/oschwald/maxminddb-golang v1.8.0
|
github.com/oschwald/maxminddb-golang v1.8.0
|
||||||
github.com/stretchr/testify v1.7.0
|
github.com/stretchr/testify v1.7.1
|
||||||
github.com/testcontainers/testcontainers-go v0.11.1
|
github.com/testcontainers/testcontainers-go v0.12.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.2 // indirect
|
||||||
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
|
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
|
||||||
github.com/containerd/cgroups v0.0.0-20210114181951-8a68de567b68 // indirect
|
github.com/containerd/cgroups v1.0.3 // indirect
|
||||||
github.com/containerd/containerd v1.5.0-beta.4 // indirect
|
github.com/containerd/containerd v1.6.1 // 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.13+incompatible // indirect
|
||||||
github.com/docker/go-connections v0.4.0 // indirect
|
github.com/docker/go-connections v0.4.0 // 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.10.1 // indirect
|
||||||
github.com/goccy/go-json v0.7.10 // 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/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/sys/mount v0.3.1 // indirect
|
||||||
github.com/moby/sys/mountinfo v0.4.1 // indirect
|
github.com/moby/sys/mountinfo v0.6.0 // indirect
|
||||||
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635 // 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/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.2 // indirect
|
||||||
github.com/opencontainers/runc v1.0.0-rc93 // indirect
|
github.com/opencontainers/runc v1.1.0 // 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/sirupsen/logrus v1.8.1 // indirect
|
||||||
github.com/ugorji/go/codec v1.2.6 // indirect
|
github.com/ugorji/go/codec v1.2.7 // indirect
|
||||||
go.opencensus.io v0.22.3 // indirect
|
go.opencensus.io v0.23.0 // indirect
|
||||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
|
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect
|
||||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
|
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
|
||||||
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359 // indirect
|
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 // 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-20220317150908-0efb43f6373e // indirect
|
||||||
google.golang.org/grpc v1.33.2 // indirect
|
google.golang.org/grpc v1.45.0 // indirect
|
||||||
google.golang.org/protobuf v1.27.1 // indirect
|
google.golang.org/protobuf v1.27.1 // 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.0-20210107192922-496545a6307b // indirect
|
||||||
|
@ -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{
|
||||||
@ -38,10 +38,10 @@ func buildContainer() testcontainers.ContainerRequest {
|
|||||||
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{
|
BindMounts: map[string]string{
|
||||||
filepath.Join(dirname, "/../test/GeoIP2-City-Test.mmdb"): "/tmp/GeoIP2-City-Test.mmdb",
|
"/tmp/GeoIP2-City-Test.mmdb": filepath.Join(dir, "/../test/GeoIP2-City-Test.mmdb"),
|
||||||
filepath.Join(dirname, "/../test/GeoLite2-ASN-Test.mmdb"): "/tmp/GeoLite2-ASN-Test.mmdb",
|
"/tmp/GeoLite2-ASN-Test.mmdb": filepath.Join(dir, "/../test/GeoLite2-ASN-Test.mmdb"),
|
||||||
filepath.Join(dirname, "/../test/server.pem"): "/tmp/server.pem",
|
"/tmp/server.pem": filepath.Join(dir, "/../test/server.pem"),
|
||||||
filepath.Join(dirname, "/../test/server.key"): "/tmp/server.key",
|
"/tmp/server.key": filepath.Join(dir, "/../test/server.key"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
|
// Version to be defined at build time
|
||||||
var Version = "tobedefined"
|
var Version = "tobedefined"
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
"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 +32,7 @@ func HeadersToSortedString(headers http.Header) string {
|
|||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 +59,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
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package setting
|
package setting
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@ -26,62 +28,79 @@ type settings struct {
|
|||||||
TLSKeyPath string
|
TLSKeyPath string
|
||||||
TrustedHeader string
|
TrustedHeader string
|
||||||
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")
|
Server: serverSettings{
|
||||||
address := flag.String("bind", defaultAddress, "Listening address (see https://pkg.go.dev/net?#Listen)")
|
ReadTimeout: 10 * time.Second,
|
||||||
addressTLS := flag.String("tls-bind", "", "Listening address for TLS (see https://pkg.go.dev/net?#Listen)")
|
WriteTimeout: 10 * time.Second,
|
||||||
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()
|
// Setup initializes the App object parsing the flags
|
||||||
|
func Setup(args []string) (output string, err error) {
|
||||||
|
flags := flag.NewFlagSet("whatismyip", flag.ContinueOnError)
|
||||||
|
var buf bytes.Buffer
|
||||||
|
flags.SetOutput(&buf)
|
||||||
|
|
||||||
if *ver {
|
flags.StringVar(&App.GeodbPath.City, "geoip2-city", "", "Path to GeoIP2 city database")
|
||||||
fmt.Printf("whaismyip version %s", core.Version)
|
flags.StringVar(&App.GeodbPath.ASN, "geoip2-asn", "", "Path to GeoIP2 ASN database")
|
||||||
os.Exit(0)
|
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")
|
||||||
|
|
||||||
|
err = flags.Parse(args)
|
||||||
|
if err != nil {
|
||||||
|
return buf.String(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
if *city == "" || *asn == "" {
|
if App.version {
|
||||||
exitWithError("geoip2-city and geoip2-asn parameters are mandatory")
|
return fmt.Sprintf("whatismyip version %s", core.Version), ErrVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
if (*addressTLS != "") && (*tlsCrtPath == "" || *tlsKeyPath == "") {
|
if App.GeodbPath.City == "" || App.GeodbPath.ASN == "" {
|
||||||
exitWithError("In order to use TLS -tls-crt and -tls-key flags are mandatory")
|
return "", fmt.Errorf("geoip2-city and geoip2-asn parameters are mandatory")
|
||||||
}
|
}
|
||||||
|
|
||||||
if *template != "" {
|
if (App.TLSAddress != "") && (App.TLSCrtPath == "" || App.TLSKeyPath == "") {
|
||||||
info, err := os.Stat(*template)
|
return "", fmt.Errorf("In order to use TLS -tls-crt and -tls-key flags are mandatory")
|
||||||
if os.IsNotExist(err) || info.IsDir() {
|
}
|
||||||
exitWithError(*template + " doesn't exist or it's not a file")
|
|
||||||
|
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{
|
return buf.String(), nil
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
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")
|
||||||
|
}
|
@ -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
77
models/geo_test.go
Normal 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")))
|
||||||
|
}
|
@ -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"
|
||||||
@ -15,6 +14,7 @@ import (
|
|||||||
|
|
||||||
const userAgentPattern = `curl|wget|libwww-perl|python|ansible-httpget|HTTPie|WindowsPowerShell|http_request|Go-http-client|^$`
|
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"`
|
||||||
@ -46,12 +46,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 +84,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"],
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package router
|
package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
@ -9,7 +10,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestIP4RootFromCli(t *testing.T) {
|
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, _ := http.NewRequest("GET", "/", nil)
|
||||||
req.Header.Set("X-Real-IP", testIP.ipv4)
|
req.Header.Set("X-Real-IP", testIP.ipv4)
|
||||||
@ -37,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()
|
||||||
@ -60,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)
|
||||||
|
|
||||||
@ -74,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)
|
||||||
|
|
||||||
@ -106,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")
|
||||||
|
@ -28,9 +28,11 @@ Host:
|
|||||||
`
|
`
|
||||||
|
|
||||||
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"},
|
||||||
|
}
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
app.ServeHTTP(w, req)
|
app.ServeHTTP(w, req)
|
||||||
|
@ -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)
|
||||||
|
@ -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{
|
||||||
|
@ -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)
|
||||||
|
Reference in New Issue
Block a user