Skip to content

sansjack/whereyouat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

whereyouat

Determine a client’s location based on their IP address via TCP RPC or HTTP.

I recently found out that a company I’ve been working for has been using an external paid API to check if an IP is in the EU (GDPR). The main reason? To figure out if a cookie banner needs to be shown. I went down a rabbit hole learning how this works offline and decided to sharpen my Go skills along the way.

This tool can also do full IP tracking and provide city-level geolocation, but my goal was just a fast way to check a client’s country.

Note: This won’t return results for localhost—I patched my IPv4 as the remote address for testing purposes.

Big thanks to:

Features

  • TCP RPC and HTTP JSON-RPC – Includes a Go client example
  • IP Geolocation – Automatic location detection from client IP
  • Auto-updating Database – Fetches the latest GeoLite2 database automatically
  • Environment Configuration – Simple .env support

Quick Start

Install Dependencies

make deps

Run the Server

make run

Server endpoints:

  • TCP RPC: localhost:1234
  • HTTP JSON-RPC: localhost:8080

Test with Clients

TCP Client:

make tcp-client

cURL:

curl -X POST http://localhost:8080/rpc \
  -H "Content-Type: application/json" \
  -d '{"method":"LocationService.Calculate","params":[{}],"id":1}'

Configuration

Create a .env file (see .env.example):

TCP_ADDRESS=:1234
HTTP_ADDRESS=:8080
MMDB_GITHUB_API_URL=https://api.github.com/repos/P3TERX/GeoLite.mmdb/releases/latest
DB_DIR=db
DB_FILENAME=GeoLite2-Country.mmdb
TAG_FILE=GeoLite2-Country.mmdb.tag

API

LocationService.Calculate

Determine a client’s location based on their IP address.

Request:

{
  "method": "LocationService.Calculate",
  "params": [{}],
  "id": 1
}

Response:

{
  "result": {
    "Location": "United States",
    "IsoCode": "US"
  },
  "error": null,
  "id": 1
}

Makefile Commands

make help         # Show available commands
make build        # Build server and client binaries
make run          # Run the server
make tcp-client   # Run TCP client example
make test         # Run tests
make clean        # Remove build artifacts
make deps         # Download dependencies

About

Identify request origin countries using Golang

Resources

Stars

Watchers

Forks

Contributors