diff options
| author | Loyalsoldier <[email protected]> | 2024-10-08 06:23:47 +0800 |
|---|---|---|
| committer | Loyalsoldier <[email protected]> | 2024-10-08 06:23:47 +0800 |
| commit | f3bb9c0328795b413884c1baddbcb8de5a39b93f (patch) | |
| tree | 5ab6dc5c45a10172bbb6097d87322566399762d2 /plugin | |
| parent | 1e4cb746b9ca67b9d9dabdc65f42f495eb4cfbcd (diff) | |
Chore: get rid of v2ray-core & update dependencies
Diffstat (limited to 'plugin')
| -rw-r--r-- | plugin/v2ray/dat_in.go | 3 | ||||
| -rw-r--r-- | plugin/v2ray/dat_out.go | 34 | ||||
| -rw-r--r-- | plugin/v2ray/geoip.pb.go | 250 | ||||
| -rw-r--r-- | plugin/v2ray/geoip.proto | 21 |
4 files changed, 288 insertions, 20 deletions
diff --git a/plugin/v2ray/dat_in.go b/plugin/v2ray/dat_in.go index 96de3d70..37d8bb5a 100644 --- a/plugin/v2ray/dat_in.go +++ b/plugin/v2ray/dat_in.go @@ -10,7 +10,6 @@ import ( "strings" "github.com/Loyalsoldier/geoip/lib" - router "github.com/v2fly/v2ray-core/v5/app/router/routercommon" "google.golang.org/protobuf/proto" ) @@ -167,7 +166,7 @@ func (g *geoIPDatIn) generateEntries(reader io.Reader, entries map[string]*lib.E return err } - var geoipList router.GeoIPList + var geoipList GeoIPList if err := proto.Unmarshal(geoipBytes, &geoipList); err != nil { return err } diff --git a/plugin/v2ray/dat_out.go b/plugin/v2ray/dat_out.go index 771d4e27..1e64ede3 100644 --- a/plugin/v2ray/dat_out.go +++ b/plugin/v2ray/dat_out.go @@ -4,14 +4,13 @@ import ( "encoding/json" "fmt" "log" + "net/netip" "os" "path/filepath" "sort" "strings" "github.com/Loyalsoldier/geoip/lib" - router "github.com/v2fly/v2ray-core/v5/app/router/routercommon" - "github.com/v2fly/v2ray-core/v5/infra/conf/rule" "google.golang.org/protobuf/proto" ) @@ -101,8 +100,8 @@ func (g *geoIPDatOut) GetDescription() string { } func (g *geoIPDatOut) Output(container lib.Container) error { - geoIPList := new(router.GeoIPList) - geoIPList.Entry = make([]*router.GeoIP, 0, 300) + geoIPList := new(GeoIPList) + geoIPList.Entry = make([]*GeoIP, 0, 300) updated := false switch len(g.Want) { @@ -188,32 +187,31 @@ func (g *geoIPDatOut) Output(container lib.Container) error { return nil } -func (g *geoIPDatOut) generateGeoIP(entry *lib.Entry) (*router.GeoIP, error) { - var entryCidr []string +func (g *geoIPDatOut) generateGeoIP(entry *lib.Entry) (*GeoIP, error) { + var entryCidr []netip.Prefix var err error switch g.OnlyIPType { case lib.IPv4: - entryCidr, err = entry.MarshalText(lib.IgnoreIPv6) + entryCidr, err = entry.MarshalPrefix(lib.IgnoreIPv6) case lib.IPv6: - entryCidr, err = entry.MarshalText(lib.IgnoreIPv4) + entryCidr, err = entry.MarshalPrefix(lib.IgnoreIPv4) default: - entryCidr, err = entry.MarshalText() + entryCidr, err = entry.MarshalPrefix() } if err != nil { return nil, err } - v2rayCIDR := make([]*router.CIDR, 0, 1024) - for _, cidrStr := range entryCidr { - cidr, err := rule.ParseIP(cidrStr) - if err != nil { - return nil, err - } - v2rayCIDR = append(v2rayCIDR, cidr) + v2rayCIDR := make([]*CIDR, 0, len(entryCidr)) + for _, prefix := range entryCidr { + v2rayCIDR = append(v2rayCIDR, &CIDR{ + Ip: prefix.Addr().AsSlice(), + Prefix: uint32(prefix.Bits()), + }) } if len(v2rayCIDR) > 0 { - return &router.GeoIP{ + return &GeoIP{ CountryCode: entry.GetName(), Cidr: v2rayCIDR, }, nil @@ -223,7 +221,7 @@ func (g *geoIPDatOut) generateGeoIP(entry *lib.Entry) (*router.GeoIP, error) { } // Sort by country code to make reproducible builds -func (g *geoIPDatOut) sort(list *router.GeoIPList) { +func (g *geoIPDatOut) sort(list *GeoIPList) { sort.SliceStable(list.Entry, func(i, j int) bool { return list.Entry[i].CountryCode < list.Entry[j].CountryCode }) diff --git a/plugin/v2ray/geoip.pb.go b/plugin/v2ray/geoip.pb.go new file mode 100644 index 00000000..d7def9f9 --- /dev/null +++ b/plugin/v2ray/geoip.pb.go @@ -0,0 +1,250 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.35.1 +// protoc v5.28.2 +// source: geoip.proto + +package v2ray + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type CIDR struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // IP address, should be either 4 or 16 bytes. + Ip []byte `protobuf:"bytes,1,opt,name=ip,proto3" json:"ip,omitempty"` + // Number of leading ones in the network mask. + Prefix uint32 `protobuf:"varint,2,opt,name=prefix,proto3" json:"prefix,omitempty"` +} + +func (x *CIDR) Reset() { + *x = CIDR{} + mi := &file_geoip_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *CIDR) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*CIDR) ProtoMessage() {} + +func (x *CIDR) ProtoReflect() protoreflect.Message { + mi := &file_geoip_proto_msgTypes[0] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use CIDR.ProtoReflect.Descriptor instead. +func (*CIDR) Descriptor() ([]byte, []int) { + return file_geoip_proto_rawDescGZIP(), []int{0} +} + +func (x *CIDR) GetIp() []byte { + if x != nil { + return x.Ip + } + return nil +} + +func (x *CIDR) GetPrefix() uint32 { + if x != nil { + return x.Prefix + } + return 0 +} + +type GeoIP struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + CountryCode string `protobuf:"bytes,1,opt,name=country_code,json=countryCode,proto3" json:"country_code,omitempty"` + Cidr []*CIDR `protobuf:"bytes,2,rep,name=cidr,proto3" json:"cidr,omitempty"` +} + +func (x *GeoIP) Reset() { + *x = GeoIP{} + mi := &file_geoip_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *GeoIP) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GeoIP) ProtoMessage() {} + +func (x *GeoIP) ProtoReflect() protoreflect.Message { + mi := &file_geoip_proto_msgTypes[1] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use GeoIP.ProtoReflect.Descriptor instead. +func (*GeoIP) Descriptor() ([]byte, []int) { + return file_geoip_proto_rawDescGZIP(), []int{1} +} + +func (x *GeoIP) GetCountryCode() string { + if x != nil { + return x.CountryCode + } + return "" +} + +func (x *GeoIP) GetCidr() []*CIDR { + if x != nil { + return x.Cidr + } + return nil +} + +type GeoIPList struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Entry []*GeoIP `protobuf:"bytes,1,rep,name=entry,proto3" json:"entry,omitempty"` +} + +func (x *GeoIPList) Reset() { + *x = GeoIPList{} + mi := &file_geoip_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *GeoIPList) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GeoIPList) ProtoMessage() {} + +func (x *GeoIPList) ProtoReflect() protoreflect.Message { + mi := &file_geoip_proto_msgTypes[2] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use GeoIPList.ProtoReflect.Descriptor instead. +func (*GeoIPList) Descriptor() ([]byte, []int) { + return file_geoip_proto_rawDescGZIP(), []int{2} +} + +func (x *GeoIPList) GetEntry() []*GeoIP { + if x != nil { + return x.Entry + } + return nil +} + +var File_geoip_proto protoreflect.FileDescriptor + +var file_geoip_proto_rawDesc = []byte{ + 0x0a, 0x0b, 0x67, 0x65, 0x6f, 0x69, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x12, 0x67, + 0x65, 0x6f, 0x69, 0x70, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x76, 0x32, 0x72, 0x61, + 0x79, 0x22, 0x2e, 0x0a, 0x04, 0x43, 0x49, 0x44, 0x52, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x70, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x02, 0x69, 0x70, 0x12, 0x16, 0x0a, 0x06, 0x70, 0x72, 0x65, + 0x66, 0x69, 0x78, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, + 0x78, 0x22, 0x58, 0x0a, 0x05, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x12, 0x21, 0x0a, 0x0c, 0x63, 0x6f, + 0x75, 0x6e, 0x74, 0x72, 0x79, 0x5f, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x0b, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x72, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x2c, 0x0a, + 0x04, 0x63, 0x69, 0x64, 0x72, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x18, 0x2e, 0x67, 0x65, + 0x6f, 0x69, 0x70, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x76, 0x32, 0x72, 0x61, 0x79, + 0x2e, 0x43, 0x49, 0x44, 0x52, 0x52, 0x04, 0x63, 0x69, 0x64, 0x72, 0x22, 0x3c, 0x0a, 0x09, 0x47, + 0x65, 0x6f, 0x49, 0x50, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x2f, 0x0a, 0x05, 0x65, 0x6e, 0x74, 0x72, + 0x79, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x19, 0x2e, 0x67, 0x65, 0x6f, 0x69, 0x70, 0x2e, + 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, 0x47, 0x65, 0x6f, + 0x49, 0x50, 0x52, 0x05, 0x65, 0x6e, 0x74, 0x72, 0x79, 0x42, 0x2c, 0x5a, 0x2a, 0x67, 0x69, 0x74, + 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x4c, 0x6f, 0x79, 0x61, 0x6c, 0x73, 0x6f, 0x6c, + 0x64, 0x69, 0x65, 0x72, 0x2f, 0x67, 0x65, 0x6f, 0x69, 0x70, 0x2f, 0x70, 0x6c, 0x75, 0x67, 0x69, + 0x6e, 0x2f, 0x76, 0x32, 0x72, 0x61, 0x79, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_geoip_proto_rawDescOnce sync.Once + file_geoip_proto_rawDescData = file_geoip_proto_rawDesc +) + +func file_geoip_proto_rawDescGZIP() []byte { + file_geoip_proto_rawDescOnce.Do(func() { + file_geoip_proto_rawDescData = protoimpl.X.CompressGZIP(file_geoip_proto_rawDescData) + }) + return file_geoip_proto_rawDescData +} + +var file_geoip_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_geoip_proto_goTypes = []any{ + (*CIDR)(nil), // 0: geoip.plugin.v2ray.CIDR + (*GeoIP)(nil), // 1: geoip.plugin.v2ray.GeoIP + (*GeoIPList)(nil), // 2: geoip.plugin.v2ray.GeoIPList +} +var file_geoip_proto_depIdxs = []int32{ + 0, // 0: geoip.plugin.v2ray.GeoIP.cidr:type_name -> geoip.plugin.v2ray.CIDR + 1, // 1: geoip.plugin.v2ray.GeoIPList.entry:type_name -> geoip.plugin.v2ray.GeoIP + 2, // [2:2] is the sub-list for method output_type + 2, // [2:2] is the sub-list for method input_type + 2, // [2:2] is the sub-list for extension type_name + 2, // [2:2] is the sub-list for extension extendee + 0, // [0:2] is the sub-list for field type_name +} + +func init() { file_geoip_proto_init() } +func file_geoip_proto_init() { + if File_geoip_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_geoip_proto_rawDesc, + NumEnums: 0, + NumMessages: 3, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_geoip_proto_goTypes, + DependencyIndexes: file_geoip_proto_depIdxs, + MessageInfos: file_geoip_proto_msgTypes, + }.Build() + File_geoip_proto = out.File + file_geoip_proto_rawDesc = nil + file_geoip_proto_goTypes = nil + file_geoip_proto_depIdxs = nil +} diff --git a/plugin/v2ray/geoip.proto b/plugin/v2ray/geoip.proto new file mode 100644 index 00000000..b25ba08a --- /dev/null +++ b/plugin/v2ray/geoip.proto @@ -0,0 +1,21 @@ +syntax = "proto3"; + +package geoip.plugin.v2ray; +option go_package = "github.com/Loyalsoldier/geoip/plugin/v2ray"; + +message CIDR { + // IP address, should be either 4 or 16 bytes. + bytes ip = 1; + + // Number of leading ones in the network mask. + uint32 prefix = 2; +} + +message GeoIP { + string country_code = 1; + repeated CIDR cidr = 2; +} + +message GeoIPList { + repeated GeoIP entry = 1; +} |
