diff --git a/config/GeoLite2-City.mmdb b/config/GeoLite2-City.mmdb new file mode 100644 index 0000000..c777ece Binary files /dev/null and b/config/GeoLite2-City.mmdb differ diff --git a/config/city.free.ipdb b/config/city.free.ipdb deleted file mode 100644 index 5efca3a..0000000 Binary files a/config/city.free.ipdb and /dev/null differ diff --git a/controller/visitor.go b/controller/visitor.go index 3cf83f6..01530a7 100644 --- a/controller/visitor.go +++ b/controller/visitor.go @@ -2,6 +2,7 @@ package controller import ( "encoding/json" + "fmt" "github.com/gin-gonic/gin" "goflylivechat/common" "goflylivechat/models" @@ -57,7 +58,7 @@ import ( // }) // } func PostVisitorLogin(c *gin.Context) { - ipcity := tools.ParseIp(c.ClientIP()) + avator := "" userAgent := c.GetHeader("User-Agent") if tools.IsMobile(userAgent) { @@ -73,25 +74,14 @@ func PostVisitorLogin(c *gin.Context) { id = tools.Uuid() } refer := c.PostForm("refer") - var ( - city string - name string - ) - - if ipcity != nil { - city = ipcity.CountryName + ipcity.RegionName + ipcity.CityName - name = ipcity.CountryName + ipcity.RegionName + ipcity.CityName - if ipcity.CityName == "本机地址" || ipcity.RegionName == "本机地址" || ipcity.CountryName == "本机地址" { - city = "local address" - } - } else { - city = "​​Unrecognized Region​​" - name = "visitor" + name := "Guest" + city := "" + countryname, cityname := tools.GetCity("./config/GeoLite2-City.mmdb", c.ClientIP()) + if countryname != "" || cityname != "" { + city = fmt.Sprintf("%s %s", countryname, cityname) + name = fmt.Sprintf("%s Guest", city) } - if name == "本机地址本机地址" { - name = "local visitor" - } client_ip := c.ClientIP() extra := c.PostForm("extra") extraJson := tools.Base64Decode(extra) @@ -108,7 +98,7 @@ func PostVisitorLogin(c *gin.Context) { } } //log.Println(name,avator,c.ClientIP(),toId,id,refer,city,client_ip) - if name == "" || avator == "" || toId == "" || id == "" || refer == "" || city == "" || client_ip == "" { + if name == "" || avator == "" || toId == "" || id == "" || refer == "" || client_ip == "" { c.JSON(200, gin.H{ "code": 400, "msg": "error", diff --git a/go.mod b/go.mod index 120d475..c0c8c7a 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,8 @@ module goflylivechat -go 1.16 +go 1.24 + +toolchain go1.24.5 require ( github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f @@ -14,10 +16,93 @@ require ( github.com/gorilla/websocket v1.4.2 github.com/ipipdotnet/ipdb-go v1.3.0 github.com/jinzhu/gorm v1.9.14 - github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect - github.com/modern-go/reflect2 v1.0.1 // indirect github.com/satori/go.uuid v1.2.0 github.com/sirupsen/logrus v1.4.2 github.com/spf13/cobra v0.0.5 github.com/zh-five/xdaemon v0.1.1 ) + +require ( + github.com/BurntSushi/toml v0.3.1 // indirect + github.com/PuerkitoBio/goquery v1.5.1 // indirect + github.com/andybalholm/cascadia v1.1.0 // indirect + github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6 // indirect + github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff // indirect + github.com/bradfitz/gomemcache v0.0.0-20190329173943-551aad21a668 // indirect + github.com/bradleypeabody/gorilla-sessions-memcache v0.0.0-20181103040241-659414f458e1 // indirect + github.com/coreos/etcd v3.3.10+incompatible // indirect + github.com/coreos/go-etcd v2.0.0+incompatible // indirect + github.com/coreos/go-semver v0.2.0 // indirect + github.com/cpuguy83/go-md2man v1.0.10 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd // indirect + github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 // indirect + github.com/fsnotify/fsnotify v1.4.7 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8 // indirect + github.com/go-playground/assert/v2 v2.0.1 // indirect + github.com/go-playground/locales v0.13.0 // indirect + github.com/go-playground/universal-translator v0.17.0 // indirect + github.com/go-playground/validator/v10 v10.4.1 // indirect + github.com/gobuffalo/envy v1.7.0 // indirect + github.com/gobuffalo/logger v1.0.0 // indirect + github.com/gobuffalo/packd v0.3.0 // indirect + github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect + github.com/golang/protobuf v1.3.3 // indirect + github.com/gomodule/redigo v2.0.0+incompatible // indirect + github.com/google/gofuzz v1.0.0 // indirect + github.com/gorilla/context v1.1.1 // indirect + github.com/gorilla/securecookie v1.1.1 // indirect + github.com/gorilla/sessions v1.1.3 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect + github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.0.1 // indirect + github.com/joho/godotenv v1.3.0 // indirect + github.com/json-iterator/go v1.1.9 // indirect + github.com/karrick/godirwalk v1.10.12 // indirect + github.com/kidstuff/mongostore v0.0.0-20181113001930-e650cd85ee4b // indirect + github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect + github.com/kr/pretty v0.1.0 // indirect + github.com/kr/pty v1.1.1 // indirect + github.com/kr/text v0.1.0 // indirect + github.com/leodido/go-urn v1.2.0 // indirect + github.com/lib/pq v1.1.1 // indirect + github.com/magiconair/properties v1.8.0 // indirect + github.com/mattn/go-isatty v0.0.12 // indirect + github.com/mattn/go-sqlite3 v1.14.0 // indirect + github.com/memcachier/mc v2.0.1+incompatible // indirect + github.com/mitchellh/go-homedir v1.1.0 // indirect + github.com/mitchellh/mapstructure v1.1.2 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.1 // indirect + github.com/oschwald/geoip2-golang/v2 v2.0.0-beta.3 // indirect + github.com/oschwald/maxminddb-golang/v2 v2.0.0-beta.7 // indirect + github.com/pelletier/go-toml v1.2.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/quasoft/memstore v0.0.0-20180925164028-84a050167438 // indirect + github.com/rogpeppe/go-internal v1.3.0 // indirect + github.com/russross/blackfriday v1.5.2 // indirect + github.com/spf13/afero v1.1.2 // indirect + github.com/spf13/cast v1.3.0 // indirect + github.com/spf13/jwalterweatherman v1.0.0 // indirect + github.com/spf13/pflag v1.0.3 // indirect + github.com/spf13/viper v1.3.2 // indirect + github.com/stretchr/objx v0.5.2 // indirect + github.com/stretchr/testify v1.10.0 // indirect + github.com/ugorji/go v1.1.7 // indirect + github.com/ugorji/go/codec v1.1.7 // indirect + github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77 // indirect + golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect + golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect + golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect + golang.org/x/sys v0.33.0 // indirect + golang.org/x/text v0.3.2 // indirect + golang.org/x/tools v0.0.0-20190624180213-70d37148ca0c // indirect + gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect + gopkg.in/errgo.v2 v2.1.0 // indirect + gopkg.in/go-playground/assert.v1 v1.2.1 // indirect + gopkg.in/go-playground/validator.v9 v9.29.1 // indirect + gopkg.in/yaml.v2 v2.2.8 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum index 6793666..e9a3ce5 100644 --- a/go.sum +++ b/go.sum @@ -116,6 +116,10 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/oschwald/geoip2-golang/v2 v2.0.0-beta.3 h1:K633WQsXWjRQeOAxroNcpMLuw/Sy6Cz7S7nmBGkBXO4= +github.com/oschwald/geoip2-golang/v2 v2.0.0-beta.3/go.mod h1:mN6THvXcxNmn58/SmW+aCVT4VrVEyb8EyedGIxHhgRk= +github.com/oschwald/maxminddb-golang/v2 v2.0.0-beta.7 h1:8ivtp2oRTsp7hTpkMgS5kLDvXC2SQoC2JuLph13ZXp8= +github.com/oschwald/maxminddb-golang/v2 v2.0.0-beta.7/go.mod h1:A1wLWQkiHqLUux3/cnHBBKxjYW4s7TZQnQ55fLa37NA= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -138,10 +142,12 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= @@ -173,6 +179,8 @@ golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= +golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -186,3 +194,4 @@ gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWd gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/tools/geo.go b/tools/geo.go new file mode 100644 index 0000000..762d51d --- /dev/null +++ b/tools/geo.go @@ -0,0 +1,23 @@ +package tools + +import ( + "github.com/oschwald/geoip2-golang/v2" + "net/netip" +) + +func GetCity(path, ipAddress string) (string, string) { + db, err := geoip2.Open(path) + if err != nil { + return "", "" + } + defer db.Close() + ip, err := netip.ParseAddr(ipAddress) + if err != nil { + return "", "" + } + record, err := db.City(ip) + if err != nil { + return "", "" + } + return record.Country.Names.English, record.City.Names.English +} diff --git a/tools/geo_test.go b/tools/geo_test.go new file mode 100644 index 0000000..84dd71e --- /dev/null +++ b/tools/geo_test.go @@ -0,0 +1,7 @@ +package tools + +import "testing" + +func TestGetCity(t *testing.T) { + GetCity("../config/GeoLite2-City.mmdb", "") +} diff --git a/tools/ip.go b/tools/ip.go deleted file mode 100644 index 5c79724..0000000 --- a/tools/ip.go +++ /dev/null @@ -1,77 +0,0 @@ -package tools - -import ( - "errors" - "github.com/ipipdotnet/ipdb-go" - "net" -) - -func ParseIp(myip string) *ipdb.CityInfo { - db, err := ipdb.NewCity("./config/city.free.ipdb") - if err != nil { - return nil - } - db.Reload("./config/city.free.ipdb") - c, err := db.FindInfo(myip, "CN") - if err != nil { - return nil - } - return c -} -func GetServerIP() (net.IP, error) { - ifaces, err := net.Interfaces() - if err != nil { - return nil, err - } - for _, iface := range ifaces { - if iface.Flags&net.FlagUp == 0 { - continue // interface down - } - if iface.Flags&net.FlagLoopback != 0 { - continue // loopback interface - } - addrs, err := iface.Addrs() - if err != nil { - return nil, err - } - for _, addr := range addrs { - ip := getIpFromAddr(addr) - if ip == nil { - continue - } - return ip, nil - } - } - return nil, errors.New("connected to the network?") -} -func getIpFromAddr(addr net.Addr) net.IP { - var ip net.IP - switch v := addr.(type) { - case *net.IPNet: - ip = v.IP - case *net.IPAddr: - ip = v.IP - } - if ip == nil || ip.IsLoopback() { - return nil - } - ip = ip.To4() - if ip == nil { - return nil // not an ipv4 address - } - - return ip -} - -//获取出站IP地址 -func GetOutboundIP() (net.IP, error) { - conn, err := net.Dial("udp", "8.8.8.8:80") - if err != nil { - return nil, err - } - defer conn.Close() - - localAddr := conn.LocalAddr().(*net.UDPAddr) - - return localAddr.IP, nil -} diff --git a/tools/ip_test.go b/tools/ip_test.go deleted file mode 100644 index fe1e824..0000000 --- a/tools/ip_test.go +++ /dev/null @@ -1,11 +0,0 @@ -package tools - -import ( - "log" - "testing" -) - -func TestGetOutboundIP(t *testing.T) { - ip, err := GetOutboundIP() - log.Println(ip, err) -}