diff --git a/launcher-common/ipc.go b/launcher-common/ipc.go index 6a05cb2..2d8abea 100644 --- a/launcher-common/ipc.go +++ b/launcher-common/ipc.go @@ -3,13 +3,12 @@ package launcher_common import ( "github.com/luskaner/ageLANServer/common" "net" - "os" - "path" ) const ConfigAdminIpcRevert byte = 0 const ConfigAdminIpcSetup byte = 1 const ConfigAdminIpcExit byte = 2 +const configAdminIpcName = common.Name + `-launcher-config-admin-agent` type ( ConfigAdminIpcSetupCommand struct { @@ -23,7 +22,3 @@ type ( Certificate bool } ) - -func ConfigAdminIpcName() string { - return path.Join(os.TempDir(), common.Name+`-launcher-config-admin-agent`) -} diff --git a/launcher-common/ipc_other.go b/launcher-common/ipc_other.go new file mode 100644 index 0000000..174f7e7 --- /dev/null +++ b/launcher-common/ipc_other.go @@ -0,0 +1,12 @@ +//go:build !windows + +package launcher_common + +import ( + "os" + "path" +) + +func ConfigAdminIpcPath() string { + return path.Join(os.TempDir(), configAdminIpcName) +} diff --git a/launcher-common/ipc_windows.go b/launcher-common/ipc_windows.go new file mode 100644 index 0000000..f467969 --- /dev/null +++ b/launcher-common/ipc_windows.go @@ -0,0 +1,5 @@ +package launcher_common + +func ConfigAdminIpcPath() string { + return `\\.\pipe\` + configAdminIpcName +} diff --git a/launcher-config-admin-agent/internal/ipc.go b/launcher-config-admin-agent/internal/ipc.go index c26e408..0411881 100644 --- a/launcher-config-admin-agent/internal/ipc.go +++ b/launcher-config-admin-agent/internal/ipc.go @@ -7,7 +7,6 @@ import ( launcherCommon "github.com/luskaner/ageLANServer/launcher-common" "github.com/luskaner/ageLANServer/launcher-common/executor" "net" - "os" ) var mappedCdn = false @@ -121,30 +120,16 @@ func handleRevert(decoder *gob.Decoder) int { } func RunIpcServer() (errorCode int) { - ipcPath := launcherCommon.ConfigAdminIpcName() - - if err := os.Remove(ipcPath); err != nil && !os.IsNotExist(err) { - errorCode = ErrListen - return - } - - defer func() { - _ = os.Remove(ipcPath) - }() - - l, err := net.Listen("unix", ipcPath) + l, err := SetupIpcServer() if err != nil { errorCode = ErrListen return } defer func(l net.Listener) { _ = l.Close() + RevertIpcServer() }(l) - if err = os.Chmod(ipcPath, 0666); err != nil { - return - } - var conn net.Conn for { conn, err = l.Accept() diff --git a/launcher-config-admin-agent/internal/ipc_other.go b/launcher-config-admin-agent/internal/ipc_other.go new file mode 100644 index 0000000..78e5696 --- /dev/null +++ b/launcher-config-admin-agent/internal/ipc_other.go @@ -0,0 +1,30 @@ +//go:build !windows + +package internal + +import ( + launcherCommon "github.com/luskaner/ageLANServer/launcher-common" + "net" + "os" +) + +func SetupIpcServer() (listener net.Listener, err error) { + ipcPath := launcherCommon.ConfigAdminIpcPath() + + if err = os.Remove(ipcPath); err != nil && !os.IsNotExist(err) { + return + } + + listener, err = net.Listen("unix", ipcPath) + + if err != nil { + return + } + + err = os.Chmod(ipcPath, 0666) + return +} + +func RevertIpcServer() { + _ = os.Remove(launcherCommon.ConfigAdminIpcPath()) +} diff --git a/launcher-config-admin-agent/internal/ipc_windows.go b/launcher-config-admin-agent/internal/ipc_windows.go new file mode 100644 index 0000000..c6c6551 --- /dev/null +++ b/launcher-config-admin-agent/internal/ipc_windows.go @@ -0,0 +1,26 @@ +package internal + +import ( + "fmt" + "github.com/Microsoft/go-winio" + launcherCommon "github.com/luskaner/ageLANServer/launcher-common" + "net" + "os/user" +) + +func SetupIpcServer() (listener net.Listener, err error) { + var u *user.User + u, err = user.Current() + if err != nil { + return nil, err + } + pc := &winio.PipeConfig{ + InputBufferSize: 1_024, + OutputBufferSize: 1, + SecurityDescriptor: fmt.Sprintf("D:P(A;;GA;;;%s)", u.Uid), + MessageMode: true, + } + return winio.ListenPipe(launcherCommon.ConfigAdminIpcPath(), pc) +} + +func RevertIpcServer() {} diff --git a/launcher-config/go.mod b/launcher-config/go.mod index 1c53323..5d94419 100644 --- a/launcher-config/go.mod +++ b/launcher-config/go.mod @@ -3,6 +3,7 @@ module github.com/luskaner/ageLANServer/launcher-config go 1.23.0 require ( + github.com/Microsoft/go-winio v0.6.2 github.com/deckarep/golang-set/v2 v2.7.0 github.com/luskaner/ageLANServer/common v0.0.0-20241111160911-2abef906e211 github.com/luskaner/ageLANServer/launcher-common v0.0.0-20241111163256-bdcc7ac24582 diff --git a/launcher-config/go.sum b/launcher-config/go.sum index b92f433..606de2f 100644 --- a/launcher-config/go.sum +++ b/launcher-config/go.sum @@ -1,3 +1,5 @@ +github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY= +github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU= github.com/andygrunwald/vdf v1.1.0 h1:gmstp0R7DOepIZvWoSJY97ix7QOrsxpGPU6KusKXqvw= github.com/andygrunwald/vdf v1.1.0/go.mod h1:f31AAs7HOKvs5B167iwLHwKuqKc4bE46Vdt7xQogA0o= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= diff --git a/launcher-config/internal/admin.go b/launcher-config/internal/admin.go index 4e63b9e..057e4ef 100644 --- a/launcher-config/internal/admin.go +++ b/launcher-config/internal/admin.go @@ -93,7 +93,7 @@ func ConnectAgentIfNeeded() (err error) { return } var conn net.Conn - conn, err = net.Dial("unix", launcherCommon.ConfigAdminIpcName()) + conn, err = DialIPC() if err != nil { return } diff --git a/launcher-config/internal/admin_other.go b/launcher-config/internal/admin_other.go index 0186fd6..22d5012 100644 --- a/launcher-config/internal/admin_other.go +++ b/launcher-config/internal/admin_other.go @@ -6,6 +6,8 @@ import ( "fmt" "github.com/luskaner/ageLANServer/common/executor" "github.com/luskaner/ageLANServer/common/process" + launcherCommon "github.com/luskaner/ageLANServer/launcher-common" + "net" "time" ) @@ -26,3 +28,7 @@ func postAgentStart(file string) { } } } + +func DialIPC() (net.Conn, error) { + return net.Dial("unix", launcherCommon.ConfigAdminIpcPath()) +} diff --git a/launcher-config/internal/admin_windows.go b/launcher-config/internal/admin_windows.go index 3e9857b..287401f 100644 --- a/launcher-config/internal/admin_windows.go +++ b/launcher-config/internal/admin_windows.go @@ -1,4 +1,14 @@ package internal +import ( + "github.com/Microsoft/go-winio" + launcherCommon "github.com/luskaner/ageLANServer/launcher-common" + "net" +) + func preAgentStart() {} func postAgentStart(_ string) {} + +func DialIPC() (net.Conn, error) { + return winio.DialPipe(launcherCommon.ConfigAdminIpcPath(), nil) +}