Add country lookup

This commit is contained in:
idanoo 2022-11-20 18:11:08 +13:00
parent 8f012fc275
commit 38fa1f01ff
Signed by: idanoo
GPG Key ID: 387387CDBC02F132
7 changed files with 92 additions and 4 deletions

View File

@ -1,4 +1,5 @@
MATRIX_WEBHOOK_URL= MATRIX_WEBHOOK_URL=
MATRIX_WEBHOOK_API_KEY= MATRIX_WEBHOOK_API_KEY=
MATRIX_CHANNEL= MATRIX_CHANNEL=
PORT= PORT=
IP2LOCATION_API_KEY=

5
go.mod
View File

@ -2,4 +2,7 @@ module tinker.nz/idanoo/go-mastodon-matrix-webhooks
go 1.19 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
)

2
go.sum
View File

@ -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 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=

Binary file not shown.

Binary file not shown.

81
main.go
View File

@ -2,10 +2,12 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt"
"log" "log"
"net/http" "net/http"
"os" "os"
"github.com/ip2location/ip2location-go/v9"
"github.com/joho/godotenv" "github.com/joho/godotenv"
) )
@ -13,6 +15,7 @@ var MATRIX_WEBHOOK_URL string
var MATRIX_WEBHOOK_API_KEY string var MATRIX_WEBHOOK_API_KEY string
var MATRIX_CHANNEL string var MATRIX_CHANNEL string
var PORT string var PORT string
var IP2LOCATION_API_KEY string
func init() { func init() {
err := godotenv.Load() err := godotenv.Load()
@ -39,6 +42,8 @@ func init() {
if PORT == "" { if PORT == "" {
log.Fatal("PORT empty or invalid") log.Fatal("PORT empty or invalid")
} }
IP2LOCATION_API_KEY = os.Getenv("IP2LOCATION_API_KEY")
} }
func main() { func main() {
@ -48,6 +53,7 @@ func main() {
} }
} }
// Handle requests
func handler(w http.ResponseWriter, r *http.Request) { func handler(w http.ResponseWriter, r *http.Request) {
if r.Body != nil { if r.Body != nil {
var i IdentifyingRequest var i IdentifyingRequest
@ -58,9 +64,80 @@ func handler(w http.ResponseWriter, r *http.Request) {
} }
if i.Event == "report.created" { 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" { } 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
}

View File

@ -6,6 +6,11 @@ type IdentifyingRequest struct {
Event string `json:"event"` Event string `json:"event"`
} }
type MatrixWebhook struct {
Body string `json:"body"`
Key string `json:"key"`
}
type MastodonSignUpEvent struct { type MastodonSignUpEvent struct {
Event string `json:"event"` Event string `json:"event"`
CreatedAt time.Time `json:"created_at"` CreatedAt time.Time `json:"created_at"`