Improve GeoIP function do show more data and replace the IF by switch/case
This commit is contained in:
parent
e6de45c095
commit
c48c9f2e30
|
@ -6,22 +6,75 @@ import (
|
||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleGeoIPCommand(ipAddress string, dbPath string) (string, error) {
|
func HandleGeoIPCommand(ipAddress string, dbCity string, dbASN string) (string, error) {
|
||||||
db, err := geoip2.Open(dbPath)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("error opening GeoIP database: %v", err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
ip := net.ParseIP(ipAddress)
|
ip := net.ParseIP(ipAddress)
|
||||||
record, err := db.City(ip)
|
|
||||||
if err != nil {
|
// Check if IP/Hostname is valid
|
||||||
return "", fmt.Errorf("error looking up IP address: %v", err)
|
if ip == nil {
|
||||||
|
resolvedIP, err := net.LookupIP(ipAddress)
|
||||||
|
if err != nil || len(resolvedIP) == 0 {
|
||||||
|
return "", fmt.Errorf("Invalid IP or hostname: %s", ipAddress)
|
||||||
|
}
|
||||||
|
ip = resolvedIP[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
if record.Country.IsoCode != "" {
|
var (
|
||||||
return fmt.Sprintf("IP: %s, Country: %s", ipAddress, record.Country.IsoCode), nil
|
city = "N/A"
|
||||||
|
state = "N/A"
|
||||||
|
country = "N/A"
|
||||||
|
asn = "N/A"
|
||||||
|
isp = "N/A"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Lookup City, State and Country info
|
||||||
|
if cityDb, err := geoip2.Open(dbCity); err == nil {
|
||||||
|
defer cityDb.Close()
|
||||||
|
|
||||||
|
if cityRecord, err := cityDb.City(ip); err == nil {
|
||||||
|
if name := cityRecord.City.Names["en"]; name != "" {
|
||||||
|
city = name
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(cityRecord.Subdivisions) > 0 {
|
||||||
|
if name := cityRecord.Subdivisions[0].Names["en"]; name != "" {
|
||||||
|
state = name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if name := cityRecord.Country.Names["en"]; name != "" {
|
||||||
|
country = name
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return "", fmt.Errorf("City Lookup error: %v", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return "", fmt.Errorf("Error opening City database: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return "Country not found", nil
|
// Lookup ASN info
|
||||||
|
if asnDb, err := geoip2.Open(dbASN); err == nil {
|
||||||
|
defer asnDb.Close()
|
||||||
|
|
||||||
|
if asnRecord, err := asnDb.ASN(ip); err == nil {
|
||||||
|
if asnNum := asnRecord.AutonomousSystemNumber; asnNum != 0 {
|
||||||
|
asn = fmt.Sprintf("%d", asnNum)
|
||||||
|
}
|
||||||
|
|
||||||
|
if org := asnRecord.AutonomousSystemOrganization; org != "" {
|
||||||
|
isp = org
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return "", fmt.Errorf("ASN lookup error: %v", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return "", fmt.Errorf("Error opening ASN database: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format the response
|
||||||
|
response := fmt.Sprintf(
|
||||||
|
"IP: %s | Location: %s, %s, %s | ASN: %s | ISP: %s",
|
||||||
|
ip.String(), city, state, country, asn, isp,
|
||||||
|
)
|
||||||
|
|
||||||
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
package commands
|
||||||
|
|
||||||
|
func HandleSuaMae () (string) {
|
||||||
|
return "A mãe do Luketa veio rolante até você!"
|
||||||
|
}
|
15
main.go
15
main.go
|
@ -32,7 +32,7 @@ func main() {
|
||||||
updateConfig.Timeout = 60
|
updateConfig.Timeout = 60
|
||||||
updates := bot.GetUpdatesChan(updateConfig)
|
updates := bot.GetUpdatesChan(updateConfig)
|
||||||
|
|
||||||
// negrice
|
// Convert Chat ID from String to Int64
|
||||||
chatID, err := strconv.ParseInt(config.ChatID, 10, 64)
|
chatID, err := strconv.ParseInt(config.ChatID, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Rip conversion: %v", err)
|
log.Fatalf("Rip conversion: %v", err)
|
||||||
|
@ -46,7 +46,9 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
text := update.Message.Text
|
text := update.Message.Text
|
||||||
if strings.HasPrefix(text, "/geoip") {
|
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(text, "/geoip"):
|
||||||
args := strings.Fields(text)
|
args := strings.Fields(text)
|
||||||
if len(args) != 2 {
|
if len(args) != 2 {
|
||||||
bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Usage: /geoip <IP>"))
|
bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Usage: /geoip <IP>"))
|
||||||
|
@ -54,12 +56,19 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
ipAddress := args[1]
|
ipAddress := args[1]
|
||||||
response, err := commands.HandleGeoIPCommand(ipAddress, config.GeoIP_City)
|
response, err := commands.HandleGeoIPCommand(ipAddress, config.GeoIP_City, config.GeoIP_ASN)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, fmt.Sprintf("Error: %v", err)))
|
bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, fmt.Sprintf("Error: %v", err)))
|
||||||
} else {
|
} else {
|
||||||
bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, response))
|
bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, response))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case strings.HasPrefix(text, "/suamae"):
|
||||||
|
response := commands.HandleSuaMae()
|
||||||
|
bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, response))
|
||||||
|
|
||||||
|
default:
|
||||||
|
fmt.Printf("%s.\n", text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue