Fix: vmess udp crash

This commit is contained in:
gVisor bot 2020-02-17 17:34:19 +08:00
parent 85ecb84e88
commit 05d45cbedc
9 changed files with 81 additions and 43 deletions

View file

@ -65,8 +65,13 @@ func newConn(c net.Conn, a C.ProxyAdapter) C.Conn {
return &conn{c, []string{a.Name()}}
}
type packetConn struct {
type PacketConn interface {
net.PacketConn
WriteWithMetadata(p []byte, metadata *C.Metadata) (n int, err error)
}
type packetConn struct {
PacketConn
chain C.Chain
}
@ -78,8 +83,8 @@ func (c *packetConn) AppendToChains(a C.ProxyAdapter) {
c.chain = append(c.chain, a.Name())
}
func newPacketConn(c net.PacketConn, a C.ProxyAdapter) C.PacketConn {
return &packetConn{c, []string{a.Name()}}
func newPacketConn(pc PacketConn, a C.ProxyAdapter) C.PacketConn {
return &packetConn{pc, []string{a.Name()}}
}
type Proxy struct {

View file

@ -5,6 +5,7 @@ import (
"net"
"github.com/Dreamacro/clash/component/dialer"
"github.com/Dreamacro/clash/component/resolver"
C "github.com/Dreamacro/clash/constant"
)
@ -31,7 +32,22 @@ func (d *Direct) DialUDP(metadata *C.Metadata) (C.PacketConn, error) {
if err != nil {
return nil, err
}
return newPacketConn(pc, d), nil
return newPacketConn(&directPacketConn{pc}, d), nil
}
type directPacketConn struct {
net.PacketConn
}
func (dp *directPacketConn) WriteWithMetadata(p []byte, metadata *C.Metadata) (n int, err error) {
if !metadata.Resolved() {
ip, err := resolver.ResolveIP(metadata.Host)
if err != nil {
return 0, err
}
metadata.DstIP = ip
}
return dp.WriteTo(p, metadata.UDPAddr())
}
func NewDirect() *Direct {

View file

@ -95,7 +95,7 @@ func (ss *ShadowSocks) DialUDP(metadata *C.Metadata) (C.PacketConn, error) {
}
pc = ss.cipher.PacketConn(pc)
return newPacketConn(&ssUDPConn{PacketConn: pc, rAddr: addr}, ss), nil
return newPacketConn(&ssPacketConn{PacketConn: pc, rAddr: addr}, ss), nil
}
func (ss *ShadowSocks) MarshalJSON() ([]byte, error) {
@ -183,21 +183,29 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
}, nil
}
type ssUDPConn struct {
type ssPacketConn struct {
net.PacketConn
rAddr net.Addr
}
func (uc *ssUDPConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
func (spc *ssPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
packet, err := socks5.EncodeUDPPacket(socks5.ParseAddrToSocksAddr(addr), b)
if err != nil {
return
}
return uc.PacketConn.WriteTo(packet[3:], uc.rAddr)
return spc.PacketConn.WriteTo(packet[3:], spc.rAddr)
}
func (uc *ssUDPConn) ReadFrom(b []byte) (int, net.Addr, error) {
n, _, e := uc.PacketConn.ReadFrom(b)
func (spc *ssPacketConn) WriteWithMetadata(p []byte, metadata *C.Metadata) (n int, err error) {
packet, err := socks5.EncodeUDPPacket(socks5.ParseAddr(metadata.RemoteAddress()), p)
if err != nil {
return
}
return spc.PacketConn.WriteTo(packet[3:], spc.rAddr)
}
func (spc *ssPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
n, _, e := spc.PacketConn.ReadFrom(b)
addr := socks5.SplitAddr(b[:n])
var from net.Addr
if e == nil {

View file

@ -110,7 +110,7 @@ func (ss *Socks5) DialUDP(metadata *C.Metadata) (_ C.PacketConn, err error) {
pc.Close()
}()
return newPacketConn(&socksUDPConn{PacketConn: pc, rAddr: bindAddr.UDPAddr(), tcpConn: c}, ss), nil
return newPacketConn(&socksPacketConn{PacketConn: pc, rAddr: bindAddr.UDPAddr(), tcpConn: c}, ss), nil
}
func NewSocks5(option Socks5Option) *Socks5 {
@ -138,13 +138,13 @@ func NewSocks5(option Socks5Option) *Socks5 {
}
}
type socksUDPConn struct {
type socksPacketConn struct {
net.PacketConn
rAddr net.Addr
tcpConn net.Conn
}
func (uc *socksUDPConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
func (uc *socksPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
packet, err := socks5.EncodeUDPPacket(socks5.ParseAddrToSocksAddr(addr), b)
if err != nil {
return
@ -152,7 +152,15 @@ func (uc *socksUDPConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
return uc.PacketConn.WriteTo(packet, uc.rAddr)
}
func (uc *socksUDPConn) ReadFrom(b []byte) (int, net.Addr, error) {
func (uc *socksPacketConn) WriteWithMetadata(p []byte, metadata *C.Metadata) (n int, err error) {
packet, err := socks5.EncodeUDPPacket(socks5.ParseAddr(metadata.RemoteAddress()), p)
if err != nil {
return
}
return uc.PacketConn.WriteTo(packet, uc.rAddr)
}
func (uc *socksPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
n, a, e := uc.PacketConn.ReadFrom(b)
if e != nil {
return 0, nil, e
@ -167,7 +175,7 @@ func (uc *socksUDPConn) ReadFrom(b []byte) (int, net.Addr, error) {
return n - addrLength - 3, a, nil
}
func (uc *socksUDPConn) Close() error {
func (uc *socksPacketConn) Close() error {
uc.tcpConn.Close()
return uc.PacketConn.Close()
}

View file

@ -2,12 +2,14 @@ package outbound
import (
"context"
"errors"
"fmt"
"net"
"strconv"
"strings"
"github.com/Dreamacro/clash/component/dialer"
"github.com/Dreamacro/clash/component/resolver"
"github.com/Dreamacro/clash/component/vmess"
C "github.com/Dreamacro/clash/constant"
)
@ -44,6 +46,15 @@ func (v *Vmess) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn,
}
func (v *Vmess) DialUDP(metadata *C.Metadata) (C.PacketConn, error) {
// vmess use stream-oriented udp, so clash needs a net.UDPAddr
if !metadata.Resolved() {
ip, err := resolver.ResolveIP(metadata.Host)
if err != nil {
return nil, errors.New("can't resolve ip")
}
metadata.DstIP = ip
}
ctx, cancel := context.WithTimeout(context.Background(), tcpTimeout)
defer cancel()
c, err := dialer.DialContext(ctx, "tcp", v.server)
@ -55,7 +66,7 @@ func (v *Vmess) DialUDP(metadata *C.Metadata) (C.PacketConn, error) {
if err != nil {
return nil, fmt.Errorf("new vmess client error: %v", err)
}
return newPacketConn(&vmessUDPConn{Conn: c, rAddr: metadata.UDPAddr()}, v), nil
return newPacketConn(&vmessPacketConn{Conn: c, rAddr: metadata.UDPAddr()}, v), nil
}
func NewVmess(option VmessOption) (*Vmess, error) {
@ -116,16 +127,20 @@ func parseVmessAddr(metadata *C.Metadata) *vmess.DstAddr {
}
}
type vmessUDPConn struct {
type vmessPacketConn struct {
net.Conn
rAddr net.Addr
}
func (uc *vmessUDPConn) WriteTo(b []byte, addr net.Addr) (int, error) {
func (uc *vmessPacketConn) WriteTo(b []byte, addr net.Addr) (int, error) {
return uc.Conn.Write(b)
}
func (uc *vmessUDPConn) ReadFrom(b []byte) (int, net.Addr, error) {
func (uc *vmessPacketConn) WriteWithMetadata(p []byte, metadata *C.Metadata) (n int, err error) {
return uc.Conn.Write(p)
}
func (uc *vmessPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
n, err := uc.Conn.Read(b)
return n, uc.rAddr, err
}

View file

@ -1,24 +1,25 @@
package nat
import (
"net"
"sync"
C "github.com/Dreamacro/clash/constant"
)
type Table struct {
mapping sync.Map
}
func (t *Table) Set(key string, pc net.PacketConn) {
func (t *Table) Set(key string, pc C.PacketConn) {
t.mapping.Store(key, pc)
}
func (t *Table) Get(key string) net.PacketConn {
func (t *Table) Get(key string) C.PacketConn {
item, exist := t.mapping.Load(key)
if !exist {
return nil
}
return item.(net.PacketConn)
return item.(C.PacketConn)
}
func (t *Table) GetOrCreateLock(key string) (*sync.WaitGroup, bool) {

View file

@ -53,6 +53,7 @@ type Conn interface {
type PacketConn interface {
net.PacketConn
Connection
WriteWithMetadata(p []byte, metadata *Metadata) (n int, err error)
}
type ProxyAdapter interface {

View file

@ -81,8 +81,8 @@ func handleHTTP(request *adapters.HTTPAdapter, outbound net.Conn) {
}
}
func handleUDPToRemote(packet C.UDPPacket, pc net.PacketConn, addr net.Addr) {
if _, err := pc.WriteTo(packet.Data(), addr); err != nil {
func handleUDPToRemote(packet C.UDPPacket, pc C.PacketConn, metadata *C.Metadata) {
if _, err := pc.WriteWithMetadata(packet.Data(), metadata); err != nil {
return
}
DefaultManager.Upload() <- int64(len(packet.Data()))

View file

@ -190,16 +190,7 @@ func handleUDPConn(packet *inbound.PacketAdapter) {
key := packet.LocalAddr().String()
pc := natTable.Get(key)
if pc != nil {
if !metadata.Resolved() {
ip, err := resolver.ResolveIP(metadata.Host)
if err != nil {
log.Warnln("[UDP] Resolve %s failed: %s, %#v", metadata.Host, err.Error(), metadata)
return
}
metadata.DstIP = ip
}
handleUDPToRemote(packet, pc, metadata.UDPAddr())
handleUDPToRemote(packet, pc, metadata)
return
}
@ -246,14 +237,7 @@ func handleUDPConn(packet *inbound.PacketAdapter) {
wg.Wait()
pc := natTable.Get(key)
if pc != nil {
if !metadata.Resolved() {
ip, err := resolver.ResolveIP(metadata.Host)
if err != nil {
return
}
metadata.DstIP = ip
}
handleUDPToRemote(packet, pc, metadata.UDPAddr())
handleUDPToRemote(packet, pc, metadata)
}
}()
}