sshd.SSHListener: Use HandlerFunc instead of terminal channel feed

This commit is contained in:
Andrey Petrov 2016-07-17 16:49:14 -04:00
parent 62fbe2dc32
commit ace2bc5124
4 changed files with 29 additions and 39 deletions

View File

@ -178,11 +178,8 @@ func (h *Host) Connect(term *sshd.Terminal) {
// Serve our chat room onto the listener
func (h *Host) Serve() {
terminals := h.listener.ServeTerminal()
for term := range terminals {
go h.Connect(term)
}
h.listener.HandlerFunc = h.Connect
h.listener.Serve()
}
func (h *Host) completeName(partial string) string {

View File

@ -19,11 +19,6 @@ func (a RejectAuth) Check(net.Addr, ssh.PublicKey) (bool, error) {
return false, errRejectAuth
}
func consume(ch <-chan *Terminal) {
for _ = range ch {
}
}
func TestClientReject(t *testing.T) {
signer, err := NewRandomSigner(512)
config := MakeAuth(RejectAuth{})
@ -35,7 +30,7 @@ func TestClientReject(t *testing.T) {
}
defer s.Close()
go consume(s.ServeTerminal())
go s.Serve()
conn, err := ssh.Dial("tcp", s.Addr().String(), NewClientConfig("foo"))
if err == nil {

View File

@ -10,8 +10,10 @@ import (
// Container for the connection and ssh-related configuration
type SSHListener struct {
net.Listener
config *ssh.ServerConfig
RateLimit func() rateio.Limiter
config *ssh.ServerConfig
RateLimit func() rateio.Limiter
HandlerFunc func(term *Terminal)
}
// Make an SSH listener socket
@ -42,32 +44,24 @@ func (l *SSHListener) handleConn(conn net.Conn) (*Terminal, error) {
}
// Accept incoming connections as terminal requests and yield them
func (l *SSHListener) ServeTerminal() <-chan *Terminal {
ch := make(chan *Terminal)
func (l *SSHListener) Serve() {
defer l.Close()
for {
conn, err := l.Accept()
go func() {
for {
conn, err := l.Accept()
if err != nil {
logger.Printf("Failed to accept connection: %v", err)
break
}
// Goroutineify to resume accepting sockets early
go func() {
term, err := l.handleConn(conn)
if err != nil {
logger.Printf("Failed to handshake: %v", err)
return
}
ch <- term
}()
if err != nil {
logger.Printf("Failed to accept connection: %v", err)
break
}
l.Close()
close(ch)
}()
return ch
// Goroutineify to resume accepting sockets early
go func() {
term, err := l.handleConn(conn)
if err != nil {
logger.Printf("Failed to handshake: %v", err)
return
}
l.HandlerFunc(term)
}()
}
}

View File

@ -34,7 +34,11 @@ func TestServeTerminals(t *testing.T) {
t.Fatal(err)
}
terminals := s.ServeTerminal()
terminals := make(chan *Terminal)
s.HandlerFunc = func(term *Terminal) {
terminals <- term
}
go s.Serve()
go func() {
// Accept one terminal, read from it, echo back, close.