-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.go
65 lines (53 loc) · 1.41 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Package protomux implements a simple means to multiplex TCP protocols over the same port.
package protomux
import "net"
// Protocol ...
type Protocol int
// Interface for protocol matching
type probe interface {
apply([]byte) bool
}
const (
// None - no protocol has been matched
None Protocol = iota
// HTTP protocol has been matched
HTTP Protocol = iota
// TLS protocol has been matched
TLS Protocol = iota
)
var standardProbes = map[Protocol]probe{
HTTP: httpProbe{},
TLS: tlsProbe{},
}
type Listener struct {
net.Listener
probes map[Protocol]probe
}
// Listen will start a TCP listener on the supplied address.
func Listen(addr string) (net.Listener, error) {
listener, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
return &Listener{listener, standardProbes}, nil
}
// Accept waits for and returns the next connection to the listener
// after applying protocol detection probes to it. The net.Conn returned
// will be untouched with all bytes ready to read.
func (p *Listener) Accept() (net.Conn, error) {
conn, err := p.Listener.Accept()
if err != nil {
return nil, err
}
pConn := wrapConn(conn)
pConn.fillBuffer()
// in the case when no probe matches, the proto will simply
// remain at None and the user can decide how to proceed.
for proto, probe := range p.probes {
if probe.apply(pConn.buffer.Bytes()) {
pConn.setProtocol(proto)
break
}
}
return pConn, nil
}