mirror of https://github.com/v2ray/v2ray-core
parent
a57531ef5d
commit
f34ad57b58
|
@ -49,7 +49,7 @@ func Pipe(timer *signal.ActivityTimer, reader Reader, writer Writer) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
timer.UpdateActivity()
|
timer.Update()
|
||||||
|
|
||||||
if buffer.IsEmpty() {
|
if buffer.IsEmpty() {
|
||||||
buffer.Release()
|
buffer.Release()
|
||||||
|
|
|
@ -12,7 +12,7 @@ type ActivityTimer struct {
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ActivityTimer) UpdateActivity() {
|
func (t *ActivityTimer) Update() {
|
||||||
select {
|
select {
|
||||||
case t.updated <- true:
|
case t.updated <- true:
|
||||||
default:
|
default:
|
||||||
|
@ -37,7 +37,8 @@ func (t *ActivityTimer) run() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CancelAfterInactivity(ctx context.Context, cancel context.CancelFunc, timeout time.Duration) *ActivityTimer {
|
func CancelAfterInactivity(ctx context.Context, timeout time.Duration) (context.Context, *ActivityTimer) {
|
||||||
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
timer := &ActivityTimer{
|
timer := &ActivityTimer{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
cancel: cancel,
|
cancel: cancel,
|
||||||
|
@ -45,5 +46,5 @@ func CancelAfterInactivity(ctx context.Context, cancel context.CancelFunc, timeo
|
||||||
updated: make(chan bool, 1),
|
updated: make(chan bool, 1),
|
||||||
}
|
}
|
||||||
go timer.run()
|
go timer.run()
|
||||||
return timer
|
return ctx, timer
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,14 +60,12 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in
|
||||||
log.Info("Dokodemo: Invalid destination. Discarding...")
|
log.Info("Dokodemo: Invalid destination. Discarding...")
|
||||||
return errors.New("Dokodemo: Unable to get destination.")
|
return errors.New("Dokodemo: Unable to get destination.")
|
||||||
}
|
}
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
timeout := time.Second * time.Duration(d.config.Timeout)
|
timeout := time.Second * time.Duration(d.config.Timeout)
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
timeout = time.Minute * 2
|
timeout = time.Minute * 2
|
||||||
}
|
}
|
||||||
timer := signal.CancelAfterInactivity(ctx, cancel, timeout)
|
ctx, timer := signal.CancelAfterInactivity(ctx, timeout)
|
||||||
|
|
||||||
inboundRay, err := dispatcher.Dispatch(ctx, dest)
|
inboundRay, err := dispatcher.Dispatch(ctx, dest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -108,15 +108,12 @@ func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dial
|
||||||
|
|
||||||
conn.SetReusable(false)
|
conn.SetReusable(false)
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
timeout := time.Second * time.Duration(v.timeout)
|
timeout := time.Second * time.Duration(v.timeout)
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
timeout = time.Minute * 5
|
timeout = time.Minute * 5
|
||||||
}
|
}
|
||||||
log.Debug("Freedom: Cancel after ", timeout)
|
log.Debug("Freedom: Cancel after ", timeout)
|
||||||
timer := signal.CancelAfterInactivity(ctx, cancel, timeout)
|
ctx, timer := signal.CancelAfterInactivity(ctx, timeout)
|
||||||
|
|
||||||
requestDone := signal.ExecuteAsync(func() error {
|
requestDone := signal.ExecuteAsync(func() error {
|
||||||
v2writer := buf.NewWriter(conn)
|
v2writer := buf.NewWriter(conn)
|
||||||
|
@ -137,10 +134,9 @@ func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dial
|
||||||
})
|
})
|
||||||
|
|
||||||
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
|
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
|
||||||
log.Info("Freedom: Connection ending with ", err)
|
|
||||||
input.CloseError()
|
input.CloseError()
|
||||||
output.CloseError()
|
output.CloseError()
|
||||||
return err
|
return errors.Base(err).Message("Freedom: Connection ends.")
|
||||||
}
|
}
|
||||||
|
|
||||||
runtime.KeepAlive(timer)
|
runtime.KeepAlive(timer)
|
||||||
|
|
|
@ -121,14 +121,11 @@ func (s *Server) handleConnect(ctx context.Context, request *http.Request, reade
|
||||||
return errors.Base(err).Message("HTTP|Server: Failed to write back OK response.")
|
return errors.Base(err).Message("HTTP|Server: Failed to write back OK response.")
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
timeout := time.Second * time.Duration(s.config.Timeout)
|
timeout := time.Second * time.Duration(s.config.Timeout)
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
timeout = time.Minute * 2
|
timeout = time.Minute * 2
|
||||||
}
|
}
|
||||||
timer := signal.CancelAfterInactivity(ctx, cancel, timeout)
|
ctx, timer := signal.CancelAfterInactivity(ctx, timeout)
|
||||||
ray, err := dispatcher.Dispatch(ctx, dest)
|
ray, err := dispatcher.Dispatch(ctx, dest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -90,17 +90,13 @@ func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, diale
|
||||||
request.Option |= RequestOptionOneTimeAuth
|
request.Option |= RequestOptionOneTimeAuth
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
ctx, timer := signal.CancelAfterInactivity(ctx, time.Minute*2)
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
timer := signal.CancelAfterInactivity(ctx, cancel, time.Minute*2)
|
|
||||||
|
|
||||||
if request.Command == protocol.RequestCommandTCP {
|
if request.Command == protocol.RequestCommandTCP {
|
||||||
bufferedWriter := buf.NewBufferedWriter(conn)
|
bufferedWriter := buf.NewBufferedWriter(conn)
|
||||||
bodyWriter, err := WriteTCPRequest(request, bufferedWriter)
|
bodyWriter, err := WriteTCPRequest(request, bufferedWriter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Info("Shadowsocks|Client: Failed to write request: ", err)
|
return errors.Base(err).Message("Shadowsocks|Client: Failed to write request")
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := bufferedWriter.SetBuffered(false); err != nil {
|
if err := bufferedWriter.SetBuffered(false); err != nil {
|
||||||
|
@ -131,8 +127,7 @@ func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, diale
|
||||||
})
|
})
|
||||||
|
|
||||||
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
|
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
|
||||||
log.Info("Shadowsocks|Client: Connection ends with ", err)
|
return errors.Base(err).Message("Shadowsocks|Client: Connection ends.")
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -147,8 +142,7 @@ func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, diale
|
||||||
|
|
||||||
requestDone := signal.ExecuteAsync(func() error {
|
requestDone := signal.ExecuteAsync(func() error {
|
||||||
if err := buf.PipeUntilEOF(timer, outboundRay.OutboundInput(), writer); err != nil {
|
if err := buf.PipeUntilEOF(timer, outboundRay.OutboundInput(), writer); err != nil {
|
||||||
log.Info("Shadowsocks|Client: Failed to transport all UDP request: ", err)
|
return errors.Base(err).Message("Shadowsocks|Client: Failed to transport all UDP request")
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
@ -162,15 +156,13 @@ func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, diale
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := buf.PipeUntilEOF(timer, reader, outboundRay.OutboundOutput()); err != nil {
|
if err := buf.PipeUntilEOF(timer, reader, outboundRay.OutboundOutput()); err != nil {
|
||||||
log.Info("Shadowsocks|Client: Failed to transport all UDP response: ", err)
|
return errors.Base(err).Message("Shadowsocks|Client: Failed to transport all UDP response.")
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
|
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
|
||||||
log.Info("Shadowsocks|Client: Connection ends with ", err)
|
return errors.Base(err).Message("Shadowsocks|Client: Connection ends.")
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -134,8 +134,7 @@ func (s *Server) handleConnection(ctx context.Context, conn internet.Connection,
|
||||||
request, bodyReader, err := ReadTCPSession(s.user, bufferedReader)
|
request, bodyReader, err := ReadTCPSession(s.user, bufferedReader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Access(conn.RemoteAddr(), "", log.AccessRejected, err)
|
log.Access(conn.RemoteAddr(), "", log.AccessRejected, err)
|
||||||
log.Info("Shadowsocks|Server: Failed to create request from: ", conn.RemoteAddr(), ": ", err)
|
return errors.Base(err).Message("Shadowsocks|Server: Failed to create request from: ", conn.RemoteAddr())
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
conn.SetReadDeadline(time.Time{})
|
conn.SetReadDeadline(time.Time{})
|
||||||
|
|
||||||
|
@ -147,9 +146,8 @@ func (s *Server) handleConnection(ctx context.Context, conn internet.Connection,
|
||||||
|
|
||||||
ctx = protocol.ContextWithUser(ctx, request.User)
|
ctx = protocol.ContextWithUser(ctx, request.User)
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
|
||||||
userSettings := s.user.GetSettings()
|
userSettings := s.user.GetSettings()
|
||||||
timer := signal.CancelAfterInactivity(ctx, cancel, userSettings.PayloadTimeout)
|
ctx, timer := signal.CancelAfterInactivity(ctx, userSettings.PayloadTimeout)
|
||||||
ray, err := dispatcher.Dispatch(ctx, dest)
|
ray, err := dispatcher.Dispatch(ctx, dest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -159,8 +157,7 @@ func (s *Server) handleConnection(ctx context.Context, conn internet.Connection,
|
||||||
bufferedWriter := buf.NewBufferedWriter(conn)
|
bufferedWriter := buf.NewBufferedWriter(conn)
|
||||||
responseWriter, err := WriteTCPResponse(request, bufferedWriter)
|
responseWriter, err := WriteTCPResponse(request, bufferedWriter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warning("Shadowsocks|Server: Failed to write response: ", err)
|
return errors.Base(err).Message("Shadowsocks|Server: Failed to write response.")
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
payload, err := ray.InboundOutput().Read()
|
payload, err := ray.InboundOutput().Read()
|
||||||
|
@ -177,8 +174,7 @@ func (s *Server) handleConnection(ctx context.Context, conn internet.Connection,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := buf.PipeUntilEOF(timer, ray.InboundOutput(), responseWriter); err != nil {
|
if err := buf.PipeUntilEOF(timer, ray.InboundOutput(), responseWriter); err != nil {
|
||||||
log.Info("Shadowsocks|Server: Failed to transport all TCP response: ", err)
|
return errors.Base(err).Message("Shadowsocks|Server: Failed to transport all TCP response.")
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -188,18 +184,15 @@ func (s *Server) handleConnection(ctx context.Context, conn internet.Connection,
|
||||||
defer ray.InboundInput().Close()
|
defer ray.InboundInput().Close()
|
||||||
|
|
||||||
if err := buf.PipeUntilEOF(timer, bodyReader, ray.InboundInput()); err != nil {
|
if err := buf.PipeUntilEOF(timer, bodyReader, ray.InboundInput()); err != nil {
|
||||||
log.Info("Shadowsocks|Server: Failed to transport all TCP request: ", err)
|
return errors.Base(err).Message("Shadowsocks|Server: Failed to transport all TCP request.")
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
|
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
|
||||||
log.Info("Shadowsocks|Server: Connection ends with ", err)
|
|
||||||
cancel()
|
|
||||||
ray.InboundInput().CloseError()
|
ray.InboundInput().CloseError()
|
||||||
ray.InboundOutput().CloseError()
|
ray.InboundOutput().CloseError()
|
||||||
return err
|
return errors.Base(err).Message("Shadowsocks|Server: Connection ends.")
|
||||||
}
|
}
|
||||||
|
|
||||||
runtime.KeepAlive(timer)
|
runtime.KeepAlive(timer)
|
||||||
|
|
|
@ -81,10 +81,7 @@ func (c *Client) Process(ctx context.Context, ray ray.OutboundRay, dialer proxy.
|
||||||
return errors.Base(err).RequireUserAction().Message("Socks|Client: Failed to establish connection to server.")
|
return errors.Base(err).RequireUserAction().Message("Socks|Client: Failed to establish connection to server.")
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
ctx, timer := signal.CancelAfterInactivity(ctx, time.Minute*2)
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
timer := signal.CancelAfterInactivity(ctx, cancel, time.Minute*2)
|
|
||||||
|
|
||||||
var requestFunc func() error
|
var requestFunc func() error
|
||||||
var responseFunc func() error
|
var responseFunc func() error
|
||||||
|
|
|
@ -110,14 +110,11 @@ func (*Server) handleUDP() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Server) transport(ctx context.Context, reader io.Reader, writer io.Writer, dest net.Destination, dispatcher dispatcher.Interface) error {
|
func (v *Server) transport(ctx context.Context, reader io.Reader, writer io.Writer, dest net.Destination, dispatcher dispatcher.Interface) error {
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
timeout := time.Second * time.Duration(v.config.Timeout)
|
timeout := time.Second * time.Duration(v.config.Timeout)
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
timeout = time.Minute * 2
|
timeout = time.Minute * 2
|
||||||
}
|
}
|
||||||
timer := signal.CancelAfterInactivity(ctx, cancel, timeout)
|
ctx, timer := signal.CancelAfterInactivity(ctx, timeout)
|
||||||
|
|
||||||
ray, err := dispatcher.Dispatch(ctx, dest)
|
ray, err := dispatcher.Dispatch(ctx, dest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -193,10 +193,8 @@ func (v *Handler) Process(ctx context.Context, network net.Network, connection i
|
||||||
userSettings := request.User.GetSettings()
|
userSettings := request.User.GetSettings()
|
||||||
|
|
||||||
ctx = protocol.ContextWithUser(ctx, request.User)
|
ctx = protocol.ContextWithUser(ctx, request.User)
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
timer := signal.CancelAfterInactivity(ctx, cancel, userSettings.PayloadTimeout)
|
ctx, timer := signal.CancelAfterInactivity(ctx, userSettings.PayloadTimeout)
|
||||||
ray, err := dispatcher.Dispatch(ctx, request.Destination())
|
ray, err := dispatcher.Dispatch(ctx, request.Destination())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -105,10 +105,7 @@ func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dial
|
||||||
|
|
||||||
session := encoding.NewClientSession(protocol.DefaultIDHash)
|
session := encoding.NewClientSession(protocol.DefaultIDHash)
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
ctx, timer := signal.CancelAfterInactivity(ctx, time.Minute*2)
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
timer := signal.CancelAfterInactivity(ctx, cancel, time.Minute*2)
|
|
||||||
|
|
||||||
requestDone := signal.ExecuteAsync(func() error {
|
requestDone := signal.ExecuteAsync(func() error {
|
||||||
writer := buf.NewBufferedWriter(conn)
|
writer := buf.NewBufferedWriter(conn)
|
||||||
|
|
Loading…
Reference in New Issue