2026-10-01 18:25:53 +08:00
|
|
|
package router
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/xtls/xray-core/app/dns"
|
|
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
|
"github.com/xtls/xray-core/common/errors"
|
|
|
|
|
"github.com/xtls/xray-core/common/geodata"
|
|
|
|
|
"github.com/xtls/xray-core/common/log"
|
2026-10-04 07:12:54 +08:00
|
|
|
xlua "github.com/xtls/xray-core/common/lua"
|
2026-10-01 18:25:53 +08:00
|
|
|
"github.com/xtls/xray-core/features/routing"
|
|
|
|
|
lua "github.com/yuin/gopher-lua"
|
|
|
|
|
)
|
|
|
|
|
|
2026-10-04 05:22:47 +08:00
|
|
|
const scriptExecutionTimeout = 6 * time.Second
|
2026-10-01 18:25:53 +08:00
|
|
|
|
|
|
|
|
type scriptEngine struct {
|
2026-10-05 06:38:24 +08:00
|
|
|
pool *xlua.Pool
|
2026-10-01 18:25:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newScriptEngine(path string, router *Router) (*scriptEngine, error) {
|
2026-10-04 07:12:54 +08:00
|
|
|
program, err := xlua.CompileFile(path)
|
2026-10-01 18:25:53 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-10-05 06:38:24 +08:00
|
|
|
|
|
|
|
|
pool, err := xlua.NewPool(router.ctx, scriptExecutionTimeout, program.NewStateFactory(
|
2026-10-04 05:22:47 +08:00
|
|
|
scriptExecutionTimeout*20,
|
2026-10-03 21:57:54 +08:00
|
|
|
func(L *lua.LState) {
|
2026-10-01 18:25:53 +08:00
|
|
|
geodata.RegisterLua(L)
|
|
|
|
|
log.RegisterLua(L)
|
|
|
|
|
router.RegisterLua(L)
|
|
|
|
|
dns.RegisterLua(L, router.dns)
|
2026-10-03 21:57:54 +08:00
|
|
|
},
|
|
|
|
|
func(L *lua.LState) error {
|
|
|
|
|
if L.GetGlobal("HandleRoute").Type() != lua.LTFunction {
|
|
|
|
|
return errors.New("routing script must define HandleRoute(...)")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}))
|
2026-10-01 18:25:53 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-10-05 06:38:24 +08:00
|
|
|
|
2026-10-01 18:25:53 +08:00
|
|
|
errors.LogInfo(router.ctx, "routing script initialized from ", path)
|
2026-10-05 06:38:24 +08:00
|
|
|
return &scriptEngine{pool: pool}, nil
|
2026-10-01 18:25:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *scriptEngine) close() {
|
|
|
|
|
e.pool.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *scriptEngine) pickRoute(ctx routing.Context) (routing.Route, error) {
|
2026-10-05 06:38:24 +08:00
|
|
|
var outboundTag, ruleTag string
|
|
|
|
|
var routeErr error
|
|
|
|
|
|
|
|
|
|
if err := e.pool.WithState(nil, 0, func(L *lua.LState) error {
|
|
|
|
|
if err := callLuaRoute(L, ctx); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
outboundTag, ruleTag, routeErr = readLuaRouteResult(L)
|
|
|
|
|
return nil
|
|
|
|
|
}); err != nil {
|
2026-10-01 18:25:53 +08:00
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-10-05 06:38:24 +08:00
|
|
|
|
|
|
|
|
if routeErr != nil {
|
|
|
|
|
return nil, routeErr
|
|
|
|
|
}
|
|
|
|
|
if outboundTag == "" {
|
2026-10-01 18:25:53 +08:00
|
|
|
return nil, common.ErrNoClue
|
|
|
|
|
}
|
2026-10-05 06:38:24 +08:00
|
|
|
|
|
|
|
|
return &Route{Context: ctx, outboundTag: outboundTag, ruleTag: ruleTag}, nil
|
2026-10-01 18:25:53 +08:00
|
|
|
}
|