Sync PHP backend feature parity: IP detection, database backends, API endpoints, and frontend

- IP detection: Cloudflare IPv6, ULA IPv6, proxy header chain, offline GeoIP DB
- Database: add SQLite (pure Go, no CGo) and MSSQL backends
- API: add JSON result sharing endpoint and ID obfuscation
- Frontend: add modern CSS design, design switcher, favicon
- Compatibility: ?cors parameter support, human-friendly distance rounding
- Update Go to 1.21, add modernc.org/sqlite and maxminddb deps
This commit is contained in:
Maddie Zhan
2026-04-30 13:53:52 +08:00
parent 603cbdeec5
commit cd20f44d20
39 changed files with 3005 additions and 851 deletions
+138 -4
View File
@@ -5,10 +5,13 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"strconv"
"strings"
"github.com/oschwald/maxminddb-golang"
log "github.com/sirupsen/logrus"
"github.com/umahmood/haversine"
@@ -140,16 +143,147 @@ func calculateDistance(clientLocation string, unit string) string {
}
dist, km := haversine.Distance(clientCoord, serverCoord)
unitString := " mi"
switch unit {
case "km":
dist = km
unitString = " km"
rounded := roundToNearest10(dist)
if dist < 20 {
return "<20 km"
}
return fmt.Sprintf("%.0f km", rounded)
case "NM":
dist = km * 0.539957
unitString = " NM"
return fmt.Sprintf("%.2f NM", dist)
default: // miles
distMi := dist
rounded := roundToNearest10(distMi)
if distMi < 15 {
return "<15 mi"
}
return fmt.Sprintf("%.0f mi", rounded)
}
}
// roundToNearest10 rounds a float64 to the nearest 10, matching PHP round($d, -1)
func roundToNearest10(val float64) float64 {
return float64(int64(val/10+0.5)) * 10
}
// GeoIP database holder (lazily opened on first use)
var (
geoIPReader *maxminddb.Reader
geoIPOpened bool
)
// getGeoIPData looks up the given IP in the configured GeoIP .mmdb database
// and returns ISP and country information if available.
// It returns nil if GeoIP is not configured or the lookup fails.
func getGeoIPData(ipStr string) *struct {
ASName string
CountryName string
} {
conf := config.LoadedConfig()
if conf.GeoIPDatabaseFile == "" {
return nil
}
return fmt.Sprintf("%.2f%s", dist, unitString)
if !geoIPOpened {
geoIPOpened = true
if _, err := os.Stat(conf.GeoIPDatabaseFile); os.IsNotExist(err) {
log.Warnf("GeoIP database file not found: %s", conf.GeoIPDatabaseFile)
return nil
}
reader, err := maxminddb.Open(conf.GeoIPDatabaseFile)
if err != nil {
log.Warnf("Failed to open GeoIP database: %s", err)
return nil
}
geoIPReader = reader
}
if geoIPReader == nil {
return nil
}
ip := net.ParseIP(ipStr)
if ip == nil {
return nil
}
// Try ipinfo.io offline database format first
var ipinfoResult map[string]interface{}
if err := geoIPReader.Lookup(ip, &ipinfoResult); err != nil {
log.Warnf("GeoIP lookup failed: %s", err)
return nil
}
if len(ipinfoResult) == 0 {
return nil
}
result := &struct {
ASName string
CountryName string
}{}
// ipinfo.io offline format uses "as_name" and "country_name"
if v, ok := ipinfoResult["as_name"].(string); ok {
result.ASName = v
}
if v, ok := ipinfoResult["country_name"].(string); ok {
result.CountryName = v
}
// If ipinfo format fields are empty, try standard MaxMind GeoIP2 format
if result.ASName == "" {
// Try autonomous_system > organization
if as, ok := ipinfoResult["autonomous_system"].(map[string]interface{}); ok {
if v, ok := as["organization"].(string); ok {
result.ASName = v
}
}
}
if result.CountryName == "" {
if country, ok := ipinfoResult["country"].(map[string]interface{}); ok {
if v, ok := country["names"].(map[string]interface{}); ok {
if n, ok := v["en"].(string); ok {
result.CountryName = n
}
}
}
// Fallback: direct "country" string field (as used by some GeoIP DBs)
if result.CountryName == "" {
if v, ok := ipinfoResult["country"].(string); ok {
result.CountryName = v
}
}
}
if result.ASName == "" && result.CountryName == "" {
return nil
}
return result
}
// getISPInfoByPriority tries to fetch ISP info using the ipinfo.io API first,
// then falls back to the configured offline GeoIP database, mirroring PHP behavior.
func getISPInfoByPriority(addr string) results.IPInfoResponse {
// First try: ipinfo.io API
info := getIPInfo(addr)
if info.Organization != "" || info.Country != "" {
return info
}
// Second try: offline GeoIP database
geo := getGeoIPData(addr)
if geo != nil {
info.Organization = geo.ASName
info.Country = geo.CountryName
return info
}
// Fallback: empty result (will show IP only)
return info
}