chore: code cleanup

This commit is contained in:
wwqgtxx 2025-02-17 19:43:58 +08:00
parent 9962a0d091
commit 808fdcf624
9 changed files with 46 additions and 45 deletions

View File

@ -16,7 +16,7 @@ import (
tlsC "github.com/metacubex/mihomo/component/tls" tlsC "github.com/metacubex/mihomo/component/tls"
C "github.com/metacubex/mihomo/constant" C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/transport/anytls" "github.com/metacubex/mihomo/transport/anytls"
"github.com/sagernet/sing/common"
M "github.com/sagernet/sing/common/metadata" M "github.com/sagernet/sing/common/metadata"
"github.com/sagernet/sing/common/uot" "github.com/sagernet/sing/common/uot"
) )
@ -130,7 +130,7 @@ func NewAnyTLS(option AnyTLSOption) (*AnyTLS, error) {
dialer: singDialer, dialer: singDialer,
} }
runtime.SetFinalizer(outbound, func(o *AnyTLS) { runtime.SetFinalizer(outbound, func(o *AnyTLS) {
common.Close(o.client) _ = o.client.Close()
}) })
return outbound, nil return outbound, nil

View File

@ -10,6 +10,7 @@ import (
"strings" "strings"
"github.com/metacubex/mihomo/adapter/inbound" "github.com/metacubex/mihomo/adapter/inbound"
"github.com/metacubex/mihomo/common/atomic"
"github.com/metacubex/mihomo/common/buf" "github.com/metacubex/mihomo/common/buf"
N "github.com/metacubex/mihomo/common/net" N "github.com/metacubex/mihomo/common/net"
C "github.com/metacubex/mihomo/constant" C "github.com/metacubex/mihomo/constant"
@ -17,7 +18,7 @@ import (
"github.com/metacubex/mihomo/listener/sing" "github.com/metacubex/mihomo/listener/sing"
"github.com/metacubex/mihomo/transport/anytls/padding" "github.com/metacubex/mihomo/transport/anytls/padding"
"github.com/metacubex/mihomo/transport/anytls/session" "github.com/metacubex/mihomo/transport/anytls/session"
"github.com/sagernet/sing/common/atomic"
"github.com/sagernet/sing/common/auth" "github.com/sagernet/sing/common/auth"
"github.com/sagernet/sing/common/bufio" "github.com/sagernet/sing/common/bufio"
M "github.com/sagernet/sing/common/metadata" M "github.com/sagernet/sing/common/metadata"

View File

@ -8,13 +8,13 @@ import (
"net" "net"
"time" "time"
tlsC "github.com/metacubex/mihomo/component/tls" "github.com/metacubex/mihomo/common/atomic"
"github.com/metacubex/mihomo/common/buf"
C "github.com/metacubex/mihomo/constant" C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/transport/anytls/padding" "github.com/metacubex/mihomo/transport/anytls/padding"
"github.com/metacubex/mihomo/transport/anytls/session" "github.com/metacubex/mihomo/transport/anytls/session"
"github.com/metacubex/mihomo/transport/vmess" "github.com/metacubex/mihomo/transport/vmess"
"github.com/sagernet/sing/common/atomic"
"github.com/sagernet/sing/common/buf"
M "github.com/sagernet/sing/common/metadata" M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network" N "github.com/sagernet/sing/common/network"
) )
@ -91,7 +91,7 @@ func (c *Client) CreateOutboundTLSConnection(ctx context.Context) (net.Conn, err
ctx, cancel := context.WithTimeout(ctx, C.DefaultTLSTimeout) ctx, cancel := context.WithTimeout(ctx, C.DefaultTLSTimeout)
defer cancel() defer cancel()
err := utlsConn.(*tlsC.UConn).HandshakeContext(ctx) err := utlsConn.HandshakeContext(ctx)
return utlsConn, err return utlsConn, err
} }
} }

View File

