Close connection more aggressively.

pull/27/head
V2Ray 9 years ago
parent 64103229d6
commit a78dbe7133

@ -27,9 +27,6 @@ func (reader CryptionReader) Read(blocks []byte) (int, error) {
if nBytes > 0 { if nBytes > 0 {
reader.stream.XORKeyStream(blocks[:nBytes], blocks[:nBytes]) reader.stream.XORKeyStream(blocks[:nBytes], blocks[:nBytes])
} }
if err != nil && err != io.EOF {
log.Error("Error reading blocks: %v", err)
}
return nBytes, err return nBytes, err
} }

@ -0,0 +1,30 @@
package net
import (
"net"
"time"
)
var (
emptyTime time.Time
)
type TimeOutReader struct {
timeout int
connection net.Conn
}
func NewTimeOutReader(timeout int, connection net.Conn) *TimeOutReader {
return &TimeOutReader{
timeout: timeout,
connection: connection,
}
}
func (reader *TimeOutReader) Read(p []byte) (n int, err error) {
deadline := time.Duration(reader.timeout) * time.Second
reader.connection.SetReadDeadline(time.Now().Add(deadline))
n, err = reader.connection.Read(p)
reader.connection.SetReadDeadline(emptyTime)
return
}

@ -7,6 +7,7 @@ import (
"net" "net"
"strconv" "strconv"
"sync" "sync"
"time"
"github.com/v2ray/v2ray-core" "github.com/v2ray/v2ray-core"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
@ -56,6 +57,10 @@ func (server *SocksServer) AcceptConnections(listener net.Listener) {
if err != nil { if err != nil {
log.Error("Error on accepting socks connection: %v", err) log.Error("Error on accepting socks connection: %v", err)
} }
if tcpConn, ok := connection.(*net.TCPConn); ok {
tcpConn.SetKeepAlive(true)
tcpConn.SetKeepAlivePeriod(4 * time.Second)
}
go server.HandleConnection(connection) go server.HandleConnection(connection)
} }
} }
@ -63,7 +68,7 @@ func (server *SocksServer) AcceptConnections(listener net.Listener) {
func (server *SocksServer) HandleConnection(connection net.Conn) error { func (server *SocksServer) HandleConnection(connection net.Conn) error {
defer connection.Close() defer connection.Close()
reader := connection.(io.Reader) reader := v2net.NewTimeOutReader(4, connection)
auth, auth4, err := protocol.ReadAuthentication(reader) auth, auth4, err := protocol.ReadAuthentication(reader)
if err != nil && err != protocol.ErrorSocksVersion4 { if err != nil && err != protocol.ErrorSocksVersion4 {

@ -54,6 +54,10 @@ func (handler *VMessInboundHandler) AcceptConnections(listener net.Listener) err
if err != nil { if err != nil {
return log.Error("Failed to accpet connection: %s", err.Error()) return log.Error("Failed to accpet connection: %s", err.Error())
} }
if tcpConn, ok := connection.(*net.TCPConn); ok {
tcpConn.SetKeepAlive(true)
tcpConn.SetKeepAlivePeriod(4 * time.Second)
}
go handler.HandleConnection(connection) go handler.HandleConnection(connection)
} }
return nil return nil
@ -62,18 +66,15 @@ func (handler *VMessInboundHandler) AcceptConnections(listener net.Listener) err
func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error { func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error {
defer connection.Close() defer connection.Close()
reader := protocol.NewVMessRequestReader(handler.clients) connReader := v2net.NewTimeOutReader(4, connection)
requestReader := protocol.NewVMessRequestReader(handler.clients)
// Timeout 4 seconds to prevent DoS attack request, err := requestReader.Read(connReader)
connection.SetReadDeadline(time.Now().Add(requestReadTimeOut))
request, err := reader.Read(connection)
if err != nil { if err != nil {
log.Warning("VMessIn: Invalid request from (%s): %v", connection.RemoteAddr().String(), err) log.Warning("VMessIn: Invalid request from (%s): %v", connection.RemoteAddr().String(), err)
return err return err
} }
log.Debug("VMessIn: Received request for %s", request.Address.String()) log.Debug("VMessIn: Received request for %s", request.Address.String())
// Clear read timeout
connection.SetReadDeadline(zeroTime)
ray := handler.vPoint.DispatchToOutbound(v2net.NewTCPPacket(request.Destination())) ray := handler.vPoint.DispatchToOutbound(v2net.NewTCPPacket(request.Destination()))
input := ray.InboundInput() input := ray.InboundInput()
@ -82,7 +83,7 @@ func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error
readFinish.Lock() readFinish.Lock()
writeFinish.Lock() writeFinish.Lock()
go handleInput(request, connection, input, &readFinish) go handleInput(request, connReader, input, &readFinish)
responseKey := md5.Sum(request.RequestKey[:]) responseKey := md5.Sum(request.RequestKey[:])
responseIV := md5.Sum(request.RequestIV[:]) responseIV := md5.Sum(request.RequestIV[:])

@ -170,8 +170,7 @@ func handleResponse(conn *net.TCPConn, request *protocol.VMessRequest, output ch
response := protocol.VMessResponse{} response := protocol.VMessResponse{}
nBytes, err := decryptResponseReader.Read(response[:]) nBytes, err := decryptResponseReader.Read(response[:])
if err != nil { if err != nil {
log.Error("VMessOut: Failed to read VMess response (%d bytes): %v", nBytes, err) //log.Error("VMessOut: Failed to read VMess response (%d bytes): %v", nBytes, err)
log.Error(InfoTimeNotSync)
return return
} }
if !bytes.Equal(response[:], request.ResponseHeader[:]) { if !bytes.Equal(response[:], request.ResponseHeader[:]) {

Loading…
Cancel
Save