ソースを参照

add retry on socks and vmess inbound

Claire Raymond 10 年 前
コミット
f10f08c87d
2 ファイル変更29 行追加16 行削除
  1. 18 10
      proxy/socks/socks.go
  2. 11 6
      proxy/vmess/vmessin.go

+ 18 - 10
proxy/socks/socks.go

@@ -4,7 +4,6 @@ import (
 	"errors"
 	"io"
 	"net"
-	"strconv"
 	"sync"
 	"time"
 
@@ -12,6 +11,7 @@ import (
 	"github.com/v2ray/v2ray-core/common/alloc"
 	"github.com/v2ray/v2ray-core/common/log"
 	v2net "github.com/v2ray/v2ray-core/common/net"
+	"github.com/v2ray/v2ray-core/common/retry"
 	"github.com/v2ray/v2ray-core/proxy"
 	jsonconfig "github.com/v2ray/v2ray-core/proxy/socks/config/json"
 	"github.com/v2ray/v2ray-core/proxy/socks/protocol"
@@ -37,7 +37,11 @@ func NewSocksServer(dispatcher app.PacketDispatcher, config *jsonconfig.SocksCon
 }
 
 func (server *SocksServer) Listen(port uint16) error {
-	listener, err := net.Listen("tcp", ":"+strconv.Itoa(int(port)))
+	listener, err := net.ListenTCP("tcp", &net.TCPAddr{
+		IP:   []byte{0, 0, 0, 0},
+		Port: int(port),
+		Zone: "",
+	})
 	if err != nil {
 		log.Error("Socks failed to listen on port %d: %v", port, err)
 		return err
@@ -50,18 +54,22 @@ func (server *SocksServer) Listen(port uint16) error {
 	return nil
 }
 
-func (server *SocksServer) AcceptConnections(listener net.Listener) {
+func (server *SocksServer) AcceptConnections(listener *net.TCPListener) {
 	for server.accepting {
-		connection, err := listener.Accept()
-		if err != nil {
-			log.Error("Socks failed to accept new connection %v", err)
-			continue
-		}
-		go server.HandleConnection(connection)
+		retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
+			connection, err := listener.AcceptTCP()
+			if err != nil {
+				log.Error("Socks failed to accept new connection %v", err)
+				return err
+			}
+			go server.HandleConnection(connection)
+			return nil
+		})
+
 	}
 }
 
-func (server *SocksServer) HandleConnection(connection net.Conn) error {
+func (server *SocksServer) HandleConnection(connection *net.TCPConn) error {
 	defer connection.Close()
 
 	reader := v2net.NewTimeOutReader(120, connection)

+ 11 - 6
proxy/vmess/vmessin.go

@@ -11,6 +11,7 @@ import (
 	v2io "github.com/v2ray/v2ray-core/common/io"
 	"github.com/v2ray/v2ray-core/common/log"
 	v2net "github.com/v2ray/v2ray-core/common/net"
+	"github.com/v2ray/v2ray-core/common/retry"
 	"github.com/v2ray/v2ray-core/proxy"
 	"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
 	"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
@@ -53,12 +54,16 @@ func (handler *VMessInboundHandler) Listen(port uint16) error {
 
 func (handler *VMessInboundHandler) AcceptConnections(listener *net.TCPListener) error {
 	for handler.accepting {
-		connection, err := listener.AcceptTCP()
-		if err != nil {
-			log.Error("Failed to accpet connection: %s", err.Error())
-			continue
-		}
-		go handler.HandleConnection(connection)
+		retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
+			connection, err := listener.AcceptTCP()
+			if err != nil {
+				log.Error("Failed to accpet connection: %s", err.Error())
+				return err
+			}
+			go handler.HandleConnection(connection)
+			return nil
+		})
+
 	}
 	return nil
 }