forked from docker/libchan
/
listener.go
49 lines (43 loc) · 1.2 KB
/
listener.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
package spdy
import (
"net"
)
// TransportListener is a listener which accepts new
// connections angi rd spawns spdy transports.
type TransportListener struct {
listener net.Listener
auth Authenticator
}
// NewTransportListener creates a new listen transport using
// a network listeners and function to authenticate
// new connections. TransportListener expects tls session
// handling to occur by the authenticator or the listener,
// TransportListener will not perform tls handshakes.
func NewTransportListener(listener net.Listener, auth Authenticator) (*TransportListener, error) {
return &TransportListener{
listener: listener,
auth: auth,
}, nil
}
// Close closes the underlying listener
func (l *TransportListener) Close() error {
return l.listener.Close()
}
// AcceptTransport waits for a new network connections
// and creates a new stream. Connections which fail
// authentication will not be returned.
func (l *TransportListener) AcceptTransport() (*Transport, error) {
for {
conn, err := l.listener.Accept()
if err != nil {
return nil, err
}
authErr := l.auth(conn)
if authErr != nil {
// TODO log
conn.Close()
continue
}
return newSession(conn, true)
}
}