Refactored the code and moved each function to be a module (file)

This commit is contained in:
null31 2024-09-24 23:21:37 +02:00
parent fcbee25fa0
commit 9d2cc2674d
Signed by: null31
GPG Key ID: CAB4CF4EE49720FF
1 changed files with 21 additions and 104 deletions

125
main.go
View File

@ -3,11 +3,9 @@ package main
import ( import (
"fmt" "fmt"
"log" "log"
"net"
"strings" "strings"
"crypto/tls" "crypto/tls"
"github.com/oschwald/geoip2-golang"
"github.com/thoj/go-ircevent" "github.com/thoj/go-ircevent"
"irc_bot/utils" "irc_bot/utils"
) )
@ -19,9 +17,10 @@ func main() {
log.Fatalf("Error loading config: %v", err) log.Fatalf("Error loading config: %v", err)
} }
// Initialize the IRC bot
irccon := irc.IRC(config.Nickname, config.Nickname) irccon := irc.IRC(config.Nickname, config.Nickname)
irccon.VerboseCallbackHandler = true irccon.VerboseCallbackHandler = config.Debug
irccon.Debug = true irccon.Debug = config.Callback
irccon.UseTLS = config.SSL irccon.UseTLS = config.SSL
irccon.TLSConfig = &tls.Config{InsecureSkipVerify: true} irccon.TLSConfig = &tls.Config{InsecureSkipVerify: true}
@ -36,6 +35,7 @@ func main() {
} }
}) })
// Read the messages to get the content and call the apropriate command if has
irccon.AddCallback("PRIVMSG", func(e *irc.Event) { irccon.AddCallback("PRIVMSG", func(e *irc.Event) {
if len(e.Arguments) < 2 { if len(e.Arguments) < 2 {
return return
@ -48,108 +48,25 @@ func main() {
} }
cmd := strings.TrimPrefix(parts[0], "!") cmd := strings.TrimPrefix(parts[0], "!")
if commandFunc, ok := commands[cmd]; ok { switch {
commandFunc(irccon, e, parts) // For each new command, add a new case
case cmd == "geoip":
// Call GeoIP command that return the query data
response, err := utils.HandleGeoIPCommand(parts[1], config.GeoIP_City, config.GeoIP_ASN)
if err != nil {
irccon.Privmsg(e.Arguments[0], fmt.Sprintf("%v", err))
} else {
irccon.Privmsg(e.Arguments[0], response)
}
case cmd == "commands":
// List which are the available commands to use
irccon.Privmsg(e.Arguments[0], "Current available commands: !geoip")
default:
return
} }
}) })
irccon.Loop() irccon.Loop()
} }
type CommandFunc func(irccon *irc.Connection, e *irc.Event, args []string)
var commands = map[string]CommandFunc{
"geoip": handleGeoIPCommand,
// add more commands here
}
func handleGeoIPCommand(irccon *irc.Connection, e *irc.Event, args []string) {
if len(args) < 2 {
irccon.Privmsg(e.Arguments[0], "Usage: !geoip <IP or domain>")
return
}
ipOrDomain := args[1]
config, err := LoadConfig("config.json")
if err != nil {
log.Fatalf("Error loading config: %v", err)
}
cityDbPath := config.GeoIPDatabase // Update with actual path
asnDbPath := config.GeoIPASN // Update with actual path
ip := net.ParseIP(ipOrDomain)
if ip == nil {
resolvedIP, err := net.LookupIP(ipOrDomain)
if err != nil || len(resolvedIP) == 0 {
irccon.Privmsg(e.Arguments[0], fmt.Sprintf("Invalid IP address or domain: %s", ipOrDomain))
return
}
ip = resolvedIP[0]
}
var (
city = "N/A"
subdivision = "N/A"
country = "N/A"
latitude = "N/A"
longitude = "N/A"
asn = "N/A"
isp = "N/A"
)
// Lookup City Information
if cityDb, err := geoip2.Open(cityDbPath); 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 != "" {
subdivision = name
}
}
if name := cityRecord.Country.Names["en"]; name != "" {
country = name
}
if lat := cityRecord.Location.Latitude; lat != 0 {
latitude = fmt.Sprintf("%.6f", lat)
}
if lon := cityRecord.Location.Longitude; lon != 0 {
longitude = fmt.Sprintf("%.6f", lon)
}
} else {
log.Printf("City lookup error: %v", err)
}
} else {
log.Printf("Error opening City database: %v", err)
}
// Lookup ASN Information
if asnDb, err := geoip2.Open(asnDbPath); 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 {
log.Printf("ASN lookup error: %v", err)
}
} else {
log.Printf("Error opening ASN database: %v", err)
}
response := fmt.Sprintf(
"IP: %s | Location: %s, %s, %s | Coordinates: %s, %s | ASN: %s | ISP: %s",
ip.String(), city, subdivision, country, latitude, longitude, asn, isp,
)
irccon.Privmsg(e.Arguments[0], response)
}