Skip to content

Commit e61a6de

Browse files
committed
warp: refactor identity creation
Signed-off-by: Mark Pashmfouroush <mark@markpash.me>
1 parent df4f9a6 commit e61a6de

File tree

3 files changed

+233
-495
lines changed

3 files changed

+233
-495
lines changed

app/app.go

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"fmt"
77
"log/slog"
88
"net/netip"
9-
"os"
10-
"path/filepath"
119

1210
"github.com/bepass-org/warp-plus/psiphon"
1311
"github.com/bepass-org/warp-plus/warp"
@@ -39,18 +37,6 @@ func RunWarp(ctx context.Context, l *slog.Logger, opts WarpOptions) error {
3937
return errors.New("must provide country for psiphon")
4038
}
4139

42-
// create necessary file structures
43-
if err := makeDirs(); err != nil {
44-
return err
45-
}
46-
l.Debug("'primary' and 'secondary' directories are ready")
47-
48-
// Change the current working directory to 'stuff'
49-
if err := os.Chdir("stuff"); err != nil {
50-
return fmt.Errorf("error changing to 'stuff' directory: %w", err)
51-
}
52-
l.Debug("Changed working directory to 'stuff'")
53-
5440
// create identities
5541
if err := createPrimaryAndSecondaryIdentities(l.With("subsystem", "warp/account"), opts.License); err != nil {
5642
return err
@@ -94,7 +80,7 @@ func RunWarp(ctx context.Context, l *slog.Logger, opts WarpOptions) error {
9480
}
9581

9682
func runWarp(ctx context.Context, l *slog.Logger, bind netip.AddrPort, endpoint string) error {
97-
conf, err := wiresocks.ParseConfig("./primary/wgcf-profile.ini", endpoint)
83+
conf, err := wiresocks.ParseConfig("./stuff/primary/wgcf-profile.ini", endpoint)
9884
if err != nil {
9985
return err
10086
}
@@ -122,7 +108,7 @@ func runWarp(ctx context.Context, l *slog.Logger, bind netip.AddrPort, endpoint
122108
}
123109

124110
func runWarpWithPsiphon(ctx context.Context, l *slog.Logger, bind netip.AddrPort, endpoint string, country string) error {
125-
conf, err := wiresocks.ParseConfig("./primary/wgcf-profile.ini", endpoint)
111+
conf, err := wiresocks.ParseConfig("./stuff/primary/wgcf-profile.ini", endpoint)
126112
if err != nil {
127113
return err
128114
}
@@ -157,7 +143,7 @@ func runWarpWithPsiphon(ctx context.Context, l *slog.Logger, bind netip.AddrPort
157143

158144
func runWarpInWarp(ctx context.Context, l *slog.Logger, bind netip.AddrPort, endpoints []string) error {
159145
// Run outer warp
160-
conf, err := wiresocks.ParseConfig("./primary/wgcf-profile.ini", endpoints[0])
146+
conf, err := wiresocks.ParseConfig("./stuff/primary/wgcf-profile.ini", endpoints[0])
161147
if err != nil {
162148
return err
163149
}
@@ -181,7 +167,7 @@ func runWarpInWarp(ctx context.Context, l *slog.Logger, bind netip.AddrPort, end
181167
}
182168

183169
// Run inner warp
184-
conf, err = wiresocks.ParseConfig("./secondary/wgcf-profile.ini", addr.String())
170+
conf, err = wiresocks.ParseConfig("./stuff/secondary/wgcf-profile.ini", addr.String())
185171
if err != nil {
186172
return err
187173
}
@@ -208,43 +194,17 @@ func runWarpInWarp(ctx context.Context, l *slog.Logger, bind netip.AddrPort, end
208194

209195
func createPrimaryAndSecondaryIdentities(l *slog.Logger, license string) error {
210196
// make primary identity
211-
warp.UpdatePath("./primary")
212-
if !warp.CheckProfileExists(license) {
213-
err := warp.LoadOrCreateIdentity(l, license)
214-
if err != nil {
215-
return err
216-
}
217-
}
218-
// make secondary
219-
warp.UpdatePath("./secondary")
220-
if !warp.CheckProfileExists(license) {
221-
err := warp.LoadOrCreateIdentity(l, license)
222-
if err != nil {
223-
return err
224-
}
225-
}
226-
return nil
227-
}
228-
229-
func makeDirs() error {
230-
stuffDir := "stuff"
231-
primaryDir := "primary"
232-
secondaryDir := "secondary"
233-
234-
// Check if 'stuff' directory exists, if not create it
235-
if _, err := os.Stat(stuffDir); os.IsNotExist(err) {
236-
if err := os.Mkdir(stuffDir, 0o755); err != nil {
237-
return fmt.Errorf("error creating 'stuff' directory: %w", err)
238-
}
197+
err := warp.LoadOrCreateIdentity(l, "./stuff/primary", license)
198+
if err != nil {
199+
l.Error("couldn't load primary warp identity")
200+
return err
239201
}
240202

241-
// Create 'primary' and 'secondary' directories if they don't exist
242-
for _, dir := range []string{primaryDir, secondaryDir} {
243-
if _, err := os.Stat(filepath.Join(stuffDir, dir)); os.IsNotExist(err) {
244-
if err := os.Mkdir(filepath.Join(stuffDir, dir), 0o755); err != nil {
245-
return fmt.Errorf("error creating '%s' directory: %w", dir, err)
246-
}
247-
}
203+
// make secondary
204+
err = warp.LoadOrCreateIdentity(l, "./stuff/secondary", license)
205+
if err != nil {
206+
l.Error("couldn't load secondary warp identity")
207+
return err
248208
}
249209

250210
return nil

0 commit comments

Comments
 (0)