-
Notifications
You must be signed in to change notification settings - Fork 1
/
awssh.go
64 lines (53 loc) · 1.15 KB
/
awssh.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
package awssh
import (
"io/ioutil"
"net"
"time"
homedir "github.com/mitchellh/go-homedir"
)
const (
ConnectHost string = "127.0.0.1"
)
func fetchEmptyPort(host string) (port string, err error) {
l, err := net.Listen("tcp", host+":0")
if err == nil {
defer l.Close()
addr := l.Addr().String()
_, port, err := net.SplitHostPort(addr)
if err == nil {
return port, nil
} else {
return "", err
}
} else {
return "", err
}
}
func waitOpenPort(host, localPort string) {
for {
conn, _ := net.DialTimeout("tcp", net.JoinHostPort(host, localPort), 200000*time.Nanosecond)
if conn != nil {
conn.Close()
break
}
}
}
func guessPublickey(identityFile, publickey string) (guessedPublickey string) {
guessedPublickey = publickey
if publickey == "identity-file+'.pub'" {
guessedPublickey = identityFile + ".pub"
}
return guessedPublickey
}
func readPublicKey(filePath string) (publicKey string, err error) {
fullPath, err := homedir.Expand(filePath)
if err != nil {
return "", err
}
publicKeyBytes, err := ioutil.ReadFile(fullPath)
if err != nil {
return "", err
}
publicKey = string(publicKeyBytes)
return publicKey, nil
}