vibetunnel/mac/VibeTunnel/Core/Models/AppConstants.swift
Lachlan Donald 745f5090bb
feat: add Tailscale Serve integration with automatic authentication (#472)
* feat: add secure Tailscale Serve integration support

- Add --enable-tailscale-serve flag to bind server to localhost
- Implement Tailscale identity header authentication
- Add security validations for localhost origin and proxy headers
- Create TailscaleServeService to manage tailscale serve process
- Fix dev script to properly pass arguments through pnpm
- Add comprehensive auth middleware tests for all auth methods
- Ensure secure integration with Tailscale's reverse proxy

* refactor: use isFromLocalhostAddress helper for Tailscale auth

- Extract localhost checking logic into dedicated helper function
- Makes the code clearer and addresses review feedback
- Maintains the same security checks for Tailscale authentication

* feat(web): Add Tailscale Serve integration support

- Add TailscaleServeService to manage background tailscale serve process
- Add --enable-tailscale-serve and --use-tailscale-serve flags
- Force localhost binding when Tailscale Serve is enabled
- Enhance auth middleware to support Tailscale identity headers
- Add isFromLocalhostAddress helper for secure localhost validation
- Fix dev script to properly pass CLI arguments through pnpm
- Add comprehensive auth middleware tests (17 tests)
- Use 'tailscale serve reset' for thorough cleanup

The server now automatically manages the Tailscale Serve proxy process,
providing secure HTTPS access through Tailscale networks without manual
configuration.

* feat(mac): Add Tailscale Serve toggle in Remote Access settings

- Add 'Enable Tailscale Serve Integration' toggle in RemoteAccessSettingsView
- Pass --use-tailscale-serve flag from both BunServer and DevServerManager
- Show HTTPS URL when Tailscale Serve is enabled, HTTP when disabled
- Fix URL copy bug in ServerInfoSection for Tailscale addresses
- Update authentication documentation with new integration mode
- Server automatically restarts when toggle is changed

The macOS app now provides a user-friendly toggle to enable secure
Tailscale Serve integration without manual configuration.

* fix(security): Remove dangerous --allow-tailscale-auth flag

- Remove --allow-tailscale-auth flag that allowed header spoofing
- Remove --use-tailscale-serve alias for consistency
- Keep only --enable-tailscale-serve which safely manages everything
- Update all references in server.ts to use enableTailscaleServe
- Update macOS app to use --enable-tailscale-serve flag
- Update documentation to remove manual setup mode

The --allow-tailscale-auth flag was dangerous because it allowed users to
enable Tailscale header authentication while binding to network interfaces,
which would allow anyone on the network to spoof the Tailscale headers.

Now there's only one safe way to use Tailscale integration: --enable-tailscale-serve,
which forces localhost binding and manages the proxy automatically.

* fix: address PR feedback from Peter and Cursor

- Fix Promise hang bug in TailscaleServeService when process exits with code 0
- Move tailscaleServeEnabled string to AppConstants.UserDefaultsKeys
- Create TailscaleURLHelper for URL construction logic
- Add Linux support to TailscaleServeService with common Tailscale paths
- Update all references to use centralized constants
- Fix code formatting issues

* feat: Add Tailscale Serve status monitoring and error visibility

* fix: Correct pass-through argument logic for boolean flags and duplicates

- Track processed argument indices instead of checking if arg already exists in serverArgs
- Add set of known boolean flags that don't take values
- Allow duplicate arguments to be passed through
- Only treat non-dash arguments as values for non-boolean flags

This fixes issues where:
1. Boolean flags like --verbose were incorrectly consuming the next argument
2. Duplicate flags couldn't be passed through to the server

* fix: Resolve promise hanging and orphaned processes in Tailscale serve

- Add settled flag to prevent multiple promise resolutions
- Handle exit code 0 as a failure case during startup
- Properly terminate child process in cleanup method
- Add timeout for graceful shutdown before force killing

This fixes:
1. Promise hanging when tailscale serve exits with code 0
2. Orphaned processes when startup fails or cleanup is called

---------

Co-authored-by: Peter Steinberger <steipete@gmail.com>
2025-07-30 02:30:10 +02:00

195 lines
7.1 KiB
Swift

import Foundation
/// Central location for app-wide constants and configuration values.
///
/// Provides a single source of truth for application constants, UserDefaults keys,
/// and default values. This helps maintain consistency across the app and makes
/// configuration changes easier to manage.
enum AppConstants {
/// Current version of the welcome dialog
/// Increment this when significant changes require re-showing the welcome flow
static let currentWelcomeVersion = 5
/// UserDefaults keys
enum UserDefaultsKeys {
static let welcomeVersion = "welcomeVersion"
static let preventSleepWhenRunning = "preventSleepWhenRunning"
// Server Configuration
static let serverPort = "serverPort"
static let dashboardAccessMode = "dashboardAccessMode"
static let cleanupOnStartup = "cleanupOnStartup"
static let authenticationMode = "authenticationMode"
// Development Settings
static let debugMode = "debugMode"
static let useDevServer = "useDevServer"
static let devServerPath = "devServerPath"
static let logLevel = "logLevel"
// Application Preferences
static let preferredGitApp = "preferredGitApp"
static let preferredTerminal = "preferredTerminal"
static let showInDock = "showInDock"
static let updateChannel = "updateChannel"
/// Remote Access Settings
static let tailscaleServeEnabled = "tailscaleServeEnabled"
// New Session keys
static let newSessionCommand = "NewSession.command"
static let newSessionWorkingDirectory = "NewSession.workingDirectory"
static let newSessionSpawnWindow = "NewSession.spawnWindow"
static let newSessionTitleMode = "NewSession.titleMode"
/// Quick Start Commands
static let quickStartCommands = "quickStartCommands"
}
/// Raw string values for DashboardAccessMode
enum DashboardAccessModeRawValues {
static let localhost = "localhost"
static let network = "network"
}
/// Default values for UserDefaults
enum Defaults {
/// Sleep prevention is enabled by default for better user experience
static let preventSleepWhenRunning = true
// Server Configuration
static let serverPort = 4_020
static let dashboardAccessMode = DashboardAccessModeRawValues.network
static let cleanupOnStartup = true
static let authenticationMode = "os"
// Development Settings
static let debugMode = false
static let useDevServer = false
static let devServerPath = ""
static let logLevel = "info"
// Application Preferences
static let showInDock = false
static let updateChannel = "stable"
}
/// Helper to get boolean value with proper default
static func boolValue(for key: String) -> Bool {
// If the key doesn't exist in UserDefaults, return our default
if UserDefaults.standard.object(forKey: key) == nil {
switch key {
case UserDefaultsKeys.preventSleepWhenRunning:
return Defaults.preventSleepWhenRunning
case UserDefaultsKeys.cleanupOnStartup:
return Defaults.cleanupOnStartup
case UserDefaultsKeys.debugMode:
return Defaults.debugMode
case UserDefaultsKeys.useDevServer:
return Defaults.useDevServer
case UserDefaultsKeys.showInDock:
return Defaults.showInDock
default:
return false
}
}
return UserDefaults.standard.bool(forKey: key)
}
/// Helper to get string value with proper default
static func stringValue(for key: String) -> String {
// First check if we have a string value
if let value = UserDefaults.standard.string(forKey: key) {
return value
}
// If the key doesn't exist at all, return our default
if UserDefaults.standard.object(forKey: key) == nil {
switch key {
case UserDefaultsKeys.dashboardAccessMode:
return Defaults.dashboardAccessMode
case UserDefaultsKeys.authenticationMode:
return Defaults.authenticationMode
case UserDefaultsKeys.devServerPath:
return Defaults.devServerPath
case UserDefaultsKeys.logLevel:
return Defaults.logLevel
case UserDefaultsKeys.updateChannel:
return Defaults.updateChannel
default:
return ""
}
}
// Key exists but contains non-string value, return empty string
return ""
}
/// Helper to get integer value with proper default
static func intValue(for key: String) -> Int {
// If the key doesn't exist in UserDefaults, return our default
if UserDefaults.standard.object(forKey: key) == nil {
switch key {
case UserDefaultsKeys.serverPort:
return Defaults.serverPort
default:
return 0
}
}
return UserDefaults.standard.integer(forKey: key)
}
}
// MARK: - Convenience Methods
extension AppConstants {
/// Check if the app is in development mode (debug or dev server enabled)
static func isInDevelopmentMode() -> Bool {
let debug = DebugConfig.current()
let devServer = DevServerConfig.current()
return debug.debugMode || devServer.useDevServer
}
/// Get development status for UI display
static func getDevelopmentStatus() -> (debugMode: Bool, useDevServer: Bool) {
let debug = DebugConfig.current()
let devServer = DevServerConfig.current()
return (debug.debugMode, devServer.useDevServer)
}
/// Preference helpers
static func getPreferredGitApp() -> String? {
UserDefaults.standard.string(forKey: UserDefaultsKeys.preferredGitApp)
}
static func setPreferredGitApp(_ app: String?) {
if let app {
UserDefaults.standard.set(app, forKey: UserDefaultsKeys.preferredGitApp)
} else {
UserDefaults.standard.removeObject(forKey: UserDefaultsKeys.preferredGitApp)
}
}
static func getPreferredTerminal() -> String? {
UserDefaults.standard.string(forKey: UserDefaultsKeys.preferredTerminal)
}
static func setPreferredTerminal(_ terminal: String?) {
if let terminal {
UserDefaults.standard.set(terminal, forKey: UserDefaultsKeys.preferredTerminal)
} else {
UserDefaults.standard.removeObject(forKey: UserDefaultsKeys.preferredTerminal)
}
}
/// Get current dashboard access mode
static func getDashboardAccessMode() -> DashboardAccessMode {
let rawValue = stringValue(for: UserDefaultsKeys.dashboardAccessMode)
return DashboardAccessMode(rawValue: rawValue) ?? .network
}
/// Set dashboard access mode
static func setDashboardAccessMode(_ mode: DashboardAccessMode) {
UserDefaults.standard.set(mode.rawValue, forKey: UserDefaultsKeys.dashboardAccessMode)
}
}