1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-28 08:06:24 -07:00

Add JSON support to Lua and IPHub plugin

This commit is contained in:
Eggbertx 2023-10-27 17:03:13 -07:00
parent 21e14f356f
commit 735470081c
5 changed files with 68 additions and 3 deletions

View file

@ -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

View file

@ -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)

1
go.mod
View file

@ -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
)

2
go.sum
View file

@ -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=

View file

@ -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)))