GeoIP

This commit is contained in:
LittleSheep 2025-03-15 18:06:20 +08:00
parent 32e91e2601
commit bcb2cd2f9c
11 changed files with 114 additions and 24 deletions

2
.gitignore vendored
View File

@ -2,4 +2,6 @@
/uploads
/keys
geoip.mmdb
.DS_Store

2
go.mod
View File

@ -67,6 +67,8 @@ require (
github.com/nats-io/nkeys v0.4.7 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/nicksnyder/go-i18n/v2 v2.5.0 // indirect
github.com/oschwald/geoip2-golang v1.11.0 // indirect
github.com/oschwald/maxminddb-golang v1.13.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c // indirect
github.com/pkg/errors v0.9.1 // indirect

4
go.sum
View File

@ -278,6 +278,10 @@ github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/nicksnyder/go-i18n/v2 v2.5.0 h1:3wH1gpaekcgGuwzWdSu7JwJhH9Tk87k1ezt0i1p2/Is=
github.com/nicksnyder/go-i18n/v2 v2.5.0/go.mod h1:DrhgsSDZxoAfvVrBVLXoxZn/pN5TXqaDbq7ju94viiQ=
github.com/oschwald/geoip2-golang v1.11.0 h1:hNENhCn1Uyzhf9PTmquXENiWS6AlxAEnBII6r8krA3w=
github.com/oschwald/geoip2-golang v1.11.0/go.mod h1:P9zG+54KPEFOliZ29i7SeYZ/GM6tfEL+rgSn03hYuUo=
github.com/oschwald/maxminddb-golang v1.13.0 h1:R8xBorY71s84yO06NgTmQvqvTvlS/bnYZrrWX1MElnU=
github.com/oschwald/maxminddb-golang v1.13.0/go.mod h1:BU0z8BfFVhi1LQaonTwwGQlsHUEu9pWNdMfmq4ztm0o=
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c h1:dAMKvw0MlJT1GshSTtih8C2gDs04w8dReiOGXrGLNoY=

View File

@ -5,9 +5,12 @@ import "gorm.io/datatypes"
type AuditRecord struct {
BaseModel
Action string `json:"action"`
Metadata datatypes.JSONMap `json:"metadata"`
UserAgent string `json:"user_agent"`
IpAddress string `json:"ip_address"`
AccountID uint `json:"account_id"`
Action string `json:"action"`
Metadata datatypes.JSONMap `json:"metadata"`
Location *string `json:"location"`
CoordinateX *float64 `json:"coordinate_x"`
CoordinateY *float64 `json:"coordinate_y"`
UserAgent string `json:"user_agent"`
IpAddress string `json:"ip_address"`
AccountID uint `json:"account_id"`
}

View File

@ -35,7 +35,9 @@ type AuthFactor struct {
type AuthTicket struct {
BaseModel
Location string `json:"location"`
Location *string `json:"location"`
CoordinateX *float64 `json:"coordinate_x"`
CoordinateY *float64 `json:"coordinate_y"`
IpAddress string `json:"ip_address"`
UserAgent string `json:"user_agent"`
StepRemain int `json:"step_remain"`

View File

@ -5,11 +5,13 @@ import "gorm.io/datatypes"
type ActionEvent struct {
BaseModel
Type string `json:"type"`
Metadata datatypes.JSONMap `json:"metadata"`
Location string `json:"location"`
IpAddress string `json:"ip_address"`
UserAgent string `json:"user_agent"`
Type string `json:"type"`
Metadata datatypes.JSONMap `json:"metadata"`
Location *string `json:"location"`
CoordinateX *float64 `json:"coordinate_x"`
CoordinateY *float64 `json:"coordinate_y"`
IpAddress string `json:"ip_address"`
UserAgent string `json:"user_agent"`
Account Account `json:"account"`
AccountID uint `json:"account_id"`

View File

@ -2,8 +2,10 @@ package database
import (
"fmt"
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
"git.solsynth.dev/hypernet/passport/pkg/internal/gap"
"github.com/oschwald/geoip2-golang"
"github.com/rs/zerolog/log"
"github.com/samber/lo"
"github.com/spf13/viper"
@ -28,3 +30,14 @@ func NewGorm() error {
return err
}
var Gc *geoip2.Reader
func NewGeoDB() error {
conn, err := geoip2.Open(viper.GetString("geoip_db"))
if err != nil {
return fmt.Errorf("failed to open geoip database: %v", err)
}
Gc = conn
return nil
}

View File

@ -1,9 +1,13 @@
package services
import (
"net"
"strings"
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
"github.com/rs/zerolog/log"
"github.com/samber/lo"
)
var (
@ -13,23 +17,57 @@ var (
// AddEvent to keep operation logs by user themselves clear to query
func AddEvent(user uint, event string, meta map[string]any, ip, ua string) {
var location *string
var coordinateX, coordinateY *float64
netIp := net.ParseIP(ip)
record, err := database.Gc.City(netIp)
if err == nil {
var locationNames []string
locationNames = append(locationNames, record.City.Names["en"])
for _, subs := range record.Subdivisions {
locationNames = append(locationNames, subs.Names["en"])
}
location = lo.ToPtr(strings.Join(locationNames, ", "))
coordinateX = &record.Location.Latitude
coordinateY = &record.Location.Longitude
}
writeEventQueue = append(writeEventQueue, models.ActionEvent{
Type: event,
Metadata: meta,
IpAddress: ip,
UserAgent: ua,
AccountID: user,
Type: event,
Metadata: meta,
IpAddress: ip,
UserAgent: ua,
Location: location,
CoordinateX: coordinateX,
CoordinateY: coordinateY,
AccountID: user,
})
}
// AddAuditRecord to keep logs to make administrators' operations clear to query
func AddAuditRecord(operator models.Account, act, ip, ua string, metadata map[string]any) {
var location *string
var coordinateX, coordinateY *float64
netIp := net.ParseIP(ip)
record, err := database.Gc.City(netIp)
if err == nil {
var locationNames []string
locationNames = append(locationNames, record.City.Names["en"])
for _, subs := range record.Subdivisions {
locationNames = append(locationNames, subs.Names["en"])
}
location = lo.ToPtr(strings.Join(locationNames, ", "))
coordinateX = &record.Location.Latitude
coordinateY = &record.Location.Longitude
}
writeAuditQueue = append(writeAuditQueue, models.AuditRecord{
Action: act,
Metadata: metadata,
IpAddress: ip,
UserAgent: ua,
AccountID: operator.ID,
Action: act,
Metadata: metadata,
IpAddress: ip,
UserAgent: ua,
Location: location,
CoordinateX: coordinateX,
CoordinateY: coordinateY,
AccountID: operator.ID,
})
}

View File

@ -2,9 +2,12 @@ package services
import (
"fmt"
"git.solsynth.dev/hypernet/nexus/pkg/nex/localize"
"net"
"strings"
"time"
"git.solsynth.dev/hypernet/nexus/pkg/nex/localize"
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"gorm.io/datatypes"
@ -68,19 +71,36 @@ func NewTicket(user models.Account, ip, ua string) (models.AuthTicket, error) {
}
}
var location *string
var coordinateX, coordinateY *float64
netIp := net.ParseIP(ip)
record, err := database.Gc.City(netIp)
if err == nil {
var locationNames []string
locationNames = append(locationNames, record.City.Names["en"])
for _, subs := range record.Subdivisions {
locationNames = append(locationNames, subs.Names["en"])
}
location = lo.ToPtr(strings.Join(locationNames, ", "))
coordinateX = &record.Location.Latitude
coordinateY = &record.Location.Longitude
}
ticket = models.AuthTicket{
Claims: []string{"*"},
Audiences: []string{InternalTokenAudience},
IpAddress: ip,
UserAgent: ua,
StepRemain: steps,
Location: location,
CoordinateX: coordinateX,
CoordinateY: coordinateY,
ExpiredAt: nil,
AvailableAt: nil,
AccountID: user.ID,
}
err := database.C.Save(&ticket).Error
err = database.C.Save(&ticket).Error
return ticket, err
}

View File

@ -83,6 +83,9 @@ func main() {
} else if err := database.RunMigration(database.C); err != nil {
log.Fatal().Err(err).Msg("An error occurred when running database auto migration.")
}
if err := database.NewGeoDB(); err != nil {
log.Fatal().Err(err).Msg("An error occurred when connect to geoip database.")
}
// Initialize cache
if err := cache.NewStore(); err != nil {

View File

@ -7,6 +7,7 @@ domain = "id.solsynth.dev"
templates_dir = "templates"
locales_dir = "locales"
geoip_db = "geoip.mmdb"
frontend_app = "https://solsynth.dev"