✨ GeoIP
This commit is contained in:
@ -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
|
||||
}
|
||||
|
@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user