/
ssh.go
55 lines (46 loc) · 1.13 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
package main
import (
ssh "code.google.com/p/go.crypto/ssh"
"io/ioutil"
)
type Credentials struct {
username string
password string
}
type CredentialsChan chan *Credentials
func StartServer(ip string, keyFile string, credentialsChan CredentialsChan) error {
// The PasswordCallback always returns false. The supplied credentials get
// passed to the specified callback for further processing.
config := &ssh.ServerConfig{
PasswordCallback: func(conn *ssh.ServerConn, user, pass string) bool {
credentialsChan <- &Credentials{user, pass}
return false
},
}
// Read the server's private key
pemBytes, err := ioutil.ReadFile(keyFile)
if err != nil {
return err
}
if err = config.SetRSAPrivateKey(pemBytes); err != nil {
return err
}
// set up listener
listener, err := ssh.Listen("tcp", ip, config)
if err != nil {
return err
}
go processConnections(listener)
return nil
}
func processConnections(listener *ssh.Listener) {
for {
// accept new connection
sConn, err := listener.Accept()
if err != nil {
continue
}
// dispatch handshake and discard result (will always be false)
go sConn.Handshake()
}
}