Skip to content

Commit afb623e

Browse files
committed
WIP
1 parent 1b6d675 commit afb623e

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

pipe.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package main
2+
3+
import (
4+
"net"
5+
6+
"golang.org/x/crypto/ssh"
7+
)
8+
9+
type Downstream struct {
10+
*ssh.ServerConn
11+
Channels <-chan ssh.NewChannel
12+
}
13+
14+
type Upstream struct {
15+
ssh.Conn
16+
extensions map[string][]byte
17+
}
18+
19+
type PipeSession struct {
20+
Downstream *Downstream
21+
Upstream *Upstream
22+
}
23+
24+
func NewPipeSession(c net.Conn, config *ssh.ServerConfig) (session *PipeSession, err error) {
25+
conn, chans, reqs, err := ssh.NewServerConn(c, config)
26+
if err != nil {
27+
return nil, err
28+
}
29+
go ssh.DiscardRequests(reqs)
30+
defer func() {
31+
if err != nil {
32+
conn.Close()
33+
}
34+
}()
35+
session = &PipeSession{
36+
Downstream: &Downstream{conn, chans},
37+
}
38+
return session, nil
39+
}
40+
41+
func (s *PipeSession) InitUpstream(c net.Conn, addr string, config *ssh.ClientConfig) error {
42+
conn, _, _, err := ssh.NewClientConn(nil, addr, config)
43+
if err != nil {
44+
return err
45+
}
46+
s.Upstream = &Upstream{conn, nil}
47+
return nil
48+
}
49+
50+
func (s *PipeSession) Close() {
51+
if s.Downstream != nil {
52+
s.Downstream.Close()
53+
}
54+
if s.Upstream != nil {
55+
s.Upstream.Close()
56+
}
57+
}

0 commit comments

Comments
 (0)