Skip to content

Commit a20f9ce

Browse files
committed
Bumped v0.5.7
Signed-off-by: Vishal Rana <[email protected]>
1 parent 9f002b8 commit a20f9ce

File tree

4 files changed

+16
-103
lines changed

4 files changed

+16
-103
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
IMAGE = labstack/tunnel
2-
VERSION = 0.5.6
2+
VERSION = 0.5.7
33

44
publish:
55
git tag v$(VERSION)

cmd/daemon.go

+1-12
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,12 @@ var daemonCmd = &cobra.Command{
7373
if args[0] == "start" {
7474
daemon.Start()
7575
} else if args[0] == "stop" {
76-
c, err := getClient()
77-
if err != nil {
78-
exit(err)
79-
}
80-
defer c.Close()
81-
req := new(daemon.StopDaemonRequest)
82-
rep := new(daemon.StopDaemonReply)
83-
err = c.Call("Server.StopDaemon", req, rep)
84-
if err != nil {
85-
exit(err)
86-
}
8776
defer os.Remove(viper.GetString("daemon_addr"))
8877
defer os.Remove(viper.GetString("daemon_pid"))
8978
d, _ := ioutil.ReadFile(viper.GetString("daemon_pid"))
9079
pid, _ := strconv.Atoi(string(d))
9180
p, _ := os.FindProcess(pid)
92-
p.Kill()
81+
p.Signal(os.Interrupt)
9382
}
9483
},
9584
}

daemon/connection.go

+12-56
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ import (
1919

2020
type (
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 {
@@ -43,6 +41,7 @@ type (
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

132131
func (c *Connection) connect() {
133132
RECONNECT:
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-
}

daemon/server.go

+2-34
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/fsnotify/fsnotify"
77
"github.com/labstack/gommon/log"
88
"github.com/spf13/viper"
9-
"golang.org/x/sync/errgroup"
109
"gopkg.in/resty.v1"
1110
"io/ioutil"
1211
"net"
@@ -47,12 +46,6 @@ type (
4746

4847
RMReply struct {
4948
}
50-
51-
StopDaemonRequest struct {
52-
}
53-
54-
StopDaemonReply struct {
55-
}
5649
)
5750

5851
const (
@@ -68,7 +61,7 @@ func (s *Server) Connect(req *ConnectRequest, rep *ConnectReply) (err error) {
6861
}
6962
go c.connect()
7063
<-c.startChan
71-
return
64+
return s.findConnection(c)
7265
}
7366

7467
func (s *Server) PS(req *PSRequest, rep *PSReply) (err error) {
@@ -81,23 +74,10 @@ func (s *Server) PS(req *PSRequest, rep *PSReply) (err error) {
8174
func (s *Server) RM(req *RMRequest, rep *RMReply) error {
8275
if c, ok := s.Connections[req.Name]; ok {
8376
c.stop()
84-
return c.delete()
8577
}
8678
return nil
8779
}
8880

89-
func (s *Server) stopConnections() error {
90-
g := new(errgroup.Group)
91-
for _, c := range s.Connections {
92-
c := c
93-
g.Go(func() error {
94-
c.stop()
95-
return c.delete()
96-
})
97-
}
98-
return g.Wait()
99-
}
100-
10181
func Start() {
10282
log.Info("starting daemon")
10383
r := resty.New()
@@ -119,16 +99,9 @@ func Start() {
11999
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
120100
go func() {
121101
<-c
122-
if err := s.stopConnections(); err != nil {
123-
log.Error("failed stopping connections: %v", err)
124-
}
102+
log.Warn("stopping daemon")
125103
}()
126104

127-
// Cleanup
128-
// if err := s.deleteAll(); err != nil {
129-
// log.Error(err)
130-
// }
131-
132105
// Listen
133106
l, e := net.Listen("tcp", "127.0.0.1:0")
134107
if e != nil {
@@ -142,11 +115,6 @@ func Start() {
142115
rpc.Accept(l)
143116
}
144117

145-
func (s *Server) StopDaemon(req *StopDaemonRequest, rep *StopDaemonReply) (err error) {
146-
log.Warn("stopping daemon")
147-
return s.stopConnections()
148-
}
149-
150118
func (s *Server) findConnection(c *Connection) (err error) {
151119
e := new(Error)
152120
res, err := s.resty.R().

0 commit comments

Comments
 (0)