Skip to content

Commit

Permalink
Chore: get rid of v2ray-core & update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Loyalsoldier committed Oct 9, 2024
1 parent a5a300d commit e8edc71
Show file tree
Hide file tree
Showing 7 changed files with 401 additions and 279 deletions.
18 changes: 2 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,8 @@ toolchain go1.22.6

require (
github.com/oschwald/maxminddb-golang v1.13.1
github.com/v2fly/v2ray-core/v5 v5.17.1
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba
google.golang.org/protobuf v1.34.2
google.golang.org/protobuf v1.35.1
)

require (
github.com/adrg/xdg v0.5.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pires/go-proxyproto v0.7.0 // indirect
github.com/quic-go/quic-go v0.46.0 // indirect
go.starlark.net v0.0.0-20230612165344-9532f5667272 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
require golang.org/x/sys v0.21.0 // indirect
219 changes: 7 additions & 212 deletions go.sum

Large diffs are not rendered by default.

135 changes: 104 additions & 31 deletions lib/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,21 @@ import (
"net"
"net/netip"
"strings"
"sync"

"go4.org/netipx"
)

type Entry struct {
name string
mu *sync.Mutex
ipv4Builder *netipx.IPSetBuilder
ipv6Builder *netipx.IPSetBuilder
ipv4Set *netipx.IPSet
ipv6Set *netipx.IPSet
}

func NewEntry(name string) *Entry {
return &Entry{
name: strings.ToUpper(strings.TrimSpace(name)),
mu: new(sync.Mutex),
ipv4Builder: new(netipx.IPSetBuilder),
ipv6Builder: new(netipx.IPSetBuilder),
name: strings.ToUpper(strings.TrimSpace(name)),
}
}

Expand All @@ -38,6 +35,38 @@ func (e *Entry) hasIPv6Builder() bool {
return e.ipv6Builder != nil
}

func (e *Entry) hasIPv4Set() bool {
return e.ipv4Set != nil
}

func (e *Entry) hasIPv6Set() bool {
return e.ipv6Set != nil
}

func (e *Entry) GetIPv4Set() (*netipx.IPSet, error) {
if err := e.buildIPSet(); err != nil {
return nil, err
}

if e.hasIPv4Set() {
return e.ipv4Set, nil
}

return nil, fmt.Errorf("entry %s has no ipv4 set", e.GetName())
}

func (e *Entry) GetIPv6Set() (*netipx.IPSet, error) {
if err := e.buildIPSet(); err != nil {
return nil, err
}

if e.hasIPv6Set() {
return e.ipv6Set, nil
}

return nil, fmt.Errorf("entry %s has no ipv6 set", e.GetName())
}

func (e *Entry) processPrefix(src any) (*netip.Prefix, IPType, error) {
switch src := src.(type) {
case net.IP:
Expand Down Expand Up @@ -218,9 +247,6 @@ func (e *Entry) processPrefix(src any) (*netip.Prefix, IPType, error) {
}

func (e *Entry) add(prefix *netip.Prefix, ipType IPType) error {
e.mu.Lock()
defer e.mu.Unlock()

switch ipType {
case IPv4:
if !e.hasIPv4Builder() {
Expand All @@ -240,9 +266,6 @@ func (e *Entry) add(prefix *netip.Prefix, ipType IPType) error {
}

func (e *Entry) remove(prefix *netip.Prefix, ipType IPType) error {
e.mu.Lock()
defer e.mu.Unlock()

switch ipType {
case IPv4:
if e.hasIPv4Builder() {
Expand Down Expand Up @@ -281,7 +304,27 @@ func (e *Entry) RemovePrefix(cidr string) error {
return nil
}

func (e *Entry) MarshalText(opts ...IgnoreIPOption) ([]string, error) {
func (e *Entry) buildIPSet() error {
if e.hasIPv4Builder() && !e.hasIPv4Set() {
ipv4set, err := e.ipv4Builder.IPSet()
if err != nil {
return err
}
e.ipv4Set = ipv4set
}

if e.hasIPv6Builder() && !e.hasIPv6Set() {
ipv6set, err := e.ipv6Builder.IPSet()
if err != nil {
return err
}
e.ipv6Set = ipv6set
}

return nil
}

func (e *Entry) MarshalPrefix(opts ...IgnoreIPOption) ([]netip.Prefix, error) {
var ignoreIPType IPType
for _, opt := range opts {
if opt != nil {
Expand All @@ -296,32 +339,62 @@ func (e *Entry) MarshalText(opts ...IgnoreIPOption) ([]string, error) {
disableIPv6 = true
}

prefixSet := make([]string, 0, 1024)
if err := e.buildIPSet(); err != nil {
return nil, err
}

if !disableIPv4 && e.hasIPv4Builder() {
ipv4set, err := e.ipv4Builder.IPSet()
if err != nil {
return nil, err
}
prefixes := ipv4set.Prefixes()
for _, prefix := range prefixes {
prefixSet = append(prefixSet, prefix.String())
prefixes := make([]netip.Prefix, 0, 1024)

if !disableIPv4 && e.hasIPv4Set() {
prefixes = append(prefixes, e.ipv4Set.Prefixes()...)
}

if !disableIPv6 && e.hasIPv6Set() {
prefixes = append(prefixes, e.ipv6Set.Prefixes()...)
}

if len(prefixes) > 0 {
return prefixes, nil
}

return nil, fmt.Errorf("entry %s has no prefix", e.GetName())
}

func (e *Entry) MarshalText(opts ...IgnoreIPOption) ([]string, error) {
var ignoreIPType IPType
for _, opt := range opts {
if opt != nil {
ignoreIPType = opt()
}
}
disableIPv4, disableIPv6 := false, false
switch ignoreIPType {
case IPv4:
disableIPv4 = true
case IPv6:
disableIPv6 = true
}

if !disableIPv6 && e.hasIPv6Builder() {
ipv6set, err := e.ipv6Builder.IPSet()
if err != nil {
return nil, err
if err := e.buildIPSet(); err != nil {
return nil, err
}

cidrList := make([]string, 0, 1024)

if !disableIPv4 && e.hasIPv4Set() {
for _, prefix := range e.ipv4Set.Prefixes() {
cidrList = append(cidrList, prefix.String())
}
prefixes := ipv6set.Prefixes()
for _, prefix := range prefixes {
prefixSet = append(prefixSet, prefix.String())
}

if !disableIPv6 && e.hasIPv6Set() {
for _, prefix := range e.ipv6Set.Prefixes() {
cidrList = append(cidrList, prefix.String())
}
}

if len(prefixSet) > 0 {
return prefixSet, nil
if len(cidrList) > 0 {
return cidrList, nil
}

return nil, fmt.Errorf("entry %s has no prefix", e.GetName())
Expand Down
3 changes: 1 addition & 2 deletions plugin/v2ray/dat_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"

"github.com/v2fly/geoip/lib"
router "github.com/v2fly/v2ray-core/v5/app/router/routercommon"
"google.golang.org/protobuf/proto"
)

Expand Down Expand Up @@ -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
}
Expand Down
34 changes: 16 additions & 18 deletions plugin/v2ray/dat_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"encoding/json"
"fmt"
"log"
"net/netip"
"os"
"path/filepath"
"sort"
"strings"

"github.com/v2fly/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"
)

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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
})
Expand Down
Loading

0 comments on commit e8edc71

Please sign in to comment.