Skip to content

Commit 2112448

Browse files
committed
misc: try to improve logging even further
Signed-off-by: Mark Pashmfouroush <mark@markpash.me>
1 parent e9d8c05 commit 2112448

File tree

11 files changed

+172
-347
lines changed

11 files changed

+172
-347
lines changed

app/app.go

Lines changed: 78 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"log"
7+
"log/slog"
88
"net"
99
"net/netip"
1010
"os"
@@ -20,7 +20,6 @@ const singleMTU = 1400
2020
const doubleMTU = 1320
2121

2222
type WarpOptions struct {
23-
LogLevel string
2423
Bind netip.AddrPort
2524
Endpoint string
2625
License string
@@ -37,7 +36,7 @@ type ScanOptions struct {
3736
MaxRTT time.Duration
3837
}
3938

40-
func RunWarp(ctx context.Context, opts WarpOptions) error {
39+
func RunWarp(ctx context.Context, l *slog.Logger, opts WarpOptions) error {
4140
if opts.Psiphon != nil && opts.Gool {
4241
return errors.New("can't use psiphon and gool at the same time")
4342
}
@@ -50,16 +49,16 @@ func RunWarp(ctx context.Context, opts WarpOptions) error {
5049
if err := makeDirs(); err != nil {
5150
return err
5251
}
53-
log.Println("'primary' and 'secondary' directories are ready")
52+
l.Debug("'primary' and 'secondary' directories are ready")
5453

5554
// Change the current working directory to 'stuff'
5655
if err := os.Chdir("stuff"); err != nil {
5756
return fmt.Errorf("error changing to 'stuff' directory: %w", err)
5857
}
59-
log.Println("Changed working directory to 'stuff'")
58+
l.Debug("Changed working directory to 'stuff'")
6059

6160
// create identities
62-
if err := createPrimaryAndSecondaryIdentities(opts.License); err != nil {
61+
if err := createPrimaryAndSecondaryIdentities(l.With("subsystem", "warp/account"), opts.License); err != nil {
6362
return err
6463
}
6564

@@ -72,112 +71,146 @@ func RunWarp(ctx context.Context, opts WarpOptions) error {
7271
return err
7372
}
7473

75-
log.Printf("scan results: %+v", res)
74+
l.Info("scan results", "endpoints", res)
7675

7776
endpoints = make([]string, len(res))
7877
for i := 0; i < len(res); i++ {
7978
endpoints[i] = res[i].AddrPort.String()
8079
}
8180
}
82-
log.Printf("using warp endpoints: %+v", endpoints)
81+
l.Info("using warp endpoints", "endpoints", endpoints)
8382

8483
var warpErr error
8584
switch {
8685
case opts.Psiphon != nil:
86+
l.Info("running in Psiphon (cfon) mode")
8787
// run primary warp on a random tcp port and run psiphon on bind address
88-
warpErr = runWarpWithPsiphon(ctx, opts.Bind, endpoints, opts.Psiphon.Country, opts.LogLevel == "debug")
88+
warpErr = runWarpWithPsiphon(ctx, l, opts.Bind, endpoints[0], opts.Psiphon.Country)
8989
case opts.Gool:
90+
l.Info("running in warp-in-warp (gool) mode")
9091
// run warp in warp
91-
warpErr = runWarpInWarp(ctx, opts.Bind, endpoints, opts.LogLevel == "debug")
92+
warpErr = runWarpInWarp(ctx, l, opts.Bind, endpoints)
9293
default:
94+
l.Info("running in normal warp mode")
9395
// just run primary warp on bindAddress
94-
_, warpErr = runWarp(ctx, opts.Bind, endpoints, "./primary/wgcf-profile.ini", opts.LogLevel == "debug", true, true, singleMTU)
96+
warpErr = runWarp(ctx, l, opts.Bind, endpoints[0])
9597
}
9698

9799
return warpErr
98100
}
99101

100-
func runWarp(ctx context.Context, bind netip.AddrPort, endpoints []string, confPath string, verbose, startProxy bool, trick bool, mtu int) (*wiresocks.VirtualTun, error) {
101-
conf, err := wiresocks.ParseConfig(confPath, endpoints[0])
102+
func runWarp(ctx context.Context, l *slog.Logger, bind netip.AddrPort, endpoint string) error {
103+
conf, err := wiresocks.ParseConfig("./primary/wgcf-profile.ini", endpoint)
102104
if err != nil {
103-
log.Println(err)
104-
return nil, err
105+
return err
105106
}
106-
conf.Interface.MTU = mtu
107+
conf.Interface.MTU = singleMTU
107108

108109
for i, peer := range conf.Peers {
109-
peer.KeepAlive = 10
110-
if trick {
111-
peer.Trick = true
112-
peer.KeepAlive = 3
113-
}
114-
110+
peer.Trick = true
111+
peer.KeepAlive = 3
115112
conf.Peers[i] = peer
116113
}
117114

118-
tnet, err := wiresocks.StartWireguard(ctx, conf, verbose)
115+
tnet, err := wiresocks.StartWireguard(ctx, l, conf)
119116
if err != nil {
120-
log.Println(err)
121-
return nil, err
117+
return err
122118
}
123119

124-
if startProxy {
125-
tnet.StartProxy(bind)
126-
}
120+
tnet.StartProxy(bind)
121+
l.Info("Serving proxy", "address", bind)
127122

128-
return tnet, nil
123+
return nil
129124
}
130125

131-
func runWarpWithPsiphon(ctx context.Context, bind netip.AddrPort, endpoints []string, country string, verbose bool) error {
126+
func runWarpWithPsiphon(ctx context.Context, l *slog.Logger, bind netip.AddrPort, endpoint string, country string) error {
132127
// make a random bind address for warp
133128
warpBindAddress, err := findFreePort("tcp")
134129
if err != nil {
135-
log.Println("There are no free tcp ports on Device!")
136130
return err
137131
}
138132

139-
_, err = runWarp(ctx, warpBindAddress, endpoints, "./primary/wgcf-profile.ini", verbose, true, true, singleMTU)
133+
conf, err := wiresocks.ParseConfig("./primary/wgcf-profile.ini", endpoint)
134+
if err != nil {
135+
return err
136+
}
137+
conf.Interface.MTU = singleMTU
138+
139+
for i, peer := range conf.Peers {
140+
peer.Trick = true
141+
peer.KeepAlive = 3
142+
conf.Peers[i] = peer
143+
}
144+
145+
tnet, err := wiresocks.StartWireguard(ctx, l, conf)
140146
if err != nil {
141147
return err
142148
}
143149

150+
tnet.StartProxy(warpBindAddress)
151+
144152
// run psiphon
145-
err = psiphon.RunPsiphon(warpBindAddress.String(), bind.String(), country, ctx)
153+
err = psiphon.RunPsiphon(ctx, l.With("subsystem", "psiphon"), warpBindAddress.String(), bind.String(), country)
146154
if err != nil {
147-
log.Printf("unable to run psiphon %v", err)
148155
return fmt.Errorf("unable to run psiphon %w", err)
149156
}
150157

151-
log.Printf("Serving on %s", bind)
158+
l.Info("Serving proxy", "address", bind)
152159

153160
return nil
154161
}
155162

156-
func runWarpInWarp(ctx context.Context, bind netip.AddrPort, endpoints []string, verbose bool) error {
163+
func runWarpInWarp(ctx context.Context, l *slog.Logger, bind netip.AddrPort, endpoints []string) error {
157164
// Run outer warp
158-
vTUN, err := runWarp(ctx, netip.AddrPort{}, endpoints, "./secondary/wgcf-profile.ini", verbose, false, true, singleMTU)
165+
conf, err := wiresocks.ParseConfig("./primary/wgcf-profile.ini", endpoints[0])
166+
if err != nil {
167+
return err
168+
}
169+
conf.Interface.MTU = singleMTU
170+
171+
for i, peer := range conf.Peers {
172+
peer.Trick = true
173+
peer.KeepAlive = 3
174+
conf.Peers[i] = peer
175+
}
176+
177+
tnet, err := wiresocks.StartWireguard(ctx, l.With("gool", "outer"), conf)
159178
if err != nil {
160179
return err
161180
}
162181

163182
// Run virtual endpoint
164183
virtualEndpointBindAddress, err := findFreePort("udp")
165184
if err != nil {
166-
log.Println("There are no free udp ports on Device!")
167185
return err
168186
}
169-
addr := endpoints[1]
170-
err = wiresocks.NewVtunUDPForwarder(virtualEndpointBindAddress.String(), addr, vTUN, singleMTU, ctx)
187+
188+
// Create a UDP port forward between localhost and the remote endpoint
189+
err = wiresocks.NewVtunUDPForwarder(ctx, virtualEndpointBindAddress.String(), endpoints[1], tnet, singleMTU)
171190
if err != nil {
172-
log.Println(err)
173191
return err
174192
}
175193

176194
// Run inner warp
177-
_, err = runWarp(ctx, bind, []string{virtualEndpointBindAddress.String()}, "./primary/wgcf-profile.ini", verbose, true, false, doubleMTU)
195+
conf, err = wiresocks.ParseConfig("./secondary/wgcf-profile.ini", virtualEndpointBindAddress.String())
196+
if err != nil {
197+
return err
198+
}
199+
conf.Interface.MTU = doubleMTU
200+
201+
for i, peer := range conf.Peers {
202+
peer.KeepAlive = 10
203+
conf.Peers[i] = peer
204+
}
205+
206+
tnet, err = wiresocks.StartWireguard(ctx, l.With("gool", "inner"), conf)
178207
if err != nil {
179208
return err
180209
}
210+
211+
tnet.StartProxy(bind)
212+
213+
l.Info("Serving proxy", "address", bind)
181214
return nil
182215
}
183216

@@ -207,22 +240,20 @@ func findFreePort(network string) (netip.AddrPort, error) {
207240
return netip.MustParseAddrPort(listener.Addr().String()), nil
208241
}
209242

210-
func createPrimaryAndSecondaryIdentities(license string) error {
243+
func createPrimaryAndSecondaryIdentities(l *slog.Logger, license string) error {
211244
// make primary identity
212245
warp.UpdatePath("./primary")
213246
if !warp.CheckProfileExists(license) {
214-
err := warp.LoadOrCreateIdentity(license)
247+
err := warp.LoadOrCreateIdentity(l, license)
215248
if err != nil {
216-
log.Printf("error: %v", err)
217249
return err
218250
}
219251
}
220252
// make secondary
221253
warp.UpdatePath("./secondary")
222254
if !warp.CheckProfileExists(license) {
223-
err := warp.LoadOrCreateIdentity(license)
255+
err := warp.LoadOrCreateIdentity(l, license)
224256
if err != nil {
225-
log.Printf("error: %v", err)
226257
return err
227258
}
228259
}

ipscanner/internal/iterator/iterator.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package iterator
33
import (
44
"crypto/rand"
55
"errors"
6-
"fmt"
7-
"log"
86
"math/big"
97
"net"
108
"net/netip"
@@ -262,17 +260,18 @@ func NewIterator(opts *statute.ScannerOptions) *IpGenerator {
262260

263261
ipRange, err := newIPRange(cidr)
264262
if err != nil {
265-
fmt.Printf("Error parsing CIDR %s: %v\n", cidr, err)
263+
// TODO
266264
continue
267265
}
268266
ranges = append(ranges, ipRange)
269267
}
270268
if len(ranges) == 0 {
271-
log.Fatal("No valid CIDR ranges found")
269+
// TODO
270+
return nil
272271
}
273272
err := shuffleSubnetsIpRange(ranges)
274273
if err != nil {
275-
fmt.Println(err)
274+
// TODO
276275
return nil
277276
}
278277
return &IpGenerator{

ipscanner/internal/statute/default.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package statute
33
import (
44
"context"
55
"crypto/tls"
6-
"fmt"
76
"net"
87
"net/http"
98
"net/netip"
@@ -72,7 +71,6 @@ func DefaultHTTPClientFunc(rawDialer TDialerFunc, tlsDialer TDialerFunc, quicDia
7271
}
7372

7473
func DefaultDialerFunc(ctx context.Context, network, addr string) (net.Conn, error) {
75-
fmt.Println(addr)
7674
d := &net.Dialer{
7775
Timeout: FinalOptions.ConnectionTimeout, // Connection timeout
7876
// Add other custom settings as needed

main.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"log"
7+
"log/slog"
88
"net/netip"
99
"os"
1010
"os/signal"
@@ -76,13 +76,19 @@ func main() {
7676
os.Exit(1)
7777
}
7878

79+
l := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
80+
81+
if *verbose {
82+
l = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
83+
}
84+
7985
if *psiphon && *gool {
80-
log.Fatal(errors.New("can't use cfon and gool at the same time"))
86+
fatal(l, errors.New("can't use cfon and gool at the same time"))
8187
}
8288

8389
bindAddrPort, err := netip.ParseAddrPort(*bind)
8490
if err != nil {
85-
log.Fatal(fmt.Errorf("invalid bind address: %w", err))
91+
fatal(l, fmt.Errorf("invalid bind address: %w", err))
8692
}
8793

8894
opts := app.WarpOptions{
@@ -92,36 +98,36 @@ func main() {
9298
Gool: *gool,
9399
}
94100

95-
if *verbose {
96-
opts.LogLevel = "debug"
97-
log.Printf("setting log level to: %s", opts.LogLevel)
98-
}
99-
100101
if *psiphon {
101-
log.Printf("psiphon mode enabled, using country %s", *country)
102+
l.Info("psiphon mode enabled", "country", *country)
102103
opts.Psiphon = &app.PsiphonOptions{Country: *country}
103104
}
104105

105106
if *scan {
106-
log.Printf("scanner mode enabled, using %s max RTT", rtt)
107+
l.Info("scanner mode enabled", "max-rtt", rtt)
107108
opts.Scan = &app.ScanOptions{MaxRTT: *rtt}
108109
}
109110

110111
// If the endpoint is not set, choose a random warp endpoint
111112
if opts.Endpoint == "" {
112113
addrPort, err := warp.RandomWarpEndpoint()
113114
if err != nil {
114-
log.Fatal(err)
115+
fatal(l, err)
115116
}
116117
opts.Endpoint = addrPort.String()
117118
}
118119

119120
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
120121
go func() {
121-
if err := app.RunWarp(ctx, opts); err != nil {
122-
log.Fatal(err)
122+
if err := app.RunWarp(ctx, l, opts); err != nil {
123+
fatal(l, err)
123124
}
124125
}()
125126

126127
<-ctx.Done()
127128
}
129+
130+
func fatal(l *slog.Logger, err error) {
131+
l.Error(err.Error())
132+
os.Exit(1)
133+
}

0 commit comments

Comments
 (0)