mirror of
https://github.com/fosrl/newt.git
synced 2026-02-07 21:46:39 +00:00
Kind of working
This commit is contained in:
38
main.go
38
main.go
@@ -155,10 +155,27 @@ var (
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Check if we're running as a Windows service
|
||||
if isWindowsService() {
|
||||
runService("NewtWireguardService", false, os.Args[1:])
|
||||
return
|
||||
}
|
||||
|
||||
// Handle service management commands on Windows (install, remove, start, stop, etc.)
|
||||
if handleServiceCommand() {
|
||||
return
|
||||
}
|
||||
|
||||
// Prepare context for graceful shutdown and signal handling
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
// Run the main newt logic
|
||||
runNewtMain(ctx)
|
||||
}
|
||||
|
||||
// runNewtMain contains the main newt logic, extracted for service support
|
||||
func runNewtMain(ctx context.Context) {
|
||||
// if PANGOLIN_ENDPOINT, NEWT_ID, and NEWT_SECRET are set as environment variables, they will be used as default values
|
||||
endpoint = os.Getenv("PANGOLIN_ENDPOINT")
|
||||
id = os.Getenv("NEWT_ID")
|
||||
@@ -1462,10 +1479,8 @@ persistent_keepalive_interval=5`, util.FixKey(privateKey.String()), util.FixKey(
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for interrupt signal
|
||||
sigCh := make(chan os.Signal, 1)
|
||||
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-sigCh
|
||||
// Wait for context cancellation (from signal or service stop)
|
||||
<-ctx.Done()
|
||||
|
||||
// Close clients first (including WGTester)
|
||||
closeClients()
|
||||
@@ -1490,7 +1505,20 @@ persistent_keepalive_interval=5`, util.FixKey(privateKey.String()), util.FixKey(
|
||||
client.Close()
|
||||
}
|
||||
logger.Info("Exiting...")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// runNewtMainWithArgs is used by the Windows service to run newt with specific arguments
|
||||
// It sets os.Args and then calls runNewtMain
|
||||
func runNewtMainWithArgs(ctx context.Context, args []string) {
|
||||
// Set os.Args to include the program name plus the provided args
|
||||
// This allows flag parsing to work correctly
|
||||
os.Args = append([]string{os.Args[0]}, args...)
|
||||
|
||||
// Setup Windows logging if running as a service
|
||||
setupWindowsEventLog()
|
||||
|
||||
// Run the main newt logic
|
||||
runNewtMain(ctx)
|
||||
}
|
||||
|
||||
// validateTLSConfig validates the TLS configuration
|
||||
|
||||
Reference in New Issue
Block a user