Add deadline interface

This commit is contained in:
世界 2023-04-17 17:57:35 +08:00
parent 66670cdfb7
commit 4f682e05f1
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
6 changed files with 19 additions and 10 deletions

View file

@ -8,7 +8,6 @@ import (
"net"
"os"
"github.com/sagernet/sing/common/bufio/deadline"
"github.com/sagernet/sing/common/debug"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
@ -96,7 +95,7 @@ func (c *Client) DialContextConn(ctx context.Context, conn net.Conn) (net.Conn,
return nil, err
}
c.logger.TraceContext(ctx, "clint handshake finished")
return deadline.NewConn(newClientConn(hashConn)), nil
return newClientConn(hashConn), nil
case 3:
stream := newStreamWrapper(conn, c.password)
err := c.tlsHandshake(ctx, stream, generateSessionID(c.password))
@ -117,6 +116,6 @@ func (c *Client) DialContextConn(ctx context.Context, conn net.Conn) (net.Conn,
hmacVerify := hmac.New(sha1.New, []byte(c.password))
hmacVerify.Write(serverRandom)
hmacVerify.Write([]byte("S"))
return deadline.NewConn(newVerifiedConn(conn, hmacAdd, hmacVerify, readHMAC)), nil
return newVerifiedConn(conn, hmacAdd, hmacVerify, readHMAC), nil
}
}

2
go.mod
View file

@ -3,7 +3,7 @@ module github.com/sagernet/sing-shadowtls
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.8.0
golang.org/x/sys v0.7.0
)

5
go.sum
View file

@ -1,6 +1,5 @@
github.com/sagernet/sing v0.2.3-0.20230413112320-59e662e6e2ed h1:7Vg+lQanPwjxdYaln/EpKK7OcClvoUNm7Tt9V0FPnpY=
github.com/sagernet/sing v0.2.3-0.20230413112320-59e662e6e2ed/go.mod h1:Ta8nHnDLAwqySzKhGoKk4ZIB+vJ3GTKj7UPrWYvM+4w=
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.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=

View file

@ -12,7 +12,6 @@ import (
"github.com/sagernet/sing/common/auth"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
"github.com/sagernet/sing/common/bufio/deadline"
"github.com/sagernet/sing/common/debug"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
@ -145,7 +144,7 @@ func (s *Service) NewConnection(ctx context.Context, conn net.Conn, metadata M.M
if err == nil {
s.logger.TraceContext(ctx, "handshake finished")
handshakeConn.Close()
return s.handler.NewConnection(ctx, bufio.NewCachedConn(deadline.NewConn(newConn(conn)), request), metadata)
return s.handler.NewConnection(ctx, bufio.NewCachedConn(newConn(conn), request), metadata)
} else if err == os.ErrPermission {
s.logger.WarnContext(ctx, "fallback connection")
hashConn.Fallback()
@ -248,6 +247,6 @@ func (s *Service) NewConnection(ctx context.Context, conn net.Conn, metadata M.M
return E.Cause(err, "handshake relay")
}
s.logger.TraceContext(ctx, "handshake relay finished")
return s.handler.NewConnection(ctx, bufio.NewCachedConn(deadline.NewConn(newVerifiedConn(conn, hmacAdd, hmacVerify, nil)), clientFirstFrame), metadata)
return s.handler.NewConnection(ctx, bufio.NewCachedConn(newVerifiedConn(conn, hmacAdd, hmacVerify, nil), clientFirstFrame), metadata)
}
}

View file

@ -90,6 +90,10 @@ func (c *shadowConn) WriteVectorised(buffers []*buf.Buffer) error {
return c.writer.WriteVectorised(append([]*buf.Buffer{buf.As(header[:])}, buffers...))
}
func (c *shadowConn) NeedAdditionalReadDeadline() bool {
return true
}
func (c *shadowConn) Upstream() any {
return c.Conn
}

View file

@ -142,6 +142,14 @@ func (c *verifiedConn) WriteVectorised(buffers []*buf.Buffer) error {
return c.writer.WriteVectorised(append([]*buf.Buffer{buf.As(header[:])}, buffers...))
}
func (c *verifiedConn) NeedAdditionalReadDeadline() bool {
return true
}
func (c *verifiedConn) Upstream() any {
return c.Conn
}
func verifyApplicationData(frame []byte, hmac hash.Hash, update bool) bool {
if frame[1] != 3 || frame[2] != 3 || len(frame) < tlsHmacHeaderSize {
return false