Restore GeoIP support to now support chat users. Closes #1304

This commit is contained in:
Gabe Kangas 2021-08-12 14:55:21 -07:00 committed by Jannik
parent 5adf2cdecd
commit 6f57f570d2
2 changed files with 55 additions and 48 deletions

View file

@ -14,6 +14,7 @@ import (
"github.com/owncast/owncast/core/data"
"github.com/owncast/owncast/core/user"
"github.com/owncast/owncast/core/webhooks"
"github.com/owncast/owncast/geoip"
"github.com/owncast/owncast/utils"
)
@ -64,12 +65,12 @@ func (s *ChatServer) Run() {
}
// Addclient registers new connection as a User.
func (s *ChatServer) Addclient(conn *websocket.Conn, user *user.User, accessToken string, userAgent string) *ChatClient {
func (s *ChatServer) Addclient(conn *websocket.Conn, user *user.User, accessToken string, userAgent string, ipAddress string) *ChatClient {
client := &ChatClient{
server: s,
conn: conn,
User: user,
ipAddress: conn.RemoteAddr().String(),
ipAddress: ipAddress,
accessToken: accessToken,
send: make(chan []byte, 256),
UserAgent: userAgent,
@ -96,6 +97,11 @@ func (s *ChatServer) Addclient(conn *websocket.Conn, user *user.User, accessToke
s.sendWelcomeMessageToClient(client)
}
// Asynchronously, optionally, fetch GeoIP data.
go func(client *ChatClient) {
client.Geo = geoip.GetGeoFromIP(ipAddress)
}(client)
return client
}
@ -172,8 +178,9 @@ func (s *ChatServer) HandleClientConnection(w http.ResponseWriter, r *http.Reque
}
userAgent := r.UserAgent()
ipAddress := utils.GetIPAddressFromRequest(r)
s.Addclient(conn, user, accessToken, userAgent)
s.Addclient(conn, user, accessToken, userAgent, ipAddress)
}
// Broadcast sends message to all connected clients.

View file

@ -37,62 +37,62 @@ func GetGeoFromIP(ip string) *GeoDetails {
}
}
return nil
return fetchGeoForIP(ip)
}
// FetchGeoForIP makes an API call to get geo details for an IP address.
func FetchGeoForIP(ip string) {
// fetchGeoForIP makes an API call to get geo details for an IP address.
func fetchGeoForIP(ip string) *GeoDetails {
// If GeoIP has been disabled then don't try to access it.
if !_enabled {
return
return nil
}
// Don't re-fetch if we already have it.
if _, ok := _geoIPCache[ip]; ok {
return
if geoDetails, ok := _geoIPCache[ip]; ok {
return &geoDetails
}
go func() {
db, err := geoip2.Open(geoIPDatabasePath)
if err != nil {
log.Traceln("GeoIP support is disabled. visit http://owncast.online/docs/geoip to learn how to enable.", err)
_enabled = false
return
db, err := geoip2.Open(geoIPDatabasePath)
if err != nil {
log.Traceln("GeoIP support is disabled. visit http://owncast.online/docs/geoip to learn how to enable.", err)
_enabled = false
return nil
}
defer db.Close()
ipObject := net.ParseIP(ip)
record, err := db.City(ipObject)
if err != nil {
log.Warnln(err)
return nil
}
// If no country is available then exit
if record.Country.IsoCode == "" {
return nil
}
// If we believe this IP to be anonymous then no reason to report it
if record.Traits.IsAnonymousProxy {
return nil
}
var regionName = "Unknown"
if len(record.Subdivisions) > 0 {
if region, ok := record.Subdivisions[0].Names["en"]; ok {
regionName = region
}
}
defer db.Close()
response := GeoDetails{
CountryCode: record.Country.IsoCode,
RegionName: regionName,
TimeZone: record.Location.TimeZone,
}
ipObject := net.ParseIP(ip)
_geoIPCache[ip] = response
record, err := db.City(ipObject)
if err != nil {
log.Warnln(err)
return
}
// If no country is available then exit
if record.Country.IsoCode == "" {
return
}
// If we believe this IP to be anonymous then no reason to report it
if record.Traits.IsAnonymousProxy {
return
}
var regionName = "Unknown"
if len(record.Subdivisions) > 0 {
if region, ok := record.Subdivisions[0].Names["en"]; ok {
regionName = region
}
}
response := GeoDetails{
CountryCode: record.Country.IsoCode,
RegionName: regionName,
TimeZone: record.Location.TimeZone,
}
_geoIPCache[ip] = response
}()
return &response
}