Skip to content

Commit

Permalink
ipscanner: overhaul logging and ping code
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Pashmfouroush <[email protected]>
  • Loading branch information
markpash committed Mar 12, 2024
1 parent 0564b6c commit 8f534e2
Show file tree
Hide file tree
Showing 18 changed files with 370 additions and 370 deletions.
6 changes: 5 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,15 @@ func RunWarp(ctx context.Context, opts WarpOptions) error {
if err != nil {
return err
}

log.Printf("scan results: %+v", res)

endpoints = make([]string, len(res))
for i := 0; i < len(res); i++ {
endpoints[i] = res[i].String()
endpoints[i] = res[i].AddrPort.String()
}
}
log.Printf("using warp endpoints: %+v", endpoints)

var warpErr error
switch {
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ require (
github.com/MakeNowJust/heredoc/v2 v2.0.1
github.com/Psiphon-Labs/psiphon-tunnel-core v2.0.28+incompatible
github.com/bepass-org/proxy v0.0.0-20240201095508-c86216dd0aea
github.com/davecgh/go-spew v1.1.1
github.com/fatih/color v1.16.0
github.com/flynn/noise v1.1.0
github.com/go-ini/ini v1.67.0
github.com/hashicorp/golang-lru v1.0.2
github.com/peterbourgon/ff/v4 v4.0.0-alpha.4
github.com/quic-go/quic-go v0.40.1
github.com/refraction-networking/conjure v0.7.11-0.20240130155008-c8df96195ab2
github.com/refraction-networking/utls v1.3.3
github.com/rodaine/table v1.1.1
golang.org/x/crypto v0.19.0
golang.org/x/net v0.21.0
golang.org/x/sys v0.17.0
Expand Down Expand Up @@ -48,6 +49,8 @@ require (
github.com/juju/ratelimit v1.0.2 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/libp2p/go-reuseport v0.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/miekg/dns v1.1.44-0.20210804161652-ab67aa642300 // indirect
github.com/mroth/weightedrand v1.0.0 // indirect
github.com/onsi/ginkgo/v2 v2.9.5 // indirect
Expand Down
19 changes: 17 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ github.com/elazarl/goproxy v0.0.0-20200809112317-0581fc3aee2d h1:rtM8HsT3NG37YPj
github.com/elazarl/goproxy v0.0.0-20200809112317-0581fc3aee2d/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM=
github.com/elazarl/goproxy/ext v0.0.0-20200809112317-0581fc3aee2d h1:st1tmvy+4duoRj+RaeeJoECWCWM015fBtf/4aR+hhqk=
github.com/elazarl/goproxy/ext v0.0.0-20200809112317-0581fc3aee2d/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/florianl/go-nfqueue v1.1.1-0.20200829120558-a2f196e98ab0 h1:7ZJyJV4KiWBijCCzUPvVaqxsDxO36+KD0XKBdEN3I+8=
github.com/florianl/go-nfqueue v1.1.1-0.20200829120558-a2f196e98ab0/go.mod h1:2z3Tfqwv2ueuK6h563xUHRcCh1mv38wS9EjiWiesk84=
github.com/flynn/noise v1.1.0 h1:KjPQoQCEFdZDiP03phOvGi11+SVVhBG2wOWAorLsstg=
Expand All @@ -78,8 +80,8 @@ github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu
github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4=
github.com/google/btree v1.0.1/go.mod h1:xXMiIv4Fb/0kKde4SpL7qlzvu5cMJDRkFDxJfI9uaxA=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8=
github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo=
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd h1:1FjCyPC+syAzJ5/2S8fqdZK1R22vvA0J7JZKcuOIQ7Y=
Expand All @@ -105,6 +107,13 @@ github.com/libp2p/go-reuseport v0.4.0 h1:nR5KU7hD0WxXCJbmw7r2rhRYruNRl2koHw8fQsc
github.com/libp2p/go-reuseport v0.4.0/go.mod h1:ZtI03j/wO5hZVDFo2jKywN6bYKWLOy8Se6DrI2E1cLU=
github.com/marusama/semaphore v0.0.0-20171214154724-565ffd8e868a h1:6SRny9FLB1eWasPyDUqBQnMi9NhXU01XIlB0ao89YoI=
github.com/marusama/semaphore v0.0.0-20171214154724-565ffd8e868a/go.mod h1:TmeOqAKoDinfPfSohs14CO3VcEf7o+Bem6JiNe05yrQ=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mdlayher/netlink v1.4.2-0.20210930205308-a81a8c23d40a h1:yk5OmRew64lWdeNanQ3l0hDgUt1E8MfipPhh/GO9Tuw=
github.com/mdlayher/netlink v1.4.2-0.20210930205308-a81a8c23d40a/go.mod h1:qw8F9IVzxa0GpqhVAfOw8DNyo7ec/jxI6bPWPEg1MV4=
github.com/mdlayher/socket v0.0.0-20210624160740-9dbe287ded84 h1:L1jnQ6o+K3M574eez7eTxbsia6H1SfJaVpaXY33L37Q=
Expand Down Expand Up @@ -164,6 +173,10 @@ github.com/refraction-networking/obfs4 v0.1.2 h1:J842O4fGSkd2W8ogYj0KN6gqVVY+Cpq
github.com/refraction-networking/obfs4 v0.1.2/go.mod h1:wAl/+gWiLsrcykJA3nKJHx89f5/gXGM8UKvty7+mvbM=
github.com/refraction-networking/utls v1.3.3 h1:f/TBLX7KBciRyFH3bwupp+CE4fzoYKCirhdRcC490sw=
github.com/refraction-networking/utls v1.3.3/go.mod h1:DlecWW1LMlMJu+9qpzzQqdHDT/C2LAe03EdpLUz/RL8=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rodaine/table v1.1.1 h1:zBliy3b4Oj6JRmncse2Z85WmoQvDrXOYuy0JXCt8Qz8=
github.com/rodaine/table v1.1.1/go.mod h1:iqTRptjn+EVcrVBYtNMlJ2wrJZa3MpULUmcXFpfcziA=
github.com/ryanuber/go-glob v0.0.0-20170128012129-256dc444b735 h1:7YvPJVmEeFHR1Tj9sZEYsmarJEQfMVYpd/Vyy/A8dqE=
github.com/ryanuber/go-glob v0.0.0-20170128012129-256dc444b735/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
github.com/sergeyfrolov/bsbuffer v0.0.0-20180903213811-94e85abb8507 h1:ML7ZNtcln5UBo5Wv7RIv9Xg3Pr5VuRCWLFXEwda54Y4=
Expand Down Expand Up @@ -228,7 +241,9 @@ golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
14 changes: 11 additions & 3 deletions ipscanner/example/cfscanner/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package main

import "github.com/bepass-org/wireguard-go/ipscanner"
import (
"context"

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

func main() {
// new scanner
scanner := ipscanner.NewScanner(
ipscanner.WithHTTPPing(),
ipscanner.WithUseIPv6(true),
)
go scanner.Run()
select {}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go scanner.Run(ctx)

<-ctx.Done()
}
62 changes: 46 additions & 16 deletions ipscanner/example/warpscanner/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package main

import (
"fmt"
"context"
"log/slog"
"net"
"net/netip"
"os"
"time"

"github.com/bepass-org/wireguard-go/ipscanner"
"github.com/bepass-org/wireguard-go/ipscanner/internal/statute"
"github.com/bepass-org/wireguard-go/warp"
"github.com/fatih/color"
"github.com/rodaine/table"
)

var (
Expand All @@ -30,34 +35,59 @@ func canConnectIPv6(remoteAddr netip.AddrPort) bool {
return true
}

func RunScan(privKey, pubKey string) (result []netip.AddrPort) {
func RunScan(privKey, pubKey string) (result []statute.IPInfo) {
// new scanner
scanner := ipscanner.NewScanner(
ipscanner.WithLogger(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))),
ipscanner.WithWarpPing(),
ipscanner.WithWarpPrivateKey(privKey),
ipscanner.WithWarpPeerPublicKey(pubKey),
ipscanner.WithUseIPv6(canConnectIPv6(googlev6DNSAddr80)),
ipscanner.WithUseIPv4(true),
ipscanner.WithMaxDesirableRTT(500),
ipscanner.WithMaxDesirableRTT(500*time.Millisecond),
ipscanner.WithCidrList(warp.WarpPrefixes()),
)
scanner.Run()
var ipList []netip.Addr

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()

scanner.Run(ctx)

t := time.NewTicker(1 * time.Second)
defer t.Stop()

for {
ipList = scanner.GetAvailableIPs()
if len(ipList) > 2 {
scanner.Stop()
break
ipList := scanner.GetAvailableIPs()
if len(ipList) > 1 {
for i := 0; i < 2; i++ {
result = append(result, ipList[i])
}
return
}

select {
case <-ctx.Done():
// Context is done
return
case <-t.C:
// Prevent the loop from spinning too fast
continue
}
time.Sleep(1 * time.Second)
}
for i := 0; i < 2; i++ {
result = append(result, netip.AddrPortFrom(ipList[i], warp.RandomWarpPort()))
}
return
}

func main() {
fmt.Println(RunScan(privKey, pubKey))
time.Sleep(10 * time.Second)
result := RunScan(privKey, pubKey)

headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()

tbl := table.New("Address", "RTT (ping)", "Time")
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)

for _, info := range result {
tbl.AddRow(info.AddrPort, info.RTT, info.CreatedAt)
}

tbl.Print()
}
70 changes: 21 additions & 49 deletions ipscanner/internal/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package engine

import (
"context"
"fmt"
"log/slog"
"net/netip"
"strings"
"time"

"github.com/bepass-org/wireguard-go/ipscanner/internal/iterator"
Expand All @@ -13,92 +12,65 @@ import (
)

type Engine struct {
generator *iterator.IpGenerator
ipQueue *IPQueue
ctx context.Context
cancelFunc context.CancelFunc
ping func(netip.Addr) (int, error)
statute.Logger
generator *iterator.IpGenerator
ipQueue *IPQueue
ping func(netip.Addr) (statute.IPInfo, error)
log *slog.Logger
}

func NewScannerEngine(opts *statute.ScannerOptions, ctx ...context.Context) *Engine {
func NewScannerEngine(opts *statute.ScannerOptions) *Engine {
queue := NewIPQueue(opts)
var contextToUse context.Context
var cancel context.CancelFunc

if len(ctx) > 0 {
contextToUse = ctx[0]
} else {
contextToUse, cancel = context.WithCancel(context.Background())
}
p := ping.Ping{
Options: opts,
}
return &Engine{
ipQueue: queue,
ctx: contextToUse,
cancelFunc: cancel,
ping: p.DoPing,
generator: iterator.NewIterator(opts),
Logger: opts.Logger,
ipQueue: queue,
ping: p.DoPing,
generator: iterator.NewIterator(opts),
log: opts.Logger.With(slog.String("subsystem", "engine")),
}
}

func (e *Engine) GetAvailableIPs(desc bool) []netip.Addr {
func (e *Engine) GetAvailableIPs(desc bool) []statute.IPInfo {
if e.ipQueue != nil {
return e.ipQueue.AvailableIPs(desc)
}
return nil
}

func (e *Engine) Run() {
func (e *Engine) Run(ctx context.Context) {
for {
select {
case <-e.ctx.Done():
fmt.Println("Context Done!")
case <-ctx.Done():
return
case <-e.ipQueue.available:
e.Logger.Debug("New Scanning Round Started")
e.log.Debug("Started new scanning round")
batch, err := e.generator.NextBatch()
if err != nil {
e.Logger.Error("Error while generating IP: %v", err)
e.log.Error("Error while generating IP: %v", err)
// in case of disastrous error, to prevent resource draining wait for 2 seconds and try again
time.Sleep(2 * time.Second)
continue
}
for _, ip := range batch {
select {
case <-e.ctx.Done():
fmt.Println("Context Done!")
case <-ctx.Done():
return
default:
e.Logger.Debug("Pinging IP: %s", ip)
if rtt, err := e.ping(ip); err == nil {
ipInfo := statute.IPInfo{
IP: ip,
RTT: rtt,
CreatedAt: time.Now(),
}
e.Logger.Debug("IP: %s, RTT: %d", ip, rtt)
e.log.Debug("pinging IP", "addr", ip)
if ipInfo, err := e.ping(ip); err == nil {
e.log.Debug("ping success", "addr", ipInfo.AddrPort, "rtt", ipInfo.RTT)
e.ipQueue.Enqueue(ipInfo)
} else {
// if timeout error
if strings.Contains(err.Error(), ": i/o timeout") {
e.Logger.Debug("Timeout Error: %s", ip)
continue
}
e.Logger.Error("Error while pinging IP: %s, Error: %v", ip, err)
e.log.Error("ping error", "addr", ip, "error", err)
}
}
}
default:
e.Logger.Debug("Engine: call the expire function")
e.log.Debug("calling expire")
e.ipQueue.Expire()
time.Sleep(200 * time.Millisecond)
}
}
}

func (e *Engine) Cancel() {
e.cancelFunc()
}
Loading

0 comments on commit 8f534e2

Please sign in to comment.