peip

ip identifier
Log | Files | Refs | README

commit 4a29096fc0418e7cb4b1d5412e850ccb76cbd074
Author: aperturefever <glyr@pm.me>
Date:   Sat, 26 Sep 2026 18:28:32 +0300

Initial commit

Diffstat:
A.gitignore | 6++++++
AMakefile | 33+++++++++++++++++++++++++++++++++
AREADME.md | 0
Ago.mod | 5+++++
Ago.sum | 2++
Amain.go | 42++++++++++++++++++++++++++++++++++++++++++
Apledge_openbsd.go | 15+++++++++++++++
Apledge_other.go | 5+++++
8 files changed, 108 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,6 @@ +# built binaries +peip +*.test + +# editors +*.swp diff --git a/Makefile b/Makefile @@ -0,0 +1,33 @@ +# Makefile for peip +.PHONY: build openbsd clean install help + +BIN_NAME=peip +BIN_PATH=/var/www/cgi-bin/$(BIN_NAME) +OWNER=root:www +GO=go +GOOS_OPENBSD=openbsd +GOARCH_OPENBSD=amd64 +LDFLAGS_OPENBSD=-linkmode external -extldflags -static + +all: build +## build: build project for the current host (non-openbsd) +build: + $(GO) build -o $(BIN_NAME) . +## openbsd: cross-build a static binary for openbsd/amd64 +openbsd: + GOOS=$(GOOS_OPENBSD) GOARCH=$(GOARCH_OPENBSD) $(GO) build -ldflags "$(LDFLAGS_OPENBSD)" -o $(BIN_NAME) . +## clean: cleanup current and destination project binaries +clean: + rm -f $(BIN_PATH) + rm -f $(BIN_NAME) + +## install: build for openbsd and place in destination directories +install: openbsd + mkdir -p $(dir $(BIN_PATH)) + cp $(BIN_NAME) $(BIN_PATH) + chmod 755 $(BIN_PATH) + -chown $(OWNER) $(BIN_PATH) +## help: this help +help: + @echo "Available targets:" + @grep -E '^## ' Makefile | sed 's/^## /:: /' diff --git a/README.md b/README.md diff --git a/go.mod b/go.mod @@ -0,0 +1,5 @@ +module peip + +go 1.26.5 + +require golang.org/x/sys v0.47.0 diff --git a/go.sum b/go.sum @@ -0,0 +1,2 @@ +golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs= +golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= diff --git a/main.go b/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "fmt" + "net" + "net/http" + "net/http/cgi" + "strings" +) + +func handleRequest(w http.ResponseWriter, r *http.Request) { + envVal := r.Header.Get("X-Forwarded-For") + if envVal == "" { + http.Error(w, "Missing Header", http.StatusBadRequest) + return + } + + remoteIP := envVal + if comma := strings.Index(envVal, ","); comma != -1 { + remoteIP = envVal[:comma] + } + remoteIP = strings.TrimSpace(remoteIP) + + if parsedIP := net.ParseIP(remoteIP); parsedIP == nil { + w.Header().Set("Content-Type", "text/plain; charset=utf-8") + w.WriteHeader(http.StatusBadRequest) + fmt.Fprintf(w, "Invalid IP: %s", remoteIP) + return + } + + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.WriteHeader(http.StatusOK) + fmt.Fprint(w, remoteIP) +} + +func main() { + pledge() + + if err := cgi.Serve(http.HandlerFunc(handleRequest)); err != nil { + panic(err) + } +} diff --git a/pledge_openbsd.go b/pledge_openbsd.go @@ -0,0 +1,15 @@ +//go:build openbsd + +package main + +import ( + "fmt" + + "golang.org/x/sys/unix" +) + +func pledge() { + if err := unix.Pledge("stdio", ""); err != nil { + panic(fmt.Sprintf("pledge failed: %v", err)) + } +} diff --git a/pledge_other.go b/pledge_other.go @@ -0,0 +1,5 @@ +//go:build !openbsd + +package main + +func pledge() {}