Skip to content

Commit 69b0727

Browse files
committed
misc: replace stuff folder with better handling
This changes the default behaviour of the stuff folder to be placed in a warp-plus folder based on OS defaults for Cache Home. Default locations can be found here: https://pkg.go.dev/github.com/adrg/xdg#readme-default-locations Signed-off-by: Mark Pashmfouroush <mark@markpash.me>
1 parent 69977d9 commit 69b0727

File tree

6 files changed

+64
-40
lines changed

6 files changed

+64
-40
lines changed

app/app.go

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import (
66
"fmt"
77
"log/slog"
88
"net/netip"
9+
"path"
910

1011
"github.com/bepass-org/warp-plus/psiphon"
1112
"github.com/bepass-org/warp-plus/warp"
1213
"github.com/bepass-org/warp-plus/wiresocks"
14+
"github.com/go-ini/ini"
1315
)
1416

1517
const singleMTU = 1330
@@ -22,6 +24,7 @@ type WarpOptions struct {
2224
Psiphon *PsiphonOptions
2325
Gool bool
2426
Scan *wiresocks.ScanOptions
27+
CacheDir string
2528
}
2629

2730
type PsiphonOptions struct {
@@ -38,14 +41,25 @@ func RunWarp(ctx context.Context, l *slog.Logger, opts WarpOptions) error {
3841
}
3942

4043
// create identities
41-
if err := createPrimaryAndSecondaryIdentities(l.With("subsystem", "warp/account"), opts.License); err != nil {
44+
if err := createPrimaryAndSecondaryIdentities(l.With("subsystem", "warp/account"), opts); err != nil {
4245
return err
4346
}
4447

4548
// Decide Working Scenario
4649
endpoints := []string{opts.Endpoint, opts.Endpoint}
4750

4851
if opts.Scan != nil {
52+
cfg, err := ini.Load(path.Join(opts.CacheDir, "primary", "wgcf-profile.ini"))
53+
if err != nil {
54+
return fmt.Errorf("failed to read file: %w", err)
55+
}
56+
57+
// Reading the private key from the 'Interface' section
58+
opts.Scan.PrivateKey = cfg.Section("Interface").Key("PrivateKey").String()
59+
60+
// Reading the public key from the 'Peer' section
61+
opts.Scan.PublicKey = cfg.Section("Peer").Key("PublicKey").String()
62+
4963
res, err := wiresocks.RunScan(ctx, l, *opts.Scan)
5064
if err != nil {
5165
return err
@@ -65,22 +79,22 @@ func RunWarp(ctx context.Context, l *slog.Logger, opts WarpOptions) error {
6579
case opts.Psiphon != nil:
6680
l.Info("running in Psiphon (cfon) mode")
6781
// run primary warp on a random tcp port and run psiphon on bind address
68-
warpErr = runWarpWithPsiphon(ctx, l, opts.Bind, endpoints[0], opts.Psiphon.Country)
82+
warpErr = runWarpWithPsiphon(ctx, l, opts, endpoints[0])
6983
case opts.Gool:
7084
l.Info("running in warp-in-warp (gool) mode")
7185
// run warp in warp
72-
warpErr = runWarpInWarp(ctx, l, opts.Bind, endpoints)
86+
warpErr = runWarpInWarp(ctx, l, opts, endpoints)
7387
default:
7488
l.Info("running in normal warp mode")
7589
// just run primary warp on bindAddress
76-
warpErr = runWarp(ctx, l, opts.Bind, endpoints[0])
90+
warpErr = runWarp(ctx, l, opts, endpoints[0])
7791
}
7892

7993
return warpErr
8094
}
8195

82-
func runWarp(ctx context.Context, l *slog.Logger, bind netip.AddrPort, endpoint string) error {
83-
conf, err := wiresocks.ParseConfig("./stuff/primary/wgcf-profile.ini", endpoint)
96+
func runWarp(ctx context.Context, l *slog.Logger, opts WarpOptions, endpoint string) error {
97+
conf, err := wiresocks.ParseConfig(path.Join(opts.CacheDir, "primary", "wgcf-profile.ini"), endpoint)
8498
if err != nil {
8599
return err
86100
}
@@ -97,18 +111,18 @@ func runWarp(ctx context.Context, l *slog.Logger, bind netip.AddrPort, endpoint
97111
return err
98112
}
99113

100-
_, err = tnet.StartProxy(bind)
114+
_, err = tnet.StartProxy(opts.Bind)
101115
if err != nil {
102116
return err
103117
}
104118

105-
l.Info("serving proxy", "address", bind)
119+
l.Info("serving proxy", "address", opts.Bind)
106120

107121
return nil
108122
}
109123

110-
func runWarpWithPsiphon(ctx context.Context, l *slog.Logger, bind netip.AddrPort, endpoint string, country string) error {
111-
conf, err := wiresocks.ParseConfig("./stuff/primary/wgcf-profile.ini", endpoint)
124+
func runWarpWithPsiphon(ctx context.Context, l *slog.Logger, opts WarpOptions, endpoint string) error {
125+
conf, err := wiresocks.ParseConfig(path.Join(opts.CacheDir, "primary", "wgcf-profile.ini"), endpoint)
112126
if err != nil {
113127
return err
114128
}
@@ -131,19 +145,19 @@ func runWarpWithPsiphon(ctx context.Context, l *slog.Logger, bind netip.AddrPort
131145
}
132146

133147
// run psiphon
134-
err = psiphon.RunPsiphon(ctx, l.With("subsystem", "psiphon"), warpBind.String(), bind.String(), country)
148+
err = psiphon.RunPsiphon(ctx, l.With("subsystem", "psiphon"), warpBind.String(), opts.Bind.String(), opts.Psiphon.Country)
135149
if err != nil {
136150
return fmt.Errorf("unable to run psiphon %w", err)
137151
}
138152

139-
l.Info("serving proxy", "address", bind)
153+
l.Info("serving proxy", "address", opts.Bind)
140154

141155
return nil
142156
}
143157

144-
func runWarpInWarp(ctx context.Context, l *slog.Logger, bind netip.AddrPort, endpoints []string) error {
158+
func runWarpInWarp(ctx context.Context, l *slog.Logger, opts WarpOptions, endpoints []string) error {
145159
// Run outer warp
146-
conf, err := wiresocks.ParseConfig("./stuff/primary/wgcf-profile.ini", endpoints[0])
160+
conf, err := wiresocks.ParseConfig(path.Join(opts.CacheDir, "primary", "wgcf-profile.ini"), endpoints[0])
147161
if err != nil {
148162
return err
149163
}
@@ -167,7 +181,7 @@ func runWarpInWarp(ctx context.Context, l *slog.Logger, bind netip.AddrPort, end
167181
}
168182

169183
// Run inner warp
170-
conf, err = wiresocks.ParseConfig("./stuff/secondary/wgcf-profile.ini", addr.String())
184+
conf, err = wiresocks.ParseConfig(path.Join(opts.CacheDir, "secondary", "wgcf-profile.ini"), addr.String())
171185
if err != nil {
172186
return err
173187
}
@@ -183,25 +197,25 @@ func runWarpInWarp(ctx context.Context, l *slog.Logger, bind netip.AddrPort, end
183197
return err
184198
}
185199

186-
_, err = tnet.StartProxy(bind)
200+
_, err = tnet.StartProxy(opts.Bind)
187201
if err != nil {
188202
return err
189203
}
190204

191-
l.Info("serving proxy", "address", bind)
205+
l.Info("serving proxy", "address", opts.Bind)
192206
return nil
193207
}
194208

195-
func createPrimaryAndSecondaryIdentities(l *slog.Logger, license string) error {
209+
func createPrimaryAndSecondaryIdentities(l *slog.Logger, opts WarpOptions) error {
196210
// make primary identity
197-
err := warp.LoadOrCreateIdentity(l, "./stuff/primary", license)
211+
err := warp.LoadOrCreateIdentity(l, path.Join(opts.CacheDir, "primary"), opts.License)
198212
if err != nil {
199213
l.Error("couldn't load primary warp identity")
200214
return err
201215
}
202216

203217
// make secondary
204-
err = warp.LoadOrCreateIdentity(l, "./stuff/secondary", license)
218+
err = warp.LoadOrCreateIdentity(l, path.Join(opts.CacheDir, "secondary"), opts.License)
205219
if err != nil {
206220
l.Error("couldn't load secondary warp identity")
207221
return err

example_config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
"cfon": false,
88
"country": "DE",
99
"scan": true,
10-
"rtt": "1000ms"
10+
"rtt": "1000ms",
11+
"cache-dir": ""
1112
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ require (
3131
github.com/Psiphon-Labs/goptlib v0.0.0-20200406165125-c0e32a7a3464 // indirect
3232
github.com/Psiphon-Labs/psiphon-tls v0.0.0-20240305020009-09f917290799 // indirect
3333
github.com/Psiphon-Labs/quic-go v0.0.0-20240305203241-7c4a760d03cc // indirect
34+
github.com/adrg/xdg v0.4.0 // indirect
3435
github.com/andybalholm/brotli v1.0.5 // indirect
3536
github.com/armon/go-proxyproto v0.0.0-20180202201750-5b7edb60ff5f // indirect
3637
github.com/bifurcation/mint v0.0.0-20180306135233-198357931e61 // indirect

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ github.com/Psiphon-Labs/psiphon-tls v0.0.0-20240305020009-09f917290799 h1:dHFQz6
1616
github.com/Psiphon-Labs/psiphon-tls v0.0.0-20240305020009-09f917290799/go.mod h1:ECTyVpleBW9oR/iHi185js4Fs7YD5T8A6tujOUzltxs=
1717
github.com/Psiphon-Labs/quic-go v0.0.0-20240305203241-7c4a760d03cc h1:o9jpHz1Vuum0oasqBX4kKB8VQrR+VJzEJsBg6XAz5YU=
1818
github.com/Psiphon-Labs/quic-go v0.0.0-20240305203241-7c4a760d03cc/go.mod h1:1gvBCJ18gsMqvZXkPkq0u9/BQKvjNS5RFWwF5uLl2Ys=
19+
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
20+
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
1921
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
2022
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
2123
github.com/armon/go-proxyproto v0.0.0-20180202201750-5b7edb60ff5f h1:SaJ6yqg936TshyeFZqQE+N+9hYkIeL9AMr7S4voCl10=
@@ -243,6 +245,7 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
243245
golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
244246
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
245247
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
248+
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
246249
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
247250
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
248251
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

main.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import (
88
"net/netip"
99
"os"
1010
"os/signal"
11+
"path"
1112
"syscall"
1213
"time"
1314

1415
_ "net/http/pprof"
1516

17+
"github.com/adrg/xdg"
1618
"github.com/bepass-org/warp-plus/app"
1719
"github.com/bepass-org/warp-plus/warp"
1820
"github.com/bepass-org/warp-plus/wiresocks"
@@ -22,6 +24,8 @@ import (
2224
"github.com/peterbourgon/ff/v4/ffjson"
2325
)
2426

27+
const appName = "warp-plus"
28+
2529
var psiphonCountries = []string{
2630
"AT",
2731
"BE",
@@ -56,7 +60,7 @@ var psiphonCountries = []string{
5660
}
5761

5862
func main() {
59-
fs := ff.NewFlagSet("warp-plus")
63+
fs := ff.NewFlagSet(appName)
6064
var (
6165
v4 = fs.BoolShort('4', "only use IPv4 for random warp endpoint")
6266
v6 = fs.BoolShort('6', "only use IPv6 for random warp endpoint")
@@ -69,6 +73,7 @@ func main() {
6973
country = fs.StringEnumLong("country", fmt.Sprintf("psiphon country code (valid values: %s)", psiphonCountries), psiphonCountries...)
7074
scan = fs.BoolLong("scan", "enable warp scanning")
7175
rtt = fs.DurationLong("rtt", 1000*time.Millisecond, "scanner rtt limit")
76+
cacheDir = fs.StringLong("cache-dir", "", "directory to store generated profiles")
7277
_ = fs.String('c', "config", "", "path to config file")
7378
)
7479

@@ -117,6 +122,17 @@ func main() {
117122
Gool: *gool,
118123
}
119124

125+
switch {
126+
case *cacheDir != "":
127+
opts.CacheDir = *cacheDir
128+
case xdg.CacheHome != "":
129+
opts.CacheDir = path.Join(xdg.CacheHome, appName)
130+
case os.Getenv("HOME") != "":
131+
opts.CacheDir = path.Join(os.Getenv("HOME"), ".cache", appName)
132+
default:
133+
opts.CacheDir = "warp_plus_cache"
134+
}
135+
120136
if *psiphon {
121137
l.Info("psiphon mode enabled", "country", *country)
122138
opts.Psiphon = &app.PsiphonOptions{Country: *country}

wiresocks/scanner.go

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,28 @@ package wiresocks
33
import (
44
"context"
55
"errors"
6-
"fmt"
76
"log/slog"
87
"time"
98

109
"github.com/bepass-org/warp-plus/ipscanner"
1110
"github.com/bepass-org/warp-plus/warp"
12-
"github.com/go-ini/ini"
1311
)
1412

1513
type ScanOptions struct {
16-
V4 bool
17-
V6 bool
18-
MaxRTT time.Duration
14+
V4 bool
15+
V6 bool
16+
MaxRTT time.Duration
17+
PrivateKey string
18+
PublicKey string
1919
}
2020

2121
func RunScan(ctx context.Context, l *slog.Logger, opts ScanOptions) (result []ipscanner.IPInfo, err error) {
22-
cfg, err := ini.Load("./stuff/primary/wgcf-profile.ini")
23-
if err != nil {
24-
return nil, fmt.Errorf("failed to read file: %w", err)
25-
}
26-
27-
// Reading the private key from the 'Interface' section
28-
privateKey := cfg.Section("Interface").Key("PrivateKey").String()
29-
30-
// Reading the public key from the 'Peer' section
31-
publicKey := cfg.Section("Peer").Key("PublicKey").String()
32-
3322
// new scanner
3423
scanner := ipscanner.NewScanner(
3524
ipscanner.WithLogger(l.With(slog.String("subsystem", "scanner"))),
3625
ipscanner.WithWarpPing(),
37-
ipscanner.WithWarpPrivateKey(privateKey),
38-
ipscanner.WithWarpPeerPublicKey(publicKey),
26+
ipscanner.WithWarpPrivateKey(opts.PrivateKey),
27+
ipscanner.WithWarpPeerPublicKey(opts.PublicKey),
3928
ipscanner.WithUseIPv4(opts.V4),
4029
ipscanner.WithUseIPv6(opts.V6),
4130
ipscanner.WithMaxDesirableRTT(opts.MaxRTT),

0 commit comments

Comments
 (0)