-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdarwin.go
106 lines (90 loc) · 3.13 KB
/
darwin.go
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
// Special support for Darwin clients.
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
)
// Basic AppleScript to launch our ssh session.
var terminalTemplate = `
tell app "Terminal" to do script "%s; exit"
`
// This has to be a separate block of AppleScript, because the "create
// window with default profile" will fail if iTerm is not installed.
var itermTemplate = `
tell application "iTerm"
activate
if (count of windows) = 0 then
set newWindow to (create window with default profile)
tell current session of newWindow
write text "%s; exit"
end tell
else
tell current window
tell current session of current tab
set newPane to (split vertically with default profile)
tell newPane
write text "%s; exit"
end tell
end tell
end tell
end if
end tell
`
var launchScriptTemplate = `#!/bin/bash
tempdir=$1
cd $tempdir
mkfifo done
osascript %s
cat done # This will block until the dd command writes to the fifo.
cd /tmp
rm -rf ${tempdir}
`
// darwinConnectCommand returns a temporary bash script that wraps two
// other scripts in a temporary folder. The order of invocation goes
// like this,
//
// launch-and-wait.sh
// `- osascript terminal.script
// `- connect.sh
//
// It has to be done like this because the osascript command that
// launches the terminal runs asynchronously, but we want the ssh
// command to block. The cat/dd commands on the fifo handle the
// synchronization.
func darwinConnectCommand(ssh_command string) []string {
tempdir, _ := os.MkdirTemp("/tmp", "rdevcon-*")
connectScriptPath := tempdir + "/" + "connect.sh"
connectScriptContents := fmt.Sprintf("#!/bin/bash\n%s\ndd if=/dev/null of=%s/done\n",
ssh_command, tempdir)
os.WriteFile(connectScriptPath, []byte(connectScriptContents), 0700)
var terminalScript string
matches, _ := filepath.Glob("/Applications/iTerm*.app")
if len(matches) > 0 {
terminalScript = fmt.Sprintf(itermTemplate, connectScriptPath, connectScriptPath)
} else {
terminalScript = fmt.Sprintf(terminalTemplate, connectScriptPath)
}
terminalScriptPath := tempdir + "/" + "terminal.scpt"
os.WriteFile(terminalScriptPath, []byte(terminalScript), 0700)
launchScriptPath := tempdir + "/" + "launch-and-wait.sh"
os.WriteFile(launchScriptPath, []byte(fmt.Sprintf(launchScriptTemplate, terminalScriptPath)), 0700)
return []string{launchScriptPath, tempdir}
}
// sudo askpass program, created in a temp folder that is deleted after use
var askpassScriptTemplate = `#!/bin/bash
osascript -e 'Tell application "System Events" to display dialog "Authentication required%s:" default answer "" with hidden answer buttons {"OK"} default button 1' -e 'text returned of result'
`
func darwinSudoCommand(authMessage string, args []string) error {
tempdir, _ := os.MkdirTemp("/tmp", "rdevcon-sudo-*")
defer os.RemoveAll(tempdir)
askpassScriptPath := tempdir + "/" + "askpass.sh"
os.WriteFile(askpassScriptPath, []byte(fmt.Sprintf(askpassScriptTemplate, authMessage)), 0700)
cmd := exec.Command("sudo", "-A")
cmd.Args = append(cmd.Args, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = append(os.Environ(), "SUDO_ASKPASS="+askpassScriptPath)
return cmd.Run()
}