-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathawaken_darwin.go
More file actions
executable file
·239 lines (225 loc) · 7.17 KB
/
Copy pathawaken_darwin.go
File metadata and controls
executable file
·239 lines (225 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package awaken
import (
"fmt"
"go-client/global"
"go-client/pkg/config"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
func getCommandFromArgs(connectInfo map[string]string, argFormat string) string {
for key, value := range connectInfo {
argFormat = strings.Replace(argFormat, "{"+key+"}", value, 1)
}
return argFormat
}
// validateAppPath checks if the application path exists
func validateAppPath(appPath string) error {
if appPath == "" {
return fmt.Errorf("application path is empty")
}
// Check if path exists
if _, err := os.Stat(appPath); os.IsNotExist(err) {
return fmt.Errorf("application path does not exist: %s", appPath)
}
return nil
}
func awakenRDPCommand(filePath string, cfg *config.AppConfig) (*exec.Cmd, error) {
global.LOG.Debug(filePath)
cmd := exec.Command("open", filePath)
return cmd, nil
}
func awakenVNCCommand(r *Rouse, cfg *config.AppConfig) (*exec.Cmd, error) {
var appItem *config.AppItem
appLst := cfg.MacOS.RemoteDesktop
for _, app := range appLst {
if app.IsActive() && app.IsMatchProtocol("vnc") {
appItem = &app
break
}
}
if appItem == nil {
return nil, nil
}
connectMap := map[string]string{
"name": r.getName(),
"protocol": r.Protocol,
"username": r.getUserName(),
"value": r.Value,
"host": r.Host,
"port": strconv.Itoa(r.Port),
}
if !appItem.IsInternal {
if err := validateAppPath(appItem.Path); err != nil {
return nil, fmt.Errorf("No VNC application configured or found (selected path: %s, reason: %v)", appItem.Path, err)
}
}
commands := getCommandFromArgs(connectMap, appItem.ArgFormat)
cmd := exec.Command(appItem.Path, strings.Split(commands, " ")...)
// 设置环境变量(只对这个子进程有效)
cmd.Env = append(os.Environ(),
"VNC_USERNAME="+r.getUserName(),
"VNC_PASSWORD="+r.Value,
)
return cmd, nil
}
func awakenSSHCommand(r *Rouse, cfg *config.AppConfig) (*exec.Cmd, error) {
var appItem *config.AppItem
var appLst []config.AppItem
switch r.Protocol {
case "ssh", "telnet":
appLst = cfg.MacOS.Terminal
case "sftp":
appLst = append(cfg.MacOS.FileTransfer, cfg.MacOS.Terminal...)
}
for _, app := range appLst {
if app.IsActive() && app.IsMatchProtocol(r.Protocol) {
appItem = &app
break
}
}
if appItem == nil {
return nil, nil
}
// telnet 协议使用 ssh 的配置参数格式
protocol := r.Protocol
if protocol == "telnet" {
protocol = "ssh"
}
var cmd *exec.Cmd
connectMap := map[string]string{
"name": r.getName(),
"protocol": protocol,
"username": r.getUserName(),
"value": r.Value,
"host": r.Host,
"port": strconv.Itoa(r.Port),
}
if appItem.IsInternal {
currentPath, _ := filepath.Abs(filepath.Dir(os.Args[0]))
commands := getCommandFromArgs(connectMap, appItem.ArgFormat)
clientPath := filepath.Join(currentPath, "client")
if appItem.Name == "iterm" {
itermCmd := fmt.Sprintf(`%s %s`, clientPath, commands)
scriptPath := filepath.Join(currentPath, "Scripts", "iterm2_loader.scpt")
//command = fmt.Sprintf(`%s "%s"`, scriptPath, command)
cmd = exec.Command("osascript", "-s", "h", scriptPath, itermCmd, "0")
} else if appItem.Name == "ghostty" {
// Ghostty has no "do script" AppleEvent; `open -na --args -e` only works
// cleanly on a cold start. Once Ghostty is already running, that path
// triggers a security confirmation and, if allowed, a broken extra tab.
// Its AppleScript dictionary avoids both, cold or warm. "wait after
// command" keeps the surface open once the client exits. "activate"
// runs last since calling it earlier opens Ghostty's own default tab;
// the window-count check picks new tab vs. new window so repeat
// connections land in the existing window instead of a new one.
ghosttyCmd := strings.NewReplacer(`\`, `\\`, `"`, `\"`).Replace(
fmt.Sprintf("%s %s", clientPath, commands),
)
script := fmt.Sprintf(`tell application "Ghostty"
set cfg to new surface configuration
set command of cfg to "%s"
set wait after command of cfg to true
if (count of windows) > 0 then
new tab in (front window) with configuration cfg
else
new window with configuration cfg
end if
activate
end tell`, ghosttyCmd)
cmd = exec.Command("osascript", "-e", script)
} else {
cmd = exec.Command(
"osascript", "-s", "h", "-e", fmt.Sprintf(`tell application "%s" to do script "%s %s" activate`,
appItem.DisplayName, clientPath, commands),
)
}
} else {
appPath := appItem.Path
if !appItem.IsInternal {
if err := validateAppPath(appItem.Path); err != nil {
return nil, fmt.Errorf("No %s application configured or found (selected path: %s, reason: %v)", strings.ToUpper(r.Protocol), appItem.Path, err)
}
}
commands := getCommandFromArgs(connectMap, appItem.ArgFormat)
cmd = exec.Command(appPath, strings.Split(commands, " ")...)
}
return cmd, nil
}
func awakenDBCommand(r *Rouse, cfg *config.AppConfig) (*exec.Cmd, error) {
var appItem *config.AppItem
appLst := cfg.MacOS.Databases
for _, app := range appLst {
if app.IsActive() && app.IsMatchProtocol(r.Protocol) {
appItem = &app
break
}
}
if appItem == nil {
return nil, nil
}
appPath := appItem.Path
connectMap := map[string]string{
"name": r.getName(),
"protocol": r.Protocol,
"username": r.getUserName(),
"value": r.Value,
"host": r.Host,
"port": strconv.Itoa(r.Port),
"dbname": r.DBName,
}
if r.Protocol == "oracle" {
connectMap["dbname"] = r.getUserName()
}
if appItem.IsInternal {
var argFormat string
switch r.Protocol {
case "redis":
argFormat = "redis-cli -h {host} -p {port} -a {username}@{value}"
case "oracle":
argFormat = "sqlplus {username}/{value}@{host}:{port}/{dbname}"
case "postgresql":
argFormat = "psql 'user={username} password={value} host={host} dbname={dbname} port={port}'"
case "mysql", "mariadb":
argFormat = "mysql -u {username} -p{value} -h {host} -P {port} {dbname}"
case "sqlserver":
argFormat = "sqlcmd -S {host},{port} -U {username} -P {value} -d {dbname}"
case "mongodb":
argFormat = "mongosh mongodb://{username}:{value}@{host}:{port}/{dbname}"
}
commands := getCommandFromArgs(connectMap, argFormat)
cmd := exec.Command(
"osascript", "-s", "h", "-e", fmt.Sprintf(`tell application "%s" to do script "%s" activate`,
appItem.DisplayName, commands),
)
return cmd, nil
} else {
if !appItem.IsInternal {
if err := validateAppPath(appItem.Path); err != nil {
return nil, fmt.Errorf("No database application configured or found (selected path: %s, reason: %v)", appItem.Path, err)
}
}
if r.Protocol == "sqlserver" {
connectMap["protocol"] = "mssql_jdbc_ms_new"
}
commands := getCommandFromArgs(connectMap, appItem.ArgFormat)
return exec.Command(appPath, strings.Split(commands, " ")...), nil
}
}
func awakenOtherCommand(r *Rouse, cfg *config.AppConfig) (*exec.Cmd, error) {
var command string
if r.Protocol == "ssh" {
currentPath := filepath.Dir(os.Args[0])
clientPath := filepath.Join(currentPath, "client")
command = fmt.Sprintf("%s %s -P %s", clientPath, r.Command, r.Value)
} else {
command = r.Command
}
cmd := exec.Command(
"osascript", "-s", "h",
"-e", fmt.Sprintf(`tell application "%s" to do script "%s"`, "Terminal", command),
)
return cmd, nil
}