From 735470081c5d10ea4d61a1f3aab2953315ca672d Mon Sep 17 00:00:00 2001 From: Eggbertx Date: Fri, 27 Oct 2023 17:03:13 -0700 Subject: [PATCH] Add JSON support to Lua and IPHub plugin --- examples/plugins/akismet.lua | 6 ++--- examples/plugins/iphub.lua | 49 ++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ pkg/gcplugin/gcplugin.go | 13 ++++++++++ 5 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 examples/plugins/iphub.lua diff --git a/examples/plugins/akismet.lua b/examples/plugins/akismet.lua index ed613a4f..da7a4051 100644 --- a/examples/plugins/akismet.lua +++ b/examples/plugins/akismet.lua @@ -64,7 +64,7 @@ local function check_akismet(post, user_agent, referrer) headers = base_headers }) if(err ~= nil) then - log.error_log(err): + log.error_log(err) :Str("subject", "akismet") :Msg("Unable to check Akismet") return err @@ -87,8 +87,8 @@ local function check_akismet(post, user_agent, referrer) end local akismet_file = assert(io.open("/etc/gochan/akismet_key.txt", "r")) -key = akismet_file:read("*a") - +key = assert(akismet_file:read("*a")):gsub("%s+", "") +akismet_file:close() local err = check_api_key() if(err ~= nil) then diff --git a/examples/plugins/iphub.lua b/examples/plugins/iphub.lua new file mode 100644 index 00000000..8473f67e --- /dev/null +++ b/examples/plugins/iphub.lua @@ -0,0 +1,49 @@ +local events = require("events") +local http = require("http") +local json = require("json") +local log = require("gclog") +local url = require("url") + +local url_prefix = "http://v2.api.iphub.info/ip/" +local key = "" +local max_block = 0 +-- https://iphub.info/api + +local function check_iphub(ip) + if(key == "") then + return nil + end + local headers = {} + headers["X-Key"] = key + local resp, err = http.get(url_prefix .. ip, { + headers = headers + }) + if(err ~= nil) then + return err + end + local json_decoded = json.decode(resp.body) + local err = json_decoded["error"] + if(err ~= nil) then + return err + end + local block = tonumber(json_decoded["block"]) + if(block > max_block) then + log.error_log(): + Str("IP", ip): + Int("block", block): + Msg("IP determined as high-risk according to IPHub") + return "Your post looks like spam" + end + + return nil +end + + +local iphf = assert(io.open("/etc/gochan/iphub_key.txt", "r")) +key = assert(iphf:read("*a")):gsub("%s+", "") +iphf:close() + +events.register_event({"message-pre-format"}, function(tr, post, req) + local ip = post.IP + return check_iphub(ip) +end) \ No newline at end of file diff --git a/go.mod b/go.mod index 3264b64b..3ba716da 100644 --- a/go.mod +++ b/go.mod @@ -38,4 +38,5 @@ require ( golang.org/x/text v0.13.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect + layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf // indirect ) diff --git a/go.sum b/go.sum index e98ba495..a8e819ab 100755 --- a/go.sum +++ b/go.sum @@ -230,5 +230,7 @@ gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf h1:rRz0YsF7VXj9fXRF6yQgFI7DzST+hsI3TeFSGupntu0= +layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf/go.mod h1:ivKkcY8Zxw5ba0jldhZCYYQfGdb2K6u9tbYK1AwMIBc= layeh.com/gopher-luar v1.0.11 h1:8zJudpKI6HWkoh9eyyNFaTM79PY6CAPcIr6X/KTiliw= layeh.com/gopher-luar v1.0.11/go.mod h1:TPnIVCZ2RJBndm7ohXyaqfhzjlZ+OA2SZR/YwL8tECk= diff --git a/pkg/gcplugin/gcplugin.go b/pkg/gcplugin/gcplugin.go index f4dd672d..4d27046f 100644 --- a/pkg/gcplugin/gcplugin.go +++ b/pkg/gcplugin/gcplugin.go @@ -23,6 +23,7 @@ import ( luaFilePath "github.com/vadv/gopher-lua-libs/filepath" luaStrings "github.com/vadv/gopher-lua-libs/strings" lua "github.com/yuin/gopher-lua" + luajson "layeh.com/gopher-json" ) var ( @@ -48,6 +49,7 @@ func preloadLua() { luaFilePath.Preload(lState) luaStrings.Preload(lState) async.Init(lState) + luajson.Preload(lState) lState.PreloadModule("http", gluahttp.NewHttpModule(&http.Client{}).Loader) lState.PreloadModule("url", func(l *lua.LState) int { @@ -65,6 +67,17 @@ func preloadLua() { l.Push(luar.New(l, err)) return 2 }, + "path_escape": func(l *lua.LState) int { + escaped := url.PathEscape(l.CheckString(1)) + l.Push(lua.LString(escaped)) + return 1 + }, + "path_unescape": func(l *lua.LState) int { + unescaped, err := url.PathUnescape(l.CheckString(1)) + l.Push(lua.LString(unescaped)) + l.Push(luar.New(l, err)) + return 2 + }, "query_escape": func(l *lua.LState) int { query := l.CheckString(1) l.Push(lua.LString(url.QueryEscape(query)))