dsh-plugin-mesh 0.5.12 → 0.5.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/client.js +55 -9
- package/lib/_selftest-peer-web.mjs +317 -0
- package/lib/_selftest-ssh-target.mjs +106 -0
- package/lib/_selftest-web-target.mjs +218 -0
- package/lib/index.js +378 -22
- package/lib/index.js.ws.bak +4589 -0
- package/lib/peer-web.js +226 -0
- package/lib/ssh-host.js +124 -19
- package/lib/ssh-target.js +98 -0
- package/lib/tsnet/manager.js +119 -0
- package/lib/web-proxy.js +57 -6
- package/lib/web-target.js +191 -0
- package/llms.txt +27 -0
- package/package.json +7 -4
- package/sidecar/main.go +242 -1
package/sidecar/main.go
CHANGED
|
@@ -154,6 +154,8 @@ type sidecar struct {
|
|
|
154
154
|
|
|
155
155
|
mu sync.Mutex
|
|
156
156
|
forwards map[int]net.Listener
|
|
157
|
+
dials map[int]net.Listener
|
|
158
|
+
listens map[int]net.Listener
|
|
157
159
|
diagnostics []string
|
|
158
160
|
}
|
|
159
161
|
|
|
@@ -293,7 +295,7 @@ func main() {
|
|
|
293
295
|
os.Exit(1)
|
|
294
296
|
}
|
|
295
297
|
|
|
296
|
-
sc := &sidecar{token: *flToken, hostname: hostname, forwards: map[int]net.Listener{}}
|
|
298
|
+
sc := &sidecar{token: *flToken, hostname: hostname, forwards: map[int]net.Listener{}, dials: map[int]net.Listener{}, listens: map[int]net.Listener{}}
|
|
297
299
|
|
|
298
300
|
// 解析器**必须在这里**装,早于任何可能启动 tsnet 的代码。
|
|
299
301
|
//
|
|
@@ -445,6 +447,14 @@ func (sc *sidecar) shutdown() {
|
|
|
445
447
|
_ = ln.Close()
|
|
446
448
|
delete(sc.forwards, port)
|
|
447
449
|
}
|
|
450
|
+
for port, ln := range sc.dials {
|
|
451
|
+
_ = ln.Close()
|
|
452
|
+
delete(sc.dials, port)
|
|
453
|
+
}
|
|
454
|
+
for port, ln := range sc.listens {
|
|
455
|
+
_ = ln.Close()
|
|
456
|
+
delete(sc.listens, port)
|
|
457
|
+
}
|
|
448
458
|
sc.mu.Unlock()
|
|
449
459
|
if sc.srv != nil {
|
|
450
460
|
_ = sc.srv.Close()
|
|
@@ -510,6 +520,10 @@ func (sc *sidecar) controlMux() http.Handler {
|
|
|
510
520
|
mux.HandleFunc("/__tsnet/fetch", sc.auth(sc.handleFetch))
|
|
511
521
|
mux.HandleFunc("/__tsnet/forward", sc.auth(sc.handleForward))
|
|
512
522
|
mux.HandleFunc("/__tsnet/unforward", sc.auth(sc.handleUnforward))
|
|
523
|
+
mux.HandleFunc("/__tsnet/dial", sc.auth(sc.handleDial))
|
|
524
|
+
mux.HandleFunc("/__tsnet/undial", sc.auth(sc.handleUndial))
|
|
525
|
+
mux.HandleFunc("/__tsnet/listen", sc.auth(sc.handleListen))
|
|
526
|
+
mux.HandleFunc("/__tsnet/unlisten", sc.auth(sc.handleUnlisten))
|
|
513
527
|
mux.HandleFunc("/__tsnet/shutdown", sc.auth(func(w http.ResponseWriter, r *http.Request) {
|
|
514
528
|
writeJSON(w, 200, map[string]any{"ok": true})
|
|
515
529
|
go func() {
|
|
@@ -758,6 +772,233 @@ func (sc *sidecar) handleUnforward(w http.ResponseWriter, r *http.Request) {
|
|
|
758
772
|
writeJSON(w, 200, map[string]any{"ok": true, "port": req.Port})
|
|
759
773
|
}
|
|
760
774
|
|
|
775
|
+
// ---------------------------------------------------------------------------
|
|
776
|
+
// outbound raw streams
|
|
777
|
+
// ---------------------------------------------------------------------------
|
|
778
|
+
|
|
779
|
+
// dialRequest asks for a loopback listener whose accepted connections are piped
|
|
780
|
+
// byte-for-byte into a TCP address reachable only through the tailnet.
|
|
781
|
+
type dialRequest struct {
|
|
782
|
+
Address string `json:"address"`
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
// handleDial is the raw-stream counterpart of handleForward.
|
|
786
|
+
//
|
|
787
|
+
// Why it has to exist: /__tsnet/fetch buffers a whole response (32 MB cap) and
|
|
788
|
+
// speaks one HTTP transaction, so it cannot carry a WebSocket upgrade or a
|
|
789
|
+
// long-lived stream. The plugin's "open the peer's DSH GUI" path needs both
|
|
790
|
+
// (`/api/remote.mux` is a WS). Rather than teach the control API about HTTP
|
|
791
|
+
// semantics, hand the caller a loopback TCP port and move bytes: every accepted
|
|
792
|
+
// connection is dialed through tsnet and spliced in both directions.
|
|
793
|
+
func (sc *sidecar) handleDial(w http.ResponseWriter, r *http.Request) {
|
|
794
|
+
if r.Method != http.MethodPost {
|
|
795
|
+
writeJSON(w, http.StatusMethodNotAllowed, map[string]any{"ok": false, "error": "POST required"})
|
|
796
|
+
return
|
|
797
|
+
}
|
|
798
|
+
var req dialRequest
|
|
799
|
+
if err := json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&req); err != nil {
|
|
800
|
+
writeJSON(w, 200, map[string]any{"ok": false, "error": err.Error()})
|
|
801
|
+
return
|
|
802
|
+
}
|
|
803
|
+
host, port, err := net.SplitHostPort(strings.TrimSpace(req.Address))
|
|
804
|
+
if err != nil || host == "" || port == "" {
|
|
805
|
+
writeJSON(w, 200, map[string]any{"ok": false, "error": "address must be host:port, got " + strconv.Quote(req.Address)})
|
|
806
|
+
return
|
|
807
|
+
}
|
|
808
|
+
if _, err := strconv.Atoi(port); err != nil {
|
|
809
|
+
writeJSON(w, 200, map[string]any{"ok": false, "error": "port is not numeric: " + port})
|
|
810
|
+
return
|
|
811
|
+
}
|
|
812
|
+
if sc.srv == nil {
|
|
813
|
+
writeJSON(w, 200, map[string]any{"ok": false, "error": "tailnet node is not started"})
|
|
814
|
+
return
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
818
|
+
if err != nil {
|
|
819
|
+
writeJSON(w, 200, map[string]any{"ok": false, "error": "listen: " + err.Error()})
|
|
820
|
+
return
|
|
821
|
+
}
|
|
822
|
+
localPort := ln.Addr().(*net.TCPAddr).Port
|
|
823
|
+
|
|
824
|
+
sc.mu.Lock()
|
|
825
|
+
sc.dials[localPort] = ln
|
|
826
|
+
sc.mu.Unlock()
|
|
827
|
+
|
|
828
|
+
go sc.serveDial(ln, req.Address)
|
|
829
|
+
sc.note("dialing tailnet %s via loopback :%d", req.Address, localPort)
|
|
830
|
+
writeJSON(w, 200, map[string]any{"ok": true, "port": localPort, "address": req.Address})
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
// serveDial accepts on the loopback listener and splices each connection to the
|
|
834
|
+
// tailnet. Failures are per-connection: one refused dial must not kill the
|
|
835
|
+
// listener, because the plugin keeps the port for the lifetime of a panel.
|
|
836
|
+
func (sc *sidecar) serveDial(ln net.Listener, address string) {
|
|
837
|
+
for {
|
|
838
|
+
conn, err := ln.Accept()
|
|
839
|
+
if err != nil {
|
|
840
|
+
// A closed listener is the normal shutdown path (undial / shutdown).
|
|
841
|
+
return
|
|
842
|
+
}
|
|
843
|
+
go func() {
|
|
844
|
+
defer conn.Close()
|
|
845
|
+
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
846
|
+
up, err := sc.srv.Dial(ctx, "tcp", address)
|
|
847
|
+
cancel()
|
|
848
|
+
if err != nil {
|
|
849
|
+
sc.note("dial %s failed: %v", address, err)
|
|
850
|
+
return
|
|
851
|
+
}
|
|
852
|
+
defer up.Close()
|
|
853
|
+
done := make(chan struct{}, 2)
|
|
854
|
+
go func() { _, _ = io.Copy(up, conn); done <- struct{}{} }()
|
|
855
|
+
go func() { _, _ = io.Copy(conn, up); done <- struct{}{} }()
|
|
856
|
+
// 两条方向都结束才算这条连接完;单方向 EOF 时先把另一端关掉,
|
|
857
|
+
// 否则 io.Copy 的另一半会永远等(HTTP keep-alive 场景很常见)。
|
|
858
|
+
<-done
|
|
859
|
+
_ = up.Close()
|
|
860
|
+
_ = conn.Close()
|
|
861
|
+
<-done
|
|
862
|
+
}()
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
// handleUndial closes one outbound stream listener by its loopback port.
|
|
867
|
+
func (sc *sidecar) handleUndial(w http.ResponseWriter, r *http.Request) {
|
|
868
|
+
if r.Method != http.MethodPost {
|
|
869
|
+
writeJSON(w, http.StatusMethodNotAllowed, map[string]any{"ok": false, "error": "POST required"})
|
|
870
|
+
return
|
|
871
|
+
}
|
|
872
|
+
var body struct {
|
|
873
|
+
Port int `json:"port"`
|
|
874
|
+
}
|
|
875
|
+
if err := json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&body); err != nil {
|
|
876
|
+
writeJSON(w, 200, map[string]any{"ok": false, "error": err.Error()})
|
|
877
|
+
return
|
|
878
|
+
}
|
|
879
|
+
sc.mu.Lock()
|
|
880
|
+
ln, exists := sc.dials[body.Port]
|
|
881
|
+
if exists {
|
|
882
|
+
delete(sc.dials, body.Port)
|
|
883
|
+
}
|
|
884
|
+
sc.mu.Unlock()
|
|
885
|
+
if !exists {
|
|
886
|
+
writeJSON(w, 200, map[string]any{"ok": true, "absent": true, "port": body.Port})
|
|
887
|
+
return
|
|
888
|
+
}
|
|
889
|
+
_ = ln.Close()
|
|
890
|
+
sc.note("stopped dialing loopback :%d", body.Port)
|
|
891
|
+
writeJSON(w, 200, map[string]any{"ok": true, "port": body.Port})
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
// ---------------------------------------------------------------------------
|
|
895
|
+
// inbound raw streams (tailnet -> loopback)
|
|
896
|
+
// ---------------------------------------------------------------------------
|
|
897
|
+
|
|
898
|
+
// handleListen is the raw-TCP counterpart of handleForward.
|
|
899
|
+
//
|
|
900
|
+
// handleForward reverse-proxies *HTTP* from the tailnet to loopback, so an SSH
|
|
901
|
+
// handshake (bare `SSH-2.0-...` bytes) gets rejected as a malformed request.
|
|
902
|
+
// This one just moves bytes: listen on the tailnet, dial 127.0.0.1:port, splice.
|
|
903
|
+
// It is the mirror image of handleDial, and the pair is what makes "SSH over
|
|
904
|
+
// tailnet" possible in both directions without any system-level tailscaled.
|
|
905
|
+
func (sc *sidecar) handleListen(w http.ResponseWriter, r *http.Request) {
|
|
906
|
+
if r.Method != http.MethodPost {
|
|
907
|
+
writeJSON(w, http.StatusMethodNotAllowed, map[string]any{"ok": false, "error": "POST required"})
|
|
908
|
+
return
|
|
909
|
+
}
|
|
910
|
+
var body struct {
|
|
911
|
+
Port int `json:"port"`
|
|
912
|
+
}
|
|
913
|
+
if err := json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&body); err != nil {
|
|
914
|
+
writeJSON(w, 200, map[string]any{"ok": false, "error": err.Error()})
|
|
915
|
+
return
|
|
916
|
+
}
|
|
917
|
+
if body.Port <= 0 || body.Port > 65535 {
|
|
918
|
+
writeJSON(w, 200, map[string]any{"ok": false, "error": "port out of range"})
|
|
919
|
+
return
|
|
920
|
+
}
|
|
921
|
+
if sc.srv == nil {
|
|
922
|
+
writeJSON(w, 200, map[string]any{"ok": false, "error": "tailnet node is not started"})
|
|
923
|
+
return
|
|
924
|
+
}
|
|
925
|
+
sc.mu.Lock()
|
|
926
|
+
_, exists := sc.listens[body.Port]
|
|
927
|
+
sc.mu.Unlock()
|
|
928
|
+
if exists {
|
|
929
|
+
writeJSON(w, 200, map[string]any{"ok": true, "already": true, "port": body.Port})
|
|
930
|
+
return
|
|
931
|
+
}
|
|
932
|
+
ln, err := sc.srv.Listen("tcp", ":"+strconv.Itoa(body.Port))
|
|
933
|
+
if err != nil {
|
|
934
|
+
writeJSON(w, 200, map[string]any{"ok": false, "error": "listen: " + err.Error()})
|
|
935
|
+
return
|
|
936
|
+
}
|
|
937
|
+
sc.mu.Lock()
|
|
938
|
+
sc.listens[body.Port] = ln
|
|
939
|
+
sc.mu.Unlock()
|
|
940
|
+
go sc.serveListen(ln, body.Port)
|
|
941
|
+
sc.note("exposing tailnet :%d -> 127.0.0.1:%d", body.Port, body.Port)
|
|
942
|
+
writeJSON(w, 200, map[string]any{"ok": true, "port": body.Port})
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
// serveListen accepts on the tailnet listener and splices each connection to
|
|
946
|
+
// the same port on loopback. Per-connection failures stay per-connection: the
|
|
947
|
+
// local sshd being down must not tear down the tailnet listener.
|
|
948
|
+
func (sc *sidecar) serveListen(ln net.Listener, port int) {
|
|
949
|
+
target := "127.0.0.1:" + strconv.Itoa(port)
|
|
950
|
+
for {
|
|
951
|
+
conn, err := ln.Accept()
|
|
952
|
+
if err != nil {
|
|
953
|
+
return
|
|
954
|
+
}
|
|
955
|
+
go func() {
|
|
956
|
+
defer conn.Close()
|
|
957
|
+
up, err := net.DialTimeout("tcp", target, 10*time.Second)
|
|
958
|
+
if err != nil {
|
|
959
|
+
sc.note("listen :%d dial %s failed: %v", port, target, err)
|
|
960
|
+
return
|
|
961
|
+
}
|
|
962
|
+
defer up.Close()
|
|
963
|
+
done := make(chan struct{}, 2)
|
|
964
|
+
go func() { _, _ = io.Copy(up, conn); done <- struct{}{} }()
|
|
965
|
+
go func() { _, _ = io.Copy(conn, up); done <- struct{}{} }()
|
|
966
|
+
<-done
|
|
967
|
+
_ = up.Close()
|
|
968
|
+
_ = conn.Close()
|
|
969
|
+
<-done
|
|
970
|
+
}()
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
// handleUnlisten stops exposing one port on the tailnet.
|
|
975
|
+
func (sc *sidecar) handleUnlisten(w http.ResponseWriter, r *http.Request) {
|
|
976
|
+
if r.Method != http.MethodPost {
|
|
977
|
+
writeJSON(w, http.StatusMethodNotAllowed, map[string]any{"ok": false, "error": "POST required"})
|
|
978
|
+
return
|
|
979
|
+
}
|
|
980
|
+
var body struct {
|
|
981
|
+
Port int `json:"port"`
|
|
982
|
+
}
|
|
983
|
+
if err := json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&body); err != nil {
|
|
984
|
+
writeJSON(w, 200, map[string]any{"ok": false, "error": err.Error()})
|
|
985
|
+
return
|
|
986
|
+
}
|
|
987
|
+
sc.mu.Lock()
|
|
988
|
+
ln, exists := sc.listens[body.Port]
|
|
989
|
+
if exists {
|
|
990
|
+
delete(sc.listens, body.Port)
|
|
991
|
+
}
|
|
992
|
+
sc.mu.Unlock()
|
|
993
|
+
if !exists {
|
|
994
|
+
writeJSON(w, 200, map[string]any{"ok": true, "absent": true, "port": body.Port})
|
|
995
|
+
return
|
|
996
|
+
}
|
|
997
|
+
_ = ln.Close()
|
|
998
|
+
sc.note("stopped exposing tailnet :%d", body.Port)
|
|
999
|
+
writeJSON(w, 200, map[string]any{"ok": true, "port": body.Port})
|
|
1000
|
+
}
|
|
1001
|
+
|
|
761
1002
|
// ---------------------------------------------------------------------------
|
|
762
1003
|
// small helpers
|
|
763
1004
|
// ---------------------------------------------------------------------------
|