Improve server error handling

This commit is contained in:
世界 2023-03-02 00:18:35 +08:00
parent 20e1caa531
commit 6e22c004f6
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
4 changed files with 35 additions and 5 deletions

View file

@ -6,7 +6,7 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
_ "github.com/sagernet/gomobile/asset" _ "github.com/sagernet/gomobile/event/key"
"github.com/sagernet/sing-box/cmd/internal/build_shared" "github.com/sagernet/sing-box/cmd/internal/build_shared"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
"github.com/sagernet/sing/common/rw" "github.com/sagernet/sing/common/rw"

View file

@ -24,13 +24,13 @@ func (l *Listener) Accept() (net.Conn, error) {
bufReader := std_bufio.NewReader(conn) bufReader := std_bufio.NewReader(conn)
header, err := proxyproto.Read(bufReader) header, err := proxyproto.Read(bufReader)
if err != nil && !(l.AcceptNoHeader && err == proxyproto.ErrNoProxyProtocol) { if err != nil && !(l.AcceptNoHeader && err == proxyproto.ErrNoProxyProtocol) {
return nil, err return nil, &Error{err}
} }
if bufReader.Buffered() > 0 { if bufReader.Buffered() > 0 {
cache := buf.NewSize(bufReader.Buffered()) cache := buf.NewSize(bufReader.Buffered())
_, err = cache.ReadFullFrom(bufReader, cache.FreeLen()) _, err = cache.ReadFullFrom(bufReader, cache.FreeLen())
if err != nil { if err != nil {
return nil, err return nil, &Error{err}
} }
conn = bufio.NewCachedConn(conn, cache) conn = bufio.NewCachedConn(conn, cache)
} }
@ -42,3 +42,21 @@ func (l *Listener) Accept() (net.Conn, error) {
} }
return conn, nil return conn, nil
} }
var _ net.Error = (*Error)(nil)
type Error struct {
error
}
func (e *Error) Unwrap() error {
return e.error
}
func (e *Error) Timeout() bool {
return false
}
func (e *Error) Temporary() bool {
return true
}

View file

@ -13,6 +13,8 @@ import (
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
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"
"go.uber.org/atomic"
) )
var _ adapter.Inbound = (*myInboundAdapter)(nil) var _ adapter.Inbound = (*myInboundAdapter)(nil)
@ -42,6 +44,8 @@ type myInboundAdapter struct {
udpAddr M.Socksaddr udpAddr M.Socksaddr
packetOutboundClosed chan struct{} packetOutboundClosed chan struct{}
packetOutbound chan *myInboundPacket packetOutbound chan *myInboundPacket
inShutdown atomic.Bool
} }
func (a *myInboundAdapter) Type() string { func (a *myInboundAdapter) Type() string {
@ -97,6 +101,7 @@ func (a *myInboundAdapter) Start() error {
} }
func (a *myInboundAdapter) Close() error { func (a *myInboundAdapter) Close() error {
a.inShutdown.Store(true)
var err error var err error
if a.clearSystemProxy != nil { if a.clearSystemProxy != nil {
err = a.clearSystemProxy() err = a.clearSystemProxy()

View file

@ -39,10 +39,17 @@ func (a *myInboundAdapter) loopTCPIn() {
for { for {
conn, err := tcpListener.Accept() conn, err := tcpListener.Accept()
if err != nil { if err != nil {
if E.IsClosed(err) { //goland:noinspection GoDeprecation
//nolint:staticcheck
if netError, isNetError := err.(net.Error); isNetError && netError.Temporary() {
a.logger.Error(err)
continue
}
if a.inShutdown.Load() && E.IsClosed(err) {
return return
} }
a.logger.Error("accept: ", err) a.tcpListener.Close()
a.logger.Error("serve error: ", err)
continue continue
} }
go a.injectTCP(conn, adapter.InboundContext{}) go a.injectTCP(conn, adapter.InboundContext{})