精简P2: geodata 测试夹具改 protowire 手工编码(geodat.proto 字段号+proto3 零值省略语义原样复刻), 不再依赖 xray-core 生成类型写 .dat 夹具
This commit is contained in:
@@ -13,8 +13,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/op/go-logging"
|
||||
xraygeodata "github.com/xtls/xray-core/common/geodata"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/encoding/protowire"
|
||||
|
||||
xuilogger "github.com/mhsanaei/3x-ui/v3/internal/logger"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/web/service"
|
||||
@@ -36,20 +35,47 @@ func newGeodataEngine(t *testing.T) *gin.Engine {
|
||||
return engine
|
||||
}
|
||||
|
||||
// The fixture databases are encoded with protowire: the wire layout
|
||||
// (geodat.proto field numbers) is the contract under test, and spelling it
|
||||
// out keeps the test independent of xray-core's generated types.
|
||||
|
||||
func geoWireBytes(num protowire.Number, payload []byte) []byte {
|
||||
out := protowire.AppendTag(nil, num, protowire.BytesType)
|
||||
return protowire.AppendBytes(out, payload)
|
||||
}
|
||||
|
||||
func geoWireString(num protowire.Number, value string) []byte {
|
||||
out := protowire.AppendTag(nil, num, protowire.BytesType)
|
||||
return protowire.AppendString(out, value)
|
||||
}
|
||||
|
||||
func geoWireVarint(num protowire.Number, value uint64) []byte {
|
||||
out := protowire.AppendTag(nil, num, protowire.VarintType)
|
||||
return protowire.AppendVarint(out, value)
|
||||
}
|
||||
|
||||
func geoFixtureDomain(typ uint64, value string, attrs ...string) []byte {
|
||||
var msg []byte
|
||||
if typ != 0 {
|
||||
msg = append(msg, geoWireVarint(1, typ)...)
|
||||
}
|
||||
msg = append(msg, geoWireString(2, value)...)
|
||||
for _, attr := range attrs {
|
||||
attrMsg := geoWireString(1, attr)
|
||||
attrMsg = append(attrMsg, geoWireVarint(2, 1)...) // bool_value = true
|
||||
msg = append(msg, geoWireBytes(3, attrMsg)...)
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
func writeGeositeDB(t *testing.T, dir string) {
|
||||
t.Helper()
|
||||
data, err := proto.Marshal(&xraygeodata.GeoSiteList{Entry: []*xraygeodata.GeoSite{
|
||||
{Code: "google", Domain: []*xraygeodata.Domain{
|
||||
{Type: xraygeodata.Domain_Domain, Value: "google.com"},
|
||||
{Type: xraygeodata.Domain_Full, Value: "ads.google.com", Attribute: []*xraygeodata.Domain_Attribute{
|
||||
{Key: "ads", TypedValue: &xraygeodata.Domain_Attribute_BoolValue{BoolValue: true}},
|
||||
}},
|
||||
}},
|
||||
{Code: "cn", Domain: []*xraygeodata.Domain{{Type: xraygeodata.Domain_Domain, Value: "baidu.com"}}},
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatalf("marshal geosite: %v", err)
|
||||
}
|
||||
google := geoWireString(1, "google")
|
||||
google = append(google, geoWireBytes(2, geoFixtureDomain(2, "google.com"))...)
|
||||
google = append(google, geoWireBytes(2, geoFixtureDomain(3, "ads.google.com", "ads"))...)
|
||||
cn := geoWireString(1, "cn")
|
||||
cn = append(cn, geoWireBytes(2, geoFixtureDomain(2, "baidu.com"))...)
|
||||
data := append(geoWireBytes(1, google), geoWireBytes(1, cn)...)
|
||||
if err := os.WriteFile(filepath.Join(dir, "geosite.dat"), data, 0o644); err != nil {
|
||||
t.Fatalf("write geosite.dat: %v", err)
|
||||
}
|
||||
@@ -58,12 +84,11 @@ func writeGeositeDB(t *testing.T, dir string) {
|
||||
func writeGeoipDB(t *testing.T, dir string) {
|
||||
t.Helper()
|
||||
prefix := netip.MustParsePrefix("10.0.0.0/8")
|
||||
data, err := proto.Marshal(&xraygeodata.GeoIPList{Entry: []*xraygeodata.GeoIP{
|
||||
{Code: "private", Cidr: []*xraygeodata.CIDR{{Ip: prefix.Addr().AsSlice(), Prefix: uint32(prefix.Bits())}}},
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatalf("marshal geoip: %v", err)
|
||||
}
|
||||
cidr := geoWireBytes(1, prefix.Addr().AsSlice())
|
||||
cidr = append(cidr, geoWireVarint(2, uint64(prefix.Bits()))...)
|
||||
entry := geoWireString(1, "private")
|
||||
entry = append(entry, geoWireBytes(2, cidr)...)
|
||||
data := geoWireBytes(1, entry)
|
||||
if err := os.WriteFile(filepath.Join(dir, "geoip.dat"), data, 0o644); err != nil {
|
||||
t.Fatalf("write geoip.dat: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user