2626 RemotePort int
2727 TargetHost string
2828 TargetPort int
29+ Channel chan int
2930 HideBanner bool
3031 }
3132
@@ -35,14 +36,19 @@ type (
3536 }
3637)
3738
39+ const (
40+ _ = iota
41+ EventReconnect
42+ )
43+
3844var (
3945 hostBytes = []byte ("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDoSLknvlFrFzroOlh1cqvcIFelHO+Wvj1UZ/p3J9bgsJGiKfh3DmBqEw1DOEwpHJz4zuV375TyjGuHuGZ4I4xztnwauhFplfEvriVHQkIDs6UnGwJVr15XUQX04r0i6mLbJs5KqIZTZuZ9ZGOj7ZWnaA7C07nPHGrERKV2Fm67rPvT6/qFikdWUbCt7KshbzdwwfxUohmv+NI7vw2X6vPU8pDaNEY7vS3YgwD/WlvQx+WDF2+iwLVW8OWWjFuQso6Eg1BSLygfPNhAHoiOWjDkijc8U9LYkUn7qsDCnvJxCoTTNmdECukeHfzrUjTSw72KZoM5KCRV78Wrctai1Qn6yRQz9BOSguxewLfzHtnT43/MLdwFXirJ/Ajquve2NAtYmyGCq5HcvpDAyi7lQ0nFBnrWv5zU3YxrISIpjovVyJjfPx8SCRlYZwVeUq6N2yAxCzJxbElZPtaTSoXBIFtoas2NXnCWPgenBa/2bbLQqfgbN8VQ9RaUISKNuYDIn4+eO72+RxF9THzZeV17pnhTVK88XU4asHot1gXwAt4vEhSjdUBC9KUIkfukI6F4JFxtvuO96octRahdV1Qg0vF+D0+SPy2HxqjgZWgPE2Xh/NmuIXwbE0wkymR2wrgj8Hd4C92keo2NBRh9dD7D2negnVYaYsC+3k/si5HNuCHnHQ== tunnel@labstack.com" )
4046)
4147
4248func Create (c * Config ) {
4349 hostKey , _ , _ , _ , err := ssh .ParseAuthorizedKey (hostBytes )
4450 if err != nil {
45- log .Fatalf ("Failed to parse host key: %v" , err )
51+ log .Fatalf ("failed to parse host key: %v" , err )
4652 }
4753 config := & ssh.ClientConfig {
4854 User : c .User ,
@@ -64,11 +70,11 @@ func Create(c *Config) {
6470 if proxy != "" {
6571 proxyURL , err := url .Parse (proxy )
6672 if err != nil {
67- log .Fatalf ("Cannot open new session: %v" , err )
73+ log .Fatalf ("cannot open new session: %v" , err )
6874 }
6975 tcp , err := net .Dial ("tcp" , proxyURL .Hostname ())
7076 if err != nil {
71- log .Fatalf ("Cannot open new session: %v" , err )
77+ log .Fatalf ("cannot open new session: %v" , err )
7278 }
7379 connReq := & http.Request {
7480 Method : "CONNECT" ,
@@ -84,27 +90,29 @@ func Create(c *Config) {
8490 connReq .Write (tcp )
8591 resp , err := http .ReadResponse (bufio .NewReader (tcp ), connReq )
8692 if err != nil {
87- log .Fatalf ("Cannot open new session: %v" , err )
93+ log .Fatalf ("cannot open new session: %v" , err )
8894 }
8995 defer resp .Body .Close ()
9096
9197 conn , chans , reqs , err := ssh .NewClientConn (tcp , c .Host , config )
9298 if err != nil {
93- log .Fatalf ("Cannot open new session: %v" , err )
99+ log .Fatalf ("cannot open new session: %v" , err )
94100 }
95101 client = ssh .NewClient (conn , chans , reqs )
96102 } else {
97103 client , err = ssh .Dial ("tcp" , c .Host , config )
98104 }
99105 if err != nil {
100- log .Fatalf ("Failed to connect: %v" , err )
106+ log .Errorf ("failed to connect: %v" , err )
107+ c .Channel <- EventReconnect
108+ return
101109 }
102110 defer client .Close ()
103111
104112 // Session
105113 sess , err := client .NewSession ()
106114 if err != nil {
107- log .Fatalf ("Failed to create session: %v" , err )
115+ log .Fatalf ("failed to create session: %v" , err )
108116 }
109117 defer sess .Close ()
110118 r , err := sess .StdoutPipe ()
@@ -116,7 +124,12 @@ func Create(c *Config) {
116124 for {
117125 line , _ , err := br .ReadLine ()
118126 if err != nil {
119- log .Fatalf ("Failed to read: %v" , err )
127+ if err == io .EOF {
128+ c .Channel <- EventReconnect
129+ return
130+ } else {
131+ log .Fatalf ("failed to read: %v" , err )
132+ }
120133 }
121134 fmt .Printf ("%s\n " , line )
122135 }
@@ -125,15 +138,15 @@ func Create(c *Config) {
125138 // Remote listener
126139 ln , err := client .Listen ("tcp" , fmt .Sprintf ("%s:%d" , c .RemoteHost , c .RemotePort ))
127140 if err != nil {
128- log .Fatalf ("Failed to listen on remote host: %v" , err )
141+ log .Fatalf ("failed to listen on remote host: %v" , err )
129142 }
130143 defer ln .Close ()
131144
132145 for {
133146 // Handle inbound connection
134147 in , err := ln .Accept ()
135148 if err != nil {
136- log .Printf ("Failed to accept connection: %v" , err )
149+ log .Printf ("failed to accept connection: %v" , err )
137150 return
138151 }
139152
@@ -143,7 +156,7 @@ func Create(c *Config) {
143156 // Target connection
144157 out , err := net .Dial ("tcp" , fmt .Sprintf ("%s:%d" , c .TargetHost , c .TargetPort ))
145158 if err != nil {
146- log .Printf ("Failed to connect to target: %v" , err )
159+ log .Printf ("failed to connect to target: %v" , err )
147160 return
148161 }
149162 defer out .Close ()
@@ -160,7 +173,7 @@ func Create(c *Config) {
160173 // Handle error
161174 err = <- errCh
162175 if err != nil && err != io .EOF {
163- log .Printf ("Failed to copy: %v" , err )
176+ log .Printf ("failed to copy: %v" , err )
164177 }
165178 }(in )
166179 }
0 commit comments