feat(dns): add Lua scripting for DNS queries

This commit is contained in:
Meo597
2026-09-26 05:37:23 +08:00
parent 60e2a0c502
commit 235843c5d2
15 changed files with 681 additions and 8 deletions
+42
View File
@@ -0,0 +1,42 @@
package geodata
import (
"testing"
"github.com/xtls/xray-core/common/net"
lua "github.com/yuin/gopher-lua"
)
func TestLuaIPMatcherAcceptsNativeIP(t *testing.T) {
L := lua.NewState()
defer L.Close()
RegisterLua(L)
ip := L.NewUserData()
ip.Value = net.ParseIP("127.0.0.1")
L.SetGlobal("ip", ip)
if err := L.DoString(`
local matcher = require("xray.geodata").ipMatcher({"127.0.0.0/8"})
assert(matcher:Match(ip))
assert(matcher:AnyMatch({ip}))
assert(matcher:Matches({ip}))
local matched, unmatched = matcher:FilterIPs({ip})
assert(#matched == 1 and #unmatched == 0)
assert(matcher:Match(matched[1]))
`); err != nil {
t.Fatal(err)
}
}
func TestLuaDomainMatcherUsesNativeMatcher(t *testing.T) {
L := lua.NewState()
defer L.Close()
RegisterLua(L)
if err := L.DoString(`
local matcher = require("xray.geodata").domainMatcher({"example.com"})
assert(matcher:MatchAny("example.com"))
assert(matcher:MatchAny("www.example.com"))
assert(#(matcher:Match("www.example.com")) == 1)
`); err != nil {
t.Fatal(err)
}
}