1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 09:36:34 -05:00

refactor tproxy handling in dokodemo

This commit is contained in:
Darien Raymond 2019-01-27 22:30:34 +01:00
parent 7bb5db712d
commit bb5a959876
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

View File

@ -117,9 +117,9 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in
return nil
}
var tConn net.Conn
responseDone := func() error {
defer timer.SetTimeout(plcy.Timeouts.UplinkOnly)
tproxyRequest := func() error {
return nil
}
var writer buf.Writer
if network == net.Network_TCP {
@ -136,41 +136,35 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in
sockopt.BindAddress = dest.Address.IP()
sockopt.BindPort = uint32(dest.Port)
}
var err error
tConn, err = internet.DialSystem(ctx, net.DestinationFromAddr(conn.RemoteAddr()), sockopt)
tConn, err := internet.DialSystem(ctx, net.DestinationFromAddr(conn.RemoteAddr()), sockopt)
if err != nil {
return err
}
defer tConn.Close()
writer = &buf.SequentialWriter{Writer: tConn}
tReader := buf.NewReader(tConn)
go func() {
defer tConn.Close()
defer common.Close(link.Writer)
tproxyRequest = func() error {
if err := buf.Copy(tReader, link.Writer, buf.UpdateActivity(timer)); err != nil {
newError("failed to transport request (TPROXY conn)").Base(err).WriteToLog()
return newError("failed to transport request (TPROXY conn)").Base(err)
}
return nil
}
}()
}
}
defer func() {
if tConn != nil {
tConn.Close()
}
}()
responseDone := func() error {
defer timer.SetTimeout(plcy.Timeouts.UplinkOnly)
if err := buf.Copy(link.Reader, writer, buf.UpdateActivity(timer)); err != nil {
return newError("failed to transport response").Base(err)
}
return nil
}
if err := task.Run(ctx, task.OnSuccess(requestDone, task.Close(link.Writer)), responseDone); err != nil {
if err := task.Run(ctx, task.OnSuccess(requestDone, task.Close(link.Writer)), responseDone, tproxyRequest); err != nil {
common.Interrupt(link.Reader)
common.Interrupt(link.Writer)
if tConn != nil {
tConn.Close()
}
return newError("connection ends").Base(err)
}