mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-16 12:32:22 +08:00
Refactor bufio
This commit is contained in:
parent
c9226aeaaf
commit
97870c9288
|
@ -3,16 +3,16 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
_ "net/http/pprof"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
|
||||
"github.com/sagernet/sing-box/common/badjson"
|
||||
"github.com/sagernet/sing-box/log"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
"runtime/debug"
|
||||
"encoding/json"
|
||||
"github.com/sagernet/sing-box/common/badjson"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
|
|
@ -334,7 +334,7 @@ func (c *ClientPacketConn) WriteBuffer(buffer *buf.Buffer) error {
|
|||
return c.ExtendedConn.WriteBuffer(buffer)
|
||||
}
|
||||
|
||||
func (c *ClientPacketConn) Headroom() int {
|
||||
func (c *ClientPacketConn) FrontHeadroom() int {
|
||||
return 2
|
||||
}
|
||||
|
||||
|
@ -486,7 +486,7 @@ func (c *ClientPacketAddrConn) LocalAddr() net.Addr {
|
|||
return c.ExtendedConn.LocalAddr()
|
||||
}
|
||||
|
||||
func (c *ClientPacketAddrConn) Headroom() int {
|
||||
func (c *ClientPacketAddrConn) FrontHeadroom() int {
|
||||
return 2 + M.MaxSocksaddrLength
|
||||
}
|
||||
|
||||
|
|
|
@ -26,8 +26,9 @@ func NewConnection(ctx context.Context, router adapter.Router, errorHandler E.Ha
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var stream net.Conn
|
||||
for {
|
||||
stream, err := session.Accept()
|
||||
stream, err = session.Accept()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -116,7 +117,7 @@ func (c *ServerConn) WriteBuffer(buffer *buf.Buffer) error {
|
|||
return c.ExtendedConn.WriteBuffer(buffer)
|
||||
}
|
||||
|
||||
func (c *ServerConn) Headroom() int {
|
||||
func (c *ServerConn) FrontHeadroom() int {
|
||||
if !c.responseWrite {
|
||||
return 1
|
||||
}
|
||||
|
@ -182,7 +183,7 @@ func (c *ServerPacketConn) Upstream() any {
|
|||
return c.ExtendedConn
|
||||
}
|
||||
|
||||
func (c *ServerPacketConn) Headroom() int {
|
||||
func (c *ServerPacketConn) FrontHeadroom() int {
|
||||
if !c.responseWrite {
|
||||
return 3
|
||||
}
|
||||
|
@ -247,7 +248,7 @@ func (c *ServerPacketAddrConn) Upstream() any {
|
|||
return c.ExtendedConn
|
||||
}
|
||||
|
||||
func (c *ServerPacketAddrConn) Headroom() int {
|
||||
func (c *ServerPacketAddrConn) FrontHeadroom() int {
|
||||
if !c.responseWrite {
|
||||
return 3 + M.MaxSocksaddrLength
|
||||
}
|
||||
|
|
6
go.mod
6
go.mod
|
@ -15,11 +15,11 @@ require (
|
|||
github.com/logrusorgru/aurora v2.0.3+incompatible
|
||||
github.com/lucas-clemente/quic-go v0.28.1
|
||||
github.com/oschwald/maxminddb-golang v1.10.0
|
||||
github.com/sagernet/sing v0.0.0-20220808004927-21369d10810d
|
||||
github.com/sagernet/sing v0.0.0-20220811152014-735372ab3ccf
|
||||
github.com/sagernet/sing-dns v0.0.0-20220803121532-9e1ffb850d91
|
||||
github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1
|
||||
github.com/sagernet/sing-shadowsocks v0.0.0-20220811135826-7e47fd1a99d9
|
||||
github.com/sagernet/sing-tun v0.0.0-20220808133432-d378b6ca536d
|
||||
github.com/sagernet/sing-vmess v0.0.0-20220804023624-e829b41c84c2
|
||||
github.com/sagernet/sing-vmess v0.0.0-20220811135656-4f3f07acf9c4
|
||||
github.com/spf13/cobra v1.5.0
|
||||
github.com/stretchr/testify v1.8.0
|
||||
github.com/xtaci/smux v1.5.16
|
||||
|
|
12
go.sum
12
go.sum
|
@ -152,16 +152,16 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR
|
|||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805 h1:hE+vtsjBCCPmxkRz9jZA+CicHgVkDT6H+Av5ZzskVxs=
|
||||
github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM=
|
||||
github.com/sagernet/sing v0.0.0-20220808004927-21369d10810d h1:vWzXLfdGyAYYbBpYFFHErtJlBXC59AieYMlUMAI6gw8=
|
||||
github.com/sagernet/sing v0.0.0-20220808004927-21369d10810d/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY=
|
||||
github.com/sagernet/sing v0.0.0-20220811152014-735372ab3ccf h1:k8ZXdBb5D6JqDTgwLAw4cazapPcLYbcJNxSFUvUff+s=
|
||||
github.com/sagernet/sing v0.0.0-20220811152014-735372ab3ccf/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY=
|
||||
github.com/sagernet/sing-dns v0.0.0-20220803121532-9e1ffb850d91 h1:jxt2PYixIkK2i7nUGW3f+PzJagEZcbNyQddBWGuqNnw=
|
||||
github.com/sagernet/sing-dns v0.0.0-20220803121532-9e1ffb850d91/go.mod h1:T77zZdE2Cm6VqnFumrpwsq+kxYsbq+vWDhmjtdSl/oM=
|
||||
github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1 h1:RYvOc69eSNMN0dwVugrDts41Nn7Ar/C/n/fvytvFcp4=
|
||||
github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1/go.mod h1:NqZjiXszgVCMQ4gVDa2V+drhS8NMfGqUqDF86EacEFc=
|
||||
github.com/sagernet/sing-shadowsocks v0.0.0-20220811135826-7e47fd1a99d9 h1:/FFNyglfOlk1x6NWhBWI+bc/kVQc7SFOSYAJ2m7FwHc=
|
||||
github.com/sagernet/sing-shadowsocks v0.0.0-20220811135826-7e47fd1a99d9/go.mod h1:3Pe1OCs1zrMyZmMB4st8GF/IL6EMHLSVnUHSS5VjnfM=
|
||||
github.com/sagernet/sing-tun v0.0.0-20220808133432-d378b6ca536d h1:AQpkoUiF8FxYVI1lf2W9Rbkk914eHjVH9M8y+F/0+Nw=
|
||||
github.com/sagernet/sing-tun v0.0.0-20220808133432-d378b6ca536d/go.mod h1:gWwYd53AqXl+Y+q6WlXUc6PkqU28sfu5VTQhyeEIFbw=
|
||||
github.com/sagernet/sing-vmess v0.0.0-20220804023624-e829b41c84c2 h1:C8sc2MYiNx0O7uQ0nieJWq5qYeIHj20XHFWPlcgoQeY=
|
||||
github.com/sagernet/sing-vmess v0.0.0-20220804023624-e829b41c84c2/go.mod h1:bNXBqSWYaG3ePl6u0xQY5zneE+ZKa3683ZpuE8S1M1w=
|
||||
github.com/sagernet/sing-vmess v0.0.0-20220811135656-4f3f07acf9c4 h1:2hLETh97+S4WnfMR27XyC7QVU1SH7FTNoCznP229YJU=
|
||||
github.com/sagernet/sing-vmess v0.0.0-20220811135656-4f3f07acf9c4/go.mod h1:82O6gzbxLha/W/jxSVQbsqf2lVdRTjMIgyLug0lpJps=
|
||||
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
|
||||
github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY=
|
||||
github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM=
|
||||
|
|
|
@ -88,11 +88,6 @@ func (n *Naive) Start() error {
|
|||
return E.Cause(err, "create TLS config")
|
||||
}
|
||||
|
||||
n.httpServer = &http.Server{
|
||||
Handler: n,
|
||||
TLSConfig: n.tlsConfig.Config(),
|
||||
}
|
||||
|
||||
var listenAddr string
|
||||
if nAddr := netip.Addr(n.listenOptions.Listen); nAddr.IsValid() {
|
||||
if n.listenOptions.ListenPort != 0 {
|
||||
|
@ -107,6 +102,10 @@ func (n *Naive) Start() error {
|
|||
}
|
||||
|
||||
if common.Contains(n.network, N.NetworkTCP) {
|
||||
n.httpServer = &http.Server{
|
||||
Handler: n,
|
||||
TLSConfig: n.tlsConfig.Config(),
|
||||
}
|
||||
tcpListener, err := net.Listen(M.NetworkFromNetAddr("tcp", netip.Addr(n.listenOptions.Listen)), listenAddr)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -325,6 +324,13 @@ func (c *naivePaddingConn) write(p []byte) (n int, err error) {
|
|||
return c.writer.Write(p)
|
||||
}
|
||||
|
||||
func (c *naivePaddingConn) FrontHeadroom() int {
|
||||
if c.writePadding < kFirstPaddings {
|
||||
return 3 + 255
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (c *naivePaddingConn) WriteBuffer(buffer *buf.Buffer) error {
|
||||
defer buffer.Release()
|
||||
if c.writePadding < kFirstPaddings {
|
||||
|
@ -345,14 +351,14 @@ func (c *naivePaddingConn) WriteBuffer(buffer *buf.Buffer) error {
|
|||
|
||||
func (c *naivePaddingConn) WriteTo(w io.Writer) (n int64, err error) {
|
||||
if c.readPadding < kFirstPaddings {
|
||||
return bufio.WriteTo0(c, w)
|
||||
return bufio.WriteToN(c, w, kFirstPaddings-c.readPadding)
|
||||
}
|
||||
return bufio.Copy(w, c.reader)
|
||||
}
|
||||
|
||||
func (c *naivePaddingConn) ReadFrom(r io.Reader) (n int64, err error) {
|
||||
if c.writePadding < kFirstPaddings {
|
||||
return bufio.ReadFrom0(c, r)
|
||||
return bufio.ReadFromN(c, r, kFirstPaddings-c.writePadding)
|
||||
}
|
||||
return bufio.Copy(c.writer, r)
|
||||
}
|
||||
|
|
|
@ -29,6 +29,7 @@ func (n *Naive) configureHTTP3Listener(listenAddr string) error {
|
|||
go func() {
|
||||
sErr := h3Server.Serve(udpListener)
|
||||
if sErr == quic.ErrServerClosed {
|
||||
udpListener.Close()
|
||||
return
|
||||
} else if sErr != nil {
|
||||
n.logger.Error("http3 server serve error: ", sErr)
|
||||
|
|
|
@ -6,7 +6,6 @@ import (
|
|||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing/common"
|
||||
F "github.com/sagernet/sing/common/format"
|
||||
)
|
||||
|
||||
|
@ -42,10 +41,6 @@ func (f *simpleFactory) NewLogger(tag string) ContextLogger {
|
|||
return &simpleLogger{f, tag}
|
||||
}
|
||||
|
||||
func (f *simpleFactory) Close() error {
|
||||
return common.Close(f.writer)
|
||||
}
|
||||
|
||||
var _ ContextLogger = (*simpleLogger)(nil)
|
||||
|
||||
type simpleLogger struct {
|
||||
|
|
|
@ -58,7 +58,6 @@ func (f *observableFactory) UnSubscribe(sub observable.Subscription[Entry]) {
|
|||
|
||||
func (f *observableFactory) Close() error {
|
||||
return common.Close(
|
||||
f.writer,
|
||||
f.observer,
|
||||
)
|
||||
}
|
||||
|
|
|
@ -23,14 +23,13 @@ func startInstance(t *testing.T, options option.Options) {
|
|||
require.NoError(t, err)
|
||||
err = instance.Start()
|
||||
if err != nil {
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
time.Sleep(time.Second)
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
instance.Close()
|
||||
})
|
||||
}
|
||||
|
|
|
@ -10,8 +10,8 @@ require (
|
|||
github.com/docker/docker v20.10.17+incompatible
|
||||
github.com/docker/go-connections v0.4.0
|
||||
github.com/gofrs/uuid v4.2.0+incompatible
|
||||
github.com/sagernet/sing v0.0.0-20220808004927-21369d10810d
|
||||
github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1
|
||||
github.com/sagernet/sing v0.0.0-20220811152014-735372ab3ccf
|
||||
github.com/sagernet/sing-shadowsocks v0.0.0-20220811135826-7e47fd1a99d9
|
||||
github.com/spyzhov/ajson v0.7.1
|
||||
github.com/stretchr/testify v1.8.0
|
||||
golang.org/x/net v0.0.0-20220809184613-07c6da5e1ced
|
||||
|
@ -56,7 +56,7 @@ require (
|
|||
github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805 // indirect
|
||||
github.com/sagernet/sing-dns v0.0.0-20220803121532-9e1ffb850d91 // indirect
|
||||
github.com/sagernet/sing-tun v0.0.0-20220808133432-d378b6ca536d // indirect
|
||||
github.com/sagernet/sing-vmess v0.0.0-20220804023624-e829b41c84c2 // indirect
|
||||
github.com/sagernet/sing-vmess v0.0.0-20220811154439-e85c9d12159e // indirect
|
||||
github.com/sirupsen/logrus v1.8.1 // indirect
|
||||
github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 // indirect
|
||||
github.com/xtaci/smux v1.5.16 // indirect
|
||||
|
|
12
test/go.sum
12
test/go.sum
|
@ -176,16 +176,16 @@ github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R
|
|||
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
|
||||
github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805 h1:hE+vtsjBCCPmxkRz9jZA+CicHgVkDT6H+Av5ZzskVxs=
|
||||
github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM=
|
||||
github.com/sagernet/sing v0.0.0-20220808004927-21369d10810d h1:vWzXLfdGyAYYbBpYFFHErtJlBXC59AieYMlUMAI6gw8=
|
||||
github.com/sagernet/sing v0.0.0-20220808004927-21369d10810d/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY=
|
||||
github.com/sagernet/sing v0.0.0-20220811152014-735372ab3ccf h1:k8ZXdBb5D6JqDTgwLAw4cazapPcLYbcJNxSFUvUff+s=
|
||||
github.com/sagernet/sing v0.0.0-20220811152014-735372ab3ccf/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY=
|
||||
github.com/sagernet/sing-dns v0.0.0-20220803121532-9e1ffb850d91 h1:jxt2PYixIkK2i7nUGW3f+PzJagEZcbNyQddBWGuqNnw=
|
||||
github.com/sagernet/sing-dns v0.0.0-20220803121532-9e1ffb850d91/go.mod h1:T77zZdE2Cm6VqnFumrpwsq+kxYsbq+vWDhmjtdSl/oM=
|
||||
github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1 h1:RYvOc69eSNMN0dwVugrDts41Nn7Ar/C/n/fvytvFcp4=
|
||||
github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1/go.mod h1:NqZjiXszgVCMQ4gVDa2V+drhS8NMfGqUqDF86EacEFc=
|
||||
github.com/sagernet/sing-shadowsocks v0.0.0-20220811135826-7e47fd1a99d9 h1:/FFNyglfOlk1x6NWhBWI+bc/kVQc7SFOSYAJ2m7FwHc=
|
||||
github.com/sagernet/sing-shadowsocks v0.0.0-20220811135826-7e47fd1a99d9/go.mod h1:3Pe1OCs1zrMyZmMB4st8GF/IL6EMHLSVnUHSS5VjnfM=
|
||||
github.com/sagernet/sing-tun v0.0.0-20220808133432-d378b6ca536d h1:AQpkoUiF8FxYVI1lf2W9Rbkk914eHjVH9M8y+F/0+Nw=
|
||||
github.com/sagernet/sing-tun v0.0.0-20220808133432-d378b6ca536d/go.mod h1:gWwYd53AqXl+Y+q6WlXUc6PkqU28sfu5VTQhyeEIFbw=
|
||||
github.com/sagernet/sing-vmess v0.0.0-20220804023624-e829b41c84c2 h1:C8sc2MYiNx0O7uQ0nieJWq5qYeIHj20XHFWPlcgoQeY=
|
||||
github.com/sagernet/sing-vmess v0.0.0-20220804023624-e829b41c84c2/go.mod h1:bNXBqSWYaG3ePl6u0xQY5zneE+ZKa3683ZpuE8S1M1w=
|
||||
github.com/sagernet/sing-vmess v0.0.0-20220811154439-e85c9d12159e h1:iRCJqAbppWRlBQvHi/hPIN+QNWHPCU6yxf+P2de2gxA=
|
||||
github.com/sagernet/sing-vmess v0.0.0-20220811154439-e85c9d12159e/go.mod h1:82O6gzbxLha/W/jxSVQbsqf2lVdRTjMIgyLug0lpJps=
|
||||
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
|
||||
github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY=
|
||||
github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM=
|
||||
|
|
|
@ -102,7 +102,7 @@ func testVMessMux(t *testing.T, protocol string) {
|
|||
user, _ := uuid.NewV4()
|
||||
startInstance(t, option.Options{
|
||||
Log: &option.LogOptions{
|
||||
Level: "trace",
|
||||
Level: "error",
|
||||
},
|
||||
Inbounds: []option.Inbound{
|
||||
{
|
||||
|
|
|
@ -13,7 +13,7 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestVMess(t *testing.T) {
|
||||
func _TestVMessAuto(t *testing.T) {
|
||||
security := "auto"
|
||||
user, err := uuid.DefaultGenerator.NewV4()
|
||||
require.NoError(t, err)
|
||||
|
@ -28,7 +28,7 @@ func TestVMess(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func _TestVMess(t *testing.T) {
|
||||
func TestVMess(t *testing.T) {
|
||||
for _, security := range []string{
|
||||
"zero",
|
||||
} {
|
||||
|
|
Loading…
Reference in New Issue
Block a user