@@ -34,35 +34,50 @@ impl SshToHttpPortForwardPool {
34
34
}
35
35
}
36
36
37
- let listen_port = next_port ( ) ;
37
+ loop {
38
+ let listen_port = next_port ( ) ;
38
39
39
- println ! (
40
- "Allocating listen port: {} for http port forward {}->{}:{}" ,
41
- listen_port,
42
- ssh_credentials. to_string( ) ,
43
- remote_host,
44
- remote_port
45
- ) ;
40
+ println ! (
41
+ "Allocating listen port: {} for http port forward {}->{}:{}" ,
42
+ listen_port,
43
+ ssh_credentials. to_string( ) ,
44
+ remote_host,
45
+ remote_port
46
+ ) ;
46
47
47
- let listen_host_port = format ! ( "127.0.0.1:{}" , listen_port) ;
48
+ let listen_host_port = format ! ( "127.0.0.1:{}" , listen_port) ;
48
49
49
- let ssh_session = SshSession :: new ( ssh_credentials. clone ( ) ) ;
50
+ let ssh_session = SshSession :: new ( ssh_credentials. clone ( ) ) ;
50
51
51
- let result = ssh_session
52
- . start_port_forward ( listen_host_port, remote_host. to_string ( ) , remote_port)
53
- . await
54
- . unwrap ( ) ;
52
+ let result = ssh_session
53
+ . start_port_forward ( listen_host_port, remote_host. to_string ( ) , remote_port)
54
+ . await ;
55
55
56
- let configuration = SshToHttpPortForwardConfiguration {
57
- listen_port,
58
- ssh_credentials : ssh_credentials. clone ( ) ,
59
- tunnel : result. clone ( ) ,
60
- } ;
56
+ if let Err ( err) = result {
57
+ match err {
58
+ my_ssh:: RemotePortForwardError :: CanNotExtractListenPort ( _) => {
59
+ panic ! ( "Can not start port forward: {:?}" , err) ;
60
+ }
61
+ my_ssh:: RemotePortForwardError :: CanNotBindListenEndpoint ( _) => {
62
+ println ! ( "Can not bind listen port: {}" , listen_port) ;
63
+ continue ;
64
+ }
65
+ }
66
+ }
67
+
68
+ let result = result. unwrap ( ) ;
61
69
62
- let configuration = Arc :: new ( configuration) ;
70
+ let configuration = SshToHttpPortForwardConfiguration {
71
+ listen_port,
72
+ ssh_credentials : ssh_credentials. clone ( ) ,
73
+ tunnel : result. clone ( ) ,
74
+ } ;
63
75
64
- access . push ( configuration. clone ( ) ) ;
76
+ let configuration = Arc :: new ( configuration ) ;
65
77
66
- configuration
78
+ access. push ( configuration. clone ( ) ) ;
79
+
80
+ return configuration;
81
+ }
67
82
}
68
83
}
0 commit comments