Skip to content

Commit

Permalink
feat: record client country and city
Browse files Browse the repository at this point in the history
  • Loading branch information
lanthora committed Oct 13, 2024
1 parent b5250b9 commit 7701f46
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cacao
sqlite.db
cookie
*.mmdb
4 changes: 4 additions & 0 deletions api/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func DeviceShow(c *gin.Context) {
OS string `json:"os"`
Version string `json:"version"`
Hostname string `json:"hostname"`
Country string `json:"country"`
City string `json:"city"`
LastActiveTime string `json:"lastActiveTime"`
}

Expand All @@ -36,6 +38,8 @@ func DeviceShow(c *gin.Context) {
OS: d.OS,
Version: d.Version,
Hostname: d.Hostname,
Country: d.Country,
City: d.City,
LastActiveTime: d.UpdatedAt.Format(time.DateTime),
})
}
Expand Down
1 change: 1 addition & 0 deletions candy/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +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(ws.ctx.ClientIP())
ws.dev.model.Save()

ws.updateSystemRoute()
Expand Down
24 changes: 18 additions & 6 deletions frontend/src/views/DeviceView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ const deviceColumns = [
key: 'hostname',
align: 'center'
},
{
title: 'IP',
dataIndex: 'ip',
key: 'ip',
align: 'center'
},
{
title: 'Network',
dataIndex: 'netid',
Expand All @@ -56,6 +50,24 @@ const deviceColumns = [
return net ? net.netname : ''
}
},
{
title: 'IP',
dataIndex: 'ip',
key: 'ip',
align: 'center'
},
{
title: 'Country',
dataIndex: 'country',
key: 'country',
align: 'center'
},
{
title: 'City',
dataIndex: 'city',
key: 'city',
align: 'center'
},
{
title: 'RX',
dataIndex: 'rx',
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.3
github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40
github.com/oschwald/geoip2-golang v1.11.0
github.com/sirupsen/logrus v1.9.3
gorm.io/gorm v1.25.11
)
Expand All @@ -33,6 +34,7 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/oschwald/maxminddb-golang v1.13.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
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.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
2 changes: 2 additions & 0 deletions model/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type Device struct {
OS string
Version string
Hostname string
Country string
City string
}

func (d *Device) Save() {
Expand Down
38 changes: 38 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package storage

import (
"net"
"os"
"path"

"github.com/glebarez/sqlite"
"github.com/lanthora/cacao/argp"
"github.com/lanthora/cacao/logger"
"github.com/oschwald/geoip2-golang"
"gorm.io/gorm"
gormlogger "gorm.io/gorm/logger"
)
Expand All @@ -32,3 +34,39 @@ var db *gorm.DB
func Get() *gorm.DB {
return db
}

func findFileByExtFromDir(dir string, ext string) (string, error) {
files, err := os.ReadDir(dir)
if err != nil {
return "", err
}
for _, file := range files {
if path.Ext(file.Name()) == ext {
return file.Name(), nil
}
}
return "", os.ErrNotExist
}

func GetCountryCity(ip string) (country, city string) {
storageDir := argp.Get("storage", ".")
filename, err := findFileByExtFromDir(storageDir, ".mmdb")
if err != nil {
logger.Debug("cannot found mmdb file: %v", err)
return
}
db, err := geoip2.Open(path.Join(storageDir, filename))
if err != nil {
logger.Debug("open mmdb failed: %v", err)
return
}
defer db.Close()
record, err := db.City(net.ParseIP(ip))
if err != nil {
logger.Debug("get location mmdb failed: %v", err)
return
}
country = record.Country.IsoCode
city = record.City.Names["en"]
return
}

0 comments on commit 7701f46

Please sign in to comment.