mirror of
https://github.com/dalbodeule/sshchat.git
synced 2025-12-08 07:05:44 +09:00
apply geoip2, config file.
This commit is contained in:
32
utils/config.go
Normal file
32
utils/config.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Port string
|
||||
Geoip string
|
||||
CountryBlacklist []string
|
||||
}
|
||||
|
||||
func GetConfig() *Config {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
|
||||
port := os.Getenv("PORT")
|
||||
geoip_dbfile := os.Getenv("GEOIP_DB")
|
||||
country_blacklist := os.Getenv("COUNTRY_BLACKLIST")
|
||||
|
||||
return &Config{
|
||||
Port: port,
|
||||
Geoip: geoip_dbfile,
|
||||
CountryBlacklist: strings.Split(country_blacklist, ","),
|
||||
}
|
||||
}
|
||||
79
utils/geoip.go
Normal file
79
utils/geoip.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
|
||||
"github.com/oschwald/geoip2-golang"
|
||||
)
|
||||
|
||||
type IpInfo struct {
|
||||
Country string
|
||||
City string
|
||||
Timezone string
|
||||
Isp string
|
||||
IsAnonymousIP bool
|
||||
}
|
||||
|
||||
func GetDB(db string) (*geoip2.Reader, error) {
|
||||
geoip, err := geoip2.Open(db)
|
||||
if err != nil {
|
||||
log.Panicf("Failed to open database: %v", err)
|
||||
}
|
||||
|
||||
return geoip, err
|
||||
}
|
||||
|
||||
func GetIPInfo(ip string, db *geoip2.Reader) *IpInfo {
|
||||
parsedIp := net.ParseIP(ip)
|
||||
|
||||
country := func(ip net.IP) string {
|
||||
country, _ := db.Country(parsedIp)
|
||||
|
||||
println(country.Country.IsoCode)
|
||||
|
||||
if country != nil && country.Country.IsoCode != "" {
|
||||
println(country.Country.IsoCode)
|
||||
return country.Country.IsoCode
|
||||
} else {
|
||||
return "ZZ"
|
||||
}
|
||||
}(parsedIp)
|
||||
city, timezone := func(ip net.IP) (string, string) {
|
||||
city, _ := db.City(parsedIp)
|
||||
|
||||
if city != nil {
|
||||
return city.City.Names["en"], city.Location.TimeZone
|
||||
} else {
|
||||
return "Unknown", "UTC+0"
|
||||
}
|
||||
}(parsedIp)
|
||||
isp := func(ip net.IP) string {
|
||||
isp, _ := db.ISP(parsedIp)
|
||||
|
||||
if isp != nil {
|
||||
return isp.ISP
|
||||
} else {
|
||||
return "Unknown"
|
||||
}
|
||||
}(parsedIp)
|
||||
isAnonymousIP := func(ip net.IP) bool {
|
||||
is, _ := db.AnonymousIP(parsedIp)
|
||||
|
||||
if is != nil {
|
||||
return is.IsAnonymousVPN ||
|
||||
is.IsPublicProxy ||
|
||||
is.IsAnonymous
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}(parsedIp)
|
||||
|
||||
return &IpInfo{
|
||||
Country: country,
|
||||
City: city,
|
||||
Timezone: timezone,
|
||||
Isp: isp,
|
||||
IsAnonymousIP: isAnonymousIP,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user