Add deadline interface

This commit is contained in:
世界 2023-04-17 18:01:32 +08:00
parent 6489261191
commit f772573405
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
8 changed files with 36 additions and 22 deletions

2
go.mod
View file

@ -3,7 +3,7 @@ module github.com/sagernet/sing-shadowsocks
go 1.18
require (
github.com/sagernet/sing v0.2.3
github.com/sagernet/sing v0.2.4-0.20230417102916-f4e8bc868f61
golang.org/x/crypto v0.7.0
lukechampine.com/blake3 v1.1.7
)

4
go.sum
View file

@ -1,8 +1,8 @@
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.3 h1:V50MvZ4c3Iij2lYFWPlzL1PyipwSzjGeN9x+Ox89vpk=
github.com/sagernet/sing v0.2.3/go.mod h1:Ta8nHnDLAwqySzKhGoKk4ZIB+vJ3GTKj7UPrWYvM+4w=
github.com/sagernet/sing v0.2.4-0.20230417102916-f4e8bc868f61 h1:1pISBNsAYZ6o1oZiNhEkoVDYWfcZQJoRtwRnJuuqndg=
github.com/sagernet/sing v0.2.4-0.20230417102916-f4e8bc868f61/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.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=

View file

@ -11,7 +11,6 @@ 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"
@ -95,15 +94,15 @@ func (m *Method) DialConn(conn net.Conn, destination M.Socksaddr) (net.Conn, err
Method: m,
destination: destination,
}
return deadline.NewConn(shadowsocksConn), shadowsocksConn.writeRequest(nil)
return shadowsocksConn, shadowsocksConn.writeRequest(nil)
}
func (m *Method) DialEarlyConn(conn net.Conn, destination M.Socksaddr) net.Conn {
return deadline.NewConn(&clientConn{
return &clientConn{
Conn: conn,
Method: m,
destination: destination,
})
}
}
func (m *Method) DialPacketConn(conn net.Conn) N.NetPacketConn {
@ -232,6 +231,10 @@ func (c *clientConn) NeedHandshake() bool {
return c.writer == nil
}
func (c *clientConn) NeedAdditionalReadDeadline() bool {
return true
}
func (c *clientConn) Upstream() any {
return c.Conn
}

View file

@ -12,7 +12,6 @@ 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"
@ -97,11 +96,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, deadline.NewConn(&serverConn{
return s.handler.NewConnection(ctx, &serverConn{
Method: s.Method,
Conn: conn,
reader: reader,
}), metadata)
}, metadata)
}
func (s *Service) NewError(ctx context.Context, err error) {
@ -185,6 +184,10 @@ func (c *serverConn) WriteTo(w io.Writer) (n int64, err error) {
return c.reader.WriteTo(w)
}
func (c *serverConn) NeedAdditionalReadDeadline() bool {
return true
}
func (c *serverConn) Upstream() any {
return c.Conn
}

View file

@ -22,7 +22,6 @@ 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"
@ -199,15 +198,15 @@ func (m *Method) DialConn(conn net.Conn, destination M.Socksaddr) (net.Conn, err
Conn: conn,
destination: destination,
}
return deadline.NewConn(shadowsocksConn), shadowsocksConn.writeRequest(nil)
return shadowsocksConn, shadowsocksConn.writeRequest(nil)
}
func (m *Method) DialEarlyConn(conn net.Conn, destination M.Socksaddr) net.Conn {
return deadline.NewConn(&clientConn{
return &clientConn{
Method: m,
Conn: conn,
destination: destination,
})
}
}
func (m *Method) DialPacketConn(conn net.Conn) N.NetPacketConn {
@ -461,6 +460,10 @@ func (c *clientConn) NeedHandshake() bool {
return c.writer == nil
}
func (c *clientConn) NeedAdditionalReadDeadline() bool {
return true
}
func (c *clientConn) Upstream() any {
return c.Conn
}

View file

@ -21,7 +21,6 @@ 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"
@ -246,7 +245,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, deadline.NewConn(protocolConn), metadata)
return s.handler.NewConnection(ctx, protocolConn, metadata)
}
type serverConn struct {
@ -382,6 +381,10 @@ func (c *serverConn) Close() error {
)
}
func (c *serverConn) NeedAdditionalReadDeadline() bool {
return true
}
func (c *serverConn) Upstream() any {
return c.Conn
}

View file

@ -17,7 +17,6 @@ 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"
@ -243,7 +242,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), deadline.NewConn(protocolConn), metadata)
return s.handler.NewConnection(auth.ContextWithUser(ctx, user), protocolConn, metadata)
}
func (s *MultiService[U]) WriteIsThreadUnsafe() {

View file

@ -13,7 +13,6 @@ 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"
@ -144,15 +143,15 @@ func (m *Method) DialConn(conn net.Conn, destination M.Socksaddr) (net.Conn, err
Conn: conn,
destination: destination,
}
return deadline.NewConn(shadowsocksConn), shadowsocksConn.writeRequest()
return shadowsocksConn, shadowsocksConn.writeRequest()
}
func (m *Method) DialEarlyConn(conn net.Conn, destination M.Socksaddr) net.Conn {
return deadline.NewConn(&clientConn{
return &clientConn{
Method: m,
Conn: conn,
destination: destination,
})
}
}
func (m *Method) DialPacketConn(conn net.Conn) N.NetPacketConn {
@ -239,6 +238,10 @@ func (c *clientConn) Write(p []byte) (n int, err error) {
return c.Conn.Write(p)
}
func (c *clientConn) NeedAdditionalReadDeadline() bool {
return true
}
func (c *clientConn) Upstream() any {
return c.Conn
}