mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-02-20 23:47:21 -05:00
refactor tproxy handling in dokodemo
This commit is contained in:
parent
7bb5db712d
commit
bb5a959876
@ -117,9 +117,9 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var tConn net.Conn
|
tproxyRequest := func() error {
|
||||||
responseDone := func() error {
|
return nil
|
||||||
defer timer.SetTimeout(plcy.Timeouts.UplinkOnly)
|
}
|
||||||
|
|
||||||
var writer buf.Writer
|
var writer buf.Writer
|
||||||
if network == net.Network_TCP {
|
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.BindAddress = dest.Address.IP()
|
||||||
sockopt.BindPort = uint32(dest.Port)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer tConn.Close()
|
||||||
|
|
||||||
writer = &buf.SequentialWriter{Writer: tConn}
|
writer = &buf.SequentialWriter{Writer: tConn}
|
||||||
tReader := buf.NewReader(tConn)
|
tReader := buf.NewReader(tConn)
|
||||||
go func() {
|
tproxyRequest = func() error {
|
||||||
defer tConn.Close()
|
|
||||||
defer common.Close(link.Writer)
|
|
||||||
if err := buf.Copy(tReader, link.Writer, buf.UpdateActivity(timer)); err != nil {
|
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() {
|
responseDone := func() error {
|
||||||
if tConn != nil {
|
defer timer.SetTimeout(plcy.Timeouts.UplinkOnly)
|
||||||
tConn.Close()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
if err := buf.Copy(link.Reader, writer, buf.UpdateActivity(timer)); err != nil {
|
if err := buf.Copy(link.Reader, writer, buf.UpdateActivity(timer)); err != nil {
|
||||||
return newError("failed to transport response").Base(err)
|
return newError("failed to transport response").Base(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
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.Reader)
|
||||||
common.Interrupt(link.Writer)
|
common.Interrupt(link.Writer)
|
||||||
if tConn != nil {
|
|
||||||
tConn.Close()
|
|
||||||
}
|
|
||||||
return newError("connection ends").Base(err)
|
return newError("connection ends").Base(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user