forked from cyfdecyf/cow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh.go
48 lines (42 loc) · 920 Bytes
/
ssh.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
package main
import (
"net"
"os/exec"
"time"
)
func SshRunning() bool {
c, err := net.Dial("tcp", config.socksAddr)
if err != nil {
return false
}
c.Close()
return true
}
func runSSH() {
if config.sshServer == "" {
return
}
_, port := splitHostPort(config.socksAddr)
alreadyRunPrinted := false
for {
if SshRunning() {
if !alreadyRunPrinted {
debug.Println("ssh socks server maybe already running, as cow can connect to",
config.socksAddr)
alreadyRunPrinted = true
}
// check server liveness in 1 minute
time.Sleep(60 * time.Second)
continue
}
// -n redirects stdin from /dev/null
// -N do not execute remote command
cmd := exec.Command("ssh", "-n", "-N", "-D", port, config.sshServer)
if err := cmd.Run(); err != nil {
debug.Println("ssh:", err)
}
debug.Println("ssh exited, reconnect")
time.Sleep(5 * time.Second)
alreadyRunPrinted = false
}
}