echoip/Makefile
ns da1995a1f4
chore: harden Makefile and Dockerfile
Makefile:
- Add .PHONY declaration for all 16 phony targets.
- Fix parse-time $(error) at line 39: replace with runtime shell
  check so the guard actually fires when the recipe runs.
- Simplify check-fmt to portable 'gofmt -l -s .' form.
- Add -trimpath -ldflags='-s -w' to install for smaller,
  reproducible binaries.

Dockerfile:
- Switch runtime to distroless/static-debian13:nonroot + USER nonroot.
- Split COPY so go.mod/go.sum cache layer is independent of source.
- Drop ENV GO111MODULE=on (no-op since Go 1.16).
2026-05-07 07:09:35 +00:00

84 lines
2.7 KiB
Makefile

DOCKER ?= docker
DOCKER_IMAGE ?= createleafcloud/echoip
OS := $(shell uname)
ifeq ($(OS),Linux)
TAR_OPTS := --wildcards
endif
XGOARCH := amd64
XGOOS := linux
XBIN := $(XGOOS)_$(XGOARCH)/echoip
.PHONY: all test vet check-fmt lint install geoip-download \
docker-multiarch-builder docker-build docker-login docker-test \
docker-push docker-pushx xinstall publish run
all: lint test install
test:
go test ./...
vet:
go vet ./...
check-fmt:
@out=$$(gofmt -l -s .); test -z "$$out" || (echo "Unformatted files:"; echo "$$out"; exit 1)
lint: check-fmt vet
install:
go install -trimpath -ldflags="-s -w" ./...
databases := GeoLite2-City GeoLite2-Country GeoLite2-ASN
$(databases):
mkdir -p data
ifdef GEOIP_LICENSE_KEY
@curl -fsSL -m 30 "https://download.maxmind.com/app/geoip_download?edition_id=$@&license_key=$(GEOIP_LICENSE_KEY)&suffix=tar.gz" | tar $(TAR_OPTS) --strip-components=1 -C $(CURDIR)/data -xzf - '*.mmdb'
@if [ -f data/city.mmdb ]; then echo "ERROR: Failed to download GEOIP databases, GEOIP_LICENSE_KEY is probably wrong. Please see https://blog.maxmind.com/2019/12/18/significant-changes-to-accessing-and-using-geolite2-databases/"; exit 1; fi
else
cp GeoLite2-City.mmdb data/city.mmdb
cp GeoLite2-Country.mmdb data/country.mmdb
cp GeoLite2-ASN.mmdb data/asn.mmdb
endif
geoip-download: $(databases)
# Create an environment to build multiarch containers (https://github.com/docker/buildx/)
docker-multiarch-builder:
DOCKER_BUILDKIT=1 $(DOCKER) build -o . https://github.com/docker/buildx.git
mkdir -p ~/.docker/cli-plugins
mv buildx ~/.docker/cli-plugins/docker-buildx
$(DOCKER) buildx create --name multiarch-builder --node multiarch-builder --driver docker-container --use
$(DOCKER) run --rm --privileged multiarch/qemu-user-static --reset -p yes
docker-build:
$(DOCKER) build -t $(DOCKER_IMAGE) .
docker-login:
$(DOCKER) login --username "$(DOCKER_USERNAME)" --password "$(DOCKER_PASSWORD)"
docker-test:
$(eval CONTAINER=$(shell $(DOCKER) run --rm --detach --publish-all $(DOCKER_IMAGE)))
$(eval DOCKER_PORT=$(shell $(DOCKER) port $(CONTAINER) | cut -d ":" -f 2))
curl -fsS -m 5 localhost:$(DOCKER_PORT) > /dev/null; $(DOCKER) stop $(CONTAINER)
docker-push: docker-test docker-login
$(DOCKER) push $(DOCKER_IMAGE)
docker-pushx: geoip-download docker-multiarch-builder docker-test docker-login
$(DOCKER) buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t $(DOCKER_IMAGE) --push .
xinstall:
env GOOS=$(XGOOS) GOARCH=$(XGOARCH) go install ./...
publish:
ifndef DEST_PATH
$(error DEST_PATH must be set when publishing)
endif
rsync -a $(GOPATH)/bin/$(XBIN) $(DEST_PATH)/$(XBIN)
@sha256sum $(GOPATH)/bin/$(XBIN)
run:
go run cmd/echoip/main.go -a data/asn.mmdb -c data/city.mmdb -f data/country.mmdb -H x-forwarded-for -r -s -p