mirror of
https://github.com/fosrl/olm.git
synced 2026-02-27 15:26:45 +00:00
18
common.go
18
common.go
@@ -13,8 +13,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/fosrl/newt/logger"
|
||||||
"github.com/fosrl/newt/websocket"
|
"github.com/fosrl/newt/websocket"
|
||||||
"github.com/fosrl/olm/logger"
|
|
||||||
"github.com/fosrl/olm/peermonitor"
|
"github.com/fosrl/olm/peermonitor"
|
||||||
"github.com/vishvananda/netlink"
|
"github.com/vishvananda/netlink"
|
||||||
"golang.org/x/crypto/chacha20poly1305"
|
"golang.org/x/crypto/chacha20poly1305"
|
||||||
@@ -69,6 +69,7 @@ var (
|
|||||||
stopPing chan struct{}
|
stopPing chan struct{}
|
||||||
olmToken string
|
olmToken string
|
||||||
gerbilServerPubKey string
|
gerbilServerPubKey string
|
||||||
|
holePunchRunning bool
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -316,6 +317,19 @@ func encryptPayload(payload []byte, serverPublicKey string) (interface{}, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func keepSendingUDPHolePunch(endpoint string, olmID string, sourcePort uint16) {
|
func keepSendingUDPHolePunch(endpoint string, olmID string, sourcePort uint16) {
|
||||||
|
// Check if hole punching is already running
|
||||||
|
if holePunchRunning {
|
||||||
|
logger.Debug("UDP hole punch already running, skipping new request")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the flag to indicate hole punching is running
|
||||||
|
holePunchRunning = true
|
||||||
|
defer func() {
|
||||||
|
holePunchRunning = false
|
||||||
|
logger.Info("UDP hole punch goroutine ended")
|
||||||
|
}()
|
||||||
|
|
||||||
host, err := resolveDomain(endpoint)
|
host, err := resolveDomain(endpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Failed to resolve endpoint: %v", err)
|
logger.Error("Failed to resolve endpoint: %v", err)
|
||||||
@@ -597,7 +611,7 @@ func configureWindows(interfaceName string, ip net.IP, ipNet *net.IPNet) error {
|
|||||||
// Bring up the interface if needed (in Windows, setting the IP usually brings it up)
|
// Bring up the interface if needed (in Windows, setting the IP usually brings it up)
|
||||||
// But we'll explicitly enable it to be sure
|
// But we'll explicitly enable it to be sure
|
||||||
cmd = exec.Command("netsh", "interface", "set", "interface",
|
cmd = exec.Command("netsh", "interface", "set", "interface",
|
||||||
fmt.Sprintf("%s", interfaceName),
|
interfaceName,
|
||||||
"admin=enable")
|
"admin=enable")
|
||||||
|
|
||||||
logger.Info("Running command: %v", cmd)
|
logger.Info("Running command: %v", cmd)
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -23,7 +23,7 @@ require (
|
|||||||
github.com/docker/go-connections v0.5.0 // indirect
|
github.com/docker/go-connections v0.5.0 // indirect
|
||||||
github.com/docker/go-units v0.5.0 // indirect
|
github.com/docker/go-units v0.5.0 // indirect
|
||||||
github.com/felixge/httpsnoop v1.0.4 // indirect
|
github.com/felixge/httpsnoop v1.0.4 // indirect
|
||||||
github.com/fosrl/newt v0.0.0-20250718235538-510e78437ca4 // indirect
|
github.com/fosrl/newt v0.0.0-20250724190153-64c22a94a47a // indirect
|
||||||
github.com/go-logr/logr v1.4.3 // indirect
|
github.com/go-logr/logr v1.4.3 // indirect
|
||||||
github.com/go-logr/stdr v1.2.2 // indirect
|
github.com/go-logr/stdr v1.2.2 // indirect
|
||||||
github.com/gogo/protobuf v1.3.2 // indirect
|
github.com/gogo/protobuf v1.3.2 // indirect
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -18,6 +18,8 @@ github.com/fosrl/newt v0.0.0-20250717220102-cd86e6b6de83 h1:jI6tP2sJNNb70Y+Ixq+o
|
|||||||
github.com/fosrl/newt v0.0.0-20250717220102-cd86e6b6de83/go.mod h1:oqHsCx1rsEc8hAVGVXemfeolIwlr19biJSQiLYi7Mvo=
|
github.com/fosrl/newt v0.0.0-20250717220102-cd86e6b6de83/go.mod h1:oqHsCx1rsEc8hAVGVXemfeolIwlr19biJSQiLYi7Mvo=
|
||||||
github.com/fosrl/newt v0.0.0-20250718235538-510e78437ca4 h1:bK/MQyTOLGthrXZ7ExvOCdW0EH0o9b5vwk/+UKnNdg0=
|
github.com/fosrl/newt v0.0.0-20250718235538-510e78437ca4 h1:bK/MQyTOLGthrXZ7ExvOCdW0EH0o9b5vwk/+UKnNdg0=
|
||||||
github.com/fosrl/newt v0.0.0-20250718235538-510e78437ca4/go.mod h1:oqHsCx1rsEc8hAVGVXemfeolIwlr19biJSQiLYi7Mvo=
|
github.com/fosrl/newt v0.0.0-20250718235538-510e78437ca4/go.mod h1:oqHsCx1rsEc8hAVGVXemfeolIwlr19biJSQiLYi7Mvo=
|
||||||
|
github.com/fosrl/newt v0.0.0-20250724190153-64c22a94a47a h1:Jgd60yfFJxb5z6L3LcoraaosHjiRgKLnMz6T3mv3D4Q=
|
||||||
|
github.com/fosrl/newt v0.0.0-20250724190153-64c22a94a47a/go.mod h1:oqHsCx1rsEc8hAVGVXemfeolIwlr19biJSQiLYi7Mvo=
|
||||||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||||
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
|
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
|
||||||
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
package logger
|
|
||||||
|
|
||||||
type LogLevel int
|
|
||||||
|
|
||||||
const (
|
|
||||||
DEBUG LogLevel = iota
|
|
||||||
INFO
|
|
||||||
WARN
|
|
||||||
ERROR
|
|
||||||
FATAL
|
|
||||||
)
|
|
||||||
|
|
||||||
var levelStrings = map[LogLevel]string{
|
|
||||||
DEBUG: "DEBUG",
|
|
||||||
INFO: "INFO",
|
|
||||||
WARN: "WARN",
|
|
||||||
ERROR: "ERROR",
|
|
||||||
FATAL: "FATAL",
|
|
||||||
}
|
|
||||||
|
|
||||||
// String returns the string representation of the log level
|
|
||||||
func (l LogLevel) String() string {
|
|
||||||
if s, ok := levelStrings[l]; ok {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
return "UNKNOWN"
|
|
||||||
}
|
|
||||||
133
logger/logger.go
133
logger/logger.go
@@ -1,133 +0,0 @@
|
|||||||
package logger
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Logger struct holds the logger instance
|
|
||||||
type Logger struct {
|
|
||||||
logger *log.Logger
|
|
||||||
level LogLevel
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
defaultLogger *Logger
|
|
||||||
once sync.Once
|
|
||||||
)
|
|
||||||
|
|
||||||
// NewLogger creates a new logger instance
|
|
||||||
func NewLogger() *Logger {
|
|
||||||
return &Logger{
|
|
||||||
logger: log.New(os.Stdout, "", 0),
|
|
||||||
level: DEBUG,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Init initializes the default logger
|
|
||||||
func Init() *Logger {
|
|
||||||
once.Do(func() {
|
|
||||||
defaultLogger = NewLogger()
|
|
||||||
})
|
|
||||||
return defaultLogger
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetLogger returns the default logger instance
|
|
||||||
func GetLogger() *Logger {
|
|
||||||
if defaultLogger == nil {
|
|
||||||
Init()
|
|
||||||
}
|
|
||||||
return defaultLogger
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetLevel sets the minimum logging level
|
|
||||||
func (l *Logger) SetLevel(level LogLevel) {
|
|
||||||
l.level = level
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetOutput sets the output destination for the logger
|
|
||||||
func (l *Logger) SetOutput(w io.Writer) {
|
|
||||||
l.logger.SetOutput(w)
|
|
||||||
}
|
|
||||||
|
|
||||||
// log handles the actual logging
|
|
||||||
func (l *Logger) log(level LogLevel, format string, args ...interface{}) {
|
|
||||||
if level < l.level {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get timezone from environment variable or use local timezone
|
|
||||||
timezone := os.Getenv("LOGGER_TIMEZONE")
|
|
||||||
var location *time.Location
|
|
||||||
var err error
|
|
||||||
|
|
||||||
if timezone != "" {
|
|
||||||
location, err = time.LoadLocation(timezone)
|
|
||||||
if err != nil {
|
|
||||||
// If invalid timezone, fall back to local
|
|
||||||
location = time.Local
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
location = time.Local
|
|
||||||
}
|
|
||||||
|
|
||||||
timestamp := time.Now().In(location).Format("2006/01/02 15:04:05")
|
|
||||||
message := fmt.Sprintf(format, args...)
|
|
||||||
l.logger.Printf("%s: %s %s", level.String(), timestamp, message)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Debug logs debug level messages
|
|
||||||
func (l *Logger) Debug(format string, args ...interface{}) {
|
|
||||||
l.log(DEBUG, format, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Info logs info level messages
|
|
||||||
func (l *Logger) Info(format string, args ...interface{}) {
|
|
||||||
l.log(INFO, format, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Warn logs warning level messages
|
|
||||||
func (l *Logger) Warn(format string, args ...interface{}) {
|
|
||||||
l.log(WARN, format, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Error logs error level messages
|
|
||||||
func (l *Logger) Error(format string, args ...interface{}) {
|
|
||||||
l.log(ERROR, format, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fatal logs fatal level messages and exits
|
|
||||||
func (l *Logger) Fatal(format string, args ...interface{}) {
|
|
||||||
l.log(FATAL, format, args...)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Global helper functions
|
|
||||||
func Debug(format string, args ...interface{}) {
|
|
||||||
GetLogger().Debug(format, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Info(format string, args ...interface{}) {
|
|
||||||
GetLogger().Info(format, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Warn(format string, args ...interface{}) {
|
|
||||||
GetLogger().Warn(format, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Error(format string, args ...interface{}) {
|
|
||||||
GetLogger().Error(format, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Fatal(format string, args ...interface{}) {
|
|
||||||
GetLogger().Fatal(format, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetOutput sets the output destination for the default logger
|
|
||||||
func SetOutput(w io.Writer) {
|
|
||||||
GetLogger().SetOutput(w)
|
|
||||||
}
|
|
||||||
45
main.go
45
main.go
@@ -13,9 +13,9 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/fosrl/newt/logger"
|
||||||
"github.com/fosrl/newt/websocket"
|
"github.com/fosrl/newt/websocket"
|
||||||
"github.com/fosrl/olm/httpserver"
|
"github.com/fosrl/olm/httpserver"
|
||||||
"github.com/fosrl/olm/logger"
|
|
||||||
"github.com/fosrl/olm/peermonitor"
|
"github.com/fosrl/olm/peermonitor"
|
||||||
"github.com/fosrl/olm/wgtester"
|
"github.com/fosrl/olm/wgtester"
|
||||||
|
|
||||||
@@ -79,9 +79,24 @@ func main() {
|
|||||||
fmt.Printf("Service status: %s\n", status)
|
fmt.Printf("Service status: %s\n", status)
|
||||||
return
|
return
|
||||||
case "debug":
|
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
|
// Pass the remaining arguments (after "debug") to the service
|
||||||
serviceArgs := os.Args[2:]
|
serviceArgs := os.Args[2:]
|
||||||
err := debugService(serviceArgs)
|
err = debugService(serviceArgs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to debug service: %v\n", err)
|
fmt.Printf("Failed to debug service: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@@ -106,8 +121,28 @@ func main() {
|
|||||||
fmt.Println("\nFor console mode, run without arguments or with standard flags.")
|
fmt.Println("\nFor console mode, run without arguments or with standard flags.")
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
fmt.Println("Unknown command:", os.Args[1])
|
// get the status and if it is Not Installed then install it first
|
||||||
fmt.Println("Use 'olm --help' for usage information.")
|
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
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -200,7 +235,7 @@ func runOlmMainWithArgs(ctx context.Context, args []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Debug: Print final values after flag parsing
|
// Debug: Print final values after flag parsing
|
||||||
fmt.Printf("After flag parsing: endpoint='%s', id='%s', secret='%s'\n", endpoint, id, secret)
|
// fmt.Printf("After flag parsing: endpoint='%s', id='%s', secret='%s'\n", endpoint, id, secret)
|
||||||
|
|
||||||
// Parse ping intervals
|
// Parse ping intervals
|
||||||
if pingIntervalStr != "" {
|
if pingIntervalStr != "" {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fosrl/olm/logger"
|
"github.com/fosrl/newt/logger"
|
||||||
"golang.org/x/sys/windows/svc"
|
"golang.org/x/sys/windows/svc"
|
||||||
"golang.org/x/sys/windows/svc/debug"
|
"golang.org/x/sys/windows/svc/debug"
|
||||||
"golang.org/x/sys/windows/svc/eventlog"
|
"golang.org/x/sys/windows/svc/eventlog"
|
||||||
@@ -32,13 +32,13 @@ var serviceArgs []string
|
|||||||
|
|
||||||
// getServiceArgsPath returns the path where service arguments are stored
|
// getServiceArgsPath returns the path where service arguments are stored
|
||||||
func getServiceArgsPath() string {
|
func getServiceArgsPath() string {
|
||||||
logDir := filepath.Join(os.Getenv("PROGRAMDATA"), "Olm")
|
logDir := filepath.Join(os.Getenv("PROGRAMDATA"), "olm")
|
||||||
return filepath.Join(logDir, "service_args.json")
|
return filepath.Join(logDir, "service_args.json")
|
||||||
}
|
}
|
||||||
|
|
||||||
// saveServiceArgs saves the service arguments to a file
|
// saveServiceArgs saves the service arguments to a file
|
||||||
func saveServiceArgs(args []string) error {
|
func saveServiceArgs(args []string) error {
|
||||||
logDir := filepath.Join(os.Getenv("PROGRAMDATA"), "Olm")
|
logDir := filepath.Join(os.Getenv("PROGRAMDATA"), "olm")
|
||||||
err := os.MkdirAll(logDir, 0755)
|
err := os.MkdirAll(logDir, 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create config directory: %v", err)
|
return fmt.Errorf("failed to create config directory: %v", err)
|
||||||
@@ -395,7 +395,7 @@ 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")
|
||||||
|
|
||||||
// Ensure the log directory exists
|
// Ensure the log directory exists
|
||||||
@@ -516,7 +516,7 @@ func isWindowsService() bool {
|
|||||||
|
|
||||||
func setupWindowsEventLog() {
|
func setupWindowsEventLog() {
|
||||||
// Create log directory if it doesn't exist
|
// Create log directory if it doesn't exist
|
||||||
logDir := filepath.Join(os.Getenv("PROGRAMDATA"), "Olm", "logs")
|
logDir := filepath.Join(os.Getenv("PROGRAMDATA"), "olm", "logs")
|
||||||
err := os.MkdirAll(logDir, 0755)
|
err := os.MkdirAll(logDir, 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to create log directory: %v\n", err)
|
fmt.Printf("Failed to create log directory: %v\n", err)
|
||||||
|
|||||||
Reference in New Issue
Block a user