浏览代码

udp for shadowsocks

v2ray 9 年之前
父节点
当前提交
dde3f60e30
共有 2 个文件被更改,包括 97 次插入22 次删除
  1. 14 0
      common/alloc/buffer.go
  2. 83 22
      proxy/shadowsocks/shadowsocks.go

+ 14 - 0
common/alloc/buffer.go

@@ -1,6 +1,7 @@
 package alloc
 
 import (
+	"io"
 	"sync"
 )
 
@@ -72,6 +73,19 @@ func (b *Buffer) Write(data []byte) (int, error) {
 	return len(data), nil
 }
 
+func (b *Buffer) Read(data []byte) (int, error) {
+	if b.Len() == 0 {
+		return 0, io.EOF
+	}
+	nBytes := copy(data, b.Value)
+	if nBytes == b.Len() {
+		b.Value = b.Value[:0]
+	} else {
+		b.Value = b.Value[nBytes:]
+	}
+	return nBytes, nil
+}
+
 type bufferPool struct {
 	chain     chan []byte
 	allocator *sync.Pool

+ 83 - 22
proxy/shadowsocks/shadowsocks.go

@@ -16,11 +16,12 @@ import (
 )
 
 type Shadowsocks struct {
-	space       app.Space
-	config      *Config
-	port        v2net.Port
-	accepting   bool
-	tcpListener *hub.TCPHub
+	space     app.Space
+	config    *Config
+	port      v2net.Port
+	accepting bool
+	tcpHub    *hub.TCPHub
+	udpHub    *hub.UDPHub
 }
 
 func (this *Shadowsocks) Port() v2net.Port {
@@ -29,8 +30,8 @@ func (this *Shadowsocks) Port() v2net.Port {
 
 func (this *Shadowsocks) Close() {
 	this.accepting = false
-	this.tcpListener.Close()
-	this.tcpListener = nil
+	this.tcpHub.Close()
+	this.tcpHub = nil
 }
 
 func (this *Shadowsocks) Listen(port v2net.Port) error {
@@ -41,17 +42,82 @@ func (this *Shadowsocks) Listen(port v2net.Port) error {
 			return proxy.ErrorAlreadyListening
 		}
 	}
+	this.accepting = true
 
-	tcpListener, err := hub.ListenTCP(port, this.handleConnection)
+	tcpHub, err := hub.ListenTCP(port, this.handleConnection)
 	if err != nil {
-		log.Error("Shadowsocks: Failed to listen on port ", port, ": ", err)
+		log.Error("Shadowsocks: Failed to listen TCP on port ", port, ": ", err)
 		return err
 	}
-	this.tcpListener = tcpListener
-	this.accepting = true
+	this.tcpHub = tcpHub
+
+	if this.config.UDP {
+		udpHub, err := hub.ListenUDP(port, this.handlerUDPPayload)
+		if err != nil {
+			log.Error("Shadowsocks: Failed to listen UDP on port ", port, ": ", err)
+		}
+		this.udpHub = udpHub
+	}
+
 	return nil
 }
 
+func (this *Shadowsocks) handlerUDPPayload(payload *alloc.Buffer, dest v2net.Destination) {
+	defer payload.Release()
+
+	iv := payload.Value[:this.config.Cipher.IVSize()]
+	key := this.config.Key
+	payload.SliceFrom(this.config.Cipher.IVSize())
+
+	reader, err := this.config.Cipher.NewDecodingStream(key, iv, payload)
+	if err != nil {
+		log.Error("Shadowsocks: Failed to create decoding stream: ", err)
+		return
+	}
+
+	request, err := ReadRequest(reader)
+	if err != nil {
+		return
+	}
+
+	buffer, _ := v2net.ReadFrom(reader, nil)
+
+	packet := v2net.NewPacket(v2net.TCPDestination(request.Address, request.Port), buffer, false)
+	ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
+	close(ray.InboundInput())
+
+	for respChunk := range ray.InboundOutput() {
+
+		response := alloc.NewBuffer().Slice(0, this.config.Cipher.IVSize())
+		rand.Read(response.Value)
+
+		writer, err := this.config.Cipher.NewEncodingStream(key, response.Value, response)
+		if err != nil {
+			log.Error("Shadowsocks: Failed to create encoding stream: ", err)
+			return
+		}
+
+		switch {
+		case request.Address.IsIPv4():
+			writer.Write([]byte{AddrTypeIPv4})
+			writer.Write(request.Address.IP())
+		case request.Address.IsIPv6():
+			writer.Write([]byte{AddrTypeIPv6})
+			writer.Write(request.Address.IP())
+		case request.Address.IsDomain():
+			writer.Write([]byte{AddrTypeDomain, byte(len(request.Address.Domain()))})
+			writer.Write([]byte(request.Address.Domain()))
+		}
+
+		writer.Write(request.Port.Bytes())
+		writer.Write(respChunk.Value)
+		respChunk.Release()
+
+		this.udpHub.WriteTo(response.Value, dest)
+		response.Release()
+	}
+}
+
 func (this *Shadowsocks) handleConnection(conn *hub.TCPConn) {
 	defer conn.Close()
 
@@ -81,22 +147,17 @@ func (this *Shadowsocks) handleConnection(conn *hub.TCPConn) {
 	packet := v2net.NewPacket(v2net.TCPDestination(request.Address, request.Port), nil, true)
 	ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
 
-	respIv := make([]byte, this.config.Cipher.IVSize())
-	rand.Read(respIv)
-
-	writer, err := this.config.Cipher.NewEncodingStream(key, respIv, conn)
-	if err != nil {
-		log.Error("Shadowsocks: Failed to create encoding stream: ", err)
-		return
-	}
-
 	var writeFinish sync.Mutex
 	writeFinish.Lock()
 	go func() {
-		firstChunk := alloc.NewBuffer().Clear()
+		firstChunk := alloc.NewBuffer().Slice(0, this.config.Cipher.IVSize())
 		defer firstChunk.Release()
 
-		firstChunk.Append(respIv)
+		writer, err := this.config.Cipher.NewEncodingStream(key, firstChunk.Value, conn)
+		if err != nil {
+			log.Error("Shadowsocks: Failed to create encoding stream: ", err)
+			return
+		}
 
 		if payload, ok := <-ray.InboundOutput(); ok {
 			firstChunk.Append(payload.Value)