-
Notifications
You must be signed in to change notification settings - Fork 1
/
conn.go
55 lines (46 loc) · 1.02 KB
/
conn.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
package main
import (
"fmt"
"log"
"net"
"os"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
func SrvListen() {
nl, err := net.Listen("tcp", "0.0.0.0:8989")
if err != nil {
log.Fatal(err)
}
defer nl.Close()
// Accept requests
for {
_, err := nl.Accept()
if err != nil {
log.Println(err)
continue
}
// TODO: Implement logic
}
}
func dialSSH(user, password, host string, port int) (*ssh.Client, func()) {
var auths []ssh.AuthMethod
aconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if err == nil {
auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(aconn).Signers))
}
if password != "" {
auths = append(auths, ssh.Password(password))
}
config := ssh.ClientConfig{
User: user,
Auth: auths,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
addr := fmt.Sprintf("%s:%d", host, port)
conn, err := ssh.Dial("tcp", addr, &config)
if err != nil {
log.Fatalf("unable to connect to [%s]: %v", addr, err)
}
return conn, func() { conn.Close() }
}