Skip to content

Commit

Permalink
misc: v6 fixes and 4,6 flags
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Pashmfouroush <[email protected]>
  • Loading branch information
markpash committed Mar 17, 2024
1 parent 9047ad1 commit 548440c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 36 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
Expand Down
9 changes: 2 additions & 7 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down
21 changes: 17 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
)
Expand All @@ -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))
Expand All @@ -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)
}
Expand Down
26 changes: 20 additions & 6 deletions warp/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
25 changes: 8 additions & 17 deletions wiresocks/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,20 @@ import (
"context"
"errors"
"fmt"
"net"
"net/netip"
"time"

"github.com/bepass-org/warp-plus/ipscanner"
"github.com/bepass-org/warp-plus/warp"
"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)
Expand All @@ -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()),
)

Expand Down

0 comments on commit 548440c

Please sign in to comment.