mirror of
https://github.com/fosrl/olm.git
synced 2026-02-17 18:36:40 +00:00
@@ -1,4 +1,4 @@
|
|||||||
package olm
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -44,6 +44,8 @@ type OlmConfig struct {
|
|||||||
|
|
||||||
// Source tracking (not in JSON)
|
// Source tracking (not in JSON)
|
||||||
sources map[string]string `json:"-"`
|
sources map[string]string `json:"-"`
|
||||||
|
|
||||||
|
Version string
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConfigSource tracks where each config value came from
|
// ConfigSource tracks where each config value came from
|
||||||
54
main.go
54
main.go
@@ -159,10 +159,54 @@ func main() {
|
|||||||
logger.Init()
|
logger.Init()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run in console mode
|
// Load configuration from file, env vars, and CLI args
|
||||||
runOlmMain(context.Background())
|
// Priority: CLI args > Env vars > Config file > Defaults
|
||||||
}
|
config, showVersion, showConfig, err := LoadConfig(os.Args[1:])
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to load configuration: %v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func runOlmMain(ctx context.Context) {
|
// Handle --show-config flag
|
||||||
olm.Run(ctx, os.Args[1:])
|
if showConfig {
|
||||||
|
config.ShowConfig()
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
olmVersion := "version_replaceme"
|
||||||
|
if showVersion {
|
||||||
|
fmt.Println("Olm version " + olmVersion)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
logger.Info("Olm version " + olmVersion)
|
||||||
|
|
||||||
|
config.Version = olmVersion
|
||||||
|
|
||||||
|
if err := SaveConfig(config); err != nil {
|
||||||
|
logger.Error("Failed to save full olm config: %v", err)
|
||||||
|
} else {
|
||||||
|
logger.Debug("Saved full olm config with all options")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new olm.Config struct and copy values from the main config
|
||||||
|
olmConfig := olm.Config{
|
||||||
|
Endpoint: config.Endpoint,
|
||||||
|
ID: config.ID,
|
||||||
|
Secret: config.Secret,
|
||||||
|
MTU: config.MTU,
|
||||||
|
DNS: config.DNS,
|
||||||
|
InterfaceName: config.InterfaceName,
|
||||||
|
LogLevel: config.LogLevel,
|
||||||
|
EnableHTTP: config.EnableHTTP,
|
||||||
|
HTTPAddr: config.HTTPAddr,
|
||||||
|
PingInterval: config.PingInterval,
|
||||||
|
PingTimeout: config.PingTimeout,
|
||||||
|
Holepunch: config.Holepunch,
|
||||||
|
TlsClientCert: config.TlsClientCert,
|
||||||
|
PingIntervalDuration: config.PingIntervalDuration,
|
||||||
|
PingTimeoutDuration: config.PingTimeoutDuration,
|
||||||
|
Version: config.Version,
|
||||||
|
}
|
||||||
|
|
||||||
|
olm.Run(context.Background(), olmConfig)
|
||||||
}
|
}
|
||||||
|
|||||||
116
olm/olm.go
116
olm/olm.go
@@ -6,10 +6,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fosrl/newt/logger"
|
"github.com/fosrl/newt/logger"
|
||||||
@@ -22,21 +20,43 @@ import (
|
|||||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Run(ctx context.Context, args []string) {
|
type Config struct {
|
||||||
// Load configuration from file, env vars, and CLI args
|
// Connection settings
|
||||||
// Priority: CLI args > Env vars > Config file > Defaults
|
Endpoint string
|
||||||
config, showVersion, showConfig, err := LoadConfig(args)
|
ID string
|
||||||
if err != nil {
|
Secret string
|
||||||
fmt.Printf("Failed to load configuration: %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle --show-config flag
|
// Network settings
|
||||||
if showConfig {
|
MTU int
|
||||||
config.ShowConfig()
|
DNS string
|
||||||
os.Exit(0)
|
InterfaceName string
|
||||||
}
|
|
||||||
|
|
||||||
|
// Logging
|
||||||
|
LogLevel string
|
||||||
|
|
||||||
|
// HTTP server
|
||||||
|
EnableHTTP bool
|
||||||
|
HTTPAddr string
|
||||||
|
|
||||||
|
// Ping settings
|
||||||
|
PingInterval string
|
||||||
|
PingTimeout string
|
||||||
|
|
||||||
|
// Advanced
|
||||||
|
Holepunch bool
|
||||||
|
TlsClientCert string
|
||||||
|
|
||||||
|
// Parsed values (not in JSON)
|
||||||
|
PingIntervalDuration time.Duration
|
||||||
|
PingTimeoutDuration time.Duration
|
||||||
|
|
||||||
|
// Source tracking (not in JSON)
|
||||||
|
sources map[string]string
|
||||||
|
|
||||||
|
Version string
|
||||||
|
}
|
||||||
|
|
||||||
|
func Run(ctx context.Context, config Config) {
|
||||||
// Extract commonly used values from config for convenience
|
// Extract commonly used values from config for convenience
|
||||||
var (
|
var (
|
||||||
endpoint = config.Endpoint
|
endpoint = config.Endpoint
|
||||||
@@ -52,6 +72,11 @@ func Run(ctx context.Context, args []string) {
|
|||||||
doHolepunch = config.Holepunch
|
doHolepunch = config.Holepunch
|
||||||
privateKey wgtypes.Key
|
privateKey wgtypes.Key
|
||||||
connected bool
|
connected bool
|
||||||
|
dev *device.Device
|
||||||
|
wgData WgData
|
||||||
|
holePunchData HolePunchData
|
||||||
|
uapiListener net.Listener
|
||||||
|
tdev tun.Device
|
||||||
)
|
)
|
||||||
|
|
||||||
stopHolepunch = make(chan struct{})
|
stopHolepunch = make(chan struct{})
|
||||||
@@ -60,14 +85,7 @@ func Run(ctx context.Context, args []string) {
|
|||||||
loggerLevel := parseLogLevel(logLevel)
|
loggerLevel := parseLogLevel(logLevel)
|
||||||
logger.GetLogger().SetLevel(parseLogLevel(logLevel))
|
logger.GetLogger().SetLevel(parseLogLevel(logLevel))
|
||||||
|
|
||||||
olmVersion := "version_replaceme"
|
if err := updates.CheckForUpdate("fosrl", "olm", config.Version); err != nil {
|
||||||
if showVersion {
|
|
||||||
fmt.Println("Olm version " + olmVersion)
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
logger.Info("Olm version " + olmVersion)
|
|
||||||
|
|
||||||
if err := updates.CheckForUpdate("fosrl", "olm", olmVersion); err != nil {
|
|
||||||
logger.Debug("Failed to check for updates: %v", err)
|
logger.Debug("Failed to check for updates: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,7 +101,7 @@ func Run(ctx context.Context, args []string) {
|
|||||||
var httpServer *httpserver.HTTPServer
|
var httpServer *httpserver.HTTPServer
|
||||||
if enableHTTP {
|
if enableHTTP {
|
||||||
httpServer = httpserver.NewHTTPServer(httpAddr)
|
httpServer = httpserver.NewHTTPServer(httpAddr)
|
||||||
httpServer.SetVersion(olmVersion)
|
httpServer.SetVersion(config.Version)
|
||||||
if err := httpServer.Start(); err != nil {
|
if err := httpServer.Start(); err != nil {
|
||||||
logger.Fatal("Failed to start HTTP server: %v", err)
|
logger.Fatal("Failed to start HTTP server: %v", err)
|
||||||
}
|
}
|
||||||
@@ -101,30 +119,6 @@ func Run(ctx context.Context, args []string) {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
// // Check if required parameters are missing and provide helpful guidance
|
|
||||||
// missingParams := []string{}
|
|
||||||
// if id == "" {
|
|
||||||
// missingParams = append(missingParams, "id (use -id flag or OLM_ID env var)")
|
|
||||||
// }
|
|
||||||
// if secret == "" {
|
|
||||||
// missingParams = append(missingParams, "secret (use -secret flag or OLM_SECRET env var)")
|
|
||||||
// }
|
|
||||||
// if endpoint == "" {
|
|
||||||
// missingParams = append(missingParams, "endpoint (use -endpoint flag or PANGOLIN_ENDPOINT env var)")
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if len(missingParams) > 0 {
|
|
||||||
// logger.Error("Missing required parameters: %v", missingParams)
|
|
||||||
// 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("Please provide them as command line flags or set as environment variables\n")
|
|
||||||
// if !enableHTTP {
|
|
||||||
// logger.Error("HTTP server is disabled, cannot receive parameters via API")
|
|
||||||
// fmt.Printf("HTTP server is disabled, cannot receive parameters via API\n")
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Create a new olm
|
// Create a new olm
|
||||||
olm, err := websocket.NewClient(
|
olm, err := websocket.NewClient(
|
||||||
"olm",
|
"olm",
|
||||||
@@ -169,13 +163,6 @@ func Run(ctx context.Context, args []string) {
|
|||||||
logger.Fatal("Failed to generate private key: %v", err)
|
logger.Fatal("Failed to generate private key: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create TUN device and network stack
|
|
||||||
var dev *device.Device
|
|
||||||
var wgData WgData
|
|
||||||
var holePunchData HolePunchData
|
|
||||||
var uapiListener net.Listener
|
|
||||||
var tdev tun.Device
|
|
||||||
|
|
||||||
sourcePort, err := FindAvailableUDPPort(49152, 65535)
|
sourcePort, err := FindAvailableUDPPort(49152, 65535)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error finding available port: %v\n", err)
|
fmt.Printf("Error finding available port: %v\n", err)
|
||||||
@@ -665,14 +652,6 @@ func Run(ctx context.Context, args []string) {
|
|||||||
httpServer.SetConnectionStatus(true)
|
httpServer.SetConnectionStatus(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CRITICAL: Save our full config AFTER websocket saves its limited config
|
|
||||||
// This ensures all 13 fields are preserved, not just the 4 that websocket saves
|
|
||||||
if err := SaveConfig(config); err != nil {
|
|
||||||
logger.Error("Failed to save full olm config: %v", err)
|
|
||||||
} else {
|
|
||||||
logger.Debug("Saved full olm config with all options")
|
|
||||||
}
|
|
||||||
|
|
||||||
if connected {
|
if connected {
|
||||||
logger.Debug("Already connected, skipping registration")
|
logger.Debug("Already connected, skipping registration")
|
||||||
return nil
|
return nil
|
||||||
@@ -685,7 +664,7 @@ func Run(ctx context.Context, args []string) {
|
|||||||
stopRegister = olm.SendMessageInterval("olm/wg/register", map[string]interface{}{
|
stopRegister = olm.SendMessageInterval("olm/wg/register", map[string]interface{}{
|
||||||
"publicKey": publicKey.String(),
|
"publicKey": publicKey.String(),
|
||||||
"relay": !doHolepunch,
|
"relay": !doHolepunch,
|
||||||
"olmVersion": olmVersion,
|
"olmVersion": config.Version,
|
||||||
}, 1*time.Second)
|
}, 1*time.Second)
|
||||||
|
|
||||||
go keepSendingPing(olm)
|
go keepSendingPing(olm)
|
||||||
@@ -704,13 +683,7 @@ func Run(ctx context.Context, args []string) {
|
|||||||
}
|
}
|
||||||
defer olm.Close()
|
defer olm.Close()
|
||||||
|
|
||||||
// Wait for interrupt signal or context cancellation
|
|
||||||
sigCh := make(chan os.Signal, 1)
|
|
||||||
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
|
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-sigCh:
|
|
||||||
logger.Info("Received interrupt signal")
|
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
logger.Info("Context cancelled")
|
logger.Info("Context cancelled")
|
||||||
}
|
}
|
||||||
@@ -740,7 +713,4 @@ func Run(ctx context.Context, args []string) {
|
|||||||
if dev != nil {
|
if dev != nil {
|
||||||
dev.Close()
|
dev.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Info("runOlmMain() exiting")
|
|
||||||
fmt.Printf("runOlmMain() exiting\n")
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user