/
conn.go
72 lines (61 loc) · 1.41 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package gitolite
import (
"log"
"net"
"golang.org/x/crypto/ssh"
)
type sshConn struct {
handler interface{}
conn *ssh.ServerConn
newChannel <-chan ssh.NewChannel
requests <-chan *ssh.Request
}
func (s *Server) serve(conn net.Conn) error {
config := ssh.ServerConfig{
PublicKeyCallback: s.config.PublicKeyCallback.wrap(),
PasswordCallback: s.config.PasswordCallback.wrap(),
}
for _, hostKey := range s.config.HostKeys {
config.AddHostKey(hostKey)
}
serverConn, channelRequestCh, globalRequestCh, err := ssh.NewServerConn(conn, &config)
if err != nil {
return err
}
// connection succeeded at this point. create the ssh connection and start the go procs.
newConn := sshConn{
handler: s.config.Handler,
conn: serverConn,
newChannel: channelRequestCh,
requests: globalRequestCh,
}
go newConn.processChannelRequests()
go newConn.processGlobalRequests()
return nil
}
func (c *sshConn) processChannelRequests() {
for req := range c.newChannel {
if req.ChannelType() != "session" {
req.Reject(ssh.UnknownChannelType, "")
continue
}
channel, requestCh, err := req.Accept()
if err != nil {
log.Println(err)
continue
}
s := &session{
handler: c.handler,
channel: channel,
requests: requestCh,
}
go s.loop()
}
}
func (c *sshConn) processGlobalRequests() {
for req := range c.requests {
if req.WantReply {
req.Reply(false, nil)
}
}
}