Projects STRLCPY reverse_ssh Commits 385a997b
🤬
  • Improve error output on malformed input, improve overall output of listen command closes #118

  • Loading...
  • NHAS committed 11 months ago
    385a997b
    1 parent 6d547cde
Revision indexing in progress... (symbol navigation in revisions will be accurate after indexed)
  • ■ ■ ■ ■ ■ ■
    internal/server/commands/listen.go
    skipped 29 lines
    30 30   log logger.Logger
    31 31  }
    32 32   
    33  -func (l *listen) server(tty io.ReadWriter, line terminal.ParsedLine) error {
     33 +func (l *listen) server(tty io.ReadWriter, line terminal.ParsedLine, onAddrs, offAddrs []string) error {
    34 34   if line.IsSet("l") {
    35 35   listeners := multiplexer.ServerMultiplexer.GetListeners()
    36 36   
    skipped 8 lines
    45 45   return nil
    46 46   }
    47 47   
    48  - on := line.IsSet("on")
    49  - off := line.IsSet("off")
    50  - 
    51  - if on {
    52  - addrs, err := line.GetArgsString("on")
     48 + for _, addr := range onAddrs {
     49 + err := multiplexer.ServerMultiplexer.StartListener("tcp", addr)
    53 50   if err != nil {
    54 51   return err
    55 52   }
    56  - 
    57  - for _, addr := range addrs {
    58  - err := multiplexer.ServerMultiplexer.StartListener("tcp", addr)
    59  - if err != nil {
    60  - return err
    61  - }
    62  - fmt.Fprintln(tty, "started listening on: ", addr)
    63  - }
     53 + fmt.Fprintln(tty, "started listening on: ", addr)
    64 54   }
    65 55   
    66  - if off {
    67  - addrs, err := line.GetArgsString("off")
     56 + for _, addr := range offAddrs {
     57 + err := multiplexer.ServerMultiplexer.StopListener(addr)
    68 58   if err != nil {
    69 59   return err
    70 60   }
    71  - 
    72  - for _, addr := range addrs {
    73  - err := multiplexer.ServerMultiplexer.StopListener(addr)
    74  - if err != nil {
    75  - return err
    76  - }
    77  - fmt.Fprintln(tty, "stopped listening on: ", addr)
    78  - }
     61 + fmt.Fprintln(tty, "stopped listening on: ", addr)
    79 62   }
    80 63   
    81 64   return nil
    82 65  }
    83 66   
    84  -func (l *listen) client(tty io.ReadWriter, line terminal.ParsedLine) error {
     67 +func (l *listen) client(tty io.ReadWriter, line terminal.ParsedLine, onAddrs, offAddrs []string) error {
    85 68   
    86 69   auto := line.IsSet("auto")
    87 70   if line.IsSet("l") && auto {
    skipped 46 lines
    134 117   return nil
    135 118   }
    136 119   
    137  - on := line.IsSet("on")
    138  - off := line.IsSet("off")
     120 + var fwRequests []internal.RemoteForwardRequest
    139 121   
    140  - if on {
    141  - var fwRequests []internal.RemoteForwardRequest
     122 + for _, addr := range onAddrs {
     123 + ip, port, err := net.SplitHostPort(addr)
     124 + if err != nil {
     125 + return err
     126 + }
    142 127   
    143  - addrs, err := line.GetArgsString("on")
     128 + p, err := strconv.Atoi(port)
    144 129   if err != nil {
    145 130   return err
    146 131   }
    147 132   
    148  - for _, addr := range addrs {
    149  - ip, port, err := net.SplitHostPort(addr)
    150  - if err != nil {
    151  - return err
     133 + fwRequests = append(fwRequests, internal.RemoteForwardRequest{
     134 + BindPort: uint32(p),
     135 + BindAddr: ip,
     136 + })
     137 + 
     138 + }
     139 + 
     140 + for _, r := range fwRequests {
     141 + 
     142 + b := ssh.Marshal(&r)
     143 + 
     144 + applied := len(foundClients)
     145 + for c, sc := range foundClients {
     146 + result, message, err := sc.SendRequest("tcpip-forward", true, b)
     147 + if !result {
     148 + applied--
     149 + fmt.Fprintln(tty, "failed to start port on (client may not support it): ", c, ": ", string(message))
     150 + continue
    152 151   }
    153 152   
    154  - p, err := strconv.Atoi(port)
    155 153   if err != nil {
    156  - return err
     154 + applied--
     155 + fmt.Fprintln(tty, "error starting port on: ", c, ": ", err)
    157 156   }
     157 + }
    158 158   
    159  - fwRequests = append(fwRequests, internal.RemoteForwardRequest{
    160  - BindPort: uint32(p),
    161  - BindAddr: ip,
    162  - })
     159 + fmt.Fprintf(tty, "started %s:%d on %d clients (total %d)\n", r.BindAddr, r.BindPort, applied, len(foundClients))
    163 160   
    164  - }
     161 + if auto {
     162 + var entry autostartEntry
     163 + 
     164 + entry.ObserverID = observers.ConnectionState.Register(func(m observer.Message) {
     165 + c := m.(observers.ClientState)
    165 166   
    166  - for _, r := range fwRequests {
    167  - b := ssh.Marshal(&r)
    168  - for c, sc := range foundClients {
    169  - result, message, err := sc.SendRequest("tcpip-forward", true, b)
    170  - if !result {
    171  - fmt.Fprintln(tty, "failed to start port on (client may not support it): ", c, ": ", string(message))
    172  - continue
     167 + if !clients.Matches(specifier, c.ID, c.IP) || c.Status == "disconnected" {
     168 + return
    173 169   }
    174 170   
     171 + client, err := clients.Get(c.ID)
    175 172   if err != nil {
    176  - fmt.Fprintln(tty, "error starting port on: ", c, ": ", err)
     173 + return
    177 174   }
    178  - }
    179 175   
    180  - if auto {
    181  - var entry autostartEntry
    182  - 
    183  - entry.ObserverID = observers.ConnectionState.Register(func(m observer.Message) {
    184  - c := m.(observers.ClientState)
    185  - 
    186  - if !clients.Matches(specifier, c.ID, c.IP) || c.Status == "disconnected" {
    187  - return
    188  - }
    189  - 
    190  - client, err := clients.Get(c.ID)
    191  - if err != nil {
    192  - return
    193  - }
    194  - 
    195  - result, message, err := client.SendRequest("tcpip-forward", true, b)
    196  - if !result {
    197  - l.log.Warning("failed to start server tcpip-forward on client: %s: %s", c.ID, message)
    198  - return
    199  - }
     176 + result, message, err := client.SendRequest("tcpip-forward", true, b)
     177 + if !result {
     178 + l.log.Warning("failed to start server tcpip-forward on client: %s: %s", c.ID, message)
     179 + return
     180 + }
    200 181   
    201  - if err != nil {
    202  - l.log.Warning("error auto starting port on: %s: %s", c.ID, err)
    203  - return
    204  - }
     182 + if err != nil {
     183 + l.log.Warning("error auto starting port on: %s: %s", c.ID, err)
     184 + return
     185 + }
    205 186   
    206  - })
     187 + })
    207 188   
    208  - entry.Criteria = specifier
     189 + entry.Criteria = specifier
    209 190   
    210  - autoStartServerPort[r] = entry
     191 + autoStartServerPort[r] = entry
    211 192   
    212  - }
    213 193   }
    214  - 
    215 194   }
    216 195   
    217  - if off {
    218  - var cancelFwRequests []internal.RemoteForwardRequest
     196 + var cancelFwRequests []internal.RemoteForwardRequest
    219 197   
    220  - addrs, err := line.GetArgsString("off")
     198 + for _, addr := range offAddrs {
     199 + ip, port, err := net.SplitHostPort(addr)
    221 200   if err != nil {
    222 201   return err
    223 202   }
    224 203   
    225  - for _, addr := range addrs {
    226  - ip, port, err := net.SplitHostPort(addr)
    227  - if err != nil {
    228  - return err
    229  - }
     204 + p, err := strconv.Atoi(port)
     205 + if err != nil {
     206 + return err
     207 + }
    230 208   
    231  - p, err := strconv.Atoi(port)
    232  - if err != nil {
    233  - return err
    234  - }
     209 + cancelFwRequests = append(cancelFwRequests, internal.RemoteForwardRequest{
     210 + BindPort: uint32(p),
     211 + BindAddr: ip,
     212 + })
    235 213   
    236  - cancelFwRequests = append(cancelFwRequests, internal.RemoteForwardRequest{
    237  - BindPort: uint32(p),
    238  - BindAddr: ip,
    239  - })
     214 + }
    240 215   
    241  - }
     216 + for _, r := range cancelFwRequests {
     217 + applied := len(foundClients)
    242 218   
    243  - for _, r := range cancelFwRequests {
    244  - b := ssh.Marshal(&r)
    245  - for c, sc := range foundClients {
    246  - result, message, err := sc.SendRequest("cancel-tcpip-forward", true, b)
    247  - if !result {
    248  - fmt.Fprintln(tty, "failed to stop port on: ", c, ": ", string(message))
    249  - continue
    250  - }
     219 + b := ssh.Marshal(&r)
     220 + for c, sc := range foundClients {
     221 + result, message, err := sc.SendRequest("cancel-tcpip-forward", true, b)
     222 + if !result {
     223 + applied--
     224 + fmt.Fprintln(tty, "failed to stop port on: ", c, ": ", string(message))
     225 + continue
     226 + }
    251 227   
    252  - if err != nil {
    253  - fmt.Fprintln(tty, "error stop port on: ", c, ": ", err)
    254  - }
     228 + if err != nil {
     229 + applied--
     230 + fmt.Fprintln(tty, "error stop port on: ", c, ": ", err)
    255 231   }
     232 + }
    256 233   
    257  - if auto {
    258  - if _, ok := autoStartServerPort[r]; ok {
    259  - observers.ConnectionState.Deregister(autoStartServerPort[r].Criteria)
    260  - }
    261  - delete(autoStartServerPort, r)
     234 + fmt.Fprintf(tty, "stopped %s:%d on %d clients\n", r.BindAddr, r.BindPort, applied)
     235 + 
     236 + if auto {
     237 + if _, ok := autoStartServerPort[r]; ok {
     238 + observers.ConnectionState.Deregister(autoStartServerPort[r].Criteria)
    262 239   }
     240 + delete(autoStartServerPort, r)
    263 241   }
    264 242   }
    265 243   
    skipped 6 lines
    272 250   return nil
    273 251   }
    274 252   
     253 + onAddrs, err := line.GetArgsString("on")
     254 + if err != nil && err != terminal.ErrFlagNotSet {
     255 + return err
     256 + }
     257 + 
     258 + if len(onAddrs) == 0 && err != terminal.ErrFlagNotSet {
     259 + return errors.New("no value specified for --on, requires port e.g --on :4343")
     260 + }
     261 + 
     262 + offAddrs, err := line.GetArgsString("off")
     263 + if err != nil && err != terminal.ErrFlagNotSet {
     264 + return err
     265 + }
     266 + 
     267 + if len(offAddrs) == 0 && err != terminal.ErrFlagNotSet {
     268 + return errors.New("no value specified for --off, requires port e.g --off :4343")
     269 + }
     270 + 
     271 + if onAddrs == nil && offAddrs == nil && !line.IsSet("l") {
     272 + return errors.New("no actionable argument supplied, please add --on, --off or -l (list)")
     273 + }
     274 + 
    275 275   if line.IsSet("server") || line.IsSet("s") {
    276  - return w.server(tty, line)
     276 + return w.server(tty, line, onAddrs, offAddrs)
    277 277   } else if line.IsSet("client") || line.IsSet("c") || line.IsSet("auto") {
    278  - return w.client(tty, line)
     278 + return w.client(tty, line, onAddrs, offAddrs)
    279 279   }
    280 280   
    281 281   return errors.New("neither server or client were specified, please choose one")
    skipped 38 lines
  • ■ ■ ■ ■ ■
    internal/users.go
    skipped 15 lines
    16 16   
    17 17  type User struct {
    18 18   sync.RWMutex
     19 +
    19 20   // This is the users connection to the server itself, creates new channels and whatnot. NOT to get io.Copy'd
    20 21   ServerConnection ssh.Conn
    21 22   
    skipped 56 lines
Please wait...
Page is in error, reload to recover