mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-22 08:31:30 +00:00
Remove stack buffer usage
This commit is contained in:
parent
9c8565cf21
commit
07ce5e0d22
|
@ -1,87 +0,0 @@
|
||||||
package debugio
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net"
|
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/log"
|
|
||||||
"github.com/sagernet/sing/common/buf"
|
|
||||||
"github.com/sagernet/sing/common/bufio"
|
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
|
||||||
N "github.com/sagernet/sing/common/network"
|
|
||||||
)
|
|
||||||
|
|
||||||
type LogConn struct {
|
|
||||||
N.ExtendedConn
|
|
||||||
logger log.Logger
|
|
||||||
prefix string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewLogConn(conn net.Conn, logger log.Logger, prefix string) N.ExtendedConn {
|
|
||||||
return &LogConn{bufio.NewExtendedConn(conn), logger, prefix}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *LogConn) Read(p []byte) (n int, err error) {
|
|
||||||
n, err = c.ExtendedConn.Read(p)
|
|
||||||
if n > 0 {
|
|
||||||
c.logger.Debug(c.prefix, " read ", buf.EncodeHexString(p[:n]))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *LogConn) Write(p []byte) (n int, err error) {
|
|
||||||
c.logger.Debug(c.prefix, " write ", buf.EncodeHexString(p))
|
|
||||||
return c.ExtendedConn.Write(p)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *LogConn) ReadBuffer(buffer *buf.Buffer) error {
|
|
||||||
err := c.ExtendedConn.ReadBuffer(buffer)
|
|
||||||
if err == nil {
|
|
||||||
c.logger.Debug(c.prefix, " read buffer ", buf.EncodeHexString(buffer.Bytes()))
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *LogConn) WriteBuffer(buffer *buf.Buffer) error {
|
|
||||||
c.logger.Debug(c.prefix, " write buffer ", buf.EncodeHexString(buffer.Bytes()))
|
|
||||||
return c.ExtendedConn.WriteBuffer(buffer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *LogConn) Upstream() any {
|
|
||||||
return c.ExtendedConn
|
|
||||||
}
|
|
||||||
|
|
||||||
type LogPacketConn struct {
|
|
||||||
N.NetPacketConn
|
|
||||||
logger log.Logger
|
|
||||||
prefix string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewLogPacketConn(conn net.PacketConn, logger log.Logger, prefix string) N.NetPacketConn {
|
|
||||||
return &LogPacketConn{bufio.NewPacketConn(conn), logger, prefix}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *LogPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
|
|
||||||
n, addr, err = c.NetPacketConn.ReadFrom(p)
|
|
||||||
if n > 0 {
|
|
||||||
c.logger.Debug(c.prefix, " read from ", addr, " ", buf.EncodeHexString(p[:n]))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *LogPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
|
||||||
c.logger.Debug(c.prefix, " write to ", addr, " ", buf.EncodeHexString(p))
|
|
||||||
return c.NetPacketConn.WriteTo(p, addr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *LogPacketConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) {
|
|
||||||
destination, err = c.NetPacketConn.ReadPacket(buffer)
|
|
||||||
if err == nil {
|
|
||||||
c.logger.Debug(c.prefix, " read packet from ", destination, " ", buf.EncodeHexString(buffer.Bytes()))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *LogPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
|
|
||||||
c.logger.Debug(c.prefix, " write packet to ", destination, " ", buf.EncodeHexString(buffer.Bytes()))
|
|
||||||
return c.NetPacketConn.WritePacket(buffer, destination)
|
|
||||||
}
|
|
|
@ -1,19 +0,0 @@
|
||||||
package debugio
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"github.com/sagernet/sing/common"
|
|
||||||
)
|
|
||||||
|
|
||||||
func PrintUpstream(obj any) {
|
|
||||||
for obj != nil {
|
|
||||||
fmt.Println(reflect.TypeOf(obj))
|
|
||||||
if u, ok := obj.(common.WithUpstream); !ok {
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
obj = u.Upstream()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,48 +0,0 @@
|
||||||
package debugio
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/sagernet/sing/common/buf"
|
|
||||||
"github.com/sagernet/sing/common/bufio"
|
|
||||||
N "github.com/sagernet/sing/common/network"
|
|
||||||
)
|
|
||||||
|
|
||||||
type RaceConn struct {
|
|
||||||
N.ExtendedConn
|
|
||||||
readAccess sync.Mutex
|
|
||||||
writeAccess sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewRaceConn(conn net.Conn) N.ExtendedConn {
|
|
||||||
return &RaceConn{ExtendedConn: bufio.NewExtendedConn(conn)}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *RaceConn) Read(p []byte) (n int, err error) {
|
|
||||||
c.readAccess.Lock()
|
|
||||||
defer c.readAccess.Unlock()
|
|
||||||
return c.ExtendedConn.Read(p)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *RaceConn) Write(p []byte) (n int, err error) {
|
|
||||||
c.writeAccess.Lock()
|
|
||||||
defer c.writeAccess.Unlock()
|
|
||||||
return c.ExtendedConn.Write(p)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *RaceConn) ReadBuffer(buffer *buf.Buffer) error {
|
|
||||||
c.readAccess.Lock()
|
|
||||||
defer c.readAccess.Unlock()
|
|
||||||
return c.ExtendedConn.ReadBuffer(buffer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *RaceConn) WriteBuffer(buffer *buf.Buffer) error {
|
|
||||||
c.writeAccess.Lock()
|
|
||||||
defer c.writeAccess.Unlock()
|
|
||||||
return c.ExtendedConn.WriteBuffer(buffer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *RaceConn) Upstream() any {
|
|
||||||
return c.ExtendedConn
|
|
||||||
}
|
|
|
@ -128,13 +128,6 @@ func (c *slowOpenConn) NeedHandshake() bool {
|
||||||
return c.conn == nil
|
return c.conn == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *slowOpenConn) ReadFrom(r io.Reader) (n int64, err error) {
|
|
||||||
if c.conn != nil {
|
|
||||||
return bufio.Copy(c.conn, r)
|
|
||||||
}
|
|
||||||
return bufio.ReadFrom0(c, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *slowOpenConn) WriteTo(w io.Writer) (n int64, err error) {
|
func (c *slowOpenConn) WriteTo(w io.Writer) (n int64, err error) {
|
||||||
if c.conn == nil {
|
if c.conn == nil {
|
||||||
select {
|
select {
|
||||||
|
|
|
@ -15,7 +15,6 @@ import (
|
||||||
"unicode"
|
"unicode"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/sagernet/sing/common"
|
|
||||||
"github.com/sagernet/sing/common/buf"
|
"github.com/sagernet/sing/common/buf"
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/sagernet/sing/common/network"
|
||||||
|
@ -82,9 +81,7 @@ func resolveSocketByNetlink(network string, source netip.AddrPort, destination n
|
||||||
return 0, 0, E.Cause(err, "write netlink request")
|
return 0, 0, E.Cause(err, "write netlink request")
|
||||||
}
|
}
|
||||||
|
|
||||||
_buffer := buf.StackNew()
|
buffer := buf.New()
|
||||||
defer common.KeepAlive(_buffer)
|
|
||||||
buffer := common.Dup(_buffer)
|
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
|
|
||||||
n, err := syscall.Read(socket, buffer.FreeBytes())
|
n, err := syscall.Read(socket, buffer.FreeBytes())
|
||||||
|
|
16
go.mod
16
go.mod
|
@ -25,14 +25,14 @@ require (
|
||||||
github.com/sagernet/gvisor v0.0.0-20230627031050-1ab0276e0dd2
|
github.com/sagernet/gvisor v0.0.0-20230627031050-1ab0276e0dd2
|
||||||
github.com/sagernet/quic-go v0.0.0-20230615020047-10f05c797c02
|
github.com/sagernet/quic-go v0.0.0-20230615020047-10f05c797c02
|
||||||
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691
|
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691
|
||||||
github.com/sagernet/sing v0.2.7
|
github.com/sagernet/sing v0.2.8-0.20230703002104-c68251b6d059
|
||||||
github.com/sagernet/sing-dns v0.1.6
|
github.com/sagernet/sing-dns v0.1.7-0.20230703131656-fd65b6178bf9
|
||||||
github.com/sagernet/sing-mux v0.1.0
|
github.com/sagernet/sing-mux v0.1.1-0.20230703132253-2cedde0fbc90
|
||||||
github.com/sagernet/sing-shadowsocks v0.2.2
|
github.com/sagernet/sing-shadowsocks v0.2.3-0.20230703131347-b044960bd355
|
||||||
github.com/sagernet/sing-shadowsocks2 v0.1.1
|
github.com/sagernet/sing-shadowsocks2 v0.1.2-0.20230703131506-ca0c6adde968
|
||||||
github.com/sagernet/sing-shadowtls v0.1.2
|
github.com/sagernet/sing-shadowtls v0.1.3-0.20230703132509-93bbad3057e4
|
||||||
github.com/sagernet/sing-tun v0.1.8
|
github.com/sagernet/sing-tun v0.1.9-0.20230703134424-fd850d00e5cd
|
||||||
github.com/sagernet/sing-vmess v0.1.6
|
github.com/sagernet/sing-vmess v0.1.7-0.20230703132834-48803e0fd8af
|
||||||
github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37
|
github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37
|
||||||
github.com/sagernet/tfo-go v0.0.0-20230303015439-ffcfd8c41cf9
|
github.com/sagernet/tfo-go v0.0.0-20230303015439-ffcfd8c41cf9
|
||||||
github.com/sagernet/utls v0.0.0-20230309024959-6732c2ab36f2
|
github.com/sagernet/utls v0.0.0-20230309024959-6732c2ab36f2
|
||||||
|
|
33
go.sum
33
go.sum
|
@ -116,22 +116,22 @@ github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691 h1:5Th31OC6yj8byL
|
||||||
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691/go.mod h1:B8lp4WkQ1PwNnrVMM6KyuFR20pU8jYBD+A4EhJovEXU=
|
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691/go.mod h1:B8lp4WkQ1PwNnrVMM6KyuFR20pU8jYBD+A4EhJovEXU=
|
||||||
github.com/sagernet/sing v0.0.0-20220817130738-ce854cda8522/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY=
|
github.com/sagernet/sing v0.0.0-20220817130738-ce854cda8522/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY=
|
||||||
github.com/sagernet/sing v0.1.8/go.mod h1:jt1w2u7lJQFFSGLiRrRIs5YWmx4kAPfWuOejuDW9qMk=
|
github.com/sagernet/sing v0.1.8/go.mod h1:jt1w2u7lJQFFSGLiRrRIs5YWmx4kAPfWuOejuDW9qMk=
|
||||||
github.com/sagernet/sing v0.2.7 h1:cOy0FfPS8q7m0aJ51wS7LRQAGc9wF+fWhHtBDj99wy8=
|
github.com/sagernet/sing v0.2.8-0.20230703002104-c68251b6d059 h1:nqTONy58Gq1mdoGx9GX+GKXdSTwOPTKF/DXK+Wn4B+A=
|
||||||
github.com/sagernet/sing v0.2.7/go.mod h1:Ta8nHnDLAwqySzKhGoKk4ZIB+vJ3GTKj7UPrWYvM+4w=
|
github.com/sagernet/sing v0.2.8-0.20230703002104-c68251b6d059/go.mod h1:Ta8nHnDLAwqySzKhGoKk4ZIB+vJ3GTKj7UPrWYvM+4w=
|
||||||
github.com/sagernet/sing-dns v0.1.6 h1:qQRxmtUjCYkRLyzkTHpBvZQeAMeKpZwYsBOcs4U7gns=
|
github.com/sagernet/sing-dns v0.1.7-0.20230703131656-fd65b6178bf9 h1:35qe74ygIKj5uQkDDD0Xtv+iWOspQsS/Lqhs2XiY0Ak=
|
||||||
github.com/sagernet/sing-dns v0.1.6/go.mod h1:DY3LYJXIsM/kezUIJxf2TwBzqTTQAepi2KgazkDfahA=
|
github.com/sagernet/sing-dns v0.1.7-0.20230703131656-fd65b6178bf9/go.mod h1:wgmbh0yruJXRO8tmfwPx6hOl6pyReWRoeHdkRehWkmw=
|
||||||
github.com/sagernet/sing-mux v0.1.0 h1:xihlDRNs1J+hYwmvW9/ZmaghjDx7O0Y5dty0pOLQGB4=
|
github.com/sagernet/sing-mux v0.1.1-0.20230703132253-2cedde0fbc90 h1:aEe2HrRc9OTS7IZ8RHyh224OhltnwRQs4/y89UsHPo8=
|
||||||
github.com/sagernet/sing-mux v0.1.0/go.mod h1:i3jKjV4pRTFTV/ly5V3oa2JMPy0SAZ5X8X4tDU9Hw94=
|
github.com/sagernet/sing-mux v0.1.1-0.20230703132253-2cedde0fbc90/go.mod h1:sm126rB5EUi9HLf4jCSHTqo+XRPbh4BoEVeLbr2WRbE=
|
||||||
github.com/sagernet/sing-shadowsocks v0.2.2 h1:ezSdVhrmIcwDXmCZF3bOJVMuVtTQWpda+1Op+Ie2TA4=
|
github.com/sagernet/sing-shadowsocks v0.2.3-0.20230703131347-b044960bd355 h1:XOgNZYnkDrx5qtNS4kqIOHMhjZuc7mJ2pY/x3EyZX8Q=
|
||||||
github.com/sagernet/sing-shadowsocks v0.2.2/go.mod h1:JIBWG6a7orB2HxBxYElViQFLUQxFVG7DuqIj8gD7uCQ=
|
github.com/sagernet/sing-shadowsocks v0.2.3-0.20230703131347-b044960bd355/go.mod h1:atEATsxqPo8qCPcFt8Rw7TFEJ70egCoMR7PziX4jmjI=
|
||||||
github.com/sagernet/sing-shadowsocks2 v0.1.1 h1:/cZteeSFXyHKg0uOparIFNj8hHrV8F2rRzTm8arpBTs=
|
github.com/sagernet/sing-shadowsocks2 v0.1.2-0.20230703131506-ca0c6adde968 h1:UctXygnZfqsFR+2hZXfpWK3pSYKLbBQMuli9GDE6QU0=
|
||||||
github.com/sagernet/sing-shadowsocks2 v0.1.1/go.mod h1:p18C731ogLED66ZgC1SNYMOXAOxJIRwcTSUk73q/rsc=
|
github.com/sagernet/sing-shadowsocks2 v0.1.2-0.20230703131506-ca0c6adde968/go.mod h1:xFxUGbtnqRLxtQftCILFeKf43GE6S83f0I6CsO9BxGE=
|
||||||
github.com/sagernet/sing-shadowtls v0.1.2 h1:wkPf4gF+cmaP0cIbArpyq+mc6GcwbMx60CssmmhEQ0s=
|
github.com/sagernet/sing-shadowtls v0.1.3-0.20230703132509-93bbad3057e4 h1:ZjLyCkEENqXzGp4PRZbQGk5wPzEq0Rg+/2jK82lmy3Q=
|
||||||
github.com/sagernet/sing-shadowtls v0.1.2/go.mod h1:rTxhbSY8jGWZOWjdeOe1vP3E+hkgen8aRA2p7YccM88=
|
github.com/sagernet/sing-shadowtls v0.1.3-0.20230703132509-93bbad3057e4/go.mod h1:8ZSSHJSNOG7cUCUYJemZNH873EsKdFqABykTypoS/2M=
|
||||||
github.com/sagernet/sing-tun v0.1.8 h1:nc5mAdsYVB5TAv0UI0DaZJrd+sOv+HVf6V5B6XfJgSI=
|
github.com/sagernet/sing-tun v0.1.9-0.20230703134424-fd850d00e5cd h1:lkJA/P1L2XE5lNDnzA2fygx6DZIks3Sx87GN2OE0jNY=
|
||||||
github.com/sagernet/sing-tun v0.1.8/go.mod h1:Vj0AcDoneVIYMx8QujpXs2NQJ5Byc0FPhnRj5V3RGdo=
|
github.com/sagernet/sing-tun v0.1.9-0.20230703134424-fd850d00e5cd/go.mod h1:XNQoXtvsmeva+dADmo/57KktLNgm5ubOyR67Niahqj8=
|
||||||
github.com/sagernet/sing-vmess v0.1.6 h1:u9VhPNMP0u1vaEjWRWitJQ4KKYPhTF0rorpAlQZcFBg=
|
github.com/sagernet/sing-vmess v0.1.7-0.20230703132834-48803e0fd8af h1:NS433dd7XSieIAiR5cMK6ArhC8ILbXJul48IMLRxqhI=
|
||||||
github.com/sagernet/sing-vmess v0.1.6/go.mod h1:XYXpk405G+kxRMNfREhROJsBxh1ccHy1v/fWSV5lx38=
|
github.com/sagernet/sing-vmess v0.1.7-0.20230703132834-48803e0fd8af/go.mod h1:HoYH8kt5m1RejF0KSYn+QkZ9Cg4a8LlHnV9ykaeNJu4=
|
||||||
github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37 h1:HuE6xSwco/Xed8ajZ+coeYLmioq0Qp1/Z2zczFaV8as=
|
github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37 h1:HuE6xSwco/Xed8ajZ+coeYLmioq0Qp1/Z2zczFaV8as=
|
||||||
github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37/go.mod h1:3skNSftZDJWTGVtVaM2jfbce8qHnmH/AGDRe62iNOg0=
|
github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37/go.mod h1:3skNSftZDJWTGVtVaM2jfbce8qHnmH/AGDRe62iNOg0=
|
||||||
github.com/sagernet/tfo-go v0.0.0-20230303015439-ffcfd8c41cf9 h1:2ItpW1nMNkPzmBTxV0/eClCklHrFSQMnUGcpUmJxVeE=
|
github.com/sagernet/tfo-go v0.0.0-20230303015439-ffcfd8c41cf9 h1:2ItpW1nMNkPzmBTxV0/eClCklHrFSQMnUGcpUmJxVeE=
|
||||||
|
@ -149,7 +149,6 @@ github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRM
|
||||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
|
|
@ -38,9 +38,7 @@ func (a *myInboundAdapter) ListenUDP() (net.PacketConn, error) {
|
||||||
|
|
||||||
func (a *myInboundAdapter) loopUDPIn() {
|
func (a *myInboundAdapter) loopUDPIn() {
|
||||||
defer close(a.packetOutboundClosed)
|
defer close(a.packetOutboundClosed)
|
||||||
_buffer := buf.StackNewPacket()
|
buffer := buf.NewPacket()
|
||||||
defer common.KeepAlive(_buffer)
|
|
||||||
buffer := common.Dup(_buffer)
|
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
buffer.IncRef()
|
buffer.IncRef()
|
||||||
defer buffer.DecRef()
|
defer buffer.DecRef()
|
||||||
|
@ -67,9 +65,7 @@ func (a *myInboundAdapter) loopUDPIn() {
|
||||||
|
|
||||||
func (a *myInboundAdapter) loopUDPOOBIn() {
|
func (a *myInboundAdapter) loopUDPOOBIn() {
|
||||||
defer close(a.packetOutboundClosed)
|
defer close(a.packetOutboundClosed)
|
||||||
_buffer := buf.StackNewPacket()
|
buffer := buf.NewPacket()
|
||||||
defer common.KeepAlive(_buffer)
|
|
||||||
buffer := common.Dup(_buffer)
|
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
buffer.IncRef()
|
buffer.IncRef()
|
||||||
defer buffer.DecRef()
|
defer buffer.DecRef()
|
||||||
|
|
|
@ -270,9 +270,7 @@ func (c *naiveH1Conn) read(p []byte) (n int, err error) {
|
||||||
if len(p) >= 3 {
|
if len(p) >= 3 {
|
||||||
paddingHdr = p[:3]
|
paddingHdr = p[:3]
|
||||||
} else {
|
} else {
|
||||||
_paddingHdr := make([]byte, 3)
|
paddingHdr = make([]byte, 3)
|
||||||
defer common.KeepAlive(_paddingHdr)
|
|
||||||
paddingHdr = common.Dup(_paddingHdr)
|
|
||||||
}
|
}
|
||||||
_, err = io.ReadFull(c.Conn, paddingHdr)
|
_, err = io.ReadFull(c.Conn, paddingHdr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -320,9 +318,7 @@ func (c *naiveH1Conn) write(p []byte) (n int, err error) {
|
||||||
if c.writePadding < kFirstPaddings {
|
if c.writePadding < kFirstPaddings {
|
||||||
paddingSize := rand.Intn(256)
|
paddingSize := rand.Intn(256)
|
||||||
|
|
||||||
_buffer := buf.StackNewSize(3 + len(p) + paddingSize)
|
buffer := buf.NewSize(3 + len(p) + paddingSize)
|
||||||
defer common.KeepAlive(_buffer)
|
|
||||||
buffer := common.Dup(_buffer)
|
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
header := buffer.Extend(3)
|
header := buffer.Extend(3)
|
||||||
binary.BigEndian.PutUint16(header, uint16(len(p)))
|
binary.BigEndian.PutUint16(header, uint16(len(p)))
|
||||||
|
@ -449,9 +445,7 @@ func (c *naiveH2Conn) read(p []byte) (n int, err error) {
|
||||||
if len(p) >= 3 {
|
if len(p) >= 3 {
|
||||||
paddingHdr = p[:3]
|
paddingHdr = p[:3]
|
||||||
} else {
|
} else {
|
||||||
_paddingHdr := make([]byte, 3)
|
paddingHdr = make([]byte, 3)
|
||||||
defer common.KeepAlive(_paddingHdr)
|
|
||||||
paddingHdr = common.Dup(_paddingHdr)
|
|
||||||
}
|
}
|
||||||
_, err = io.ReadFull(c.reader, paddingHdr)
|
_, err = io.ReadFull(c.reader, paddingHdr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -502,9 +496,7 @@ func (c *naiveH2Conn) write(p []byte) (n int, err error) {
|
||||||
if c.writePadding < kFirstPaddings {
|
if c.writePadding < kFirstPaddings {
|
||||||
paddingSize := rand.Intn(256)
|
paddingSize := rand.Intn(256)
|
||||||
|
|
||||||
_buffer := buf.StackNewSize(3 + len(p) + paddingSize)
|
buffer := buf.NewSize(3 + len(p) + paddingSize)
|
||||||
defer common.KeepAlive(_buffer)
|
|
||||||
buffer := common.Dup(_buffer)
|
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
header := buffer.Extend(3)
|
header := buffer.Extend(3)
|
||||||
binary.BigEndian.PutUint16(header, uint16(len(p)))
|
binary.BigEndian.PutUint16(header, uint16(len(p)))
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/adapter"
|
"github.com/sagernet/sing-box/adapter"
|
||||||
|
@ -112,8 +111,7 @@ func CopyEarlyConn(ctx context.Context, conn net.Conn, serverConn net.Conn) erro
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if earlyConn, isEarlyConn := common.Cast[N.EarlyConn](serverConn); isEarlyConn && earlyConn.NeedHandshake() {
|
if earlyConn, isEarlyConn := common.Cast[N.EarlyConn](serverConn); isEarlyConn && earlyConn.NeedHandshake() {
|
||||||
_payload := buf.StackNew()
|
payload := buf.NewPacket()
|
||||||
payload := common.Dup(_payload)
|
|
||||||
err := conn.SetReadDeadline(time.Now().Add(C.ReadPayloadTimeout))
|
err := conn.SetReadDeadline(time.Now().Add(C.ReadPayloadTimeout))
|
||||||
if err != os.ErrInvalid {
|
if err != os.ErrInvalid {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -133,7 +131,6 @@ func CopyEarlyConn(ctx context.Context, conn net.Conn, serverConn net.Conn) erro
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return N.HandshakeFailure(conn, err)
|
return N.HandshakeFailure(conn, err)
|
||||||
}
|
}
|
||||||
runtime.KeepAlive(_payload)
|
|
||||||
payload.Release()
|
payload.Release()
|
||||||
}
|
}
|
||||||
return bufio.CopyConn(ctx, conn, serverConn)
|
return bufio.CopyConn(ctx, conn, serverConn)
|
||||||
|
|
|
@ -65,9 +65,7 @@ func (d *DNS) handleConnection(ctx context.Context, conn net.Conn, metadata adap
|
||||||
if queryLength == 0 {
|
if queryLength == 0 {
|
||||||
return dns.RCodeFormatError
|
return dns.RCodeFormatError
|
||||||
}
|
}
|
||||||
_buffer := buf.StackNewSize(int(queryLength))
|
buffer := buf.NewSize(int(queryLength))
|
||||||
defer common.KeepAlive(_buffer)
|
|
||||||
buffer := common.Dup(_buffer)
|
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
_, err = buffer.ReadFullFrom(conn, int(queryLength))
|
_, err = buffer.ReadFullFrom(conn, int(queryLength))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -84,9 +82,7 @@ func (d *DNS) handleConnection(ctx context.Context, conn net.Conn, metadata adap
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_responseBuffer := buf.StackNewPacket()
|
responseBuffer := buf.NewPacket()
|
||||||
defer common.KeepAlive(_responseBuffer)
|
|
||||||
responseBuffer := common.Dup(_responseBuffer)
|
|
||||||
defer responseBuffer.Release()
|
defer responseBuffer.Release()
|
||||||
responseBuffer.Resize(2, 0)
|
responseBuffer.Resize(2, 0)
|
||||||
n, err := response.PackBuffer(responseBuffer.FreeBytes())
|
n, err := response.PackBuffer(responseBuffer.FreeBytes())
|
||||||
|
|
|
@ -199,9 +199,7 @@ func (t *Transport) fetchServers0(ctx context.Context, iface *net.Interface) err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Transport) fetchServersResponse(iface *net.Interface, packetConn net.PacketConn, transactionID dhcpv4.TransactionID) error {
|
func (t *Transport) fetchServersResponse(iface *net.Interface, packetConn net.PacketConn, transactionID dhcpv4.TransactionID) error {
|
||||||
_buffer := buf.StackNewSize(dhcpv4.MaxMessageSize)
|
buffer := buf.NewSize(dhcpv4.MaxMessageSize)
|
||||||
defer common.KeepAlive(_buffer)
|
|
||||||
buffer := common.Dup(_buffer)
|
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
|
@ -41,9 +41,7 @@ func WriteClientHello(stream io.Writer, hello ClientHello) error {
|
||||||
requestLen += 8 // recvBPS
|
requestLen += 8 // recvBPS
|
||||||
requestLen += 2 // auth len
|
requestLen += 2 // auth len
|
||||||
requestLen += len(hello.Auth)
|
requestLen += len(hello.Auth)
|
||||||
_request := buf.StackNewSize(requestLen)
|
request := buf.NewSize(requestLen)
|
||||||
defer common.KeepAlive(_request)
|
|
||||||
request := common.Dup(_request)
|
|
||||||
defer request.Release()
|
defer request.Release()
|
||||||
common.Must(
|
common.Must(
|
||||||
request.WriteByte(Version),
|
request.WriteByte(Version),
|
||||||
|
@ -99,9 +97,7 @@ func ReadServerHello(stream io.Reader) (*ServerHello, error) {
|
||||||
responseLen += 8 // sendBPS
|
responseLen += 8 // sendBPS
|
||||||
responseLen += 8 // recvBPS
|
responseLen += 8 // recvBPS
|
||||||
responseLen += 2 // message len
|
responseLen += 2 // message len
|
||||||
_response := buf.StackNewSize(responseLen)
|
response := buf.NewSize(responseLen)
|
||||||
defer common.KeepAlive(_response)
|
|
||||||
response := common.Dup(_response)
|
|
||||||
defer response.Release()
|
defer response.Release()
|
||||||
_, err := response.ReadFullFrom(stream, responseLen)
|
_, err := response.ReadFullFrom(stream, responseLen)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -131,9 +127,7 @@ func WriteServerHello(stream io.Writer, hello ServerHello) error {
|
||||||
responseLen += 8 // recvBPS
|
responseLen += 8 // recvBPS
|
||||||
responseLen += 2 // message len
|
responseLen += 2 // message len
|
||||||
responseLen += len(hello.Message)
|
responseLen += len(hello.Message)
|
||||||
_response := buf.StackNewSize(responseLen)
|
response := buf.NewSize(responseLen)
|
||||||
defer common.KeepAlive(_response)
|
|
||||||
response := common.Dup(_response)
|
|
||||||
defer response.Release()
|
defer response.Release()
|
||||||
if hello.OK {
|
if hello.OK {
|
||||||
common.Must(response.WriteByte(1))
|
common.Must(response.WriteByte(1))
|
||||||
|
@ -185,9 +179,7 @@ func WriteClientRequest(stream io.Writer, request ClientRequest) error {
|
||||||
requestLen += 2 // host len
|
requestLen += 2 // host len
|
||||||
requestLen += len(request.Host)
|
requestLen += len(request.Host)
|
||||||
requestLen += 2 // port
|
requestLen += 2 // port
|
||||||
_buffer := buf.StackNewSize(requestLen)
|
buffer := buf.NewSize(requestLen)
|
||||||
defer common.KeepAlive(_buffer)
|
|
||||||
buffer := common.Dup(_buffer)
|
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
if request.UDP {
|
if request.UDP {
|
||||||
common.Must(buffer.WriteByte(1))
|
common.Must(buffer.WriteByte(1))
|
||||||
|
@ -213,9 +205,7 @@ func ReadServerResponse(stream io.Reader) (*ServerResponse, error) {
|
||||||
responseLen += 1 // ok
|
responseLen += 1 // ok
|
||||||
responseLen += 4 // udp session id
|
responseLen += 4 // udp session id
|
||||||
responseLen += 2 // message len
|
responseLen += 2 // message len
|
||||||
_response := buf.StackNewSize(responseLen)
|
response := buf.NewSize(responseLen)
|
||||||
defer common.KeepAlive(_response)
|
|
||||||
response := common.Dup(_response)
|
|
||||||
defer response.Release()
|
defer response.Release()
|
||||||
_, err := response.ReadFullFrom(stream, responseLen)
|
_, err := response.ReadFullFrom(stream, responseLen)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -243,9 +233,7 @@ func WriteServerResponse(stream io.Writer, response ServerResponse) error {
|
||||||
responseLen += 4 // udp session id
|
responseLen += 4 // udp session id
|
||||||
responseLen += 2 // message len
|
responseLen += 2 // message len
|
||||||
responseLen += len(response.Message)
|
responseLen += len(response.Message)
|
||||||
_buffer := buf.StackNewSize(responseLen)
|
buffer := buf.NewSize(responseLen)
|
||||||
defer common.KeepAlive(_buffer)
|
|
||||||
buffer := common.Dup(_buffer)
|
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
if response.OK {
|
if response.OK {
|
||||||
common.Must(buffer.WriteByte(1))
|
common.Must(buffer.WriteByte(1))
|
||||||
|
@ -338,9 +326,7 @@ func WriteUDPMessage(conn quic.Connection, message UDPMessage) error {
|
||||||
messageLen += 1 // frag count
|
messageLen += 1 // frag count
|
||||||
messageLen += 2 // data len
|
messageLen += 2 // data len
|
||||||
messageLen += len(message.Data)
|
messageLen += len(message.Data)
|
||||||
_buffer := buf.StackNewSize(messageLen)
|
buffer := buf.NewSize(messageLen)
|
||||||
defer common.KeepAlive(_buffer)
|
|
||||||
buffer := common.Dup(_buffer)
|
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
err := writeUDPMessage(conn, message, buffer)
|
err := writeUDPMessage(conn, message, buffer)
|
||||||
if errSize, ok := err.(quic.ErrMessageTooLarge); ok {
|
if errSize, ok := err.(quic.ErrMessageTooLarge); ok {
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"io"
|
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
@ -72,17 +71,6 @@ func (c *ClientConn) WriteBuffer(buffer *buf.Buffer) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ClientConn) ReadFrom(r io.Reader) (n int64, err error) {
|
|
||||||
if !c.headerWritten {
|
|
||||||
return bufio.ReadFrom0(c, r)
|
|
||||||
}
|
|
||||||
return bufio.Copy(c.ExtendedConn, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *ClientConn) WriteTo(w io.Writer) (n int64, err error) {
|
|
||||||
return bufio.Copy(w, c.ExtendedConn)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *ClientConn) FrontHeadroom() int {
|
func (c *ClientConn) FrontHeadroom() int {
|
||||||
if !c.headerWritten {
|
if !c.headerWritten {
|
||||||
return KeyLength + 5 + M.MaxSocksaddrLength
|
return KeyLength + 5 + M.MaxSocksaddrLength
|
||||||
|
@ -203,39 +191,18 @@ func ClientHandshakeRaw(conn net.Conn, key [KeyLength]byte, command byte, destin
|
||||||
|
|
||||||
func ClientHandshake(conn net.Conn, key [KeyLength]byte, destination M.Socksaddr, payload []byte) error {
|
func ClientHandshake(conn net.Conn, key [KeyLength]byte, destination M.Socksaddr, payload []byte) error {
|
||||||
headerLen := KeyLength + M.SocksaddrSerializer.AddrPortLen(destination) + 5
|
headerLen := KeyLength + M.SocksaddrSerializer.AddrPortLen(destination) + 5
|
||||||
var header *buf.Buffer
|
header := buf.NewSize(headerLen + len(payload))
|
||||||
defer header.Release()
|
defer header.Release()
|
||||||
var writeHeader bool
|
|
||||||
if len(payload) > 0 && headerLen+len(payload) < 65535 {
|
|
||||||
buffer := buf.StackNewSize(headerLen + len(payload))
|
|
||||||
defer common.KeepAlive(buffer)
|
|
||||||
header = common.Dup(buffer)
|
|
||||||
} else {
|
|
||||||
buffer := buf.StackNewSize(headerLen)
|
|
||||||
defer common.KeepAlive(buffer)
|
|
||||||
header = common.Dup(buffer)
|
|
||||||
writeHeader = true
|
|
||||||
}
|
|
||||||
common.Must1(header.Write(key[:]))
|
common.Must1(header.Write(key[:]))
|
||||||
common.Must1(header.Write(CRLF))
|
common.Must1(header.Write(CRLF))
|
||||||
common.Must(header.WriteByte(CommandTCP))
|
common.Must(header.WriteByte(CommandTCP))
|
||||||
common.Must(M.SocksaddrSerializer.WriteAddrPort(header, destination))
|
common.Must(M.SocksaddrSerializer.WriteAddrPort(header, destination))
|
||||||
common.Must1(header.Write(CRLF))
|
common.Must1(header.Write(CRLF))
|
||||||
if !writeHeader {
|
|
||||||
common.Must1(header.Write(payload))
|
common.Must1(header.Write(payload))
|
||||||
}
|
|
||||||
|
|
||||||
_, err := conn.Write(header.Bytes())
|
_, err := conn.Write(header.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return E.Cause(err, "write request")
|
return E.Cause(err, "write request")
|
||||||
}
|
}
|
||||||
|
|
||||||
if writeHeader {
|
|
||||||
_, err = conn.Write(payload)
|
|
||||||
if err != nil {
|
|
||||||
return E.Cause(err, "write payload")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -258,14 +225,12 @@ func ClientHandshakePacket(conn net.Conn, key [KeyLength]byte, destination M.Soc
|
||||||
headerLen := KeyLength + 2*M.SocksaddrSerializer.AddrPortLen(destination) + 9
|
headerLen := KeyLength + 2*M.SocksaddrSerializer.AddrPortLen(destination) + 9
|
||||||
payloadLen := payload.Len()
|
payloadLen := payload.Len()
|
||||||
var header *buf.Buffer
|
var header *buf.Buffer
|
||||||
defer header.Release()
|
|
||||||
var writeHeader bool
|
var writeHeader bool
|
||||||
if payload.Start() >= headerLen {
|
if payload.Start() >= headerLen {
|
||||||
header = buf.With(payload.ExtendHeader(headerLen))
|
header = buf.With(payload.ExtendHeader(headerLen))
|
||||||
} else {
|
} else {
|
||||||
buffer := buf.StackNewSize(headerLen)
|
header = buf.NewSize(headerLen)
|
||||||
defer common.KeepAlive(buffer)
|
defer header.Release()
|
||||||
header = common.Dup(buffer)
|
|
||||||
writeHeader = true
|
writeHeader = true
|
||||||
}
|
}
|
||||||
common.Must1(header.Write(key[:]))
|
common.Must1(header.Write(key[:]))
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/sagernet/sing/common"
|
|
||||||
"github.com/sagernet/sing/common/auth"
|
"github.com/sagernet/sing/common/auth"
|
||||||
"github.com/sagernet/sing/common/buf"
|
"github.com/sagernet/sing/common/buf"
|
||||||
"github.com/sagernet/sing/common/bufio"
|
"github.com/sagernet/sing/common/bufio"
|
||||||
|
@ -59,7 +58,7 @@ func (s *Service[K]) UpdateUsers(userList []K, passwordList []string) error {
|
||||||
|
|
||||||
func (s *Service[K]) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
func (s *Service[K]) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
||||||
var key [KeyLength]byte
|
var key [KeyLength]byte
|
||||||
n, err := conn.Read(common.Dup(key[:]))
|
n, err := conn.Read(key[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else if n != KeyLength {
|
} else if n != KeyLength {
|
||||||
|
|
|
@ -139,9 +139,7 @@ func WriteRequest(writer io.Writer, request Request, payload []byte) error {
|
||||||
requestLen += vmess.AddressSerializer.AddrPortLen(request.Destination)
|
requestLen += vmess.AddressSerializer.AddrPortLen(request.Destination)
|
||||||
}
|
}
|
||||||
requestLen += len(payload)
|
requestLen += len(payload)
|
||||||
_buffer := buf.StackNewSize(requestLen)
|
buffer := buf.NewSize(requestLen)
|
||||||
defer common.KeepAlive(_buffer)
|
|
||||||
buffer := common.Dup(_buffer)
|
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
common.Must(
|
common.Must(
|
||||||
buffer.WriteByte(Version),
|
buffer.WriteByte(Version),
|
||||||
|
@ -239,9 +237,7 @@ func WritePacketRequest(writer io.Writer, request Request, payload []byte) error
|
||||||
requestLen += 2
|
requestLen += 2
|
||||||
requestLen += len(payload)
|
requestLen += len(payload)
|
||||||
}
|
}
|
||||||
_buffer := buf.StackNewSize(requestLen)
|
buffer := buf.NewSize(requestLen)
|
||||||
defer common.KeepAlive(_buffer)
|
|
||||||
buffer := common.Dup(_buffer)
|
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
common.Must(
|
common.Must(
|
||||||
buffer.WriteByte(Version),
|
buffer.WriteByte(Version),
|
||||||
|
|
Loading…
Reference in a new issue