mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-08-03 07:36:23 -07:00
Set up lua GeoIP handler registration and Cloudflare GeoIP header example plugin
This commit is contained in:
parent
c26b7e9e1d
commit
39908d74c6
4 changed files with 166 additions and 2 deletions
|
@ -30,8 +30,8 @@ func main() {
|
||||||
fmt.Println("Cleaning up")
|
fmt.Println("Cleaning up")
|
||||||
gcsql.Close()
|
gcsql.Close()
|
||||||
gcutil.CloseLog()
|
gcutil.CloseLog()
|
||||||
gcplugin.ClosePlugins()
|
|
||||||
geoip.Close()
|
geoip.Close()
|
||||||
|
gcplugin.ClosePlugins()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
fmt.Printf("Starting gochan v%s\n", versionStr)
|
fmt.Printf("Starting gochan v%s\n", versionStr)
|
||||||
|
@ -78,7 +78,9 @@ func main() {
|
||||||
parseCommandLine()
|
parseCommandLine()
|
||||||
serverutil.InitMinifier()
|
serverutil.InitMinifier()
|
||||||
siteCfg := config.GetSiteConfig()
|
siteCfg := config.GetSiteConfig()
|
||||||
geoip.SetupGeoIP(siteCfg.GeoIPType, siteCfg.GeoIPOptions)
|
if err = geoip.SetupGeoIP(siteCfg.GeoIPType, siteCfg.GeoIPOptions); err != nil {
|
||||||
|
gcutil.LogFatal().Err(err).Msg("Unable to initialize GeoIP")
|
||||||
|
}
|
||||||
posting.InitCaptcha()
|
posting.InitCaptcha()
|
||||||
|
|
||||||
if err = gctemplates.InitTemplates(); err != nil {
|
if err = gctemplates.InitTemplates(); err != nil {
|
||||||
|
|
35
examples/plugins/cloudflare_geoip.lua
Normal file
35
examples/plugins/cloudflare_geoip.lua
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
local geoip = require("geoip")
|
||||||
|
local log = require("gclog")
|
||||||
|
|
||||||
|
local geoip_header = "CF-IPCountry"
|
||||||
|
|
||||||
|
CFGeoIP = {}
|
||||||
|
|
||||||
|
function CFGeoIP:new()
|
||||||
|
local t = setmetatable({}, {__index = CFGeoIP})
|
||||||
|
return t
|
||||||
|
end
|
||||||
|
|
||||||
|
function CFGeoIP:init()
|
||||||
|
log.info_log():Str("dbType", "cloudflare"):Msg("GeoIP initialized")
|
||||||
|
end
|
||||||
|
|
||||||
|
function CFGeoIP:close()
|
||||||
|
return ">:("
|
||||||
|
end
|
||||||
|
|
||||||
|
function CFGeoIP.get_country(request, board, errEv)
|
||||||
|
local abbr = request.Header:Get(geoip_header)
|
||||||
|
local name, err = geoip.country_name(abbr)
|
||||||
|
if(err ~= nil) then
|
||||||
|
errEv:Err(err):Caller():Send()
|
||||||
|
return nil, err
|
||||||
|
end
|
||||||
|
return {
|
||||||
|
flag = abbr,
|
||||||
|
name = name
|
||||||
|
}, nil
|
||||||
|
end
|
||||||
|
|
||||||
|
-- local cf = CFGeoIP:new()
|
||||||
|
geoip.register_handler("cloudflare", CFGeoIP)
|
|
@ -14,6 +14,7 @@ import (
|
||||||
"github.com/gochan-org/gochan/pkg/gctemplates"
|
"github.com/gochan-org/gochan/pkg/gctemplates"
|
||||||
"github.com/gochan-org/gochan/pkg/gcutil"
|
"github.com/gochan-org/gochan/pkg/gcutil"
|
||||||
"github.com/gochan-org/gochan/pkg/manage"
|
"github.com/gochan-org/gochan/pkg/manage"
|
||||||
|
"github.com/gochan-org/gochan/pkg/posting/geoip"
|
||||||
"github.com/gochan-org/gochan/pkg/posting/uploads"
|
"github.com/gochan-org/gochan/pkg/posting/uploads"
|
||||||
"github.com/gochan-org/gochan/pkg/server/serverutil"
|
"github.com/gochan-org/gochan/pkg/server/serverutil"
|
||||||
luar "layeh.com/gopher-luar"
|
luar "layeh.com/gopher-luar"
|
||||||
|
@ -101,6 +102,7 @@ func preloadLua() {
|
||||||
lState.PreloadModule("gclog", gcutil.PreloadModule)
|
lState.PreloadModule("gclog", gcutil.PreloadModule)
|
||||||
lState.PreloadModule("gcsql", gcsql.PreloadModule)
|
lState.PreloadModule("gcsql", gcsql.PreloadModule)
|
||||||
lState.PreloadModule("gctemplates", gctemplates.PreloadModule)
|
lState.PreloadModule("gctemplates", gctemplates.PreloadModule)
|
||||||
|
lState.PreloadModule("geoip", geoip.PreloadModule)
|
||||||
lState.PreloadModule("manage", manage.PreloadModule)
|
lState.PreloadModule("manage", manage.PreloadModule)
|
||||||
lState.PreloadModule("uploads", uploads.PreloadModule)
|
lState.PreloadModule("uploads", uploads.PreloadModule)
|
||||||
lState.PreloadModule("serverutil", serverutil.PreloadModule)
|
lState.PreloadModule("serverutil", serverutil.PreloadModule)
|
||||||
|
|
125
pkg/posting/geoip/preload.go
Normal file
125
pkg/posting/geoip/preload.go
Normal file
|
@ -0,0 +1,125 @@
|
||||||
|
package geoip
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
lua "github.com/yuin/gopher-lua"
|
||||||
|
luar "layeh.com/gopher-luar"
|
||||||
|
)
|
||||||
|
|
||||||
|
type luaHandler struct {
|
||||||
|
lState *lua.LState
|
||||||
|
initFunc lua.LValue
|
||||||
|
getCountryFunc lua.LValue
|
||||||
|
closeFunc lua.LValue
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lh *luaHandler) Init(options map[string]any) error {
|
||||||
|
if lh.initFunc == lua.LNil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
optionsT := lh.lState.NewTable()
|
||||||
|
for k, v := range options {
|
||||||
|
optionsT.RawSetString(k, luar.New(lh.lState, v))
|
||||||
|
}
|
||||||
|
p := lua.P{
|
||||||
|
Fn: lh.initFunc,
|
||||||
|
NRet: 1,
|
||||||
|
}
|
||||||
|
err := lh.lState.CallByParam(p, optionsT)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
errStr := lua.LVAsString(lh.lState.Get(-1))
|
||||||
|
if errStr != "" {
|
||||||
|
return errors.New(errStr)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lh *luaHandler) GetCountry(request *http.Request, board string, errEv *zerolog.Event) (*Country, error) {
|
||||||
|
p := lua.P{
|
||||||
|
Fn: lh.getCountryFunc,
|
||||||
|
NRet: 2,
|
||||||
|
}
|
||||||
|
err := lh.lState.CallByParam(p,
|
||||||
|
luar.New(lh.lState, request),
|
||||||
|
lua.LString(board),
|
||||||
|
luar.New(lh.lState, errEv))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
countryTable, ok := lh.lState.Get(-2).(*lua.LTable)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("invalid value returned by get_country (expected table)")
|
||||||
|
}
|
||||||
|
errStr := lua.LVAsString(lh.lState.Get(-1))
|
||||||
|
if errStr != "" {
|
||||||
|
return nil, errors.New(errStr)
|
||||||
|
}
|
||||||
|
name, ok := countryTable.RawGetString("name").(lua.LString)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("invalid name value in table returned gy bet_country (expected string)")
|
||||||
|
}
|
||||||
|
flag, ok := countryTable.RawGetString("flag").(lua.LString)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("invalid flag value in table returned gy bet_country (expected string)")
|
||||||
|
}
|
||||||
|
return &Country{
|
||||||
|
Name: name.String(),
|
||||||
|
Flag: flag.String(),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lh *luaHandler) Close() error {
|
||||||
|
if lh.closeFunc == lua.LNil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
p := lua.P{
|
||||||
|
Fn: lh.closeFunc,
|
||||||
|
NRet: 1,
|
||||||
|
}
|
||||||
|
err := lh.lState.CallByParam(p)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
errStr := lua.LVAsString(lh.lState.Get(-1))
|
||||||
|
if errStr != "" {
|
||||||
|
return errors.New(errStr)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func PreloadModule(l *lua.LState) int {
|
||||||
|
t := l.NewTable()
|
||||||
|
l.SetFuncs(t, map[string]lua.LGFunction{
|
||||||
|
"register_handler": func(l *lua.LState) int {
|
||||||
|
name := l.CheckString(1)
|
||||||
|
handlerTable := l.CheckTable(2)
|
||||||
|
initFuncVal := handlerTable.RawGetString("init")
|
||||||
|
lookupFunc := handlerTable.RawGetString("get_country")
|
||||||
|
closeFuncVal := handlerTable.RawGetString("close")
|
||||||
|
handler := &luaHandler{
|
||||||
|
lState: l,
|
||||||
|
initFunc: initFuncVal,
|
||||||
|
getCountryFunc: lookupFunc,
|
||||||
|
closeFunc: closeFuncVal,
|
||||||
|
}
|
||||||
|
|
||||||
|
RegisterGeoIPHandler(name, handler)
|
||||||
|
return 0
|
||||||
|
},
|
||||||
|
"country_name": func(l *lua.LState) int {
|
||||||
|
abbr := l.CheckString(1)
|
||||||
|
name, err := GetCountryName(abbr)
|
||||||
|
l.Push(lua.LString(name))
|
||||||
|
l.Push(luar.New(l, err))
|
||||||
|
return 2
|
||||||
|
},
|
||||||
|
})
|
||||||
|
l.Push(t)
|
||||||
|
return 1
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue