diff --git a/.env.example b/.env.example index 8eb033c..47fcb48 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,5 @@ MATRIX_WEBHOOK_URL= MATRIX_WEBHOOK_API_KEY= MATRIX_CHANNEL= -PORT= \ No newline at end of file +PORT= +IP2LOCATION_API_KEY= \ No newline at end of file diff --git a/go.mod b/go.mod index 3f9678e..c8da1b9 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module tinker.nz/idanoo/go-mastodon-matrix-webhooks go 1.19 -require github.com/joho/godotenv v1.4.0 // indirect +require ( + github.com/ip2location/ip2location-go/v9 v9.5.0 // indirect + github.com/joho/godotenv v1.4.0 // indirect +) diff --git a/go.sum b/go.sum index 8c9f290..e3c581f 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ +github.com/ip2location/ip2location-go/v9 v9.5.0 h1:7gqKncm4MhBrpJIK0PmV8o6Bf8YbbSAPjORzyjAv1iM= +github.com/ip2location/ip2location-go/v9 v9.5.0/go.mod h1:s5SV6YZL10TpfPpXw//7fEJC65G/yH7Oh+Tjq9JcQEQ= github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= diff --git a/ip_lookups/IP2LOCATION-LITE-DB1.BIN b/ip_lookups/IP2LOCATION-LITE-DB1.BIN new file mode 100755 index 0000000..69dc7fe Binary files /dev/null and b/ip_lookups/IP2LOCATION-LITE-DB1.BIN differ diff --git a/ip_lookups/IP2LOCATION-LITE-DB1.IPV6.BIN b/ip_lookups/IP2LOCATION-LITE-DB1.IPV6.BIN new file mode 100755 index 0000000..a3baa0d Binary files /dev/null and b/ip_lookups/IP2LOCATION-LITE-DB1.IPV6.BIN differ diff --git a/main.go b/main.go index de34f1e..d817ce9 100644 --- a/main.go +++ b/main.go @@ -2,10 +2,12 @@ package main import ( "encoding/json" + "fmt" "log" "net/http" "os" + "github.com/ip2location/ip2location-go/v9" "github.com/joho/godotenv" ) @@ -13,6 +15,7 @@ var MATRIX_WEBHOOK_URL string var MATRIX_WEBHOOK_API_KEY string var MATRIX_CHANNEL string var PORT string +var IP2LOCATION_API_KEY string func init() { err := godotenv.Load() @@ -39,6 +42,8 @@ func init() { if PORT == "" { log.Fatal("PORT empty or invalid") } + + IP2LOCATION_API_KEY = os.Getenv("IP2LOCATION_API_KEY") } func main() { @@ -48,6 +53,7 @@ func main() { } } +// Handle requests func handler(w http.ResponseWriter, r *http.Request) { if r.Body != nil { var i IdentifyingRequest @@ -58,9 +64,80 @@ func handler(w http.ResponseWriter, r *http.Request) { } if i.Event == "report.created" { - log.Println("New report!") + var report MastodonReportEvent + err := json.NewDecoder(r.Body).Decode(&report) + if err != nil { + log.Println(err.Error()) + return + } + go sendWebhook("New report!") } else if i.Event == "account.created" { - log.Println("New account!") + var account MastodonSignUpEvent + err := json.NewDecoder(r.Body).Decode(&account) + if err != nil { + log.Println(err.Error()) + return + } + country := ipLookup(account.Object.IP) + go sendWebhook(fmt.Sprintf("*New Signup* %s has joined from %s", account.Object.Username, country)) } } } + +// sendWebhook - takes msg, sends to matrix +func sendWebhook(msgText string) error { + log.Println(msgText) + + data := MatrixWebhook{ + Key: MATRIX_WEBHOOK_API_KEY, + } + data.Body = msgText + b, err := json.Marshal(data) + if err != nil { + return err + } + req, err := http.NewRequest("POST", MATRIX_WEBHOOK_URL+"/"+MATRIX_CHANNEL, b) + req.Header.Set("X-Custom-Header", "myvalue") + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + panic(err) + } + defer resp.Body.Close() + + return nil +} + +// Lookup to country! +func ipLookup(ip string) string { + if IP2LOCATION_API_KEY == "" { + return "" + } + + apipackage := "WS25" + usessl := true + addon := "continent,country,region,city,geotargeting,country_groupings,time_zone_info" // leave blank if no need + lang := "en" // leave blank if no need + + ws, err := ip2location.OpenWS(IP2LOCATION_API_KEY, apipackage, usessl) + + if err != nil { + fmt.Print(err) + return "" + } + + res, err := ws.LookUp(ip, addon, lang) + + if err != nil { + fmt.Print(err) + return "" + } + + if res.Response != "OK" { + fmt.Printf("Error: %s\n", res.Response) + } + + return res.Country.Name +} diff --git a/structs.go b/structs.go index 3c42b22..e5d160a 100644 --- a/structs.go +++ b/structs.go @@ -6,6 +6,11 @@ type IdentifyingRequest struct { Event string `json:"event"` } +type MatrixWebhook struct { + Body string `json:"body"` + Key string `json:"key"` +} + type MastodonSignUpEvent struct { Event string `json:"event"` CreatedAt time.Time `json:"created_at"`