Service is better?

Former-commit-id: 442098be0c
This commit is contained in:
Owen
2025-07-24 11:55:05 -07:00
parent 8d72e77d57
commit 6fb2b68e21
2 changed files with 85 additions and 72 deletions

117
main.go
View File

@@ -162,23 +162,6 @@ func runOlmMainWithArgs(ctx context.Context, args []string) {
pingIntervalStr := os.Getenv("PING_INTERVAL") pingIntervalStr := os.Getenv("PING_INTERVAL")
pingTimeoutStr := os.Getenv("PING_TIMEOUT") pingTimeoutStr := os.Getenv("PING_TIMEOUT")
// Debug: Print all environment variables we're checking
// fmt.Printf("Environment variables: PANGOLIN_ENDPOINT='%s', OLM_ID='%s', OLM_SECRET='%s'\n", endpoint, id, secret)
// Setup flags for service mode
// serviceFlags.StringVar(&endpoint, "endpoint", endpoint, "Endpoint of your Pangolin server")
// serviceFlags.StringVar(&id, "id", id, "Olm ID")
// serviceFlags.StringVar(&secret, "secret", secret, "Olm secret")
// serviceFlags.StringVar(&mtu, "mtu", "1280", "MTU to use")
// serviceFlags.StringVar(&dns, "dns", "8.8.8.8", "DNS server to use")
// serviceFlags.StringVar(&logLevel, "log-level", "INFO", "Log level (DEBUG, INFO, WARN, ERROR, FATAL)")
// serviceFlags.StringVar(&interfaceName, "interface", "olm", "Name of the WireGuard interface")
// serviceFlags.StringVar(&httpAddr, "http-addr", ":9452", "HTTP server address (e.g., ':9452')")
// serviceFlags.StringVar(&pingIntervalStr, "ping-interval", "3s", "Interval for pinging the server (default 3s)")
// serviceFlags.StringVar(&pingTimeoutStr, "ping-timeout", "5s", "Timeout for each ping (default 5s)")
// serviceFlags.BoolVar(&enableHTTP, "http", false, "Enable HTTP server")
// serviceFlags.BoolVar(&testMode, "test", false, "Test WireGuard connectivity to a target")
// serviceFlags.StringVar(&testTarget, "test-target", "", "Target server:port for test mode")
if endpoint == "" { if endpoint == "" {
serviceFlags.StringVar(&endpoint, "endpoint", "", "Endpoint of your Pangolin server") serviceFlags.StringVar(&endpoint, "endpoint", "", "Endpoint of your Pangolin server")
} }
@@ -251,9 +234,9 @@ func runOlmMainWithArgs(ctx context.Context, args []string) {
logger.GetLogger().SetLevel(parseLogLevel(logLevel)) logger.GetLogger().SetLevel(parseLogLevel(logLevel))
// Log startup information // Log startup information
logger.Info("Olm service starting...") logger.Debug("Olm service starting...")
logger.Info("Parameters: endpoint='%s', id='%s', secret='%s'", endpoint, id, secret) logger.Debug("Parameters: endpoint='%s', id='%s', secret='%s'", endpoint, id, secret)
logger.Info("HTTP enabled: %v, HTTP addr: %s", enableHTTP, httpAddr) logger.Debug("HTTP enabled: %v, HTTP addr: %s", enableHTTP, httpAddr)
// Handle test mode // Handle test mode
if testMode { if testMode {
@@ -304,55 +287,55 @@ func runOlmMainWithArgs(ctx context.Context, args []string) {
}() }()
} }
// Check if required parameters are missing and provide helpful guidance // // Check if required parameters are missing and provide helpful guidance
missingParams := []string{} // missingParams := []string{}
if id == "" { // if id == "" {
missingParams = append(missingParams, "id (use -id flag or OLM_ID env var)") // missingParams = append(missingParams, "id (use -id flag or OLM_ID env var)")
} // }
if secret == "" { // if secret == "" {
missingParams = append(missingParams, "secret (use -secret flag or OLM_SECRET env var)") // missingParams = append(missingParams, "secret (use -secret flag or OLM_SECRET env var)")
} // }
if endpoint == "" { // if endpoint == "" {
missingParams = append(missingParams, "endpoint (use -endpoint flag or PANGOLIN_ENDPOINT env var)") // missingParams = append(missingParams, "endpoint (use -endpoint flag or PANGOLIN_ENDPOINT env var)")
} // }
if len(missingParams) > 0 { // if len(missingParams) > 0 {
logger.Error("Missing required parameters: %v", missingParams) // logger.Error("Missing required parameters: %v", missingParams)
logger.Error("Either provide them as command line flags or set as environment variables") // logger.Error("Either provide them as command line flags or set as environment variables")
fmt.Printf("ERROR: Missing required parameters: %v\n", missingParams) // fmt.Printf("ERROR: Missing required parameters: %v\n", missingParams)
fmt.Printf("Please provide them as command line flags or set as environment variables\n") // fmt.Printf("Please provide them as command line flags or set as environment variables\n")
if !enableHTTP { // if !enableHTTP {
logger.Error("HTTP server is disabled, cannot receive parameters via API") // logger.Error("HTTP server is disabled, cannot receive parameters via API")
fmt.Printf("HTTP server is disabled, cannot receive parameters via API\n") // fmt.Printf("HTTP server is disabled, cannot receive parameters via API\n")
return // return
} // }
} // }
// wait until we have a client id and secret and endpoint // // wait until we have a client id and secret and endpoint
waitCount := 0 // waitCount := 0
for id == "" || secret == "" || endpoint == "" { // for id == "" || secret == "" || endpoint == "" {
select { // select {
case <-ctx.Done(): // case <-ctx.Done():
logger.Info("Context cancelled while waiting for credentials") // logger.Info("Context cancelled while waiting for credentials")
return // return
default: // default:
missing := []string{} // missing := []string{}
if id == "" { // if id == "" {
missing = append(missing, "id") // missing = append(missing, "id")
} // }
if secret == "" { // if secret == "" {
missing = append(missing, "secret") // missing = append(missing, "secret")
} // }
if endpoint == "" { // if endpoint == "" {
missing = append(missing, "endpoint") // missing = append(missing, "endpoint")
} // }
waitCount++ // waitCount++
if waitCount%10 == 1 { // Log every 10 seconds instead of every second // if waitCount%10 == 1 { // Log every 10 seconds instead of every second
logger.Debug("Waiting for missing parameters: %v (waiting %d seconds)", missing, waitCount) // logger.Debug("Waiting for missing parameters: %v (waiting %d seconds)", missing, waitCount)
} // }
time.Sleep(1 * time.Second) // time.Sleep(1 * time.Second)
} // }
} // }
// parse the mtu string into an int // parse the mtu string into an int
mtuInt, err = strconv.Atoi(mtu) mtuInt, err = strconv.Atoi(mtu)

View File

@@ -69,6 +69,12 @@ func loadServiceArgs() ([]string, error) {
return nil, fmt.Errorf("failed to read service args: %v", err) return nil, fmt.Errorf("failed to read service args: %v", err)
} }
// delete the file after reading
err = os.Remove(argsPath)
if err != nil {
return nil, fmt.Errorf("failed to delete service args file: %v", err)
}
var args []string var args []string
err = json.Unmarshal(data, &args) err = json.Unmarshal(data, &args)
if err != nil { if err != nil {
@@ -228,7 +234,7 @@ func installService() error {
config := mgr.Config{ config := mgr.Config{
ServiceType: 0x10, // SERVICE_WIN32_OWN_PROCESS ServiceType: 0x10, // SERVICE_WIN32_OWN_PROCESS
StartType: mgr.StartAutomatic, StartType: mgr.StartManual,
ErrorControl: mgr.ErrorNormal, ErrorControl: mgr.ErrorNormal,
DisplayName: serviceDisplayName, DisplayName: serviceDisplayName,
Description: serviceDescription, Description: serviceDescription,
@@ -391,10 +397,28 @@ func debugService(args []string) error {
func watchLogFile(end bool) error { func watchLogFile(end bool) error {
logDir := filepath.Join(os.Getenv("PROGRAMDATA"), "Olm", "logs") logDir := filepath.Join(os.Getenv("PROGRAMDATA"), "Olm", "logs")
logPath := filepath.Join(logDir, "olm.log") logPath := filepath.Join(logDir, "olm.log")
// Open the log file
file, err := os.Open(logPath) // Ensure the log directory exists
err := os.MkdirAll(logDir, 0755)
if err != nil { if err != nil {
return fmt.Errorf("failed to open log file: %v", err) return fmt.Errorf("failed to create log directory: %v", err)
}
// Wait for the log file to be created if it doesn't exist
var file *os.File
for i := 0; i < 30; i++ { // Wait up to 15 seconds
file, err = os.Open(logPath)
if err == nil {
break
}
if i == 0 {
fmt.Printf("Waiting for log file to be created...\n")
}
time.Sleep(500 * time.Millisecond)
}
if err != nil {
return fmt.Errorf("failed to open log file after waiting: %v", err)
} }
defer file.Close() defer file.Close()
@@ -430,7 +454,13 @@ func watchLogFile(end bool) error {
// Read new content // Read new content
n, err := file.Read(buffer) n, err := file.Read(buffer)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
return fmt.Errorf("error reading log file: %v", err) // Try to reopen the file in case it was recreated
file.Close()
file, err = os.Open(logPath)
if err != nil {
return fmt.Errorf("error reopening log file: %v", err)
}
continue
} }
if n > 0 { if n > 0 {