@@ -59,16 +59,18 @@ func (t *Tunnel) keepConnected(ctx context.Context) {
5959 return
6060 default :
6161 t .gwConn , err = connect (t .address , t .serverName , t .token , t .config )
62+ if err == nil {
63+ err = proxy (ctx , t .gwConn )
64+ _ = t .gwConn .Close ()
65+ }
6266 if err != nil {
63- d := b .Duration ()
6467 klog .Errorln (err )
65- klog .Errorf ("reconnecting to %s in %.0fs" , t .address , d .Seconds ())
68+ d := b .Duration ()
69+ klog .Infof ("reconnecting to %s in %.0fs" , t .address , d .Seconds ())
6670 time .Sleep (d )
6771 continue
6872 }
6973 b .Reset ()
70- proxy (ctx , t .gwConn )
71- _ = t .gwConn .Close ()
7274 }
7375 }
7476}
@@ -220,25 +222,23 @@ func connect(gwAddr, serverName, token string, config []byte) (net.Conn, error)
220222 return gwConn , nil
221223}
222224
223- func proxy (ctx context.Context , gwConn net.Conn ) {
225+ func proxy (ctx context.Context , gwConn net.Conn ) error {
224226 cfg := yamux .DefaultConfig ()
225227 cfg .KeepAliveInterval = time .Second
226228 cfg .LogOutput = io .Discard
227229 session , err := yamux .Server (gwConn , cfg )
228230 if err != nil {
229- klog .Errorln ("failed to start a TCP multiplexing server:" , err )
230- return
231+ return fmt .Errorf ("failed to start a TCP multiplexing server: %s" , err )
231232 }
232233 defer session .Close ()
233234 for {
234235 select {
235236 case <- ctx .Done ():
236- return
237+ return nil
237238 default :
238239 gwStream , err := session .Accept ()
239240 if err != nil {
240- klog .Errorf ("failed to accept a stream: %s" , err )
241- return
241+ return fmt .Errorf ("failed to accept a stream: %s" , err )
242242 }
243243 go func (c net.Conn ) {
244244 defer c .Close ()
0 commit comments