mirror of
https://github.com/fosrl/olm.git
synced 2026-02-07 21:46:40 +00:00
168 lines
4.4 KiB
Go
168 lines
4.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"runtime"
|
|
|
|
"github.com/fosrl/newt/logger"
|
|
)
|
|
|
|
func main() {
|
|
// Check if we're running as a Windows service
|
|
if isWindowsService() {
|
|
runService("OlmWireguardService", false, os.Args[1:])
|
|
fmt.Println("Running as Windows service")
|
|
return
|
|
}
|
|
|
|
// Handle service management commands on Windows
|
|
if runtime.GOOS == "windows" {
|
|
var command string
|
|
if len(os.Args) > 1 {
|
|
command = os.Args[1]
|
|
} else {
|
|
command = "default"
|
|
}
|
|
|
|
switch command {
|
|
case "install":
|
|
err := installService()
|
|
if err != nil {
|
|
fmt.Printf("Failed to install service: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println("Service installed successfully")
|
|
return
|
|
case "remove", "uninstall":
|
|
err := removeService()
|
|
if err != nil {
|
|
fmt.Printf("Failed to remove service: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println("Service removed successfully")
|
|
return
|
|
case "start":
|
|
// Pass the remaining arguments (after "start") to the service
|
|
serviceArgs := os.Args[2:]
|
|
err := startService(serviceArgs)
|
|
if err != nil {
|
|
fmt.Printf("Failed to start service: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println("Service started successfully")
|
|
return
|
|
case "stop":
|
|
err := stopService()
|
|
if err != nil {
|
|
fmt.Printf("Failed to stop service: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println("Service stopped successfully")
|
|
return
|
|
case "status":
|
|
status, err := getServiceStatus()
|
|
if err != nil {
|
|
fmt.Printf("Failed to get service status: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Printf("Service status: %s\n", status)
|
|
return
|
|
case "debug":
|
|
// get the status and if it is Not Installed then install it first
|
|
status, err := getServiceStatus()
|
|
if err != nil {
|
|
fmt.Printf("Failed to get service status: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
if status == "Not Installed" {
|
|
err := installService()
|
|
if err != nil {
|
|
fmt.Printf("Failed to install service: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println("Service installed successfully, now running in debug mode")
|
|
}
|
|
|
|
// Pass the remaining arguments (after "debug") to the service
|
|
serviceArgs := os.Args[2:]
|
|
err = debugService(serviceArgs)
|
|
if err != nil {
|
|
fmt.Printf("Failed to debug service: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
return
|
|
case "logs":
|
|
err := watchLogFile(false)
|
|
if err != nil {
|
|
fmt.Printf("Failed to watch log file: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
return
|
|
case "config":
|
|
if runtime.GOOS == "windows" {
|
|
showServiceConfig()
|
|
} else {
|
|
fmt.Println("Service configuration is only available on Windows")
|
|
}
|
|
return
|
|
case "help", "--help", "-h":
|
|
fmt.Println("Olm WireGuard VPN Client")
|
|
fmt.Println("\nWindows Service Management:")
|
|
fmt.Println(" install Install the service")
|
|
fmt.Println(" remove Remove the service")
|
|
fmt.Println(" start [args] Start the service with optional arguments")
|
|
fmt.Println(" stop Stop the service")
|
|
fmt.Println(" status Show service status")
|
|
fmt.Println(" debug [args] Run service in debug mode with optional arguments")
|
|
fmt.Println(" logs Tail the service log file")
|
|
fmt.Println(" config Show current service configuration")
|
|
fmt.Println("\nExamples:")
|
|
fmt.Println(" olm start --enable-http --http-addr :9452")
|
|
fmt.Println(" olm debug --endpoint https://example.com --id myid --secret mysecret")
|
|
fmt.Println("\nFor console mode, run without arguments or with standard flags.")
|
|
return
|
|
default:
|
|
// get the status and if it is Not Installed then install it first
|
|
status, err := getServiceStatus()
|
|
if err != nil {
|
|
fmt.Printf("Failed to get service status: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
if status == "Not Installed" {
|
|
err := installService()
|
|
if err != nil {
|
|
fmt.Printf("Failed to install service: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println("Service installed successfully, now running")
|
|
}
|
|
|
|
// Pass the remaining arguments (after "debug") to the service
|
|
serviceArgs := os.Args[1:]
|
|
err = debugService(serviceArgs)
|
|
if err != nil {
|
|
fmt.Printf("Failed to debug service: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
return
|
|
}
|
|
}
|
|
|
|
// Setup Windows event logging if on Windows
|
|
if runtime.GOOS != "windows" {
|
|
setupWindowsEventLog()
|
|
} else {
|
|
// Initialize logger for non-Windows platforms
|
|
logger.Init()
|
|
}
|
|
|
|
// Run in console mode
|
|
runOlmMain(context.Background())
|
|
}
|
|
|
|
func runOlmMain(ctx context.Context) {
|
|
olm(ctx, os.Args[1:])
|
|
}
|