mirror of
https://github.com/owncast/owncast.git
synced 2024-11-27 08:00:16 +03:00
Restore GeoIP support to now support chat users. Closes #1304
This commit is contained in:
parent
5adf2cdecd
commit
6f57f570d2
2 changed files with 55 additions and 48 deletions
|
@ -14,6 +14,7 @@ import (
|
||||||
"github.com/owncast/owncast/core/data"
|
"github.com/owncast/owncast/core/data"
|
||||||
"github.com/owncast/owncast/core/user"
|
"github.com/owncast/owncast/core/user"
|
||||||
"github.com/owncast/owncast/core/webhooks"
|
"github.com/owncast/owncast/core/webhooks"
|
||||||
|
"github.com/owncast/owncast/geoip"
|
||||||
"github.com/owncast/owncast/utils"
|
"github.com/owncast/owncast/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -64,12 +65,12 @@ func (s *ChatServer) Run() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Addclient registers new connection as a User.
|
// 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{
|
client := &ChatClient{
|
||||||
server: s,
|
server: s,
|
||||||
conn: conn,
|
conn: conn,
|
||||||
User: user,
|
User: user,
|
||||||
ipAddress: conn.RemoteAddr().String(),
|
ipAddress: ipAddress,
|
||||||
accessToken: accessToken,
|
accessToken: accessToken,
|
||||||
send: make(chan []byte, 256),
|
send: make(chan []byte, 256),
|
||||||
UserAgent: userAgent,
|
UserAgent: userAgent,
|
||||||
|
@ -96,6 +97,11 @@ func (s *ChatServer) Addclient(conn *websocket.Conn, user *user.User, accessToke
|
||||||
s.sendWelcomeMessageToClient(client)
|
s.sendWelcomeMessageToClient(client)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Asynchronously, optionally, fetch GeoIP data.
|
||||||
|
go func(client *ChatClient) {
|
||||||
|
client.Geo = geoip.GetGeoFromIP(ipAddress)
|
||||||
|
}(client)
|
||||||
|
|
||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,8 +178,9 @@ func (s *ChatServer) HandleClientConnection(w http.ResponseWriter, r *http.Reque
|
||||||
}
|
}
|
||||||
|
|
||||||
userAgent := r.UserAgent()
|
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.
|
// Broadcast sends message to all connected clients.
|
||||||
|
|
|
@ -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.
|
// fetchGeoForIP makes an API call to get geo details for an IP address.
|
||||||
func FetchGeoForIP(ip string) {
|
func fetchGeoForIP(ip string) *GeoDetails {
|
||||||
// If GeoIP has been disabled then don't try to access it.
|
// If GeoIP has been disabled then don't try to access it.
|
||||||
if !_enabled {
|
if !_enabled {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't re-fetch if we already have it.
|
// Don't re-fetch if we already have it.
|
||||||
if _, ok := _geoIPCache[ip]; ok {
|
if geoDetails, ok := _geoIPCache[ip]; ok {
|
||||||
return
|
return &geoDetails
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
db, err := geoip2.Open(geoIPDatabasePath)
|
||||||
db, err := geoip2.Open(geoIPDatabasePath)
|
if err != nil {
|
||||||
if err != nil {
|
log.Traceln("GeoIP support is disabled. visit http://owncast.online/docs/geoip to learn how to enable.", err)
|
||||||
log.Traceln("GeoIP support is disabled. visit http://owncast.online/docs/geoip to learn how to enable.", err)
|
_enabled = false
|
||||||
_enabled = false
|
return nil
|
||||||
return
|
}
|
||||||
|
|
||||||
|
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)
|
return &response
|
||||||
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
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue