Skip to content
Open
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
91 changes: 87 additions & 4 deletions cmd/my-own-vpn/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
import (
"context"
"fmt"
"strings"

"fyne.io/fyne/v2"
"github.com/gonfva/my-own-vpn/internal/app"
"github.com/gonfva/my-own-vpn/internal/config"
"github.com/gonfva/my-own-vpn/internal/credentials"
"github.com/gonfva/my-own-vpn/internal/provider"
"github.com/gonfva/my-own-vpn/internal/provider/aws"
"github.com/gonfva/my-own-vpn/internal/provider/hetzner"
"github.com/gonfva/my-own-vpn/internal/ui"
"github.com/gonfva/my-own-vpn/internal/wireguard"
)

// Version is set during build
Expand All @@ -18,6 +24,7 @@
settingsWindow *ui.SettingsWindow
controller *app.Controller
appConfig *config.Config
credsMgr credentials.Manager
)

func main() {
Expand All @@ -31,8 +38,27 @@
appConfig = config.DefaultConfig()
}

// Create the controller
controller = app.NewController()
// Create credentials manager
credsMgr, err = credentials.NewManager()
if err != nil {
fmt.Printf("Failed to create credentials manager: %v\n", err)
return
}

// Create WireGuard client
wgClient, err := wireguard.NewClient()
if err != nil {
fmt.Printf("Failed to create WireGuard client: %v\n", err)
return
}

// Create the controller with dependencies
controller = app.NewController(app.ControllerDeps{
CredentialsManager: credsMgr,
WireGuardClient: wgClient,
ProviderFactory: createProvider,
})
controller.SetConfig(appConfig)
controller.SetOnStateChange(onControllerStateChange)
controller.SetOnError(onControllerError)

Expand Down Expand Up @@ -103,12 +129,69 @@
return
}

// TODO: Store credentials in credential manager
// TODO: Update controller/provider with new config
// Store credentials in credential manager
ctx := context.Background()
providerType := strings.ToLower(settingsConfig.Provider)
switch providerType {
case "aws":
if settingsConfig.AWSAccessKeyID != "" || settingsConfig.AWSSecretAccessKey != "" {

Check failure on line 137 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Vulnerability Scan

settingsConfig.AWSSecretAccessKey undefined (type ui.SettingsConfig has no field or method AWSSecretAccessKey)

Check failure on line 137 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Vulnerability Scan

settingsConfig.AWSAccessKeyID undefined (type ui.SettingsConfig has no field or method AWSAccessKeyID)

Check failure on line 137 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest, 1.24)

settingsConfig.AWSSecretAccessKey undefined (type ui.SettingsConfig has no field or method AWSSecretAccessKey)

Check failure on line 137 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest, 1.24)

settingsConfig.AWSAccessKeyID undefined (type ui.SettingsConfig has no field or method AWSAccessKeyID)

Check failure on line 137 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Lint

settingsConfig.AWSSecretAccessKey undefined (type ui.SettingsConfig has no field or method AWSSecretAccessKey)

Check failure on line 137 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Lint

settingsConfig.AWSAccessKeyID undefined (type ui.SettingsConfig has no field or method AWSAccessKeyID)

Check failure on line 137 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 1.24)

settingsConfig.AWSSecretAccessKey undefined (type ui.SettingsConfig has no field or method AWSSecretAccessKey)

Check failure on line 137 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 1.24)

settingsConfig.AWSAccessKeyID undefined (type ui.SettingsConfig has no field or method AWSAccessKeyID)

Check failure on line 137 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest, darwin, arm64, my-own-vpn-darwin-arm64)

settingsConfig.AWSSecretAccessKey undefined (type ui.SettingsConfig has no field or method AWSSecretAccessKey)

Check failure on line 137 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest, darwin, arm64, my-own-vpn-darwin-arm64)

settingsConfig.AWSAccessKeyID undefined (type ui.SettingsConfig has no field or method AWSAccessKeyID)

Check failure on line 137 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Test (windows-latest, 1.24)

settingsConfig.AWSSecretAccessKey undefined (type ui.SettingsConfig has no field or method AWSSecretAccessKey)

Check failure on line 137 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Test (windows-latest, 1.24)

settingsConfig.AWSAccessKeyID undefined (type ui.SettingsConfig has no field or method AWSAccessKeyID)
err := credsMgr.SaveAWS(ctx, credentials.AWSCredentials{
AccessKeyID: settingsConfig.AWSAccessKeyID,

Check failure on line 139 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Vulnerability Scan

settingsConfig.AWSAccessKeyID undefined (type ui.SettingsConfig has no field or method AWSAccessKeyID)

Check failure on line 139 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest, 1.24)

settingsConfig.AWSAccessKeyID undefined (type ui.SettingsConfig has no field or method AWSAccessKeyID)

Check failure on line 139 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Lint

settingsConfig.AWSAccessKeyID undefined (type ui.SettingsConfig has no field or method AWSAccessKeyID)

Check failure on line 139 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 1.24)

settingsConfig.AWSAccessKeyID undefined (type ui.SettingsConfig has no field or method AWSAccessKeyID)

Check failure on line 139 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest, darwin, arm64, my-own-vpn-darwin-arm64)

settingsConfig.AWSAccessKeyID undefined (type ui.SettingsConfig has no field or method AWSAccessKeyID)

Check failure on line 139 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Test (windows-latest, 1.24)

settingsConfig.AWSAccessKeyID undefined (type ui.SettingsConfig has no field or method AWSAccessKeyID)
SecretAccessKey: settingsConfig.AWSSecretAccessKey,

Check failure on line 140 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Vulnerability Scan

settingsConfig.AWSSecretAccessKey undefined (type ui.SettingsConfig has no field or method AWSSecretAccessKey)

Check failure on line 140 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest, 1.24)

settingsConfig.AWSSecretAccessKey undefined (type ui.SettingsConfig has no field or method AWSSecretAccessKey)

Check failure on line 140 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Lint

settingsConfig.AWSSecretAccessKey undefined (type ui.SettingsConfig has no field or method AWSSecretAccessKey)

Check failure on line 140 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 1.24)

settingsConfig.AWSSecretAccessKey undefined (type ui.SettingsConfig has no field or method AWSSecretAccessKey)

Check failure on line 140 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest, darwin, arm64, my-own-vpn-darwin-arm64)

settingsConfig.AWSSecretAccessKey undefined (type ui.SettingsConfig has no field or method AWSSecretAccessKey)

Check failure on line 140 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Test (windows-latest, 1.24)

settingsConfig.AWSSecretAccessKey undefined (type ui.SettingsConfig has no field or method AWSSecretAccessKey)
})
if err != nil {
fmt.Printf("Failed to save AWS credentials: %v\n", err)
tray.SetError(fmt.Sprintf("Failed to save credentials: %v", err))
return
}
}
case "hetzner":
if settingsConfig.HetznerAPIToken != "" {

Check failure on line 149 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Vulnerability Scan

settingsConfig.HetznerAPIToken undefined (type ui.SettingsConfig has no field or method HetznerAPIToken)

Check failure on line 149 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest, 1.24)

settingsConfig.HetznerAPIToken undefined (type ui.SettingsConfig has no field or method HetznerAPIToken)

Check failure on line 149 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Lint

settingsConfig.HetznerAPIToken undefined (type ui.SettingsConfig has no field or method HetznerAPIToken)

Check failure on line 149 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 1.24)

settingsConfig.HetznerAPIToken undefined (type ui.SettingsConfig has no field or method HetznerAPIToken)

Check failure on line 149 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest, darwin, arm64, my-own-vpn-darwin-arm64)

settingsConfig.HetznerAPIToken undefined (type ui.SettingsConfig has no field or method HetznerAPIToken)

Check failure on line 149 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Test (windows-latest, 1.24)

settingsConfig.HetznerAPIToken undefined (type ui.SettingsConfig has no field or method HetznerAPIToken)
err := credsMgr.SaveHetzner(ctx, credentials.HetznerCredentials{
APIToken: settingsConfig.HetznerAPIToken,

Check failure on line 151 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Vulnerability Scan

settingsConfig.HetznerAPIToken undefined (type ui.SettingsConfig has no field or method HetznerAPIToken)

Check failure on line 151 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest, 1.24)

settingsConfig.HetznerAPIToken undefined (type ui.SettingsConfig has no field or method HetznerAPIToken)

Check failure on line 151 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Lint

settingsConfig.HetznerAPIToken undefined (type ui.SettingsConfig has no field or method HetznerAPIToken) (typecheck)

Check failure on line 151 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 1.24)

settingsConfig.HetznerAPIToken undefined (type ui.SettingsConfig has no field or method HetznerAPIToken)

Check failure on line 151 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest, darwin, arm64, my-own-vpn-darwin-arm64)

settingsConfig.HetznerAPIToken undefined (type ui.SettingsConfig has no field or method HetznerAPIToken)

Check failure on line 151 in cmd/my-own-vpn/main.go

View workflow job for this annotation

GitHub Actions / Test (windows-latest, 1.24)

settingsConfig.HetznerAPIToken undefined (type ui.SettingsConfig has no field or method HetznerAPIToken)
})
if err != nil {
fmt.Printf("Failed to save Hetzner credentials: %v\n", err)
tray.SetError(fmt.Sprintf("Failed to save credentials: %v", err))
return
}
}
}

// Update controller config
controller.SetConfig(appConfig)

fmt.Println("Configuration saved successfully")
}

// createProvider creates a cloud provider based on the provider type and region.
// It loads the appropriate credentials from the credential manager.
func createProvider(ctx context.Context, providerType, region string) (provider.Provider, error) {
providerType = strings.ToLower(providerType)
switch providerType {
case "aws":
creds, err := credsMgr.LoadAWS(ctx)
if err != nil {
return nil, fmt.Errorf("failed to load AWS credentials: %w", err)
}
if creds == nil || creds.IsEmpty() {
return nil, fmt.Errorf("AWS credentials not configured")
}
return aws.New(ctx, creds.AccessKeyID, creds.SecretAccessKey, region)
case "hetzner":
creds, err := credsMgr.LoadHetzner(ctx)
if err != nil {
return nil, fmt.Errorf("failed to load Hetzner credentials: %w", err)
}
if creds == nil || creds.IsEmpty() {
return nil, fmt.Errorf("Hetzner credentials not configured")
}
return hetzner.New(creds.APIToken)
default:
return nil, fmt.Errorf("unknown provider type: %s", providerType)
}
}

func onQuit() {
fmt.Println("Quit clicked - shutting down")
// The Fyne app quit is handled by the tray's quit menu item callback
Expand Down
Loading
Loading