Snapshot uncommitted work: proxy UI, sing-box integration, socks bridge

- Lab app: proxy panel, node editor, configuration and detail pages,
  responsive layout, and related tests
- Add meshport-socks crate and sing-box bridge scripts/docs
- Add singbox_flutter binding and meshport_client_core package
- FFI, VPN helper (unified mac path), and Android/macOS runner updates
- Docs for architecture, client runtime, UI design, and validation
- UI design output artifacts under outputs/ui

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-12 10:06:15 +09:00
co-authored by Claude Haiku 5.5
parent 10b10e5bdf
commit 2f185f914d
123 changed files with 12180 additions and 326 deletions
@@ -0,0 +1,161 @@
// C ABI owns numeric contexts and malloc-allocated UTF-8 JSON responses.
package main
/*
#include <stdlib.h>
#include <stdint.h>
#include "protector.h"
*/
import "C"
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"os"
"path/filepath"
"sync"
"sync/atomic"
"unsafe"
"meshport.local/singbox/bridge"
)
type engineContext struct {
mu sync.Mutex
engine bridge.Engine
closed bool
}
var registry = struct {
sync.Mutex
contexts map[uint64]*engineContext
}{contexts: make(map[uint64]*engineContext)}
var next atomic.Uint64
type request struct {
Directory string `json:"directory,omitempty"`
Op string `json:"op"`
Config *bridge.Config `json:"config,omitempty"`
}
func dispatch(id uint64, data []byte) (any, error) {
if len(data) > 65536 {
return nil, errors.New("request too large")
}
var input request
decoder := json.NewDecoder(bytes.NewReader(data))
decoder.DisallowUnknownFields()
if err := decoder.Decode(&input); err != nil {
return nil, errors.New("invalid engine request")
}
if err := decoder.Decode(new(any)); err != io.EOF {
return nil, errors.New("trailing request data")
}
registry.Lock()
slot := registry.contexts[id]
registry.Unlock()
if slot == nil {
return nil, errors.New("engine context is closed")
}
slot.mu.Lock()
defer slot.mu.Unlock()
if slot.closed {
return nil, errors.New("engine context is closed")
}
switch input.Op {
case "configure_cache":
if slot.engine.Snapshot().State != "Stopped" || input.Directory == "" || !filepath.IsAbs(input.Directory) {
return nil, errors.New("valid stopped-engine cache directory required")
}
info, err := os.Lstat(input.Directory)
if err != nil || !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
return nil, errors.New("invalid cache directory")
}
slot.engine.CacheRoot = input.Directory
case "probe_node":
if input.Config == nil || input.Config.Proxy == nil {
return nil, errors.New("draft node required")
}
return bridge.ProbeNode(context.Background(), input.Config.Proxy, input.Config.DNS, "https://www.gstatic.com/generate_204")
case "start":
if input.Config == nil {
return nil, errors.New("managed configuration required")
}
if err := slot.engine.Start(context.Background(), *input.Config); err != nil {
return nil, err
}
case "stop":
if err := slot.engine.Stop(); err != nil {
return nil, err
}
case "status":
default:
return nil, errors.New("unknown engine operation")
}
return slot.engine.Snapshot(), nil
}
func freeContext(id uint64) {
registry.Lock()
slot := registry.contexts[id]
delete(registry.contexts, id)
registry.Unlock()
if slot != nil {
slot.mu.Lock()
defer slot.mu.Unlock()
slot.closed = true
_ = slot.engine.Close()
}
}
//export meshport_proxy_set_socket_protector
func meshport_proxy_set_socket_protector(pointer C.uintptr_t) { C.proxy_set_protector(pointer) }
//export meshport_proxy_abi_version
func meshport_proxy_abi_version() C.uint32_t { return 1 }
//export meshport_proxy_context_new
func meshport_proxy_context_new() C.uint64_t {
id := next.Add(1)
registry.Lock()
slot := &engineContext{}
slot.engine.Protect = func(fd int) error {
if C.proxy_protect(C.int(fd)) == 0 {
return errors.New("platform socket protection failed")
}
return nil
}
registry.contexts[id] = slot
registry.Unlock()
return C.uint64_t(id)
}
//export meshport_proxy_context_free
func meshport_proxy_context_free(id C.uint64_t) { freeContext(uint64(id)) }
//export meshport_proxy_call
func meshport_proxy_call(id C.uint64_t, input *C.char) (output *C.char) {
defer func() {
if recover() != nil {
output = C.CString(`{"ok":false,"error":"native engine failure"}`)
}
}()
if input == nil {
return C.CString(`{"ok":false,"error":"null request"}`)
}
result, err := dispatch(uint64(id), []byte(C.GoString(input)))
envelope := map[string]any{"ok": err == nil}
if err != nil {
envelope["error"] = err.Error()
} else {
envelope["data"] = result
}
encoded, _ := json.Marshal(envelope)
return C.CString(string(encoded))
}
//export meshport_proxy_string_free
func meshport_proxy_string_free(input *C.char) { C.free(unsafe.Pointer(input)) }
func main() {}
@@ -0,0 +1,54 @@
package main
import (
"encoding/json"
"net"
"sync"
"testing"
)
func newTestContext() uint64 {
id := next.Add(1)
registry.Lock()
registry.contexts[id] = &engineContext{}
registry.Unlock()
return id
}
func TestABIRequestLifecycle(t *testing.T) {
id := newTestContext()
defer freeContext(id)
listener, err := net.Listen("tcp4", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
port := listener.Addr().(*net.TCPAddr).Port
listener.Close()
input, _ := json.Marshal(map[string]any{"op": "start", "config": map[string]any{"listen_port": port, "mesh_socks": "127.0.0.1:9", "mesh_cidrs": []string{"10.77.0.0/24"}}})
if _, err := dispatch(id, input); err != nil {
t.Fatal(err)
}
if _, err := dispatch(id, input); err == nil {
t.Fatal("duplicate start accepted")
}
for _, bad := range []string{`{"op":"status","extra":true}`, `{"op":"status"} {}`, `{"op":"start"}`, `{"op":"wrong"}`} {
if _, err := dispatch(id, []byte(bad)); err == nil {
t.Fatal("bad request accepted", bad)
}
}
var wait sync.WaitGroup
for i := 0; i < 8; i++ {
wait.Add(1)
go func() { defer wait.Done(); _, _ = dispatch(id, []byte(`{"op":"status"}`)) }()
}
freeContext(id)
wait.Wait()
if _, err := dispatch(id, []byte(`{"op":"status"}`)); err == nil {
t.Fatal("freed context accepted")
}
probe, err := net.Listen("tcp4", net.JoinHostPort("127.0.0.1", jsonNumber(port)))
if err != nil {
t.Fatal("listener leaked", err)
}
probe.Close()
}
func jsonNumber(port int) string { data, _ := json.Marshal(port); return string(data) }
@@ -0,0 +1,9 @@
#include "protector.h"
#include <stdatomic.h>
static _Atomic(uintptr_t) protector;
void proxy_set_protector(uintptr_t value) { atomic_store(&protector, value); }
int proxy_protect(int fd) {
uintptr_t value = atomic_load(&protector);
if (!value) return 0;
return ((int (*)(int))value)(fd);
}
@@ -0,0 +1,3 @@
#include <stdint.h>
void proxy_set_protector(uintptr_t pointer);
int proxy_protect(int fd);
@@ -0,0 +1,59 @@
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"os/signal"
"syscall"
"meshport.local/singbox/bridge"
)
func run() error {
if len(os.Args) != 2 {
return fmt.Errorf("usage: meshport-singbox MANAGED-CONFIG.json")
}
file, err := os.Open(os.Args[1])
if err != nil {
return fmt.Errorf("cannot read managed configuration")
}
defer file.Close()
info, err := file.Stat()
if err != nil || info.Size() > 65536 {
return fmt.Errorf("managed configuration exceeds size limit or cannot be read")
}
decoder := json.NewDecoder(io.LimitReader(file, 65537))
decoder.DisallowUnknownFields()
var config bridge.Config
if err = decoder.Decode(&config); err != nil {
return fmt.Errorf("invalid managed configuration")
}
var extra any
if decoder.Decode(&extra) != io.EOF {
return fmt.Errorf("invalid trailing configuration")
}
var engine bridge.Engine
if err = engine.Start(context.Background(), config); err != nil {
return err
}
defer engine.Stop()
// One machine-readable readiness line, no configuration/credentials/log dump.
if err = json.NewEncoder(os.Stdout).Encode(engine.Snapshot()); err != nil {
return err
}
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(stop)
<-stop
return engine.Stop()
}
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}