mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-21 02:36:49 +00:00
ai leveled logging now goes through github.com/hanzoai/ai/log, a printf-preserving surface over luxfi/log that emits structured records to stderr for the platform pipeline (HIP-0106). The 65 beego/logs call sites are repointed; the format contract (a fmt verb makes the string a format string, otherwise trailing args are appended as " %v" fields) is preserved so every message renders identically. First cut of the beego framework removal. The beego module stays until the context, pagination, and session subpackages are migrated.
119 lines
3.1 KiB
Go
119 lines
3.1 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
// Portions Copyright 2025 The OpenAgent Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package util
|
|
|
|
import (
|
|
"net"
|
|
"path/filepath"
|
|
|
|
"github.com/hanzoai/ai/conf"
|
|
"github.com/hanzoai/ai/log"
|
|
"github.com/oschwald/geoip2-golang"
|
|
)
|
|
|
|
var (
|
|
maxmindCityDB *geoip2.Reader
|
|
maxmindASNDB *geoip2.Reader
|
|
|
|
MaxmindDownloadInProgress bool
|
|
)
|
|
|
|
// InitMaxmindDb initializes the MaxMind GeoIP2 databases
|
|
func InitMaxmindDb() error {
|
|
frontendBaseDir := conf.GetConfigString("frontendBaseDir")
|
|
|
|
var cityErr, asnErr error
|
|
|
|
// Try to open the City database file
|
|
maxmindCityDB, cityErr = geoip2.Open(filepath.Join(frontendBaseDir, "data", "GeoLite2-City.mmdb"))
|
|
if cityErr != nil {
|
|
maxmindCityDB, cityErr = geoip2.Open(filepath.Join(frontendBaseDir, "..", "data", "GeoLite2-City.mmdb"))
|
|
if cityErr != nil {
|
|
log.Warn("InitMaxmindDb() open \"GeoLite2-City.mmdb\" warning: %v", cityErr)
|
|
}
|
|
}
|
|
|
|
// Try to open the ASN database file
|
|
maxmindASNDB, asnErr = geoip2.Open(filepath.Join(frontendBaseDir, "data", "GeoLite2-ASN.mmdb"))
|
|
if asnErr != nil {
|
|
maxmindASNDB, asnErr = geoip2.Open(filepath.Join(frontendBaseDir, "..", "data", "GeoLite2-ASN.mmdb"))
|
|
if asnErr != nil {
|
|
log.Warn("InitMaxmindDb() open \"GeoLite2-ASN.mmdb\" warning: %v", asnErr)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// FindMaxmind looks up IP information using MaxMind GeoIP2
|
|
func FindMaxmind(ipstr string) (*LocationInfo, error) {
|
|
if MaxmindDownloadInProgress && (maxmindCityDB == nil || maxmindASNDB == nil) {
|
|
return &LocationInfo{
|
|
Country: Null,
|
|
Region: Null,
|
|
City: Null,
|
|
Isp: Null,
|
|
}, nil
|
|
}
|
|
|
|
// Parse the IP address
|
|
ip := net.ParseIP(ipstr)
|
|
if ip == nil {
|
|
return nil, ErrInvalidIp
|
|
}
|
|
|
|
// Initialize with default values
|
|
info := &LocationInfo{
|
|
Country: Null,
|
|
Region: Null,
|
|
City: Null,
|
|
Isp: Null,
|
|
}
|
|
|
|
if maxmindCityDB != nil {
|
|
// Look up geo information
|
|
record, err := maxmindCityDB.City(ip)
|
|
if err != nil {
|
|
panic("Get location info failed " + err.Error())
|
|
}
|
|
// Get country, region, city in English
|
|
if val, ok := record.Country.Names["en"]; ok && val != "" {
|
|
info.Country = val
|
|
}
|
|
|
|
if len(record.Subdivisions) > 0 {
|
|
if val, ok := record.Subdivisions[0].Names["en"]; ok && val != "" {
|
|
info.Region = val
|
|
}
|
|
}
|
|
|
|
if val, ok := record.City.Names["en"]; ok && val != "" {
|
|
info.City = val
|
|
}
|
|
}
|
|
|
|
if maxmindASNDB != nil {
|
|
// Look up ASN/ISP information
|
|
asnRecord, err := maxmindASNDB.ASN(ip)
|
|
if err != nil {
|
|
panic("Get ASN info failed " + err.Error())
|
|
}
|
|
info.Isp = asnRecord.AutonomousSystemOrganization
|
|
}
|
|
|
|
return info, nil
|
|
}
|