2018-11-21 10:47:06 -05:00
|
|
|
package quic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-11-22 05:57:17 -05:00
|
|
|
"time"
|
2018-11-21 10:47:06 -05:00
|
|
|
|
2023-02-03 04:08:53 -05:00
|
|
|
"github.com/quic-go/quic-go"
|
2021-02-16 15:31:50 -05:00
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/protocol/tls/cert"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/signal/done"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/transport/internet"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/transport/internet/tls"
|
2018-11-21 10:47:06 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Listener is an internet.Listener that listens for TCP connections.
|
|
|
|
type Listener struct {
|
2018-11-24 16:18:17 -05:00
|
|
|
rawConn *sysConn
|
2023-07-01 03:31:19 -04:00
|
|
|
listener *quic.Listener
|
2018-11-24 16:18:17 -05:00
|
|
|
done *done.Instance
|
2018-11-21 10:47:06 -05:00
|
|
|
addConn internet.ConnHandler
|
|
|
|
}
|
|
|
|
|
2022-04-06 00:59:25 -04:00
|
|
|
func (l *Listener) acceptStreams(conn quic.Connection) {
|
2018-11-21 10:47:06 -05:00
|
|
|
for {
|
2022-04-06 00:59:25 -04:00
|
|
|
stream, err := conn.AcceptStream(context.Background())
|
2018-11-21 10:47:06 -05:00
|
|
|
if err != nil {
|
2018-11-26 12:00:41 -05:00
|
|
|
newError("failed to accept stream").Base(err).WriteToLog()
|
2018-11-24 16:18:17 -05:00
|
|
|
select {
|
2022-04-06 00:59:25 -04:00
|
|
|
case <-conn.Context().Done():
|
2018-11-24 16:18:17 -05:00
|
|
|
return
|
|
|
|
case <-l.done.Wait():
|
2022-04-06 00:59:25 -04:00
|
|
|
if err := conn.CloseWithError(0, ""); err != nil {
|
|
|
|
newError("failed to close connection").Base(err).WriteToLog()
|
2020-10-08 22:04:35 -04:00
|
|
|
}
|
2018-11-24 16:18:17 -05:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
2018-11-21 10:47:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
conn := &interConn{
|
|
|
|
stream: stream,
|
2022-04-06 00:59:25 -04:00
|
|
|
local: conn.LocalAddr(),
|
|
|
|
remote: conn.RemoteAddr(),
|
2018-11-21 10:47:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
l.addConn(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Listener) keepAccepting() {
|
|
|
|
for {
|
2020-10-08 22:04:35 -04:00
|
|
|
conn, err := l.listener.Accept(context.Background())
|
2018-11-21 10:47:06 -05:00
|
|
|
if err != nil {
|
2022-04-06 00:59:25 -04:00
|
|
|
newError("failed to accept QUIC connections").Base(err).WriteToLog()
|
2018-11-24 16:18:17 -05:00
|
|
|
if l.done.Done() {
|
|
|
|
break
|
|
|
|
}
|
2018-11-23 17:51:07 -05:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
2018-11-21 10:47:06 -05:00
|
|
|
}
|
|
|
|
go l.acceptStreams(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Addr implements internet.Listener.Addr.
|
2018-11-22 05:57:17 -05:00
|
|
|
func (l *Listener) Addr() net.Addr {
|
|
|
|
return l.listener.Addr()
|
2018-11-21 10:47:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements internet.Listener.Close.
|
2018-11-22 05:57:17 -05:00
|
|
|
func (l *Listener) Close() error {
|
2018-11-24 16:18:17 -05:00
|
|
|
l.done.Close()
|
|
|
|
l.listener.Close()
|
|
|
|
l.rawConn.Close()
|
|
|
|
return nil
|
2018-11-21 10:47:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2018-11-21 16:02:19 -05:00
|
|
|
tlsConfig = &tls.Config{
|
|
|
|
Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil, cert.DNSNames(internalDomain), cert.CommonName(internalDomain)))},
|
|
|
|
}
|
2018-11-21 10:47:06 -05:00
|
|
|
}
|
|
|
|
|
2018-11-21 16:02:19 -05:00
|
|
|
config := streamSettings.ProtocolSettings.(*Config)
|
|
|
|
rawConn, err := internet.ListenSystemPacket(context.Background(), &net.UDPAddr{
|
2018-11-21 10:47:06 -05:00
|
|
|
IP: address.IP(),
|
|
|
|
Port: int(port),
|
|
|
|
}, streamSettings.SocketSettings)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-21 16:02:19 -05:00
|
|
|
quicConfig := &quic.Config{
|
2021-03-22 10:26:30 -04:00
|
|
|
HandshakeIdleTimeout: time.Second * 8,
|
2020-10-08 22:04:35 -04:00
|
|
|
MaxIdleTimeout: time.Second * 45,
|
2018-12-11 03:50:36 -05:00
|
|
|
MaxIncomingStreams: 32,
|
|
|
|
MaxIncomingUniStreams: -1,
|
2022-07-14 23:13:44 -04:00
|
|
|
KeepAlivePeriod: time.Second * 15,
|
2018-11-21 10:47:06 -05:00
|
|
|
}
|
|
|
|
|
2021-11-22 01:13:25 -05:00
|
|
|
conn, err := wrapSysConn(rawConn.(*net.UDPConn), config)
|
2018-11-21 16:02:19 -05:00
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-01 03:31:19 -04:00
|
|
|
tr := quic.Transport{
|
|
|
|
Conn: conn,
|
|
|
|
ConnectionIDLength: 12,
|
|
|
|
}
|
|
|
|
|
|
|
|
qListener, err := tr.Listen(tlsConfig.GetTLSConfig(), quicConfig)
|
2018-11-21 10:47:06 -05:00
|
|
|
if err != nil {
|
2018-11-24 16:18:17 -05:00
|
|
|
conn.Close()
|
2018-11-21 10:47:06 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
listener := &Listener{
|
2018-11-24 16:18:17 -05:00
|
|
|
done: done.New(),
|
|
|
|
rawConn: conn,
|
2018-11-21 10:47:06 -05:00
|
|
|
listener: qListener,
|
|
|
|
addConn: handler,
|
|
|
|
}
|
|
|
|
|
|
|
|
go listener.keepAccepting()
|
|
|
|
|
|
|
|
return listener, nil
|
|
|
|
}
|
2019-02-01 10:56:43 -05:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(internet.RegisterTransportListener(protocolName, Listen))
|
|
|
|
}
|