Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warp: refactor identity creation #56

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 13 additions & 53 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"fmt"
"log/slog"
"net/netip"
"os"
"path/filepath"

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

// create necessary file structures
if err := makeDirs(); err != nil {
return err
}
l.Debug("'primary' and 'secondary' directories are ready")

// Change the current working directory to 'stuff'
if err := os.Chdir("stuff"); err != nil {
return fmt.Errorf("error changing to 'stuff' directory: %w", err)
}
l.Debug("Changed working directory to 'stuff'")

// create identities
if err := createPrimaryAndSecondaryIdentities(l.With("subsystem", "warp/account"), opts.License); err != nil {
return err
Expand Down Expand Up @@ -94,7 +80,7 @@ func RunWarp(ctx context.Context, l *slog.Logger, opts WarpOptions) error {
}

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

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

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

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

func createPrimaryAndSecondaryIdentities(l *slog.Logger, license string) error {
// make primary identity
warp.UpdatePath("./primary")
if !warp.CheckProfileExists(license) {
err := warp.LoadOrCreateIdentity(l, license)
if err != nil {
return err
}
}
// make secondary
warp.UpdatePath("./secondary")
if !warp.CheckProfileExists(license) {
err := warp.LoadOrCreateIdentity(l, license)
if err != nil {
return err
}
}
return nil
}

func makeDirs() error {
stuffDir := "stuff"
primaryDir := "primary"
secondaryDir := "secondary"

// Check if 'stuff' directory exists, if not create it
if _, err := os.Stat(stuffDir); os.IsNotExist(err) {
if err := os.Mkdir(stuffDir, 0o755); err != nil {
return fmt.Errorf("error creating 'stuff' directory: %w", err)
}
err := warp.LoadOrCreateIdentity(l, "./stuff/primary", license)
if err != nil {
l.Error("couldn't load primary warp identity")
return err
}

// Create 'primary' and 'secondary' directories if they don't exist
for _, dir := range []string{primaryDir, secondaryDir} {
if _, err := os.Stat(filepath.Join(stuffDir, dir)); os.IsNotExist(err) {
if err := os.Mkdir(filepath.Join(stuffDir, dir), 0o755); err != nil {
return fmt.Errorf("error creating '%s' directory: %w", dir, err)
}
}
// make secondary
err = warp.LoadOrCreateIdentity(l, "./stuff/secondary", license)
if err != nil {
l.Error("couldn't load secondary warp identity")
return err
}

return nil
Expand Down
Loading
Loading