forked from cyfdecyf/cow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh.go
56 lines (50 loc) · 1.12 KB
/
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
49
50
51
52
53
54
55
56
package main
import (
"net"
"os/exec"
"time"
)
func SshRunning() bool {
c, err := net.Dial("tcp", config.SocksParent)
if err != nil {
return false
}
c.Close()
return true
}
func runSSH() {
if config.SshServer == "" {
return
}
if config.SocksParent == "" {
errl.Println("Missing option: ssh server given without socks address")
return
}
_, port := splitHostPort(config.SocksParent)
sshServer, sshPort := splitHostPort(config.SshServer)
if sshPort == "" {
sshPort = "22"
}
alreadyRunPrinted := false
for {
if SshRunning() {
if !alreadyRunPrinted {
debug.Println("ssh socks server maybe already running, as cow can connect to",
config.SocksParent)
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, "-p", sshPort, sshServer)
if err := cmd.Run(); err != nil {
debug.Println("ssh:", err)
}
// debug.Println("ssh exited, reconnect")
time.Sleep(5 * time.Second)
alreadyRunPrinted = false
}
}