Skip to content

Commit

Permalink
scanner: cleanups
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Pashmfouroush <[email protected]>
  • Loading branch information
markpash committed Mar 4, 2024
1 parent 551cb93 commit 6020d4d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 32 deletions.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func RunWarp(ctx context.Context, opts WarpOptions) error {

if opts.Scan != nil {
var err error
endpoints, err = wiresocks.RunScan(&ctx, opts.Scan.MaxRTT)
endpoints, err = wiresocks.RunScan(ctx, opts.Scan.MaxRTT)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions warp/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ func loadIdentity(identityPath string) (accountData *AccountData, err error) {
if err != nil {
fmt.Println("Error:", err)
}
return
}(file)

accountData = &AccountData{}
Expand Down Expand Up @@ -563,7 +562,7 @@ func LoadOrCreateIdentity(license string) error {

if confData.WarpPlusEnabled && !deviceStatus {
fmt.Println("Enabling device...")
deviceStatus, err = setDeviceActive(accountData, true)
deviceStatus, _ = setDeviceActive(accountData, true)
}

if !confData.WarpEnabled {
Expand Down
23 changes: 15 additions & 8 deletions warp/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ import (
"time"
)

func WarpPrefixes() []netip.Prefix {
return []netip.Prefix{
netip.MustParsePrefix("162.159.192.0/24"),
netip.MustParsePrefix("162.159.193.0/24"),
netip.MustParsePrefix("162.159.195.0/24"),
netip.MustParsePrefix("188.114.96.0/24"),
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"),
}
}

func RandomWarpEndpoint() (netip.AddrPort, error) {
ports := []int{500, 854, 859, 864, 878, 880, 890, 891, 894, 903, 908, 928, 934, 939, 942,
943, 945, 946, 955, 968, 987, 988, 1002, 1010, 1014, 1018, 1070, 1074, 1180, 1387, 1701,
Expand All @@ -18,14 +32,7 @@ func RandomWarpEndpoint() (netip.AddrPort, error) {
// Pick a random port number
randomPort := uint16(ports[rng.Intn(len(ports))])

cidrs := []netip.Prefix{
netip.MustParsePrefix("162.159.195.0/24"),
netip.MustParsePrefix("188.114.96.0/24"),
netip.MustParsePrefix("162.159.192.0/24"),
netip.MustParsePrefix("188.114.97.0/24"),
netip.MustParsePrefix("188.114.99.0/24"),
netip.MustParsePrefix("188.114.98.0/24"),
}
cidrs := WarpPrefixes()

randomIP, err := RandomIPFromPrefix(cidrs[rng.Intn(len(cidrs))])
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions warp/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
tls "github.com/refraction-networking/utls"
)

var previousIP string

// Dialer is a struct that holds various options for custom dialing.
type Dialer struct {
}
Expand Down
36 changes: 17 additions & 19 deletions wiresocks/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@ import (
"context"
"crypto/rand"
"fmt"
"log"
"net"
"net/netip"
"strings"
"time"

"github.com/bepass-org/ipscanner"
"github.com/bepass-org/wireguard-go/warp"
"github.com/go-ini/ini"
)

func canConnectIPv6(remoteAddr string) bool {
func canConnectIPv6(remoteAddr netip.AddrPort) bool {
dialer := net.Dialer{
Timeout: 5 * time.Second,
}

conn, err := dialer.Dial("tcp6", remoteAddr)
conn, err := dialer.Dial("tcp6", remoteAddr.String())
if err != nil {
return false
}
defer conn.Close()
return true
}

func RunScan(ctx *context.Context, rtt time.Duration) (result []string, err error) {
func RunScan(ctx context.Context, rtt time.Duration) (result []string, err error) {
cfg, err := ini.Load("./primary/wgcf-profile.ini")
if err != nil {
log.Printf("Failed to read file: %v", err)
return nil, fmt.Errorf("failed to read file: %v", err)
return nil, fmt.Errorf("failed to read file: %w", err)
}

// Reading the private key from the 'Interface' section
Expand All @@ -39,33 +39,31 @@ func RunScan(ctx *context.Context, rtt time.Duration) (result []string, err erro
// Reading the public key from the 'Peer' section
publicKey := cfg.Section("Peer").Key("PublicKey").String()

// TODO: ipscanner doesn't support netip.Prefix yet
prefixes := warp.WarpPrefixes()
stringedPrefixes := make([]string, len(prefixes))
for i, p := range prefixes {
stringedPrefixes[i] = p.String()
}

// new scanner
scanner := ipscanner.NewScanner(
ipscanner.WithWarpPing(),
ipscanner.WithWarpPrivateKey(privateKey),
ipscanner.WithWarpPeerPublicKey(publicKey),
ipscanner.WithUseIPv6(canConnectIPv6("[2001:4860:4860::8888]:80")),
ipscanner.WithUseIPv6(canConnectIPv6(netip.MustParseAddrPort("[2001:4860:4860::8888]:80"))),
ipscanner.WithUseIPv4(true),
ipscanner.WithMaxDesirableRTT(int(rtt.Milliseconds())),
ipscanner.WithCidrList([]string{
"162.159.192.0/24",
"162.159.193.0/24",
"162.159.195.0/24",
"188.114.96.0/24",
"188.114.97.0/24",
"188.114.98.0/24",
"188.114.99.0/24",
"2606:4700:d0::/48",
"2606:4700:d1::/48",
}),
ipscanner.WithCidrList(stringedPrefixes),
)

scanner.Run()
timeoutTimer := time.NewTimer(2 * time.Minute)
defer timeoutTimer.Stop()

for {
select {
case <-(*ctx).Done():
case <-ctx.Done():
// Context is done - canceled externally
scanner.Stop()
return nil, fmt.Errorf("user canceled the operation")
Expand Down

0 comments on commit 6020d4d

Please sign in to comment.