diff --git a/main.go b/main.go index f637cc0..f6c6973 100644 --- a/main.go +++ b/main.go @@ -177,7 +177,8 @@ func runOlmMainWithArgs(ctx context.Context, cancel context.CancelFunc, signalCt // Load configuration from file, env vars, and CLI args // Priority: CLI args > Env vars > Config file > Defaults - config, showVersion, showConfig, err := LoadConfig(os.Args[1:]) + // Use the passed args parameter instead of os.Args[1:] to support Windows service mode + config, showVersion, showConfig, err := LoadConfig(args) if err != nil { fmt.Printf("Failed to load configuration: %v\n", err) return diff --git a/service_windows.go b/service_windows.go index c103c46..48e79ce 100644 --- a/service_windows.go +++ b/service_windows.go @@ -99,15 +99,32 @@ func (s *olmService) Execute(args []string, r <-chan svc.ChangeRequest, changes // Continue with empty args if loading fails savedArgs = []string{} } + s.elog.Info(1, fmt.Sprintf("Loaded saved service args: %v", savedArgs)) // Combine service start args with saved args, giving priority to service start args + // Note: When the service is started via SCM, args[0] is the service name + // When started via s.Start(args...), the args passed are exactly what we provide finalArgs := []string{} + + // Check if we have args passed directly to Execute (from s.Start()) if len(args) > 0 { - // Skip the first arg which is typically the service name - if len(args) > 1 { + // The first arg from SCM is the service name, but when we call s.Start(args...), + // the args we pass become args[1:] in Execute. However, if started by SCM without + // args, args[0] will be the service name. + // We need to check if args[0] looks like the service name or a flag + if len(args) == 1 && args[0] == serviceName { + // Only service name, no actual args + s.elog.Info(1, "Only service name in args, checking saved args") + } else if len(args) > 1 && args[0] == serviceName { + // Service name followed by actual args finalArgs = append(finalArgs, args[1:]...) + s.elog.Info(1, fmt.Sprintf("Using service start parameters (after service name): %v", finalArgs)) + } else { + // Args don't start with service name, use them all + // This happens when args are passed via s.Start(args...) + finalArgs = append(finalArgs, args...) + s.elog.Info(1, fmt.Sprintf("Using service start parameters (direct): %v", finalArgs)) } - s.elog.Info(1, fmt.Sprintf("Using service start parameters: %v", finalArgs)) } // If no service start parameters, use saved args @@ -116,6 +133,7 @@ func (s *olmService) Execute(args []string, r <-chan svc.ChangeRequest, changes s.elog.Info(1, fmt.Sprintf("Using saved service args: %v", finalArgs)) } + s.elog.Info(1, fmt.Sprintf("Final args to use: %v", finalArgs)) s.args = finalArgs // Start the main olm functionality @@ -325,12 +343,15 @@ func removeService() error { } func startService(args []string) error { - // Save the service arguments as backup - if len(args) > 0 { - err := saveServiceArgs(args) - if err != nil { - return fmt.Errorf("failed to save service args: %v", err) - } + fmt.Printf("Starting service with args: %v\n", args) + + // Always save the service arguments so they can be loaded on service restart + err := saveServiceArgs(args) + if err != nil { + fmt.Printf("Warning: failed to save service args: %v\n", err) + // Continue anyway, args will still be passed directly + } else { + fmt.Printf("Saved service args to: %s\n", getServiceArgsPath()) } m, err := mgr.Connect() @@ -346,6 +367,7 @@ func startService(args []string) error { defer s.Close() // Pass arguments directly to the service start call + // Note: These args will appear in Execute() after the service name err = s.Start(args...) if err != nil { return fmt.Errorf("failed to start service: %v", err) diff --git a/websocket/client.go b/websocket/client.go index 54b659a..b9f5a63 100644 --- a/websocket/client.go +++ b/websocket/client.go @@ -348,6 +348,9 @@ func (c *Client) getToken() (string, []ExitNode, error) { req.Header.Set("Content-Type", "application/json") req.Header.Set("X-CSRF-Token", "x-csrf-protection") + // print out the request for debugging + logger.Debug("Requesting token from %s with body: %s", req.URL.String(), string(jsonData)) + // Make the request client := &http.Client{} if tlsConfig != nil {