Minor fixes

This commit is contained in:
世界 2022-07-20 09:41:44 +08:00
parent 45643fbed1
commit 6327c4b40c
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
16 changed files with 79 additions and 84 deletions

3
box.go
View file

@ -44,7 +44,8 @@ func New(ctx context.Context, options option.Options) (*Box, error) {
var observableLogFactory log.ObservableFactory
var logFile *os.File
if logOptions.Disabled {
logFactory = log.NewNOPFactory()
observableLogFactory = log.NewNOPFactory()
logFactory = observableLogFactory
} else {
var logWriter io.Writer
switch logOptions.Output {

View file

@ -3,6 +3,7 @@ package clashapi
import (
"bytes"
"context"
"errors"
"net"
"net/http"
"strings"
@ -85,7 +86,7 @@ func (s *Server) Start() error {
s.logger.Info("restful api listening at ", listener.Addr())
go func() {
err = s.httpServer.Serve(listener)
if err != nil && !E.IsClosed(err) {
if err != nil && !errors.Is(err, http.ErrServerClosed) {
s.logger.Error("external controller serve error: ", err)
}
}()
@ -243,6 +244,13 @@ func getLogs(logFactory log.ObservableFactory) func(w http.ResponseWriter, r *ht
return
}
subscription, done, err := logFactory.Subscribe()
if err != nil {
render.Status(r, http.StatusNoContent)
return
}
defer logFactory.UnSubscribe(subscription)
var wsConn *websocket.Conn
if websocket.IsWebSocketUpgrade(r) {
var err error
@ -257,14 +265,6 @@ func getLogs(logFactory log.ObservableFactory) func(w http.ResponseWriter, r *ht
render.Status(r, http.StatusOK)
}
subscription, done, err := logFactory.Subscribe()
if err != nil {
log.Warn(err)
render.Status(r, http.StatusInternalServerError)
return
}
defer logFactory.UnSubscribe(subscription)
buf := &bytes.Buffer{}
var logEntry log.Entry
for {

View file

@ -54,17 +54,17 @@ func (tt *tcpTracker) ID() string {
func (tt *tcpTracker) Read(b []byte) (int, error) {
n, err := tt.Conn.Read(b)
download := int64(n)
tt.manager.PushDownloaded(download)
tt.DownloadTotal.Add(download)
upload := int64(n)
tt.manager.PushUploaded(upload)
tt.UploadTotal.Add(upload)
return n, err
}
func (tt *tcpTracker) Write(b []byte) (int, error) {
n, err := tt.Conn.Write(b)
upload := int64(n)
tt.manager.PushUploaded(upload)
tt.UploadTotal.Add(upload)
download := int64(n)
tt.manager.PushDownloaded(download)
tt.DownloadTotal.Add(download)
return n, err
}
@ -112,21 +112,21 @@ func (ut *udpTracker) ID() string {
func (ut *udpTracker) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) {
destination, err = ut.PacketConn.ReadPacket(buffer)
if err == nil {
download := int64(buffer.Len())
ut.manager.PushDownloaded(download)
ut.DownloadTotal.Add(download)
upload := int64(buffer.Len())
ut.manager.PushUploaded(upload)
ut.UploadTotal.Add(upload)
}
return
}
func (ut *udpTracker) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
upload := int64(buffer.Len())
download := int64(buffer.Len())
err := ut.PacketConn.WritePacket(buffer, destination)
if err != nil {
return err
}
ut.manager.PushUploaded(upload)
ut.UploadTotal.Add(upload)
ut.manager.PushDownloaded(download)
ut.DownloadTotal.Add(download)
return nil
}

6
go.mod
View file

@ -11,10 +11,10 @@ require (
github.com/gorilla/websocket v1.5.0
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/oschwald/maxminddb-golang v1.9.0
github.com/sagernet/sing v0.0.0-20220718035659-3d74b823ed56
github.com/sagernet/sing-dns v0.0.0-20220711062726-c64e938e4619
github.com/sagernet/sing v0.0.0-20220720054545-2af19486bb1f
github.com/sagernet/sing-dns v0.0.0-20220720045209-c44590ebeb0f
github.com/sagernet/sing-shadowsocks v0.0.0-20220717063942-45a2ad9cd41f
github.com/sagernet/sing-tun v0.0.0-20220717030718-f53aabff275f
github.com/sagernet/sing-tun v0.0.0-20220720051454-d35c334b46c9
github.com/sagernet/sing-vmess v0.0.0-20220718031323-07c377156e4a
github.com/spf13/cobra v1.5.0
github.com/stretchr/testify v1.8.0

12
go.sum
View file

@ -35,14 +35,14 @@ github.com/oschwald/maxminddb-golang v1.9.0/go.mod h1:TK+s/Z2oZq0rSl4PSeAEoP0bgm
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagernet/sing v0.0.0-20220718035659-3d74b823ed56 h1:r6FHrtIE3dG9c6zl487QjxPFgADq/C0AiDwhpAZFKUw=
github.com/sagernet/sing v0.0.0-20220718035659-3d74b823ed56/go.mod h1:GbtQfZSpmtD3cXeD1qX2LCMwY8dH+bnnInDTqd92IsM=
github.com/sagernet/sing-dns v0.0.0-20220711062726-c64e938e4619 h1:oHbOmq1WS0XaZmXp6WpxzyB2xeyRIA1/L8EJKuNntfY=
github.com/sagernet/sing-dns v0.0.0-20220711062726-c64e938e4619/go.mod h1:y2fpvoxukw3G7eApIZwkcpcG/NE4AB8pCQI0Qd8rMqk=
github.com/sagernet/sing v0.0.0-20220720054545-2af19486bb1f h1:eMaEhzlP5jFCd6xvLkeb8vMayLSuAPy446QSCZgdSGg=
github.com/sagernet/sing v0.0.0-20220720054545-2af19486bb1f/go.mod h1:GbtQfZSpmtD3cXeD1qX2LCMwY8dH+bnnInDTqd92IsM=
github.com/sagernet/sing-dns v0.0.0-20220720045209-c44590ebeb0f h1:PCrkSLS+fQtBimPi/2WzjJqeTy0zJtBDaMLykyTAiwQ=
github.com/sagernet/sing-dns v0.0.0-20220720045209-c44590ebeb0f/go.mod h1:y2fpvoxukw3G7eApIZwkcpcG/NE4AB8pCQI0Qd8rMqk=
github.com/sagernet/sing-shadowsocks v0.0.0-20220717063942-45a2ad9cd41f h1:F6yiuKbBoXgWiuoP7R0YA14pDEl3emxA1mL7M16Q7gc=
github.com/sagernet/sing-shadowsocks v0.0.0-20220717063942-45a2ad9cd41f/go.mod h1:cDrLwa3zwY8AaW6a4sjipn4xgdIr3CT8TPqSW6iFOi0=
github.com/sagernet/sing-tun v0.0.0-20220717030718-f53aabff275f h1:o3YN4sFC7lQznAwutagPqBb23hal7MkgVq/VEvd7Vug=
github.com/sagernet/sing-tun v0.0.0-20220717030718-f53aabff275f/go.mod h1:p7QbUBs2ejf6UQsiHyy1xGAWOk9JWQjZTHy8pOmkWmo=
github.com/sagernet/sing-tun v0.0.0-20220720051454-d35c334b46c9 h1:kzEyWi2iJcq1oNeCCRrJ1Oh/ogbHR0CZRZ/HMxzy+mo=
github.com/sagernet/sing-tun v0.0.0-20220720051454-d35c334b46c9/go.mod h1:p7QbUBs2ejf6UQsiHyy1xGAWOk9JWQjZTHy8pOmkWmo=
github.com/sagernet/sing-vmess v0.0.0-20220718031323-07c377156e4a h1:durFxTP1xsOMeDt8x0AV/9BXAPa8uMQRKzPaVkGSOS0=
github.com/sagernet/sing-vmess v0.0.0-20220718031323-07c377156e4a/go.mod h1:VjqeHNWtDVoExWInXB7QsCeMp5RozlnJhMgfbW/n4I0=
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=

View file

@ -324,7 +324,7 @@ func (a *myInboundAdapter) NewError(ctx context.Context, err error) {
func NewError(logger log.ContextLogger, ctx context.Context, err error) {
common.Close(err)
if E.IsClosed(err) || E.IsCanceled(err) {
if E.IsClosedOrCanceled(err) {
logger.DebugContext(ctx, "connection closed")
return
}

View file

@ -116,7 +116,11 @@ func (t *Tun) NewConnection(ctx context.Context, conn net.Conn, upstreamMetadata
}
t.logger.InfoContext(ctx, "inbound connection from ", metadata.Source)
t.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
return t.router.RouteConnection(ctx, conn, metadata)
err := t.router.RouteConnection(ctx, conn, metadata)
if err != nil {
t.NewError(ctx, err)
}
return err
}
func (t *Tun) NewPacketConnection(ctx context.Context, conn N.PacketConn, upstreamMetadata M.Metadata) error {
@ -137,7 +141,11 @@ func (t *Tun) NewPacketConnection(ctx context.Context, conn N.PacketConn, upstre
}
t.logger.InfoContext(ctx, "inbound packet connection from ", metadata.Source)
t.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
return t.router.RoutePacketConnection(ctx, conn, metadata)
err := t.router.RoutePacketConnection(ctx, conn, metadata)
if err != nil {
t.NewError(ctx, err)
}
return err
}
func (t *Tun) NewError(ctx context.Context, err error) {

View file

@ -75,7 +75,7 @@ func (f Formatter) Format(ctx context.Context, level Level, tag string, message
default:
message = levelString + "[" + xd(int(timestamp.Sub(f.BaseTime)/time.Second), 4) + "] " + message
}
return message
return message + "\n"
}
func (f Formatter) FormatWithSimple(ctx context.Context, level Level, tag string, message string, timestamp time.Time) (string, string) {
@ -126,6 +126,8 @@ func (f Formatter) FormatWithSimple(ctx context.Context, level Level, tag string
} else {
message = F.ToString("[", id, "] ", message)
}
messageSimple = F.ToString("[", id, "] ", messageSimple)
}
switch {
case f.DisableTimestamp:
@ -135,7 +137,7 @@ func (f Formatter) FormatWithSimple(ctx context.Context, level Level, tag string
default:
message = levelString + "[" + xd(int(timestamp.Sub(f.BaseTime)/time.Second), 4) + "] " + message
}
return message, messageSimple
return message + "\n", messageSimple
}
func xd(value int, x int) string {

View file

@ -1,12 +1,17 @@
package log
import "context"
import (
"context"
"os"
var _ Factory = (*nopFactory)(nil)
"github.com/sagernet/sing/common/observable"
)
var _ ObservableFactory = (*nopFactory)(nil)
type nopFactory struct{}
func NewNOPFactory() Factory {
func NewNOPFactory() ObservableFactory {
return (*nopFactory)(nil)
}
@ -66,3 +71,10 @@ func (f *nopFactory) FatalContext(ctx context.Context, args ...any) {
func (f *nopFactory) PanicContext(ctx context.Context, args ...any) {
}
func (f *nopFactory) Subscribe() (subscription observable.Subscription[Entry], done <-chan struct{}, err error) {
return nil, nil, os.ErrInvalid
}
func (f *nopFactory) UnSubscribe(subscription observable.Subscription[Entry]) {
}

View file

@ -71,7 +71,6 @@ func (l *observableLogger) Log(ctx context.Context, level Level, args []any) {
panic(message)
}
l.writer.Write([]byte(message))
l.writer.Write([]byte{'\n'})
if level == LevelFatal {
os.Exit(1)
}

View file

@ -92,7 +92,7 @@ func NewDefaultRule(router adapter.Router, logger log.ContextLogger, options opt
rule.allItems = append(rule.allItems, item)
}
if len(options.Protocol) > 0 {
item := NewUserItem(options.Protocol)
item := NewProtocolItem(options.Protocol)
rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, item)
}

View file

@ -76,7 +76,7 @@ func NewDefaultDNSRule(router adapter.Router, logger log.ContextLogger, options
rule.allItems = append(rule.allItems, item)
}
if len(options.Protocol) > 0 {
item := NewUserItem(options.Protocol)
item := NewProtocolItem(options.Protocol)
rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, item)
}

View file

@ -10,7 +10,7 @@ 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-20220718035659-3d74b823ed56
github.com/sagernet/sing v0.0.0-20220720054545-2af19486bb1f
github.com/spyzhov/ajson v0.7.1
github.com/stretchr/testify v1.8.0
golang.org/x/net v0.0.0-20220708220712-1185a9018129
@ -39,9 +39,9 @@ require (
github.com/oschwald/maxminddb-golang v1.9.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sagernet/sing-dns v0.0.0-20220711062726-c64e938e4619 // indirect
github.com/sagernet/sing-dns v0.0.0-20220720045209-c44590ebeb0f // indirect
github.com/sagernet/sing-shadowsocks v0.0.0-20220717063942-45a2ad9cd41f // indirect
github.com/sagernet/sing-tun v0.0.0-20220717030718-f53aabff275f // indirect
github.com/sagernet/sing-tun v0.0.0-20220720051454-d35c334b46c9 // indirect
github.com/sagernet/sing-vmess v0.0.0-20220718031323-07c377156e4a // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/vishvananda/netlink v1.1.0 // indirect

View file

@ -62,14 +62,14 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sagernet/sing v0.0.0-20220718035659-3d74b823ed56 h1:r6FHrtIE3dG9c6zl487QjxPFgADq/C0AiDwhpAZFKUw=
github.com/sagernet/sing v0.0.0-20220718035659-3d74b823ed56/go.mod h1:GbtQfZSpmtD3cXeD1qX2LCMwY8dH+bnnInDTqd92IsM=
github.com/sagernet/sing-dns v0.0.0-20220711062726-c64e938e4619 h1:oHbOmq1WS0XaZmXp6WpxzyB2xeyRIA1/L8EJKuNntfY=
github.com/sagernet/sing-dns v0.0.0-20220711062726-c64e938e4619/go.mod h1:y2fpvoxukw3G7eApIZwkcpcG/NE4AB8pCQI0Qd8rMqk=
github.com/sagernet/sing v0.0.0-20220720054545-2af19486bb1f h1:eMaEhzlP5jFCd6xvLkeb8vMayLSuAPy446QSCZgdSGg=
github.com/sagernet/sing v0.0.0-20220720054545-2af19486bb1f/go.mod h1:GbtQfZSpmtD3cXeD1qX2LCMwY8dH+bnnInDTqd92IsM=
github.com/sagernet/sing-dns v0.0.0-20220720045209-c44590ebeb0f h1:PCrkSLS+fQtBimPi/2WzjJqeTy0zJtBDaMLykyTAiwQ=
github.com/sagernet/sing-dns v0.0.0-20220720045209-c44590ebeb0f/go.mod h1:y2fpvoxukw3G7eApIZwkcpcG/NE4AB8pCQI0Qd8rMqk=
github.com/sagernet/sing-shadowsocks v0.0.0-20220717063942-45a2ad9cd41f h1:F6yiuKbBoXgWiuoP7R0YA14pDEl3emxA1mL7M16Q7gc=
github.com/sagernet/sing-shadowsocks v0.0.0-20220717063942-45a2ad9cd41f/go.mod h1:cDrLwa3zwY8AaW6a4sjipn4xgdIr3CT8TPqSW6iFOi0=
github.com/sagernet/sing-tun v0.0.0-20220717030718-f53aabff275f h1:o3YN4sFC7lQznAwutagPqBb23hal7MkgVq/VEvd7Vug=
github.com/sagernet/sing-tun v0.0.0-20220717030718-f53aabff275f/go.mod h1:p7QbUBs2ejf6UQsiHyy1xGAWOk9JWQjZTHy8pOmkWmo=
github.com/sagernet/sing-tun v0.0.0-20220720051454-d35c334b46c9 h1:kzEyWi2iJcq1oNeCCRrJ1Oh/ogbHR0CZRZ/HMxzy+mo=
github.com/sagernet/sing-tun v0.0.0-20220720051454-d35c334b46c9/go.mod h1:p7QbUBs2ejf6UQsiHyy1xGAWOk9JWQjZTHy8pOmkWmo=
github.com/sagernet/sing-vmess v0.0.0-20220718031323-07c377156e4a h1:durFxTP1xsOMeDt8x0AV/9BXAPa8uMQRKzPaVkGSOS0=
github.com/sagernet/sing-vmess v0.0.0-20220718031323-07c377156e4a/go.mod h1:VjqeHNWtDVoExWInXB7QsCeMp5RozlnJhMgfbW/n4I0=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=

View file

@ -13,8 +13,13 @@ import (
"github.com/stretchr/testify/require"
)
const (
serverPort uint16 = 10000 + iota
clientPort
testPort
)
func TestShadowsocks(t *testing.T) {
t.Parallel()
for _, method := range []string{
"aes-128-gcm",
"aes-256-gcm",
@ -33,7 +38,6 @@ func TestShadowsocks(t *testing.T) {
}
func TestShadowsocks2022(t *testing.T) {
t.Parallel()
for _, method16 := range []string{
"2022-blake3-aes-128-gcm",
} {
@ -64,10 +68,6 @@ func TestShadowsocks2022(t *testing.T) {
}
func testShadowsocksInboundWithShadowsocksRust(t *testing.T, method string, password string) {
t.Parallel()
serverPort := mkPort(t)
clientPort := mkPort(t)
testPort := mkPort(t)
startDockerContainer(t, DockerOptions{
Image: ImageShadowsocksRustClient,
EntryPoint: "sslocal",
@ -96,10 +96,6 @@ func testShadowsocksInboundWithShadowsocksRust(t *testing.T, method string, pass
}
func testShadowsocksOutboundWithShadowsocksRust(t *testing.T, method string, password string) {
t.Parallel()
serverPort := mkPort(t)
clientPort := mkPort(t)
testPort := mkPort(t)
startDockerContainer(t, DockerOptions{
Image: ImageShadowsocksRustServer,
EntryPoint: "ssserver",
@ -139,10 +135,6 @@ func testShadowsocksOutboundWithShadowsocksRust(t *testing.T, method string, pas
}
func testShadowsocksSelf(t *testing.T, method string, password string) {
t.Parallel()
serverPort := mkPort(t)
clientPort := mkPort(t)
testPort := mkPort(t)
startInstance(t, option.Options{
Log: &option.LogOptions{
Level: "error",

View file

@ -14,7 +14,6 @@ import (
)
func TestVMess(t *testing.T) {
t.Parallel()
for _, security := range []string{
"zero",
} {
@ -32,7 +31,6 @@ func TestVMess(t *testing.T) {
}
func testVMess0(t *testing.T, security string) {
t.Parallel()
user, err := uuid.DefaultGenerator.NewV4()
require.NoError(t, err)
t.Run("self", func(t *testing.T) {
@ -56,7 +54,6 @@ func testVMess0(t *testing.T, security string) {
}
func testVMess1(t *testing.T, security string) {
t.Parallel()
user, err := uuid.DefaultGenerator.NewV4()
require.NoError(t, err)
t.Run("self", func(t *testing.T) {
@ -104,17 +101,11 @@ func testVMess1(t *testing.T, security string) {
}
func testVMessInboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, authenticatedLength bool) {
t.Parallel()
content, err := os.ReadFile("config/vmess-client.json")
require.NoError(t, err)
config, err := ajson.Unmarshal(content)
require.NoError(t, err)
serverPort := mkPort(t)
clientPort := mkPort(t)
testPort := mkPort(t)
config.MustKey("inbounds").MustIndex(0).MustKey("port").SetNumeric(float64(clientPort))
outbound := config.MustKey("outbounds").MustIndex(0).MustKey("settings").MustKey("vnext").MustIndex(0)
outbound.MustKey("port").SetNumeric(float64(serverPort))
@ -165,17 +156,11 @@ func testVMessInboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, au
}
func testVMessOutboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, globalPadding bool, authenticatedLength bool, alterId int) {
t.Parallel()
content, err := os.ReadFile("config/vmess-server.json")
require.NoError(t, err)
config, err := ajson.Unmarshal(content)
require.NoError(t, err)
serverPort := mkPort(t)
clientPort := mkPort(t)
testPort := mkPort(t)
inbound := config.MustKey("inbounds").MustIndex(0)
inbound.MustKey("port").SetNumeric(float64(serverPort))
inbound.MustKey("settings").MustKey("clients").MustIndex(0).MustKey("id").SetString(uuid.String())
@ -228,10 +213,6 @@ func testVMessOutboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, g
}
func testVMessSelf(t *testing.T, security string, uuid uuid.UUID, globalPadding bool, authenticatedLength bool) {
t.Parallel()
serverPort := mkPort(t)
clientPort := mkPort(t)
testPort := mkPort(t)
startInstance(t, option.Options{
Log: &option.LogOptions{
Level: "error",