Skip to content

Commit

Permalink
feat: change city to most specific subdivision
Browse files Browse the repository at this point in the history
  • Loading branch information
lanthora committed Oct 29, 2024
1 parent b19983a commit 08e0798
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions api/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func DeviceShow(c *gin.Context) {
Version string `json:"version"`
Hostname string `json:"hostname"`
Country string `json:"country"`
City string `json:"city"`
Region string `json:"region"`
LastActiveTime string `json:"lastActiveTime"`
}

Expand All @@ -39,7 +39,7 @@ func DeviceShow(c *gin.Context) {
Version: d.Version,
Hostname: d.Hostname,
Country: d.Country,
City: d.City,
Region: d.Region,
LastActiveTime: d.UpdatedAt.Format(time.DateTime),
})
}
Expand Down
4 changes: 2 additions & 2 deletions candy/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (ws *candysocket) handleAuthMessage(buffer []byte) error {
db.Where(ws.dev.model).First(ws.dev.model)
ws.dev.model.IP = uint32ToStrIp(message.IP)
ws.dev.model.Online = true
ws.dev.model.Country, ws.dev.model.City = storage.GetCountryCity(net.ParseIP(ws.ctx.ClientIP()))
ws.dev.model.Country, ws.dev.model.Region = storage.GetLocation(net.ParseIP(ws.ctx.ClientIP()))
ws.dev.model.Save()

ws.updateSystemRoute()
Expand Down Expand Up @@ -357,7 +357,7 @@ func (ws *candysocket) handlePeerConnMessage(buffer []byte) error {

ip := make(net.IP, 4)
binary.BigEndian.PutUint32(ip, message.IP)
ws.dev.model.Country, ws.dev.model.City = storage.GetCountryCity(ip)
ws.dev.model.Country, ws.dev.model.Region = storage.GetLocation(ip)
ws.dev.model.Save()

return nil
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/views/DeviceView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ const deviceColumns = [
sorter: (a, b) => a.country.localeCompare(b.country)
},
{
title: 'City',
dataIndex: 'city',
key: 'city',
title: 'Region',
dataIndex: 'region',
key: 'region',
align: 'center',
sorter: (a, b) => {
const tmp = a.country.localeCompare(b.country)
if (tmp == 0) {
return a.city.localeCompare(b.city)
return a.region.localeCompare(b.region)
}
return tmp
}
Expand Down
2 changes: 1 addition & 1 deletion model/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Device struct {
Version string
Hostname string
Country string
City string
Region string
}

func (d *Device) Save() {
Expand Down
11 changes: 8 additions & 3 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func findFileByExtFromDir(dir string, ext string) (string, error) {
return "", os.ErrNotExist
}

func GetCountryCity(ip net.IP) (country, city string) {
func GetLocation(ip net.IP) (country, region string) {
storageDir := argp.Get("storage", ".")
filename, err := findFileByExtFromDir(storageDir, ".mmdb")
if err != nil {
Expand All @@ -63,10 +63,15 @@ func GetCountryCity(ip net.IP) (country, city string) {
defer db.Close()
record, err := db.City(ip)
if err != nil {
logger.Debug("get location mmdb failed: %v", err)
logger.Debug("get location failed: %v", err)
return
}

country = record.Country.IsoCode
city = record.City.Names["en"]

if len(record.Subdivisions) > 0 {
region = record.Subdivisions[0].Names["en"]
}

return
}

0 comments on commit 08e0798

Please sign in to comment.