2022-08-10 20:19:16 +08:00
|
|
|
//go:build with_quic
|
|
|
|
|
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
2023-08-31 11:37:26 +08:00
|
|
|
"github.com/sagernet/quic-go"
|
2022-08-18 23:02:36 +08:00
|
|
|
"github.com/sagernet/quic-go/http3"
|
2023-09-12 21:14:11 +08:00
|
|
|
"github.com/sagernet/sing-quic"
|
2022-08-23 13:22:03 +08:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2022-08-10 20:19:16 +08:00
|
|
|
)
|
|
|
|
|
2022-08-23 13:22:03 +08:00
|
|
|
func (n *Naive) configureHTTP3Listener() error {
|
2023-08-31 11:37:26 +08:00
|
|
|
err := qtls.ConfigureHTTP3(n.tlsConfig)
|
2022-09-09 18:45:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-10 20:19:16 +08:00
|
|
|
|
2022-08-23 13:22:03 +08:00
|
|
|
udpConn, err := n.ListenUDP()
|
2022-08-10 20:19:16 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-31 11:37:26 +08:00
|
|
|
quicListener, err := qtls.ListenEarly(udpConn, n.tlsConfig, &quic.Config{
|
|
|
|
MaxIncomingStreams: 1 << 60,
|
|
|
|
Allow0RTT: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
udpConn.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
h3Server := &http3.Server{
|
|
|
|
Port: int(n.listenOptions.ListenPort),
|
|
|
|
Handler: n,
|
|
|
|
}
|
|
|
|
|
2022-08-10 20:19:16 +08:00
|
|
|
go func() {
|
2023-08-31 11:37:26 +08:00
|
|
|
sErr := h3Server.ServeListener(quicListener)
|
2022-08-23 13:22:03 +08:00
|
|
|
udpConn.Close()
|
|
|
|
if sErr != nil && !E.IsClosedOrCanceled(sErr) {
|
2022-08-10 20:19:16 +08:00
|
|
|
n.logger.Error("http3 server serve error: ", sErr)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
n.h3Server = h3Server
|
|
|
|
return nil
|
|
|
|
}
|