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"
|
|
|
|
|
|
|
|
quic "github.com/lucas-clemente/quic-go"
|
2018-11-21 16:02:19 -05:00
|
|
|
"v2ray.com/core/common"
|
2018-11-22 06:31:11 -05:00
|
|
|
"v2ray.com/core/common/buf"
|
2018-11-21 10:47:06 -05:00
|
|
|
"v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/transport/internet"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-30 08:41:11 -05:00
|
|
|
func (c *interConn) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
2018-11-30 08:48:44 -05:00
|
|
|
const BufferCount = 16
|
|
|
|
mb := make(buf.MultiBuffer, 0, BufferCount)
|
2018-11-30 08:41:11 -05:00
|
|
|
{
|
|
|
|
b := buf.New()
|
|
|
|
if _, err := b.ReadFrom(c.stream); err != nil {
|
|
|
|
b.Release()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mb = append(mb, b)
|
|
|
|
}
|
|
|
|
|
2018-11-30 08:48:44 -05:00
|
|
|
for len(mb) < BufferCount && c.stream.HasMoreData() {
|
2018-11-30 08:41:11 -05:00
|
|
|
b := buf.New()
|
|
|
|
if _, err := b.ReadFrom(c.stream); err != nil {
|
|
|
|
b.Release()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
mb = append(mb, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
return mb, nil
|
|
|
|
}
|
|
|
|
|
2018-11-22 06:31:11 -05:00
|
|
|
func (c *interConn) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
|
|
|
if mb.IsEmpty() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(mb) == 1 {
|
|
|
|
_, err := c.Write(mb[0].Bytes())
|
|
|
|
buf.ReleaseMulti(mb)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-22 07:36:36 -05:00
|
|
|
b := getBuffer()
|
|
|
|
defer putBuffer(b)
|
2018-11-22 06:31:11 -05:00
|
|
|
|
|
|
|
reader := buf.MultiBufferContainer{
|
|
|
|
MultiBuffer: mb,
|
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
for {
|
2018-11-25 15:56:24 -05:00
|
|
|
nBytes, err := reader.Read(b[:1200])
|
2018-11-22 06:31:11 -05:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2018-11-25 15:56:24 -05:00
|
|
|
if nBytes == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2018-11-22 06:31:11 -05:00
|
|
|
if _, err := c.Write(b[:nBytes]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|