1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00

better handling error from Pipe()

This commit is contained in:
Darien Raymond 2016-11-22 00:17:49 +01:00
parent a9946a2dc1
commit 6804d8f73a
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
9 changed files with 51 additions and 36 deletions

View File

@ -1,6 +1,7 @@
package io package io
import ( import (
"io"
"v2ray.com/core/common/log" "v2ray.com/core/common/log"
) )
@ -25,3 +26,11 @@ func Pipe(reader Reader, writer Writer) error {
} }
} }
} }
func PipeUntilEOF(reader Reader, writer Writer) error {
err := Pipe(reader, writer)
if err != nil && err != io.EOF {
return err
}
return nil
}

View File

@ -179,7 +179,9 @@ func (this *DokodemoDoor) HandleTCPConnection(conn internet.Connection) {
v2reader := v2io.NewAdaptiveReader(reader) v2reader := v2io.NewAdaptiveReader(reader)
defer v2reader.Release() defer v2reader.Release()
v2io.Pipe(v2reader, ray.InboundInput()) if err := v2io.PipeUntilEOF(v2reader, ray.InboundInput()); err != nil {
log.Info("Dokodemo: Failed to transport all TCP request: ", err)
}
wg.Done() wg.Done()
ray.InboundInput().Close() ray.InboundInput().Close()
}() }()
@ -189,7 +191,9 @@ func (this *DokodemoDoor) HandleTCPConnection(conn internet.Connection) {
v2writer := v2io.NewAdaptiveWriter(conn) v2writer := v2io.NewAdaptiveWriter(conn)
defer v2writer.Release() defer v2writer.Release()
v2io.Pipe(ray.InboundOutput(), v2writer) if err := v2io.PipeUntilEOF(ray.InboundOutput(), v2writer); err != nil {
log.Info("Dokodemo: Failed to transport all TCP response: ", err)
}
wg.Done() wg.Done()
}() }()

View File

@ -104,7 +104,9 @@ func (this *FreedomConnection) Dispatch(destination v2net.Destination, payload *
v2writer := v2io.NewAdaptiveWriter(conn) v2writer := v2io.NewAdaptiveWriter(conn)
defer v2writer.Release() defer v2writer.Release()
v2io.Pipe(input, v2writer) if err := v2io.PipeUntilEOF(input, v2writer); err != nil {
log.Info("Freedom: Failed to transport all TCP request: ", err)
}
if tcpConn, ok := conn.(*tcp.RawConnection); ok { if tcpConn, ok := conn.(*tcp.RawConnection); ok {
tcpConn.CloseWrite() tcpConn.CloseWrite()
} }
@ -121,7 +123,9 @@ func (this *FreedomConnection) Dispatch(destination v2net.Destination, payload *
} }
v2reader := v2io.NewAdaptiveReader(reader) v2reader := v2io.NewAdaptiveReader(reader)
v2io.Pipe(v2reader, output) if err := v2io.PipeUntilEOF(v2reader, output); err != nil {
log.Info("Freedom: Failed to transport all TCP response: ", err)
}
v2reader.Release() v2reader.Release()
ray.OutboundOutput().Close() ray.OutboundOutput().Close()

View File

@ -160,7 +160,9 @@ func (this *Server) transport(input io.Reader, output io.Writer, ray ray.Inbound
v2reader := v2io.NewAdaptiveReader(input) v2reader := v2io.NewAdaptiveReader(input)
defer v2reader.Release() defer v2reader.Release()
v2io.Pipe(v2reader, ray.InboundInput()) if err := v2io.PipeUntilEOF(v2reader, ray.InboundInput()); err != nil {
log.Info("HTTP: Failed to transport all TCP request: ", err)
}
ray.InboundInput().Close() ray.InboundInput().Close()
wg.Done() wg.Done()
}() }()
@ -169,7 +171,9 @@ func (this *Server) transport(input io.Reader, output io.Writer, ray ray.Inbound
v2writer := v2io.NewAdaptiveWriter(output) v2writer := v2io.NewAdaptiveWriter(output)
defer v2writer.Release() defer v2writer.Release()
v2io.Pipe(ray.InboundOutput(), v2writer) if err := v2io.PipeUntilEOF(ray.InboundOutput(), v2writer); err != nil {
log.Info("HTTP: Failed to transport all TCP response: ", err)
}
ray.InboundOutput().Release() ray.InboundOutput().Release()
wg.Done() wg.Done()
}() }()

View File

