Add proxy speed measurement, connection logs and standalone Android packaging

This commit is contained in:
shen
2026-10-13 23:08:57 -07:00
parent f94dab0555
commit 72566b9a8a
22 changed files with 551 additions and 91 deletions
+1 -1
View File
@@ -528,7 +528,7 @@ func (c Config) managedJSONForPlatform(platform string) ([]byte, error) {
map[string]any{"network": "udp", "port": 443, "protocol": "quic", "domain_suffix": []string{"googlevideo.com"}, "action": "reject"},
)
}
inbounds := []any{map[string]any{"type": "socks", "tag": "local", "listen": "127.0.0.1", "listen_port": c.ListenPort}}
inbounds := []any{map[string]any{"type": "mixed", "tag": "local", "listen": "127.0.0.1", "listen_port": c.ListenPort}}
route := map[string]any{"rules": routeRules, "final": final}
if len(ruleSets) > 0 {
route["rule_set"] = ruleSets
+1 -1
View File
@@ -154,7 +154,7 @@ func (e *Engine) Start(parent context.Context, config Config) error {
// Collect bounded, credential-redacted warning/error events in memory.
// Raw upstream logs must never go to stderr or a user's log file.
if !e.selfTestTrace {
options.Log = &option.LogOptions{Level: "warn", Output: os.DevNull}
options.Log = &option.LogOptions{Level: "info", Output: os.DevNull}
}
instance, err := box.New(box.Options{Options: options, Context: ctx})
if err != nil {
+56
View File
@@ -0,0 +1,56 @@
package bridge
import (
"bufio"
"context"
"io"
"net"
"net/http"
"strings"
"testing"
"time"
)
func TestMixedHTTPEntryUsesSelectedProxyAndCapturesConnections(t *testing.T) {
upstream, count := socksEcho(t)
defer upstream.Close()
entry, port := reservePort(t)
entry.Close()
var engine Engine
if err := engine.Start(context.Background(), Config{
ListenPort: port, Feature: "proxy", Mode: "global",
Proxy: map[string]any{"type": "socks", "server": "127.0.0.1", "server_port": upstream.Addr().(*net.TCPAddr).Port},
}); err != nil {
t.Fatal(err)
}
defer engine.Close()
conn, err := net.Dial("tcp", entry.Addr().String())
if err != nil {
t.Fatal(err)
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(5 * time.Second))
io.WriteString(conn, "CONNECT 198.51.100.8:443 HTTP/1.1\r\nHost: 198.51.100.8:443\r\n\r\n")
reader := bufio.NewReader(conn)
response, err := http.ReadResponse(reader, &http.Request{Method: "CONNECT"})
if err != nil || response.StatusCode != 200 {
t.Fatalf("CONNECT: %v %v", response, err)
}
io.WriteString(conn, "proxy-download")
echoed := make([]byte, len("proxy-download"))
if _, err = io.ReadFull(reader, echoed); err != nil || string(echoed) != "proxy-download" {
t.Fatal("proxy tunnel failed", err)
}
if count.Load() != 1 {
t.Fatal("download did not use selected outbound")
}
found := false
for _, event := range engine.Snapshot().RuntimeEvents {
if strings.Contains(event.Message, "198.51.100.8") {
found = true
}
}
if !found {
t.Fatal("connection missing from runtime logs")
}
}
+15 -3
View File
@@ -11,6 +11,7 @@ import (
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-box/protocol/mixed"
"github.com/sagernet/sing-box/protocol/socks"
N "github.com/sagernet/sing/common/network"
)
@@ -61,7 +62,8 @@ func (s *connectionSet) closeAll() {
}
type trackedInbound struct {
*socks.Inbound
adapter.Inbound
handler adapter.TCPInjectableInbound
listener *listener.Listener
connections connectionSet
}
@@ -73,13 +75,23 @@ func registerTrackedInbound(registry *inbound.Registry) {
if err != nil {
return nil, err
}
tracked := &trackedInbound{Inbound: delegate.(*socks.Inbound), connections: connectionSet{connections: make(map[*trackedConnection]struct{})}}
tracked := &trackedInbound{Inbound: delegate, handler: delegate.(*socks.Inbound), connections: connectionSet{connections: make(map[*trackedConnection]struct{})}}
// The delegate only handles the protocol. This listener owns the
// accepted TCP controls; its options also constrain UDP association
// allocation through the delegate's original packet-listener helper.
tracked.listener = listener.New(listener.Options{Context: ctx, Logger: logger, Network: []string{N.NetworkTCP}, Listen: options.ListenOptions, ConnectionHandler: tracked})
return tracked, nil
})
inbound.Register[option.HTTPMixedInboundOptions](registry, C.TypeMixed,
func(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.HTTPMixedInboundOptions) (adapter.Inbound, error) {
delegate, err := mixed.NewInbound(ctx, router, logger, tag, options)
if err != nil {
return nil, err
}
tracked := &trackedInbound{Inbound: delegate, handler: delegate.(*mixed.Inbound), connections: connectionSet{connections: make(map[*trackedConnection]struct{})}}
tracked.listener = listener.New(listener.Options{Context: ctx, Logger: logger, Network: []string{N.NetworkTCP}, Listen: options.ListenOptions, ConnectionHandler: tracked})
return tracked, nil
})
}
func (h *trackedInbound) Start(stage adapter.StartStage, scope *adapter.Scope) error {
@@ -103,5 +115,5 @@ func (h *trackedInbound) NewConnection(ctx context.Context, conn net.Conn, metad
_ = conn.Close()
return
}
h.Inbound.NewConnection(ctx, tracked, metadata, onClose)
h.handler.NewConnection(ctx, tracked, metadata, onClose)
}
+3 -3
View File
@@ -47,7 +47,7 @@ func (r *runtimeLogs) configure(node map[string]any) {
}
func (r *runtimeLogs) WriteMessage(level log.Level, message string) {
if level > log.LevelWarn {
if level > log.LevelInfo {
return
}
r.mu.Lock()
@@ -63,8 +63,8 @@ func (r *runtimeLogs) WriteMessage(level log.Level, message string) {
}
r.sequence++
r.events = append(r.events, RuntimeEvent{Sequence: r.sequence, Message: message})
if len(r.events) > 32 {
r.events = append([]RuntimeEvent(nil), r.events[len(r.events)-32:]...)
if len(r.events) > 128 {
r.events = append([]RuntimeEvent(nil), r.events[len(r.events)-128:]...)
}
}
+14 -3
View File
@@ -9,12 +9,12 @@ import (
func TestRuntimeLogsBoundedAndRedacted(t *testing.T) {
var logs runtimeLogs
logs.configure(map[string]any{"uuid": "secret-uuid", "password": "secret-password"})
logs.WriteMessage(log.LevelInfo, "ignored")
for i := 0; i < 40; i++ {
logs.WriteMessage(log.LevelInfo, "connection opened")
for i := 0; i < 140; i++ {
logs.WriteMessage(log.LevelError, "failure secret-uuid secret-password https://u:p@example.test/?token=secret-token")
}
events := logs.snapshot()
if len(events) != 32 || events[0].Sequence != 9 {
if len(events) != 128 || events[0].Sequence != 14 {
t.Fatal("unbounded or incorrect log sequence")
}
for _, e := range events {
@@ -34,3 +34,14 @@ func TestRuntimeLogsRedactOverlappingCredentialValues(t *testing.T) {
t.Fatal("short credential prevented full password redaction")
}
}
func TestRuntimeLogsCaptureConnectionsButSkipDebug(t *testing.T) {
var logs runtimeLogs
logs.configure(map[string]any{"password": "example-secret"})
logs.WriteMessage(log.LevelDebug, "debug packet")
logs.WriteMessage(log.LevelInfo, "outbound connection example-secret")
events := logs.snapshot()
if len(events) != 1 || strings.Contains(events[0].Message, "example-secret") {
t.Fatal("connection logs missing or credentials exposed")
}
}