diff --git a/client/core/core.go b/client/core/core.go index ae41cfe..3e13371 100644 --- a/client/core/core.go +++ b/client/core/core.go @@ -54,6 +54,7 @@ func Start() { var err error if common.WSConn != nil { common.WSConn.Close() + common.WSConn = nil } common.WSConn, err = connectWS() if err != nil && !stop { @@ -209,6 +210,11 @@ func handleAct(pack modules.Packet, wsConn *common.Conn) { if act, ok := handlers[pack.Act]; !ok { common.SendCb(modules.Packet{Code: 1, Msg: `${i18n|actionNotImplemented}`}, pack, wsConn) } else { + defer func() { + if r := recover(); r != nil { + golog.Error(`Panic: `, r) + } + }() act(pack, wsConn) } } diff --git a/client/service/basic/basic_darwin.go b/client/service/basic/basic_darwin.go new file mode 100644 index 0000000..74e4149 --- /dev/null +++ b/client/service/basic/basic_darwin.go @@ -0,0 +1,96 @@ +//go:build darwin +// +build darwin + +package basic + +/* +#cgo LDFLAGS: -framework CoreServices -framework Carbon +#include +#include + +static OSStatus SendAppleEventToSystemProcess(AEEventID EventToSend); + +OSStatus SendAppleEventToSystemProcess(AEEventID EventToSend) +{ + AEAddressDesc targetDesc; + static const ProcessSerialNumber kPSNOfSystemProcess = { 0, kSystemProcess }; + AppleEvent eventReply = {typeNull, NULL}; + AppleEvent appleEventToSend = {typeNull, NULL}; + + OSStatus error = noErr; + + error = AECreateDesc(typeProcessSerialNumber, &kPSNOfSystemProcess, sizeof(kPSNOfSystemProcess), &targetDesc); + + if (error != noErr) { + return(error); + } + + error = AECreateAppleEvent(kCoreEventClass, EventToSend, &targetDesc, kAutoGenerateReturnID, kAnyTransactionID, &appleEventToSend); + + AEDisposeDesc(&targetDesc); + if (error != noErr) { + return(error); + } + + error = AESend(&appleEventToSend, &eventReply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, NULL, NULL); + + AEDisposeDesc(&appleEventToSend); + if (error != noErr) { + return(error); + } + + AEDisposeDesc(&eventReply); + + return(error); +} +*/ +import "C" + +import ( + "errors" + "os/exec" +) + +// I'm not familiar with macOS, that's all I can do. +func init() { +} + +func Lock() error { + return errors.New(`${i18n|operationNotSupported}`) +} + +func Logoff() error { + if C.SendAppleEventToSystemProcess(C.kAEReallyLogOut) == C.noErr { + return nil + } else { + return errors.New(`${i18n|operationNotSupported}`) + } +} + +func Hibernate() error { + if C.SendAppleEventToSystemProcess(C.kAESleep) == C.noErr { + return nil + } else { + return errors.New(`${i18n|operationNotSupported}`) + } +} + +func Suspend() error { + return errors.New(`${i18n|operationNotSupported}`) +} + +func Restart() error { + if C.SendAppleEventToSystemProcess(C.kAERestart) == C.noErr { + return nil + } else { + return exec.Command(`reboot`).Run() + } +} + +func Shutdown() error { + if C.SendAppleEventToSystemProcess(C.kAEShutDown) == C.noErr { + return nil + } else { + return exec.Command(`shutdown`).Run() + } +} diff --git a/client/service/basic/basic_others.go b/client/service/basic/basic_others.go index ae84b7c..8dd7b75 100644 --- a/client/service/basic/basic_others.go +++ b/client/service/basic/basic_others.go @@ -1,4 +1,4 @@ -//go:build !linux && !windows +//go:build !linux && !windows && !darwin package basic diff --git a/client/service/terminal/terminal_others.go b/client/service/terminal/terminal_others.go index bad8bc9..00e6151 100644 --- a/client/service/terminal/terminal_others.go +++ b/client/service/terminal/terminal_others.go @@ -76,8 +76,8 @@ func InputTerminal(pack modules.Packet) error { return nil } terminal := val.(*terminal) - terminal.lastPack = time.Now().Unix() terminal.pty.Write(data) + terminal.lastPack = time.Now().Unix() return nil } @@ -123,6 +123,7 @@ func KillTerminal(pack modules.Packet) error { return nil } terminal := val.(*terminal) + terminals.Remove(termUUID) doKillTerminal(terminal) return nil } diff --git a/client/service/terminal/terminal_windows.go b/client/service/terminal/terminal_windows.go index 06f5612..7d3a3c9 100644 --- a/client/service/terminal/terminal_windows.go +++ b/client/service/terminal/terminal_windows.go @@ -107,13 +107,13 @@ func InputTerminal(pack modules.Packet) error { return nil } terminal := val.(*terminal) - terminal.lastPack = time.Now().Unix() if len(data) == 1 && data[0] == '\x03' { terminal.cmd.Process.Signal(os.Interrupt) return nil } data, _ = decodeUTF8(data) (*terminal.stdin).Write(data) + terminal.lastPack = time.Now().Unix() return nil } @@ -133,6 +133,7 @@ func KillTerminal(pack modules.Packet) error { return nil } terminal := val.(*terminal) + terminals.Remove(termUUID) doKillTerminal(terminal) return nil }