1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-09 11:51:02 -05:00
v2fly/transport/internet/quic/hub.go

138 lines
3.0 KiB
Go
Raw Normal View History

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
quic "github.com/lucas-clemente/quic-go"
"v2ray.com/core/common"
"v2ray.com/core/common/net"
2018-11-21 16:02:19 -05:00
"v2ray.com/core/common/protocol/tls/cert"
2018-11-24 16:18:17 -05:00
"v2ray.com/core/common/signal/done"
2018-11-21 10:47:06 -05:00
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/internet/tls"
)
// Listener is an internet.Listener that listens for TCP connections.
type Listener struct {
2018-11-24 16:18:17 -05:00
rawConn *sysConn
2018-11-21 10:47:06 -05: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
}
func (l *Listener) acceptStreams(session quic.Session) {
for {
stream, err := session.AcceptStream()
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 {
case <-session.Context().Done():
return
case <-l.done.Wait():
session.Close()
return
default:
time.Sleep(time.Second)
continue
}
2018-11-21 10:47:06 -05:00
}
conn := &interConn{
stream: stream,
local: session.LocalAddr(),
remote: session.RemoteAddr(),
}
l.addConn(conn)
}
}
func (l *Listener) keepAccepting() {
for {
conn, err := l.listener.Accept()
if err != nil {
2018-11-26 12:00:41 -05:00
newError("failed to accept QUIC sessions").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{
2018-11-25 15:56:24 -05:00
ConnectionIDLength: 12,
HandshakeTimeout: time.Second * 8,
2018-11-28 17:14:41 -05:00
IdleTimeout: time.Second * 30,
2018-11-29 11:17:07 -05:00
MaxIncomingStreams: 128,
MaxIncomingUniStreams: 32,
2018-11-21 10:47:06 -05:00
}
2018-11-21 16:02:19 -05:00
conn, err := wrapSysConn(rawConn, config)
if err != nil {
conn.Close()
return nil, err
}
qListener, err := quic.Listen(conn, 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
}
func init() {
common.Must(internet.RegisterTransportListener(protocolName, Listen))
}