- 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>
73 lines
2.7 KiB
Go
73 lines
2.7 KiB
Go
package bridge
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
"github.com/sagernet/sing/protocol/socks"
|
|
"github.com/sagernet/sing/protocol/socks/socks5"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type ProbeResult struct {
|
|
Method string `json:"method"`
|
|
ElapsedMS float64 `json:"elapsed_ms"`
|
|
StatusCode int `json:"status_code"`
|
|
}
|
|
|
|
// A draft probe owns an isolated loopback engine and never installs a TUN or
|
|
// changes the running instance. The public ABI fixes its HTTPS test destination.
|
|
func ProbeNode(parent context.Context, node map[string]any, dnsServer string, target string) (ProbeResult, error) {
|
|
ctx, cancel := context.WithTimeout(parent, 10*time.Second)
|
|
defer cancel()
|
|
listener, err := net.Listen("tcp4", "127.0.0.1:0")
|
|
if err != nil {
|
|
return ProbeResult{}, errors.New("cannot reserve probe port")
|
|
}
|
|
port := uint16(listener.Addr().(*net.TCPAddr).Port)
|
|
listener.Close()
|
|
var engine Engine
|
|
defer engine.Close()
|
|
if err := engine.Start(ctx, Config{ListenPort: port, Feature: "proxy", Mode: "global", Proxy: node, DNS: dnsServer}); err != nil {
|
|
return ProbeResult{}, errors.New("invalid draft proxy configuration")
|
|
}
|
|
transport := &http.Transport{DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
c, err := (&net.Dialer{}).DialContext(ctx, "tcp4", net.JoinHostPort("127.0.0.1", strconv.Itoa(int(port))))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
c.SetDeadline(time.Now().Add(8 * time.Second))
|
|
_, err = socks.ClientHandshake5(c, socks5.CommandConnect, M.ParseSocksaddr(address), "", "")
|
|
if err != nil {
|
|
c.Close()
|
|
return nil, err
|
|
}
|
|
return c, nil
|
|
}, TLSHandshakeTimeout: 8 * time.Second, DisableKeepAlives: true}
|
|
defer transport.CloseIdleConnections()
|
|
client := http.Client{Transport: transport, Timeout: 10 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
|
|
request, err := http.NewRequestWithContext(ctx, http.MethodGet, target, nil)
|
|
if err != nil {
|
|
return ProbeResult{}, errors.New("invalid probe target")
|
|
}
|
|
started := time.Now()
|
|
response, err := client.Do(request)
|
|
if err != nil {
|
|
return ProbeResult{}, errors.New("HTTPS connectivity check failed")
|
|
}
|
|
defer response.Body.Close()
|
|
_, err = io.Copy(io.Discard, io.LimitReader(response.Body, 4096))
|
|
if err != nil {
|
|
return ProbeResult{}, errors.New("connectivity response failed")
|
|
}
|
|
result := ProbeResult{Method: "HTTP connectivity", ElapsedMS: float64(time.Since(started).Microseconds()) / 1000, StatusCode: response.StatusCode}
|
|
if response.StatusCode < 200 || response.StatusCode >= 300 {
|
|
return result, errors.New("connectivity target returned a non-success status")
|
|
}
|
|
return result, nil
|
|
}
|