Xray-core/transport/internet/quic/hub.go

147 lines
3.6 KiB
Go
Raw Normal View History

2020-11-25 11:01:53 +00:00
package quic
import (
"context"
"time"
2023-02-02 05:50:21 +00:00
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/logging"
"github.com/quic-go/quic-go/qlog"
2020-12-04 01:36:16 +00:00
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors"
2020-12-04 01:36:16 +00:00
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/protocol/tls/cert"
"github.com/xtls/xray-core/common/signal/done"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/tls"
2020-11-25 11:01:53 +00:00
)
// Listener is an internet.Listener that listens for TCP connections.
type Listener struct {
rawConn *sysConn
listener *quic.Listener
2020-11-25 11:01:53 +00:00
done *done.Instance
addConn internet.ConnHandler
}
func (l *Listener) acceptStreams(conn quic.Connection) {
2020-11-25 11:01:53 +00:00
for {
stream, err := conn.AcceptStream(context.Background())
2020-11-25 11:01:53 +00:00
if err != nil {
errors.LogInfoInner(context.Background(), err, "failed to accept stream")
2020-11-25 11:01:53 +00:00
select {
case <-conn.Context().Done():
2020-11-25 11:01:53 +00:00
return
case <-l.done.Wait():
if err := conn.CloseWithError(0, ""); err != nil {
errors.LogInfoInner(context.Background(), err, "failed to close connection")
2020-11-25 11:01:53 +00:00
}
return
default:
time.Sleep(time.Second)
continue
}
}
conn := &interConn{
stream: stream,
local: conn.LocalAddr(),
remote: conn.RemoteAddr(),
2020-11-25 11:01:53 +00:00
}
l.addConn(conn)
}
}
func (l *Listener) keepAccepting() {
for {
conn, err := l.listener.Accept(context.Background())
if err != nil {
errors.LogInfoInner(context.Background(), err, "failed to accept QUIC connection")
2020-11-25 11:01:53 +00:00
if l.done.Done() {
break
}
time.Sleep(time.Second)
continue
}
go l.acceptStreams(conn)
}
}
// Addr implements internet.Listener.Addr.
func (l *Listener) Addr() net.Addr {
return l.listener.Addr()
}
// Close implements internet.Listener.Close.
func (l *Listener) Close() error {
l.done.Close()
l.listener.Close()
l.rawConn.Close()
return nil
}
// Listen creates a new Listener based on configurations.
func Listen(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, handler internet.ConnHandler) (internet.Listener, error) {
if address.Family().IsDomain() {
return nil, errors.New("domain address is not allows for listening quic")
2020-11-25 11:01:53 +00:00
}
tlsConfig := tls.ConfigFromStreamSettings(streamSettings)
if tlsConfig == nil {
tlsConfig = &tls.Config{
Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil, cert.DNSNames(internalDomain), cert.CommonName(internalDomain)))},
}
}
config := streamSettings.ProtocolSettings.(*Config)
rawConn, err := internet.ListenSystemPacket(context.Background(), &net.UDPAddr{
IP: address.IP(),
Port: int(port),
}, streamSettings.SocketSettings)
if err != nil {
return nil, err
}
quicConfig := &quic.Config{
KeepAlivePeriod: 0,
2022-12-26 00:37:35 +00:00
HandshakeIdleTimeout: time.Second * 8,
MaxIdleTimeout: time.Second * 300,
2020-11-25 11:01:53 +00:00
MaxIncomingStreams: 32,
MaxIncomingUniStreams: -1,
2023-09-30 02:56:57 +00:00
Tracer: func(ctx context.Context, p logging.Perspective, ci quic.ConnectionID) *logging.ConnectionTracer {
2023-06-18 17:42:17 +00:00
return qlog.NewConnectionTracer(&QlogWriter{connID: ci}, p, ci)
},
2020-11-25 11:01:53 +00:00
}
2021-12-01 15:06:00 +00:00
conn, err := wrapSysConn(rawConn.(*net.UDPConn), config)
2020-11-25 11:01:53 +00:00
if err != nil {
conn.Close()
return nil, err
}
tr := quic.Transport{
ConnectionIDLength: 12,
Conn: conn,
}
qListener, err := tr.Listen(tlsConfig.GetTLSConfig(), quicConfig)
2020-11-25 11:01:53 +00:00
if err != nil {
conn.Close()
return nil, err
}
listener := &Listener{
done: done.New(),
rawConn: conn,
listener: qListener,
addConn: handler,
}
go listener.keepAccepting()
return listener, nil
}
func init() {
common.Must(internet.RegisterTransportListener(protocolName, Listen))
}