■ ■ ■ ■ ■ ■
internal/server/handlers/forwardserverport.go
| 1 | + | package handlers |
| 2 | + | |
| 3 | + | import ( |
| 4 | + | "errors" |
| 5 | + | "fmt" |
| 6 | + | "net" |
| 7 | + | "sync" |
| 8 | + | "time" |
| 9 | + | |
| 10 | + | "github.com/NHAS/reverse_ssh/internal" |
| 11 | + | "github.com/NHAS/reverse_ssh/internal/server/multiplexer" |
| 12 | + | "github.com/NHAS/reverse_ssh/pkg/logger" |
| 13 | + | "golang.org/x/crypto/ssh" |
| 14 | + | ) |
| 15 | + | |
| 16 | + | var ( |
| 17 | + | currentRemoteForwardsLck sync.RWMutex |
| 18 | + | currentRemoteForwards = map[string]string{} |
| 19 | + | remoteForward = map[string]ssh.Channel{} |
| 20 | + | ) |
| 21 | + | |
| 22 | + | type chanConn struct { |
| 23 | + | channel ssh.Channel |
| 24 | + | drtMsg internal.ChannelOpenDirectMsg |
| 25 | + | } |
| 26 | + | |
| 27 | + | func (c *chanConn) Read(b []byte) (n int, err error) { |
| 28 | + | return c.channel.Read(b) |
| 29 | + | } |
| 30 | + | |
| 31 | + | func (c *chanConn) Write(b []byte) (n int, err error) { |
| 32 | + | return c.channel.Write(b) |
| 33 | + | } |
| 34 | + | |
| 35 | + | func (c *chanConn) Close() error { |
| 36 | + | return c.channel.Close() |
| 37 | + | } |
| 38 | + | |
| 39 | + | func (c *chanConn) LocalAddr() net.Addr { |
| 40 | + | return &net.TCPAddr{ |
| 41 | + | IP: net.ParseIP(c.drtMsg.Laddr), |
| 42 | + | Port: int(c.drtMsg.Lport), |
| 43 | + | } |
| 44 | + | } |
| 45 | + | |
| 46 | + | func (c *chanConn) RemoteAddr() net.Addr { |
| 47 | + | return &net.TCPAddr{ |
| 48 | + | IP: net.ParseIP(c.drtMsg.Raddr), |
| 49 | + | Port: int(c.drtMsg.Rport), |
| 50 | + | } |
| 51 | + | } |
| 52 | + | |
| 53 | + | func (c *chanConn) SetDeadline(t time.Time) error { |
| 54 | + | return errors.New("not implemented on a channel") |
| 55 | + | } |
| 56 | + | |
| 57 | + | func (c *chanConn) SetReadDeadline(t time.Time) error { |
| 58 | + | return errors.New("not implemented on a channel") |
| 59 | + | |
| 60 | + | } |
| 61 | + | |
| 62 | + | func (c *chanConn) SetWriteDeadline(t time.Time) error { |
| 63 | + | return errors.New("not implemented on a channel") |
| 64 | + | |
| 65 | + | } |
| 66 | + | |
| 67 | + | func channelToConn(channel ssh.Channel, drtMsg internal.ChannelOpenDirectMsg) net.Conn { |
| 68 | + | return &chanConn{channel, drtMsg} |
| 69 | + | } |
| 70 | + | |
| 71 | + | func ServerPortForward(clientId string) func(_ *internal.User, newChannel ssh.NewChannel, log logger.Logger) { |
| 72 | + | return func(_ *internal.User, newChannel ssh.NewChannel, log logger.Logger) { |
| 73 | + | a := newChannel.ExtraData() |
| 74 | + | |
| 75 | + | var drtMsg internal.ChannelOpenDirectMsg |
| 76 | + | err := ssh.Unmarshal(a, &drtMsg) |
| 77 | + | if err != nil { |
| 78 | + | log.Warning("Unable to unmarshal proxy %s", err) |
| 79 | + | newChannel.Reject(ssh.ResourceShortage, "Unable to unmarshal proxy") |
| 80 | + | return |
| 81 | + | } |
| 82 | + | |
| 83 | + | connection, requests, err := newChannel.Accept() |
| 84 | + | if err != nil { |
| 85 | + | newChannel.Reject(ssh.ResourceShortage, "nope") |
| 86 | + | log.Warning("Unable to accept new channel %s", err) |
| 87 | + | return |
| 88 | + | } |
| 89 | + | defer connection.Close() |
| 90 | + | |
| 91 | + | go func() { |
| 92 | + | for req := range requests { |
| 93 | + | if req.WantReply { |
| 94 | + | req.Reply(false, nil) |
| 95 | + | } |
| 96 | + | } |
| 97 | + | |
| 98 | + | StopRemoteForward(clientId) |
| 99 | + | }() |
| 100 | + | |
| 101 | + | currentRemoteForwardsLck.Lock() |
| 102 | + | remoteForward[clientId] = connection |
| 103 | + | currentRemoteForwards[clientId] = fmt.Sprintf("%s:%d", drtMsg.Raddr, drtMsg.Rport) |
| 104 | + | currentRemoteForwardsLck.Unlock() |
| 105 | + | |
| 106 | + | multiplexer.ServerMultiplexer.QueueConn(channelToConn(connection, drtMsg)) |
| 107 | + | |
| 108 | + | } |
| 109 | + | } |
| 110 | + | |
| 111 | + | func GetRemoteForwards(clientId string) string { |
| 112 | + | currentRemoteForwardsLck.RLock() |
| 113 | + | defer currentRemoteForwardsLck.RUnlock() |
| 114 | + | |
| 115 | + | return currentRemoteForwards[clientId] |
| 116 | + | } |
| 117 | + | |
| 118 | + | func StopRemoteForward(clientId string) { |
| 119 | + | currentRemoteForwardsLck.Lock() |
| 120 | + | defer currentRemoteForwardsLck.Unlock() |
| 121 | + | |
| 122 | + | if remoteForward[clientId] != nil { |
| 123 | + | remoteForward[clientId].Close() |
| 124 | + | } |
| 125 | + | |
| 126 | + | delete(remoteForward, clientId) |
| 127 | + | delete(currentRemoteForwards, clientId) |
| 128 | + | } |
| 129 | + | |