2019-02-01 14:08:21 -05:00
|
|
|
// +build !confonly
|
|
|
|
|
2018-11-21 10:47:06 -05:00
|
|
|
package quic
|
|
|
|
|
|
|
|
import (
|
2018-11-21 16:02:19 -05:00
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/rand"
|
|
|
|
"errors"
|
2018-11-21 10:47:06 -05:00
|
|
|
"time"
|
|
|
|
|
2020-10-08 22:04:35 -04:00
|
|
|
"github.com/lucas-clemente/quic-go"
|
2021-02-16 15:31:50 -05:00
|
|
|
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/buf"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet"
|
2018-11-21 10:47:06 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type sysConn struct {
|
|
|
|
conn net.PacketConn
|
|
|
|
header internet.PacketHeader
|
2018-11-21 16:02:19 -05:00
|
|
|
auth cipher.AEAD
|
2018-11-21 10:47:06 -05:00
|
|
|
}
|
|
|
|
|
2018-11-21 16:02:19 -05:00
|
|
|
func wrapSysConn(rawConn net.PacketConn, config *Config) (*sysConn, error) {
|
|
|
|
header, err := getHeader(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-11-21 10:47:06 -05:00
|
|
|
}
|
2018-11-21 16:02:19 -05:00
|
|
|
auth, err := getAuth(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &sysConn{
|
|
|
|
conn: rawConn,
|
|
|
|
header: header,
|
|
|
|
auth: auth,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-12-02 13:40:43 -05:00
|
|
|
var errInvalidPacket = errors.New("invalid packet")
|
2018-11-21 10:47:06 -05:00
|
|
|
|
2018-11-21 16:02:19 -05:00
|
|
|
func (c *sysConn) readFromInternal(p []byte) (int, net.Addr, error) {
|
2018-11-21 10:47:06 -05:00
|
|
|
buffer := getBuffer()
|
|
|
|
defer putBuffer(buffer)
|
|
|
|
|
2018-11-21 16:02:19 -05:00
|
|
|
nBytes, addr, err := c.conn.ReadFrom(buffer)
|
2018-11-21 10:47:06 -05:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-21 16:02:19 -05:00
|
|
|
payload := buffer[:nBytes]
|
|
|
|
if c.header != nil {
|
2018-12-02 13:40:43 -05:00
|
|
|
if len(payload) <= int(c.header.Size()) {
|
|
|
|
return 0, nil, errInvalidPacket
|
|
|
|
}
|
2018-11-21 16:02:19 -05:00
|
|
|
payload = payload[c.header.Size():]
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.auth == nil {
|
|
|
|
n := copy(p, payload)
|
|
|
|
return n, addr, nil
|
|
|
|
}
|
2018-11-21 10:47:06 -05:00
|
|
|
|
2018-12-02 13:40:43 -05:00
|
|
|
if len(payload) <= c.auth.NonceSize() {
|
|
|
|
return 0, nil, errInvalidPacket
|
|
|
|
}
|
|
|
|
|
2018-11-21 16:02:19 -05:00
|
|
|
nonce := payload[:c.auth.NonceSize()]
|
|
|
|
payload = payload[c.auth.NonceSize():]
|
|
|
|
|
|
|
|
p, err = c.auth.Open(p[:0], nonce, payload, nil)
|
|
|
|
if err != nil {
|
2018-12-02 13:40:43 -05:00
|
|
|
return 0, nil, errInvalidPacket
|
2018-11-21 16:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return len(p), addr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sysConn) ReadFrom(p []byte) (int, net.Addr, error) {
|
|
|
|
if c.header == nil && c.auth == nil {
|
|
|
|
return c.conn.ReadFrom(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
n, addr, err := c.readFromInternal(p)
|
2018-12-02 13:40:43 -05:00
|
|
|
if err != nil && err != errInvalidPacket {
|
2018-11-21 16:02:19 -05:00
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
return n, addr, nil
|
|
|
|
}
|
|
|
|
}
|
2018-11-21 10:47:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sysConn) WriteTo(p []byte, addr net.Addr) (int, error) {
|
2018-11-21 16:02:19 -05:00
|
|
|
if c.header == nil && c.auth == nil {
|
2018-11-21 10:47:06 -05:00
|
|
|
return c.conn.WriteTo(p, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer := getBuffer()
|
|
|
|
defer putBuffer(buffer)
|
|
|
|
|
2018-11-21 16:02:19 -05:00
|
|
|
payload := buffer
|
|
|
|
n := 0
|
|
|
|
if c.header != nil {
|
|
|
|
c.header.Serialize(payload)
|
|
|
|
n = int(c.header.Size())
|
2018-11-21 10:47:06 -05:00
|
|
|
}
|
2018-11-21 16:02:19 -05:00
|
|
|
|
|
|
|
if c.auth == nil {
|
|
|
|
nBytes := copy(payload[n:], p)
|
|
|
|
n += nBytes
|
|
|
|
} else {
|
|
|
|
nounce := payload[n : n+c.auth.NonceSize()]
|
|
|
|
common.Must2(rand.Read(nounce))
|
|
|
|
n += c.auth.NonceSize()
|
|
|
|
pp := c.auth.Seal(payload[:n], nounce, p, nil)
|
|
|
|
n = len(pp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.conn.WriteTo(payload[:n], addr)
|
2018-11-21 10:47:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sysConn) Close() error {
|
|
|
|
return c.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sysConn) LocalAddr() net.Addr {
|
|
|
|
return c.conn.LocalAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sysConn) SetDeadline(t time.Time) error {
|
|
|
|
return c.conn.SetDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sysConn) SetReadDeadline(t time.Time) error {
|
|
|
|
return c.conn.SetReadDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sysConn) SetWriteDeadline(t time.Time) error {
|
|
|
|
return c.conn.SetWriteDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
type interConn struct {
|
2018-11-26 12:00:41 -05:00
|
|
|
stream quic.Stream
|
|
|
|
local net.Addr
|
|
|
|
remote net.Addr
|
2018-11-21 10:47:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *interConn) Read(b []byte) (int, error) {
|
|
|
|
return c.stream.Read(b)
|
|
|
|
}
|
|
|
|
|
2018-11-22 06:31:11 -05:00
|
|
|
func (c *interConn) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
2018-12-27 10:36:48 -05:00
|
|
|
mb = buf.Compact(mb)
|
2018-12-27 11:00:34 -05:00
|
|
|
mb, err := buf.WriteMultiBuffer(c, mb)
|
|
|
|
buf.ReleaseMulti(mb)
|
|
|
|
return err
|
2018-11-22 06:31:11 -05:00
|
|
|
}
|
|
|
|
|
2018-11-21 10:47:06 -05:00
|
|
|
func (c *interConn) Write(b []byte) (int, error) {
|
|
|
|
return c.stream.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *interConn) Close() error {
|
2018-11-27 04:56:07 -05:00
|
|
|
return c.stream.Close()
|
2018-11-21 10:47:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *interConn) LocalAddr() net.Addr {
|
|
|
|
return c.local
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *interConn) RemoteAddr() net.Addr {
|
|
|
|
return c.remote
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *interConn) SetDeadline(t time.Time) error {
|
|
|
|
return c.stream.SetDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *interConn) SetReadDeadline(t time.Time) error {
|
|
|
|
return c.stream.SetReadDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *interConn) SetWriteDeadline(t time.Time) error {
|
|
|
|
return c.stream.SetWriteDeadline(t)
|
|
|
|
}
|