Add read deadline implementation

This commit is contained in:
世界 2023-04-08 14:41:53 +08:00
parent 17988ac0d3
commit 5c830455eb
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
8 changed files with 25 additions and 19 deletions

4
go.mod
View file

@ -3,12 +3,12 @@ module github.com/sagernet/sing-shadowsocks
go 1.18
require (
github.com/sagernet/sing v0.2.0
github.com/sagernet/sing v0.2.3-0.20230409094616-7f8eaee1b6c8
golang.org/x/crypto v0.7.0
lukechampine.com/blake3 v1.1.7
)
require (
github.com/klauspost/cpuid/v2 v2.0.12 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/sys v0.7.0 // indirect
)

8
go.sum
View file

@ -1,11 +1,11 @@
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.0.12 h1:p9dKCg8i4gmOxtv35DvrYoWqYzQrvEVdjQ762Y0OqZE=
github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
github.com/sagernet/sing v0.2.0 h1:iyc4TaeXG5XYXixl48zSDDTw46C9NOEAVFq6ZE0dA2k=
github.com/sagernet/sing v0.2.0/go.mod h1:9uHswk2hITw8leDbiLS/xn0t9nzBcbePxzm9PJhwdlw=
github.com/sagernet/sing v0.2.3-0.20230409094616-7f8eaee1b6c8 h1:BWQjek8tNzDzCeHh/8yvjfZ8Id0tl+6pJ+gcPI8tjl8=
github.com/sagernet/sing v0.2.3-0.20230409094616-7f8eaee1b6c8/go.mod h1:Ta8nHnDLAwqySzKhGoKk4ZIB+vJ3GTKj7UPrWYvM+4w=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
lukechampine.com/blake3 v1.1.7 h1:GgRMhmdsuK8+ii6UZFDL8Nb+VyMwadAgcJyfYHxG6n0=
lukechampine.com/blake3 v1.1.7/go.mod h1:tkKEOtDkNtklkXtLNEOGNq5tcV90tJiA1vAA12R78LA=

View file

@ -11,6 +11,7 @@ import (
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
"github.com/sagernet/sing/common/bufio/deadline"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/rw"
@ -94,15 +95,15 @@ func (m *Method) DialConn(conn net.Conn, destination M.Socksaddr) (net.Conn, err
Method: m,
destination: destination,
}
return shadowsocksConn, shadowsocksConn.writeRequest(nil)
return deadline.NewConn(shadowsocksConn), shadowsocksConn.writeRequest(nil)
}
func (m *Method) DialEarlyConn(conn net.Conn, destination M.Socksaddr) net.Conn {
return &clientConn{
return deadline.NewConn(&clientConn{
Conn: conn,
Method: m,
destination: destination,
}
})
}
func (m *Method) DialPacketConn(conn net.Conn) N.NetPacketConn {

View file

@ -14,6 +14,7 @@ import (
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
"github.com/sagernet/sing/common/bufio/deadline"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
@ -125,11 +126,11 @@ func (s *Service) newConnection(ctx context.Context, conn net.Conn, metadata M.M
metadata.Protocol = "shadowsocks"
metadata.Destination = destination
return s.handler.NewConnection(ctx, &serverConn{
return s.handler.NewConnection(ctx, deadline.NewConn(&serverConn{
Service: s,
Conn: conn,
reader: reader,
}, metadata)
}), metadata)
}
func (s *Service) NewError(ctx context.Context, err error) {

View file

@ -22,6 +22,7 @@ import (
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
"github.com/sagernet/sing/common/bufio/deadline"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
@ -198,15 +199,15 @@ func (m *Method) DialConn(conn net.Conn, destination M.Socksaddr) (net.Conn, err
Conn: conn,
destination: destination,
}
return shadowsocksConn, shadowsocksConn.writeRequest(nil)
return deadline.NewConn(shadowsocksConn), shadowsocksConn.writeRequest(nil)
}
func (m *Method) DialEarlyConn(conn net.Conn, destination M.Socksaddr) net.Conn {
return &clientConn{
return deadline.NewConn(&clientConn{
Method: m,
Conn: conn,
destination: destination,
}
})
}
func (m *Method) DialPacketConn(conn net.Conn) N.NetPacketConn {

View file

@ -21,6 +21,7 @@ import (
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
"github.com/sagernet/sing/common/bufio/deadline"
"github.com/sagernet/sing/common/cache"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
@ -245,7 +246,7 @@ func (s *Service) newConnection(ctx context.Context, conn net.Conn, metadata M.M
metadata.Protocol = "shadowsocks"
metadata.Destination = destination
return s.handler.NewConnection(ctx, protocolConn, metadata)
return s.handler.NewConnection(ctx, deadline.NewConn(protocolConn), metadata)
}
type serverConn struct {

View file

@ -17,6 +17,7 @@ import (
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/auth"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio/deadline"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
@ -240,7 +241,7 @@ func (s *MultiService[U]) newConnection(ctx context.Context, conn net.Conn, meta
protocolConn.reader = reader
metadata.Protocol = "shadowsocks"
metadata.Destination = destination
return s.handler.NewConnection(auth.ContextWithUser(ctx, user), protocolConn, metadata)
return s.handler.NewConnection(auth.ContextWithUser(ctx, user), deadline.NewConn(protocolConn), metadata)
}
func (s *MultiService[U]) WriteIsThreadUnsafe() {

View file

@ -13,6 +13,7 @@ import (
"github.com/sagernet/sing-shadowsocks"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio/deadline"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
@ -143,15 +144,15 @@ func (m *Method) DialConn(conn net.Conn, destination M.Socksaddr) (net.Conn, err
Conn: conn,
destination: destination,
}
return shadowsocksConn, shadowsocksConn.writeRequest()
return deadline.NewConn(shadowsocksConn), shadowsocksConn.writeRequest()
}
func (m *Method) DialEarlyConn(conn net.Conn, destination M.Socksaddr) net.Conn {
return &clientConn{
return deadline.NewConn(&clientConn{
Method: m,
Conn: conn,
destination: destination,
}
})
}
func (m *Method) DialPacketConn(conn net.Conn) N.NetPacketConn {