Files
shenleiandClaude Haiku 5.5 2f185f914d 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>
2026-10-12 10:06:15 +09:00

162 lines
4.1 KiB
Go

// 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() {}