mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-05 06:33:02 +00:00
578d903a9e
* DialSystem for Quic DialSystem() is needed in case of Android client, where the raw conn is protected for vpn service * Fix client dialer log Log such as: tunneling request to tcp:www.google.com:80 via tcp:x.x.x.x:443 the second "tcp" is misleading when using mKcp or quic transport Remove the second "tcp" and add the correct logging for transport dialer: - transport/internet/tcp: dialing TCP to tcp:x.x.x.x:443 - transport/internet/quic: dialing quic to udp:x.x.x.x:443 * Quic new stream allocation mode Currently this is how Quic works: client muxing all tcp and udp traffic through a single session, when there are more than 32 running streams in the session, the next stream request will fail and open with a new session (port). Imagine lineup the session from left to right: | | | | | | As the streams finishes, we still open stream from the left, original session. So the base session will always be there and new sessions on the right come and go. However, either due to QOS or bugs in Quic implementation, the traffic "wear out" the base session. It will become slower and in the end not receiving any data from server side. I couldn't figure out a solution for this problem at the moment, as a workaround: | | | | | | | | I came up with this new stream allocation mode, that it will never open new streams in the old sessions, but only from current or new session from right. The keeplive config is turned off from server and client side. This way old sessions will natually close and new sessions keep generating. Note the frequency of new session is still controlled by the server side. Server can assign a large max stream limit. In this case the new allocation mode will be similar to the current mode.
138 lines
3.2 KiB
Go
138 lines
3.2 KiB
Go
package quic
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/lucas-clemente/quic-go"
|
|
|
|
"github.com/xtls/xray-core/common"
|
|
"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"
|
|
)
|
|
|
|
// Listener is an internet.Listener that listens for TCP connections.
|
|
type Listener struct {
|
|
rawConn *sysConn
|
|
listener quic.Listener
|
|
done *done.Instance
|
|
addConn internet.ConnHandler
|
|
}
|
|
|
|
func (l *Listener) acceptStreams(session quic.Session) {
|
|
for {
|
|
stream, err := session.AcceptStream(context.Background())
|
|
if err != nil {
|
|
newError("failed to accept stream").Base(err).WriteToLog()
|
|
select {
|
|
case <-session.Context().Done():
|
|
return
|
|
case <-l.done.Wait():
|
|
if err := session.CloseWithError(0, ""); err != nil {
|
|
newError("failed to close session").Base(err).WriteToLog()
|
|
}
|
|
return
|
|
default:
|
|
time.Sleep(time.Second)
|
|
continue
|
|
}
|
|
}
|
|
|
|
conn := &interConn{
|
|
stream: stream,
|
|
local: session.LocalAddr(),
|
|
remote: session.RemoteAddr(),
|
|
}
|
|
|
|
l.addConn(conn)
|
|
}
|
|
}
|
|
|
|
func (l *Listener) keepAccepting() {
|
|
for {
|
|
conn, err := l.listener.Accept(context.Background())
|
|
if err != nil {
|
|
newError("failed to accept QUIC sessions").Base(err).WriteToLog()
|
|
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, newError("domain address is not allows for listening quic")
|
|
}
|
|
|
|
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{
|
|
ConnectionIDLength: 12,
|
|
KeepAlive: false,
|
|
MaxIncomingStreams: 32,
|
|
MaxIncomingUniStreams: -1,
|
|
}
|
|
|
|
conn, err := wrapSysConn(rawConn.(*net.UDPConn), config)
|
|
if err != nil {
|
|
conn.Close()
|
|
return nil, err
|
|
}
|
|
|
|
qListener, err := quic.Listen(conn, tlsConfig.GetTLSConfig(), quicConfig)
|
|
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))
|
|
}
|