-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubsub-handler.go
174 lines (159 loc) · 3.99 KB
/
pubsub-handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package tunkit
import (
"bytes"
"io"
"log/slog"
"net"
"strconv"
"sync"
"github.com/charmbracelet/ssh"
gossh "golang.org/x/crypto/ssh"
"golang.org/x/exp/maps"
)
type remoteForwardRequest struct {
BindAddr string
BindPort uint32
}
type remoteForwardSuccess struct {
BindPort uint32
}
type remoteForwardCancelRequest struct {
BindAddr string
BindPort uint32
}
type remoteForwardChannelData struct {
DestAddr string
DestPort uint32
OriginAddr string
OriginPort uint32
}
type RemoteForwards struct {
Listener net.Listener
Pubkey ssh.PublicKey
}
// PubSubHandler can be enabled by creating a PubSubHandler and
// adding the HandleSSHRequest callback to the server's RequestHandlers under
// tcpip-forward and cancel-tcpip-forward.
type PubSubHandler struct {
Logger *slog.Logger
sync.Mutex
forwards map[string]*RemoteForwards
}
func NewPubSubHandler(logger *slog.Logger) *PubSubHandler {
return &PubSubHandler{
Logger: logger,
}
}
var forwardedTCPChannelType = "forwarded-tcpip"
func (h *PubSubHandler) GetForwards() []*RemoteForwards {
return maps.Values(h.forwards)
}
func (h *PubSubHandler) GetLogger() *slog.Logger {
return h.Logger
}
func (h *PubSubHandler) GetForwardsByPubkey(pubkey ssh.PublicKey) []net.Listener {
list := []net.Listener{}
for _, v := range h.forwards {
if bytes.Equal(v.Pubkey.Marshal(), pubkey.Marshal()) {
list = append(list, v.Listener)
}
}
return list
}
func (h *PubSubHandler) HandleRequest(ctx ssh.Context, srv *ssh.Server, req *gossh.Request) (bool, []byte) {
logger := h.GetLogger()
h.Lock()
if h.forwards == nil {
h.forwards = make(map[string]*RemoteForwards)
}
h.Unlock()
conn := ctx.Value(ssh.ContextKeyConn).(*gossh.ServerConn)
switch req.Type {
case "tcpip-forward":
var reqPayload remoteForwardRequest
if err := gossh.Unmarshal(req.Payload, &reqPayload); err != nil {
logger.Error("failed to parse request payload", "err", err)
return false, []byte{}
}
addr := net.JoinHostPort(reqPayload.BindAddr, strconv.Itoa(int(reqPayload.BindPort)))
ln, err := net.Listen("tcp", addr)
if err != nil {
logger.Error("failed create net listener", "err", err)
return false, []byte{}
}
_, destPortStr, _ := net.SplitHostPort(ln.Addr().String())
destPort, _ := strconv.Atoi(destPortStr)
pubkey, _ := ctx.Value(ssh.ContextKeyPublicKey).(ssh.PublicKey)
remoteForward := RemoteForwards{
Listener: ln,
Pubkey: pubkey,
}
h.Lock()
h.forwards[addr] = &remoteForward
h.Unlock()
go func() {
<-ctx.Done()
h.Lock()
rf, ok := h.forwards[addr]
h.Unlock()
if ok {
rf.Listener.Close()
}
}()
go func() {
for {
c, err := ln.Accept()
if err != nil {
logger.Error("failed accept channel", "err", err)
break
}
originAddr, orignPortStr, _ := net.SplitHostPort(c.RemoteAddr().String())
originPort, _ := strconv.Atoi(orignPortStr)
payload := gossh.Marshal(&remoteForwardChannelData{
DestAddr: reqPayload.BindAddr,
DestPort: uint32(destPort),
OriginAddr: originAddr,
OriginPort: uint32(originPort),
})
go func() {
ch, reqs, err := conn.OpenChannel(forwardedTCPChannelType, payload)
if err != nil {
c.Close()
return
}
go gossh.DiscardRequests(reqs)
go func() {
defer ch.Close()
defer c.Close()
_, _ = io.Copy(ch, c)
}()
go func() {
defer ch.Close()
defer c.Close()
_, _ = io.Copy(c, ch)
}()
}()
}
h.Lock()
delete(h.forwards, addr)
h.Unlock()
}()
return true, gossh.Marshal(&remoteForwardSuccess{uint32(destPort)})
case "cancel-tcpip-forward":
var reqPayload remoteForwardCancelRequest
if err := gossh.Unmarshal(req.Payload, &reqPayload); err != nil {
logger.Error("failed parse payload", "err", err)
return false, []byte{}
}
addr := net.JoinHostPort(reqPayload.BindAddr, strconv.Itoa(int(reqPayload.BindPort)))
h.Lock()
rf, ok := h.forwards[addr]
h.Unlock()
if ok {
rf.Listener.Close()
}
return true, nil
default:
return false, nil
}
}