This repository has been archived on 2026-08-16. You can view files and clone it. You cannot open issues or pull requests or push a commit.
claudecodeandClaude 80aa464e09 Address codex review (#22): client IP validation, updated_at, merge robustness
1. /json client IP (codex #1, Medium): HandleJson now validates
   c.ClientIP() and returns 400 on a malformed value instead of letting
   Query fail into a 500. HandleIpInfo gained a selfLookup flag so a valid
   but DB-absent self-IP preserves the 9d07639 graceful 200 {"ip": ip}
   default (private/loopback addresses are never in a public GeoIP DB),
   while the explicit /:ip route still returns 404 on not-found.

2. updated_at=0 on initial load (codex #2, Low): stampDbUpdatedAt() now
   records the canonical mmdb's mtime both on initial load (existing
   files) and after each successful update, so /metrics never reports 0
   while serving real data.

3. Merge corruption -> permanent outage: mmdbmeld.WriteMMDB truncates the
   canonical mmdb before writing; a failed merge previously left it empty
   and the saved etag made the next daily run 304-skip the retry, so a
   restart failed to open the file and the service went 503 permanently.
   fetchDbs now backs up the canonical file before merge, restores it on
   merge/reload failure, and drops the etags so the next run retries.

4. Temp-file leaks: Reload removes the temp clone and restores d.dbFile
   when reader.Open fails (it previously assigned d.dbFile before Open);
   cloneDBFile removes the temp on a failed copy.

Verified: go build, go vet, gofmt -l, go test -race ./... all pass.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-22 17:11:58 +07:00
2025-10-13 17:27:14 +07:00
2024-08-05 15:06:39 +07:00
2024-07-14 00:14:25 +07:00
2024-11-07 08:56:28 +07:00
2024-08-05 15:06:39 +07:00
2026-06-13 17:01:46 +00:00
2024-08-05 15:16:24 +07:00
2024-07-13 23:58:47 +07:00
2026-07-03 10:17:02 +07:00

IP Info Service

A high-performance IP geolocation service built with Go that provides detailed information about IP addresses including country, ASN (Autonomous System Number), and other geolocation data.

Features

  • 🌍 IP geolocation lookup with country information
  • 🏢 ASN (Autonomous System Number) information
  • 🚀 High-performance HTTP API
  • 🔄 Automatic database updates
  • 🐳 Docker support
  • 📊 Metrics endpoint for monitoring
  • 🛡️ Input validation and error handling

Quick Start

  1. Clone the repository:
git clone <repository-url>
cd ip-info
  1. Start the service using Docker Compose:
docker-compose up -d

The service will be available at http://localhost:28080

Manual Installation

  1. Install Go 1.25 or later
  2. Clone and build:
git clone <repository-url>
cd ip-info
go mod download
go build -o ip-info
./ip-info

API Endpoints

1. Get Your IP Information

Plain Text Response:

curl http://localhost:28080/
# Returns: 192.168.1.100

JSON Response:

curl http://localhost:28080/json

Response:

{
  "ip": "192.168.1.100",
  "country": "US",
  "country_name": "United States",
  "asn": "AS15169",
  "asn_org": "Google LLC"
}

2. Look Up Specific IP Address

Query any IP address:

curl http://localhost:28080/8.8.8.8

Response:

{
  "ip": "8.8.8.8",
  "country": "US",
  "country_name": "United States",
  "asn": "AS15169",
  "asn_org": "Google LLC"
}

3. Metrics Endpoint

Monitor service health and performance:

curl http://localhost:28080/metrics

Configuration

Environment Variables

Variable Default Description
API_PORT 8080 Port for the HTTP server
GIN_TRUSTED_PROXY_IP - Trusted proxy IP for client IP detection
GIN_MODE debug Gin framework mode (debug, release)

Using .env File

Create a .env file in the project root:

API_PORT=8080
GIN_TRUSTED_PROXY_IP=127.0.0.1
GIN_MODE=release

Docker Configuration

Docker Compose

The included docker-compose.yml provides:

  • Service on port 28080 (maps to container port 8080)
  • Persistent cache volume
  • Auto-restart policy
  • Vietnam timezone configuration

Custom Docker Build

# Build image
docker build -t ip-info .

# Run container
docker run -p 8080:8080 -v $(pwd)/.cache:/app/.cache ip-info

Data Sources

The service automatically downloads and updates IP geolocation databases from:

  • Country Data: GeoLite2 Country database via jsDelivr CDN
  • ASN Data: GeoLite2 ASN database via jsDelivr CDN

Database updates happen automatically in the background to ensure data freshness.

Usage Examples

Basic IP Lookup

# Get information about Google's DNS
curl http://localhost:28080/8.8.8.8

# Get information about Cloudflare's DNS
curl http://localhost:28080/1.1.1.1

# Get your own IP info
curl http://localhost:28080/json

Integration Examples

JavaScript/Node.js:

const response = await fetch('http://localhost:28080/8.8.8.8');
const ipInfo = await response.json();
console.log(ipInfo);

Python:

import requests

response = requests.get('http://localhost:28080/8.8.8.8')
ip_info = response.json()
print(ip_info)

cURL with jq:

curl -s http://localhost:28080/8.8.8.8 | jq .

Response Format

All JSON responses include:

  • ip: The queried IP address
  • country: ISO 3166-1 alpha-2 country code
  • country_name: Full country name
  • asn: Autonomous System Number (when available)
  • asn_org: ASN organization name (when available)

Error Handling

Invalid IP Address

curl http://localhost:28080/invalid-ip

Response:

{
  "error": "invalid ip"
}

Service Unavailable

If the database is not loaded yet:

HTTP 500: Try again later

Performance

  • Startup Time: Database loads automatically on startup
  • Response Time: Sub-millisecond IP lookups after database load
  • Memory Usage: Optimized MMDB format for efficient memory usage
  • Concurrency: Built with Go's excellent concurrency support

Monitoring

Health Check

curl http://localhost:28080/metrics

Logs

The service provides structured JSON logging with different log levels. In Docker:

docker-compose logs -f ip-info

Development

Local Development

# Install dependencies
go mod download

# Run in development mode
go run main.go

# Build for production
go build -o ip-info

Project Structure

├── main.go                 # Application entry point
├── configs/               # Configuration files
├── internal/
│   ├── data/             # Database handling
│   └── services/
│       ├── api/          # HTTP API handlers
│       └── db_updater/   # Database update service
├── pkg/                  # Shared packages
├── data/                 # Database files (auto-generated)
└── docker-compose.yml    # Docker configuration

License

[Add your license information here]

Contributing

[Add contribution guidelines here]

Support

For issues and questions, please open an issue or contact the maintainers.

S
Description
Archived due to using Cloudflare instead
https://ip.thuanle.me
Readme
11 MiB
Languages
Go 98.2%
Dockerfile 1.8%