@@ -19,13 +19,11 @@ import (
1919
2020type (
2121 Header struct {
22- ID string `json:"id"`
23- Key string `json:"key"`
24- Name string `json:"name"`
25- Target string `json:"target"`
26- TLS bool `json:"tls"`
27- Started bool `json:"started"`
28- Reconnect bool `json:"reconnect"`
22+ ID string `json:"id"`
23+ Key string `json:"key"`
24+ Name string `json:"name"`
25+ Target string `json:"target"`
26+ TLS bool `json:"tls"`
2927 }
3028
3129 Configuration struct {
4341 acceptChan chan net.Conn
4442 reconnectChan chan error
4543 stopChan chan bool
44+ started bool
4645 retries time.Duration
4746 Header * Header
4847 ID string `json:"id"`
@@ -131,20 +130,13 @@ func (s *Server) newConnection(req *ConnectRequest) (c *Connection, err error) {
131130
132131func (c * Connection ) connect () {
133132RECONNECT:
134- if c .Header . Reconnect {
133+ if c .Status == ConnectionStatusReconnecting {
135134 c .retries ++
136135 if c .retries > 5 {
137136 log .Errorf ("failed to reconnect connection: %s" , c .Name )
138- if err := c .delete (); err != nil {
139- log .Error (err )
140- }
141137 return
142138 }
143139 time .Sleep (c .retries * c .retries * time .Second )
144- c .Status = ConnectionStatusReconnecting
145- if err := c .update (); err != nil {
146- log .Error (err )
147- }
148140 log .Warnf ("reconnecting connection: name=%s, retry=%d" , c .Name , c .retries )
149141 }
150142 hostKey , _ , _ , _ , err := ssh .ParseAuthorizedKey (hostBytes )
@@ -200,7 +192,7 @@ RECONNECT:
200192 }
201193 if err != nil {
202194 log .Error (err )
203- c .Header . Reconnect = true
195+ c .Status = ConnectionStatusReconnecting
204196 goto RECONNECT
205197 }
206198
@@ -210,17 +202,13 @@ RECONNECT:
210202 log .Errorf ("failed to listen on remote host: %v" , err )
211203 return
212204 }
213- if err := c .server .findConnection (c ); err != nil {
214- log .Error (err )
215- return
216- }
217- c .Header .Reconnect = false
205+ // Note: Don't close the listener as it prevents closing the underlying connection
218206 c .retries = 0
219- if ! c .Header . Started {
220- c .Header . Started = true
207+ if ! c .started {
208+ c .started = true
221209 c .startChan <- true
222210 }
223- // Note: Don't close the listener as it prevents closing the underlying connection
211+ log . Infof ( "connection %s is online" , c . Name )
224212
225213 // Close
226214 defer func () {
@@ -285,35 +273,3 @@ func (c *Connection) stop() {
285273 log .Warnf ("stopping connection: %s" , c .Name )
286274 c .stopChan <- true
287275}
288-
289- func (c * Connection ) update () error {
290- e := new (Error )
291- res , err := c .server .resty .R ().
292- SetBody (c ).
293- SetResult (c ).
294- SetError (e ).
295- Put ("/connections/" + c .ID )
296- if err != nil {
297- return err
298- }
299- if res .IsError () {
300- return fmt .Errorf ("failed to update the connection: name=%s, error=%s" , c .Name , e .Message )
301- }
302- return nil
303- }
304-
305- func (c * Connection ) delete () (err error ) {
306- log .Warnf ("removing connection: %s" , c .Name )
307- e := new (Error )
308- res , err := c .server .resty .R ().
309- SetError (e ).
310- Delete ("/connections/" + c .ID )
311- if err != nil {
312- return
313- }
314- if res .IsError () {
315- return fmt .Errorf ("failed to delete the connection: %s" , e .Message )
316- }
317- delete (c .server .Connections , c .Name )
318- return
319- }
0 commit comments