From 548440c32b92b7b8fb3fc4e936cacd869479c719 Mon Sep 17 00:00:00 2001 From: Mark Pashmfouroush Date: Sun, 17 Mar 2024 15:23:18 +0000 Subject: [PATCH] misc: v6 fixes and 4,6 flags Signed-off-by: Mark Pashmfouroush --- README.md | 6 ++++-- app/app.go | 9 ++------- main.go | 21 +++++++++++++++++---- warp/endpoint.go | 26 ++++++++++++++++++++------ wiresocks/scanner.go | 25 ++++++++----------------- 5 files changed, 51 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 8bd0c175e..ca0173689 100644 --- a/README.md +++ b/README.md @@ -36,13 +36,15 @@ NAME warp-plus FLAGS + -4 only use IPv4 for random warp endpoint + -6 only use IPv6 for random warp endpoint -v, --verbose enable verbose logging -b, --bind STRING socks bind address (default: 127.0.0.1:8086) -e, --endpoint STRING warp endpoint -k, --key STRING warp key - --country STRING psiphon country code (valid values: [AT BE BG BR CA CH CZ DE DK EE ES FI FR GB HU IE IN IT JP LV NL NO PL RO RS SE SG SK UA US]) (default: AT) - --cfon enable psiphon mode (must provide country as well) --gool enable gool mode (warp in warp) + --cfon enable psiphon mode (must provide country as well) + --country STRING psiphon country code (valid values: [AT BE BG BR CA CH CZ DE DK EE ES FI FR GB HU IE IN IT JP LV NL NO PL RO RS SE SG SK UA US]) (default: AT) --scan enable warp scanning (experimental) --rtt DURATION scanner rtt limit (default: 1s) ``` diff --git a/app/app.go b/app/app.go index dd4ec98d2..b2afbf75c 100644 --- a/app/app.go +++ b/app/app.go @@ -8,7 +8,6 @@ import ( "net/netip" "os" "path/filepath" - "time" "github.com/bepass-org/warp-plus/psiphon" "github.com/bepass-org/warp-plus/warp" @@ -24,17 +23,13 @@ type WarpOptions struct { License string Psiphon *PsiphonOptions Gool bool - Scan *ScanOptions + Scan *wiresocks.ScanOptions } type PsiphonOptions struct { Country string } -type ScanOptions struct { - MaxRTT time.Duration -} - func RunWarp(ctx context.Context, l *slog.Logger, opts WarpOptions) error { if opts.Psiphon != nil && opts.Gool { return errors.New("can't use psiphon and gool at the same time") @@ -65,7 +60,7 @@ func RunWarp(ctx context.Context, l *slog.Logger, opts WarpOptions) error { endpoints := []string{opts.Endpoint, opts.Endpoint} if opts.Scan != nil { - res, err := wiresocks.RunScan(ctx, opts.Scan.MaxRTT) + res, err := wiresocks.RunScan(ctx, *opts.Scan) if err != nil { return err } diff --git a/main.go b/main.go index 676b413ef..fa8b3500a 100644 --- a/main.go +++ b/main.go @@ -11,8 +11,11 @@ import ( "syscall" "time" + _ "net/http/pprof" + "github.com/bepass-org/warp-plus/app" "github.com/bepass-org/warp-plus/warp" + "github.com/bepass-org/warp-plus/wiresocks" "github.com/peterbourgon/ff/v4" "github.com/peterbourgon/ff/v4/ffhelp" @@ -54,13 +57,15 @@ var psiphonCountries = []string{ func main() { fs := ff.NewFlagSet("warp-plus") var ( + v4 = fs.BoolShort('4', "only use IPv4 for random warp endpoint") + v6 = fs.BoolShort('6', "only use IPv6 for random warp endpoint") verbose = fs.Bool('v', "verbose", "enable verbose logging") bind = fs.String('b', "bind", "127.0.0.1:8086", "socks bind address") endpoint = fs.String('e', "endpoint", "", "warp endpoint") key = fs.String('k', "key", "", "warp key") - country = fs.StringEnumLong("country", fmt.Sprintf("psiphon country code (valid values: %s)", psiphonCountries), psiphonCountries...) - psiphon = fs.BoolLong("cfon", "enable psiphon mode (must provide country as well)") gool = fs.BoolLong("gool", "enable gool mode (warp in warp)") + psiphon = fs.BoolLong("cfon", "enable psiphon mode (must provide country as well)") + country = fs.StringEnumLong("country", fmt.Sprintf("psiphon country code (valid values: %s)", psiphonCountries), psiphonCountries...) scan = fs.BoolLong("scan", "enable warp scanning (experimental)") rtt = fs.DurationLong("rtt", 1000*time.Millisecond, "scanner rtt limit") ) @@ -86,6 +91,14 @@ func main() { fatal(l, errors.New("can't use cfon and gool at the same time")) } + if *v4 && *v6 { + fatal(l, errors.New("can't force v4 and v6 at the same time")) + } + + if !*v4 && !*v6 { + *v4, *v6 = true, true + } + bindAddrPort, err := netip.ParseAddrPort(*bind) if err != nil { fatal(l, fmt.Errorf("invalid bind address: %w", err)) @@ -105,12 +118,12 @@ func main() { if *scan { l.Info("scanner mode enabled", "max-rtt", rtt) - opts.Scan = &app.ScanOptions{MaxRTT: *rtt} + opts.Scan = &wiresocks.ScanOptions{V4: *v4, V6: *v6, MaxRTT: *rtt} } // If the endpoint is not set, choose a random warp endpoint if opts.Endpoint == "" { - addrPort, err := warp.RandomWarpEndpoint() + addrPort, err := warp.RandomWarpEndpoint(*v4, *v6) if err != nil { fatal(l, err) } diff --git a/warp/endpoint.go b/warp/endpoint.go index bb917d969..6855ae9e2 100644 --- a/warp/endpoint.go +++ b/warp/endpoint.go @@ -17,15 +17,29 @@ func WarpPrefixes() []netip.Prefix { netip.MustParsePrefix("188.114.97.0/24"), netip.MustParsePrefix("188.114.98.0/24"), netip.MustParsePrefix("188.114.99.0/24"), - netip.MustParsePrefix("2606:4700:d0::/48"), - netip.MustParsePrefix("2606:4700:d1::/48"), + netip.MustParsePrefix("2606:4700:d0::/64"), + netip.MustParsePrefix("2606:4700:d1::/64"), } } -func RandomWarpPrefix() netip.Prefix { +func RandomWarpPrefix(v4, v6 bool) netip.Prefix { + if !v4 && !v6 { + panic("Must choose a IP version for RandomWarpPrefix") + } + cidrs := WarpPrefixes() rng := rand.New(rand.NewSource(time.Now().UnixNano())) - return cidrs[rng.Intn(len(cidrs))] + for { + cidr := cidrs[rng.Intn(len(cidrs))] + + if v4 && cidr.Addr().Is4() { + return cidr + } + + if v6 && cidr.Addr().Is6() { + return cidr + } + } } func WarpPorts() []uint16 { @@ -93,8 +107,8 @@ func RandomWarpPort() uint16 { return ports[rng.Intn(len(ports))] } -func RandomWarpEndpoint() (netip.AddrPort, error) { - randomIP, err := iputils.RandomIPFromPrefix(RandomWarpPrefix()) +func RandomWarpEndpoint(v4, v6 bool) (netip.AddrPort, error) { + randomIP, err := iputils.RandomIPFromPrefix(RandomWarpPrefix(v4, v6)) if err != nil { return netip.AddrPort{}, err } diff --git a/wiresocks/scanner.go b/wiresocks/scanner.go index e8c15b2e0..109f1908d 100644 --- a/wiresocks/scanner.go +++ b/wiresocks/scanner.go @@ -4,8 +4,6 @@ import ( "context" "errors" "fmt" - "net" - "net/netip" "time" "github.com/bepass-org/warp-plus/ipscanner" @@ -13,20 +11,13 @@ import ( "github.com/go-ini/ini" ) -func canConnectIPv6(remoteAddr netip.AddrPort) bool { - dialer := net.Dialer{ - Timeout: 5 * time.Second, - } - - conn, err := dialer.Dial("tcp6", remoteAddr.String()) - if err != nil { - return false - } - defer conn.Close() - return true +type ScanOptions struct { + V4 bool + V6 bool + MaxRTT time.Duration } -func RunScan(ctx context.Context, rtt time.Duration) (result []ipscanner.IPInfo, err error) { +func RunScan(ctx context.Context, opts ScanOptions) (result []ipscanner.IPInfo, err error) { cfg, err := ini.Load("./primary/wgcf-profile.ini") if err != nil { return nil, fmt.Errorf("failed to read file: %w", err) @@ -43,9 +34,9 @@ func RunScan(ctx context.Context, rtt time.Duration) (result []ipscanner.IPInfo, ipscanner.WithWarpPing(), ipscanner.WithWarpPrivateKey(privateKey), ipscanner.WithWarpPeerPublicKey(publicKey), - ipscanner.WithUseIPv6(canConnectIPv6(netip.MustParseAddrPort("[2001:4860:4860::8888]:80"))), - ipscanner.WithUseIPv4(true), - ipscanner.WithMaxDesirableRTT(rtt), + ipscanner.WithUseIPv4(opts.V4), + ipscanner.WithUseIPv6(opts.V6), + ipscanner.WithMaxDesirableRTT(opts.MaxRTT), ipscanner.WithCidrList(warp.WarpPrefixes()), )