@ -8,10 +8,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/metacubex/mihomo/common/atomic"
"github.com/metacubex/mihomo/transport/anytls/util" "github.com/metacubex/mihomo/transport/anytls/util"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/atomic"
) )
const CheckMark = -1 const CheckMark = -1
@ -73,7 +71,9 @@ func (p *PaddingFactory) GenerateRecordPayloadSizes(pkt uint32) (pktSizes []int)
if err != nil { if err != nil {
continue continue
} }
_min, _max = common.Min(_min, _max), common.Max(_min, _max) if _min > _max {
_min, _max = _max, _min
}
if _min <= 0 || _max <= 0 { if _min <= 0 || _max <= 0 {
continue continue
} }

View File

@ -7,14 +7,12 @@ import (
"math" "math"
"net" "net"
"sync" "sync"
"sync/atomic"
"time" "time"
"github.com/metacubex/mihomo/common/atomic"
"github.com/metacubex/mihomo/transport/anytls/padding" "github.com/metacubex/mihomo/transport/anytls/padding"
"github.com/metacubex/mihomo/transport/anytls/skiplist" "github.com/metacubex/mihomo/transport/anytls/skiplist"
"github.com/metacubex/mihomo/transport/anytls/util" "github.com/metacubex/mihomo/transport/anytls/util"
"github.com/sagernet/sing/common"
singAtomic "github.com/sagernet/sing/common/atomic"
) )
type Client struct { type Client struct {
@ -27,12 +25,12 @@ type Client struct {
idleSession *skiplist.SkipList[uint64, *Session] idleSession *skiplist.SkipList[uint64, *Session]
idleSessionLock sync.Mutex idleSessionLock sync.Mutex
padding *singAtomic.TypedValue[*padding.PaddingFactory] padding *atomic.TypedValue[*padding.PaddingFactory]
idleSessionTimeout time.Duration idleSessionTimeout time.Duration
} }
func NewClient(ctx context.Context, dialOut func(ctx context.Context) (net.Conn, error), _padding *singAtomic.TypedValue[*padding.PaddingFactory], idleSessionCheckInterval, idleSessionTimeout time.Duration) *Client { func NewClient(ctx context.Context, dialOut func(ctx context.Context) (net.Conn, error), _padding *atomic.TypedValue[*padding.PaddingFactory], idleSessionCheckInterval, idleSessionTimeout time.Duration) *Client {
c := &Client{ c := &Client{
dialOut: dialOut, dialOut: dialOut,
padding: _padding, padding: _padding,
@ -68,7 +66,7 @@ func (c *Client) CreateStream(ctx context.Context) (net.Conn, error) {
} }
stream, err = session.OpenStream() stream, err = session.OpenStream()
if err != nil { if err != nil {
common.Close(session, stream) _ = session.Close()
continue continue
} }
break break

View File

@ -7,15 +7,15 @@ import (
"net" "net"
"runtime/debug" "runtime/debug"
"sync" "sync"
"sync/atomic"
"time" "time"
"github.com/metacubex/mihomo/common/atomic"
"github.com/metacubex/mihomo/common/buf"
"github.com/metacubex/mihomo/common/pool"
"github.com/metacubex/mihomo/constant" "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/log" "github.com/metacubex/mihomo/log"
"github.com/metacubex/mihomo/transport/anytls/padding" "github.com/metacubex/mihomo/transport/anytls/padding"
"github.com/metacubex/mihomo/transport/anytls/util" "github.com/metacubex/mihomo/transport/anytls/util"
singAtomic "github.com/sagernet/sing/common/atomic"
"github.com/sagernet/sing/common/buf"
) )
type Session struct { type Session struct {
@ -33,7 +33,7 @@ type Session struct {
// pool // pool
seq uint64 seq uint64
idleSince time.Time idleSince time.Time
padding *singAtomic.TypedValue[*padding.PaddingFactory] padding *atomic.TypedValue[*padding.PaddingFactory]
// client // client
isClient bool isClient bool
@ -45,7 +45,7 @@ type Session struct {
onNewStream func(stream *Stream) onNewStream func(stream *Stream)
} }
func NewClientSession(conn net.Conn, _padding *singAtomic.TypedValue[*padding.PaddingFactory]) *Session { func NewClientSession(conn net.Conn, _padding *atomic.TypedValue[*padding.PaddingFactory]) *Session {
s := &Session{ s := &Session{
conn: conn, conn: conn,
isClient: true, isClient: true,
@ -56,7 +56,7 @@ func NewClientSession(conn net.Conn, _padding *singAtomic.TypedValue[*padding.Pa
return s return s
} }
func NewServerSession(conn net.Conn, onNewStream func(stream *Stream), _padding *singAtomic.TypedValue[*padding.PaddingFactory]) *Session { func NewServerSession(conn net.Conn, onNewStream func(stream *Stream), _padding *atomic.TypedValue[*padding.PaddingFactory]) *Session {
s := &Session{ s := &Session{
conn: conn, conn: conn,
onNewStream: onNewStream, onNewStream: onNewStream,
@ -169,7 +169,7 @@ func (s *Session) recvLoop() error {
switch hdr.Cmd() { switch hdr.Cmd() {
case cmdPSH: case cmdPSH:
if hdr.Length() > 0 { if hdr.Length() > 0 {
buffer := buf.Get(int(hdr.Length())) buffer := pool.Get(int(hdr.Length()))
if _, err := io.ReadFull(s.conn, buffer); err == nil { if _, err := io.ReadFull(s.conn, buffer); err == nil {
s.streamLock.RLock() s.streamLock.RLock()
stream, ok := s.streams[sid] stream, ok := s.streams[sid]
@ -177,9 +177,9 @@ func (s *Session) recvLoop() error {
if ok { if ok {
stream.pipeW.Write(buffer) stream.pipeW.Write(buffer)
} }
buf.Put(buffer) pool.Put(buffer)
} else { } else {
buf.Put(buffer) pool.Put(buffer)
return err return err
} }
} }
@ -211,18 +211,18 @@ func (s *Session) recvLoop() error {
//logrus.Debugln("stream fin", sid, s.streams) //logrus.Debugln("stream fin", sid, s.streams)
case cmdWaste: case cmdWaste:
if hdr.Length() > 0 { if hdr.Length() > 0 {
buffer := buf.Get(int(hdr.Length())) buffer := pool.Get(int(hdr.Length()))
if _, err := io.ReadFull(s.conn, buffer); err != nil { if _, err := io.ReadFull(s.conn, buffer); err != nil {
buf.Put(buffer) pool.Put(buffer)
return err return err
} }
buf.Put(buffer) pool.Put(buffer)
} }
case cmdSettings: case cmdSettings:
if hdr.Length() > 0 { if hdr.Length() > 0 {
buffer := buf.Get(int(hdr.Length())) buffer := pool.Get(int(hdr.Length()))
if _, err := io.ReadFull(s.conn, buffer); err != nil { if _, err := io.ReadFull(s.conn, buffer); err != nil {
buf.Put(buffer) pool.Put(buffer)
return err return err
} }
if !s.isClient { if !s.isClient {
@ -235,31 +235,31 @@ func (s *Session) recvLoop() error {
f.data = paddingF.RawScheme f.data = paddingF.RawScheme
_, err = s.writeFrame(f) _, err = s.writeFrame(f)
if err != nil { if err != nil {
buf.Put(buffer) pool.Put(buffer)
return err return err
} }
} }
} }
buf.Put(buffer) pool.Put(buffer)
} }
case cmdAlert: case cmdAlert:
if hdr.Length() > 0 { if hdr.Length() > 0 {
buffer := buf.Get(int(hdr.Length())) buffer := pool.Get(int(hdr.Length()))
if _, err := io.ReadFull(s.conn, buffer); err != nil { if _, err := io.ReadFull(s.conn, buffer); err != nil {
buf.Put(buffer) pool.Put(buffer)
return err return err
} }
if s.isClient { if s.isClient {
log.Errorln("[Alert from server] %s", string(buffer)) log.Errorln("[Alert from server] %s", string(buffer))
} }
buf.Put(buffer) pool.Put(buffer)
return nil return nil
} }
case cmdUpdatePaddingScheme: case cmdUpdatePaddingScheme:
if hdr.Length() > 0 { if hdr.Length() > 0 {
buffer := buf.Get(int(hdr.Length())) buffer := pool.Get(int(hdr.Length()))
if _, err := io.ReadFull(s.conn, buffer); err != nil { if _, err := io.ReadFull(s.conn, buffer); err != nil {
buf.Put(buffer) pool.Put(buffer)
return err return err
} }
if s.isClient { if s.isClient {
@ -269,7 +269,7 @@ func (s *Session) recvLoop() error {
log.Warnln("[Update padding failed] %x\n", md5.Sum(buffer)) log.Warnln("[Update padding failed] %x\n", md5.Sum(buffer))
} }
} }
buf.Put(buffer) pool.Put(buffer)
} }
default: default:
// I don't know what command it is (can't have data) // I don't know what command it is (can't have data)

View File

@ -14,8 +14,6 @@ import (
"math/bits" "math/bits"
"math/rand" "math/rand"
"time" "time"
"github.com/sagernet/sing/common"
) )
const ( const (
@ -102,7 +100,11 @@ func (sl *SkipList[K, V]) Insert(key K, value V) {
level := sl.randomLevel() level := sl.randomLevel()
node = newSkipListNode(level, key, value) node = newSkipListNode(level, key, value)
for i := 0; i < common.Min(level, sl.level); i++ { minLevel := level
if sl.level < level {
minLevel = sl.level
}
for i := 0; i < minLevel; i++ {
node.next[i] = prevs[i].next[i] node.next[i] = prevs[i].next[i]
prevs[i].next[i] = node prevs[i].next[i] = node
} }

View File

@ -93,7 +93,7 @@ func (t *Trojan) StreamConn(ctx context.Context, conn net.Conn) (net.Conn, error
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTLSTimeout) ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTLSTimeout)
defer cancel() defer cancel()
err := utlsConn.(*tlsC.UConn).HandshakeContext(ctx) err := utlsConn.HandshakeContext(ctx)
return utlsConn, err return utlsConn, err
} }
} else { } else {

View File

@ -36,7 +36,7 @@ func StreamTLSConn(ctx context.Context, conn net.Conn, cfg *TLSConfig) (net.Conn
if cfg.Reality == nil { if cfg.Reality == nil {
utlsConn, valid := GetUTLSConn(conn, cfg.ClientFingerprint, tlsConfig) utlsConn, valid := GetUTLSConn(conn, cfg.ClientFingerprint, tlsConfig)
if valid { if valid {
err := utlsConn.(*tlsC.UConn).HandshakeContext(ctx) err = utlsConn.HandshakeContext(ctx)
return utlsConn, err return utlsConn, err
} }
} else { } else {
@ -53,7 +53,7 @@ func StreamTLSConn(ctx context.Context, conn net.Conn, cfg *TLSConfig) (net.Conn
return tlsConn, err return tlsConn, err
} }
func GetUTLSConn(conn net.Conn, ClientFingerprint string, tlsConfig *tls.Config) (net.Conn, bool) { func GetUTLSConn(conn net.Conn, ClientFingerprint string, tlsConfig *tls.Config) (*tlsC.UConn, bool) {
if fingerprint, exists := tlsC.GetFingerprint(ClientFingerprint); exists { if fingerprint, exists := tlsC.GetFingerprint(ClientFingerprint); exists {
utlsConn := tlsC.UClient(conn, tlsConfig, fingerprint) utlsConn := tlsC.UClient(conn, tlsConfig, fingerprint)