/
listener.go
166 lines (145 loc) · 3.53 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package wt
import (
"net"
"net/http"
"net/http/httputil"
"github.com/dolfly/core/listener"
"github.com/dolfly/core/logger"
md "github.com/dolfly/core/metadata"
admission "github.com/dolfly/x/admission/wrapper"
xnet "github.com/dolfly/x/internal/net"
wt_util "github.com/dolfly/x/internal/util/wt"
limiter "github.com/dolfly/x/limiter/traffic/wrapper"
metrics "github.com/dolfly/x/metrics/wrapper"
"github.com/dolfly/x/registry"
stats "github.com/dolfly/x/stats/wrapper"
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/http3"
wt "github.com/quic-go/webtransport-go"
)
func init() {
registry.ListenerRegistry().Register("wt", NewListener)
}
type wtListener struct {
addr net.Addr
srv *wt.Server
cqueue chan net.Conn
errChan chan error
logger logger.Logger
md metadata
options listener.Options
}
func NewListener(opts ...listener.Option) listener.Listener {
options := listener.Options{}
for _, opt := range opts {
opt(&options)
}
return &wtListener{
logger: options.Logger,
options: options,
}
}
func (l *wtListener) Init(md md.Metadata) (err error) {
if err = l.parseMetadata(md); err != nil {
return
}
network := "udp"
if xnet.IsIPv4(l.options.Addr) {
network = "udp4"
}
l.addr, err = net.ResolveUDPAddr(network, l.options.Addr)
if err != nil {
return
}
mux := http.NewServeMux()
mux.Handle(l.md.path, http.HandlerFunc(l.upgrade))
l.srv = &wt.Server{
H3: http3.Server{
Addr: l.options.Addr,
TLSConfig: l.options.TLSConfig,
QuicConfig: &quic.Config{
KeepAlivePeriod: l.md.keepAlivePeriod,
HandshakeIdleTimeout: l.md.handshakeTimeout,
MaxIdleTimeout: l.md.maxIdleTimeout,
/*
Versions: []quic.VersionNumber{
quic.Version1,
quic.Version2,
},
*/
MaxIncomingStreams: int64(l.md.maxStreams),
},
Handler: mux,
},
CheckOrigin: func(r *http.Request) bool { return true },
}
l.cqueue = make(chan net.Conn, l.md.backlog)
l.errChan = make(chan error, 1)
go func() {
if err := l.srv.ListenAndServe(); err != nil {
l.logger.Error(err)
}
}()
return
}
func (l *wtListener) Accept() (conn net.Conn, err error) {
var ok bool
select {
case conn = <-l.cqueue:
conn = metrics.WrapConn(l.options.Service, conn)
conn = stats.WrapConn(conn, l.options.Stats)
conn = admission.WrapConn(l.options.Admission, conn)
conn = limiter.WrapConn(l.options.TrafficLimiter, conn)
case err, ok = <-l.errChan:
if !ok {
err = listener.ErrClosed
}
}
return
}
func (l *wtListener) Addr() net.Addr {
return l.addr
}
func (l *wtListener) Close() (err error) {
return l.srv.Close()
}
func (l *wtListener) upgrade(w http.ResponseWriter, r *http.Request) {
log := l.logger.WithFields(map[string]any{
"local": l.addr.String(),
"remote": r.RemoteAddr,
})
if l.logger.IsLevelEnabled(logger.TraceLevel) {
dump, _ := httputil.DumpRequest(r, false)
log.Trace(string(dump))
}
s, err := l.srv.Upgrade(w, r)
if err != nil {
l.logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
l.mux(s, log)
}
func (l *wtListener) mux(s *wt.Session, log logger.Logger) (err error) {
defer func() {
if err != nil {
s.CloseWithError(1, err.Error())
} else {
s.CloseWithError(0, "")
}
}()
for {
var stream wt.Stream
stream, err = s.AcceptStream(s.Context())
if err != nil {
log.Errorf("accept stream: %v", err)
return
}
select {
case l.cqueue <- wt_util.Conn(s, stream):
default:
stream.Close()
l.logger.Warnf("connection queue is full, stream %v discarded", stream.StreamID())
}
}
}