@ -2,7 +2,6 @@ package shadowsocks
import ( import (
"errors" "errors"
"io"
"sync" "sync"
"v2ray.com/core/app" "v2ray.com/core/app"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/alloc"
@ -113,18 +112,14 @@ func (this *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffe
return return
} }
if err := v2io.Pipe(responseReader, ray.OutboundOutput()); err != nil { if err := v2io.PipeUntilEOF(responseReader, ray.OutboundOutput()); err != nil {
if err != io.EOF { log.Info("Shadowsocks|Client: Failed to transport all TCP response: ", err)
log.Info("Shadowsocks|Client: Failed to transport all TCP response: ", err)
}
} }
}() }()
bufferedWriter.SetCached(false) bufferedWriter.SetCached(false)
if err := v2io.Pipe(ray.OutboundInput(), bodyWriter); err != nil { if err := v2io.PipeUntilEOF(ray.OutboundInput(), bodyWriter); err != nil {
if err != io.EOF { log.Info("Shadowsocks|Client: Failed to trasnport all TCP request: ", err)
log.Info("Shadowsocks|Client: Failed to trasnport all TCP request: ", err)
}
} }
responseMutex.Lock() responseMutex.Lock()
@ -143,10 +138,8 @@ func (this *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffe
User: user, User: user,
} }
if err := v2io.Pipe(reader, ray.OutboundOutput()); err != nil { if err := v2io.PipeUntilEOF(reader, ray.OutboundOutput()); err != nil {
if err != io.EOF { log.Info("Shadowsocks|Client: Failed to transport all UDP response: ", err)
log.Info("Shadowsocks|Client: Failed to transport all UDP response: ", err)
}
} }
}() }()
@ -159,10 +152,8 @@ func (this *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffe
return errors.New("Shadowsocks|Client: Failed to write payload: " + err.Error()) return errors.New("Shadowsocks|Client: Failed to write payload: " + err.Error())
} }
} }
if err := v2io.Pipe(ray.OutboundInput(), writer); err != nil { if err := v2io.PipeUntilEOF(ray.OutboundInput(), writer); err != nil {
if err != io.EOF { log.Info("Shadowsocks|Client: Failed to transport all UDP request: ", err)
log.Info("Shadowsocks|Client: Failed to transport all UDP request: ", err)
}
} }
responseMutex.Lock() responseMutex.Lock()

View File

@ -198,11 +198,15 @@ func (this *Server) handleConnection(conn internet.Connection) {
responseWriter.Write(payload) responseWriter.Write(payload)
bufferedWriter.SetCached(false) bufferedWriter.SetCached(false)
v2io.Pipe(ray.InboundOutput(), responseWriter) if err := v2io.PipeUntilEOF(ray.InboundOutput(), responseWriter); err != nil {
log.Info("Shadowsocks|Server: Failed to transport all TCP response: ", err)
}
} }
}() }()
v2io.Pipe(bodyReader, ray.InboundInput()) if err := v2io.PipeUntilEOF(bodyReader, ray.InboundInput()); err != nil {
log.Info("Shadowsocks|Server: Failed to transport all TCP request: ", err)
}
ray.InboundInput().Close() ray.InboundInput().Close()
writeFinish.Lock() writeFinish.Lock()

View File

@ -304,14 +304,18 @@ func (this *Server) transport(reader io.Reader, writer io.Writer, session *proxy
v2reader := v2io.NewAdaptiveReader(reader) v2reader := v2io.NewAdaptiveReader(reader)
defer v2reader.Release() defer v2reader.Release()
v2io.Pipe(v2reader, input) if err := v2io.PipeUntilEOF(v2reader, input); err != nil {
log.Info("Socks|Server: Failed to transport all TCP request: ", err)
}
input.Close() input.Close()
}() }()
v2writer := v2io.NewAdaptiveWriter(writer) v2writer := v2io.NewAdaptiveWriter(writer)
defer v2writer.Release() defer v2writer.Release()
v2io.Pipe(output, v2writer) if err := v2io.PipeUntilEOF(output, v2writer); err != nil {
log.Info("Socks|Server: Failed to transport all TCP response: ", err)
}
output.Release() output.Release()
} }

View File

@ -192,8 +192,7 @@ func (this *VMessInboundHandler) HandleConnection(connection internet.Connection
} else { } else {
requestReader = v2io.NewAdaptiveReader(bodyReader) requestReader = v2io.NewAdaptiveReader(bodyReader)
} }
err := v2io.Pipe(requestReader, input) if err := v2io.PipeUntilEOF(requestReader, input); err != nil {
if err != io.EOF {
connection.SetReusable(false) connection.SetReusable(false)
} }
@ -229,8 +228,7 @@ func (this *VMessInboundHandler) HandleConnection(connection internet.Connection
writer.SetCached(false) writer.SetCached(false)
err = v2io.Pipe(output, v2writer) if err := v2io.PipeUntilEOF(output, v2writer); err != nil {
if err != io.EOF {
connection.SetReusable(false) connection.SetReusable(false)
} }

View File

@ -1,7 +1,6 @@
package outbound package outbound
import ( import (
"io"
"sync" "sync"
"v2ray.com/core/app" "v2ray.com/core/app"
@ -107,8 +106,7 @@ func (this *VMessOutboundHandler) handleRequest(session *encoding.ClientSession,
} }
writer.SetCached(false) writer.SetCached(false)
err := v2io.Pipe(input, streamWriter) if err := v2io.PipeUntilEOF(input, streamWriter); err != nil {
if err != io.EOF {
conn.SetReusable(false) conn.SetReusable(false)
} }
@ -150,8 +148,7 @@ func (this *VMessOutboundHandler) handleResponse(session *encoding.ClientSession
bodyReader = v2io.NewAdaptiveReader(decryptReader) bodyReader = v2io.NewAdaptiveReader(decryptReader)
} }
err = v2io.Pipe(bodyReader, output) if err := v2io.PipeUntilEOF(bodyReader, output); err != nil {
if err != io.EOF {
conn.SetReusable(false) conn.SetReusable(false)
} }