转发指定SSH服务器的多个端口 到本地(Localhost)
(clientConn *ssh.Client, mapping map[string]string)
| 283 | |
| 284 | // 转发指定SSH服务器的多个端口 到本地(Localhost) |
| 285 | func TunnelMultiple(clientConn *ssh.Client, mapping map[string]string) error { |
| 286 | |
| 287 | if len(mapping) <= 0 { |
| 288 | return nil |
| 289 | } |
| 290 | |
| 291 | pipe := func(writer, reader net.Conn) { |
| 292 | defer writer.Close() |
| 293 | defer reader.Close() |
| 294 | |
| 295 | _, err := io.Copy(writer, reader) |
| 296 | if err != nil { |
| 297 | common.SmartIDELog.Warning(err.Error()) |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | //TODO: 这里可能会有问题 |
| 302 | for local, remote := range mapping { |
| 303 | |
| 304 | go func(local, remote string) { |
| 305 | |
| 306 | listener, err := net.Listen("tcp", local) |
| 307 | if err != nil { |
| 308 | common.SmartIDELog.Warning("failed to dial to remote: ", err.Error()) |
| 309 | } |
| 310 | |
| 311 | for { |
| 312 | here, err := listener.Accept() |
| 313 | if err != nil { |
| 314 | common.SmartIDELog.Warning("failed to dial to remote: ", err.Error()) |
| 315 | time.Sleep(time.Second * 1) |
| 316 | continue |
| 317 | } |
| 318 | if here == nil { |
| 319 | common.SmartIDELog.Importance("本地连接失败" + local) |
| 320 | time.Sleep(time.Second * 1) |
| 321 | continue |
| 322 | } |
| 323 | go func(here net.Conn) { |
| 324 | there, err := clientConn.Dial("tcp", remote) |
| 325 | if there == nil { |
| 326 | common.SmartIDELog.Importance("ssh 连接失败,请确保远程主机上相应端口已打开 " + remote) |
| 327 | time.Sleep(time.Second * 1) |
| 328 | return |
| 329 | } |
| 330 | if err != nil { |
| 331 | common.SmartIDELog.Warning(err.Error()) |
| 332 | } |
| 333 | go pipe(there, here) |
| 334 | go pipe(here, there) |
| 335 | }(here) |
| 336 | } |
| 337 | |
| 338 | }(local, remote) |
| 339 | |
| 340 | } |
| 341 | |
| 342 | return nil |
no test coverage detected