Skip to content

Commit e508a99

Browse files
committed
Bumped v0.5.3
Signed-off-by: Vishal Rana <[email protected]>
1 parent 5cca647 commit e508a99

File tree

11 files changed

+254
-314
lines changed

11 files changed

+254
-314
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
IMAGE = labstack/tunnel
2-
VERSION = 0.5.2
2+
VERSION = 0.5.3
33

44
publish:
55
git tag v$(VERSION)

cmd/daemon.go

Lines changed: 81 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,97 @@
11
package cmd
22

33
import (
4-
"errors"
5-
"github.com/labstack/tunnel-client/daemon"
6-
"github.com/mitchellh/go-ps"
7-
"io/ioutil"
8-
"os"
9-
"os/exec"
10-
"strconv"
11-
"time"
12-
13-
"github.com/spf13/cobra"
14-
"github.com/spf13/viper"
4+
"errors"
5+
"github.com/labstack/tunnel-client/daemon"
6+
"github.com/radovskyb/watcher"
7+
"github.com/spf13/cobra"
8+
"github.com/spf13/viper"
9+
"io"
10+
"io/ioutil"
11+
"os"
12+
"os/exec"
13+
"strconv"
14+
"time"
1515
)
1616

1717
func checkKey() {
1818
}
1919

2020
func startDaemon() {
21-
if viper.GetString("api_key") == "" {
22-
exit("To use tunnel you need an api key (https://tunnel.labstack.com) in $HOME/.tunnel/config.yaml")
23-
}
24-
start := true
25-
d, err := ioutil.ReadFile(viper.GetString("daemon_pid"))
26-
if err == nil {
27-
pid, _ := strconv.Atoi(string(d))
28-
if p, _ := ps.FindProcess(pid); p != nil {
29-
start = false
30-
}
31-
}
32-
if start {
33-
e, err := os.Executable()
34-
if err != nil {
35-
exit(err)
36-
}
37-
c := exec.Command(e, "daemon", "start")
38-
c.SysProcAttr = sysProcAttr
39-
f, err := os.OpenFile(viper.GetString("log_file"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
40-
if err != nil {
41-
exit(err)
42-
}
43-
c.Stdout = f
44-
c.Stderr = f
45-
if err := c.Start(); err != nil {
46-
exit(err)
47-
}
48-
if err := ioutil.WriteFile(viper.GetString("daemon_pid"), []byte(strconv.Itoa(c.Process.Pid)), 0644); err != nil {
49-
exit(err)
50-
}
51-
time.Sleep(time.Second) // Let the daemon start
52-
}
21+
if viper.GetString("api_key") == "" {
22+
exit("To use tunnel you need an api key (https://tunnel.labstack.com) in $HOME/.tunnel/config.yaml")
23+
}
24+
start := true
25+
d, err := ioutil.ReadFile(viper.GetString("daemon_pid"))
26+
if err == nil {
27+
pid, _ := strconv.Atoi(string(d))
28+
if p, _ := os.FindProcess(pid); p != nil {
29+
start = false
30+
}
31+
}
32+
if start {
33+
exe, err := os.Executable()
34+
if err != nil {
35+
exit(err)
36+
}
37+
c := exec.Command(exe, "daemon", "start")
38+
c.SysProcAttr = sysProcAttr
39+
f, err := os.OpenFile(viper.GetString("log_file"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
40+
if err != nil {
41+
exit(err)
42+
}
43+
c.Stdout = f
44+
c.Stderr = f
45+
if err := c.Start(); err != nil {
46+
exit(err)
47+
}
48+
if err := ioutil.WriteFile(viper.GetString("daemon_pid"), []byte(strconv.Itoa(c.Process.Pid)), 0644); err != nil {
49+
exit(err)
50+
}
51+
52+
// Wait for daemon to start
53+
w := watcher.New()
54+
w.SetMaxEvents(1)
55+
w.FilterOps(watcher.Create)
56+
go func() {
57+
e := <-w.Event
58+
if e.Name() == "daemon.addr" {
59+
w.Close()
60+
}
61+
}()
62+
w.Add(viper.GetString("root"))
63+
w.Start(50 * time.Millisecond)
64+
}
5365
}
5466

5567
var daemonCmd = &cobra.Command{
56-
Use: "daemon",
57-
Short: "Start/stop the tunnel daemon. It is automatically started as soon as the first command is executed.",
58-
Args: func(cmd *cobra.Command, args []string) error {
59-
if len(args) < 1 {
60-
return errors.New("requires an argument (start/stop)")
61-
}
62-
return nil
63-
},
64-
Run: func(cmd *cobra.Command, args []string) {
65-
if args[0] == "start" {
66-
daemon.Start()
67-
} else if args[0] == "stop" {
68-
c, err := getClient()
69-
if err != nil {
70-
exit(err)
71-
}
72-
defer c.Close()
73-
req := new(daemon.StopDaemonRequest)
74-
rep := new(daemon.StopDaemonReply)
75-
err = c.Call("Server.StopDaemon", req, rep)
76-
if err != nil {
77-
exit(err)
78-
}
79-
}
80-
},
68+
Use: "daemon",
69+
Short: "Start/stop the tunnel daemon. It is automatically started as soon as the first command is executed.",
70+
Args: func(cmd *cobra.Command, args []string) error {
71+
if len(args) < 1 {
72+
return errors.New("requires an argument (start/stop)")
73+
}
74+
return nil
75+
},
76+
Run: func(cmd *cobra.Command, args []string) {
77+
if args[0] == "start" {
78+
daemon.Start()
79+
} else if args[0] == "stop" {
80+
c, err := getClient()
81+
if err != nil {
82+
exit(err)
83+
}
84+
defer c.Close()
85+
req := new(daemon.StopDaemonRequest)
86+
rep := new(daemon.StopDaemonReply)
87+
err = c.Call("Server.StopDaemon", req, rep)
88+
if err != nil && err != io.ErrUnexpectedEOF {
89+
exit(err)
90+
}
91+
}
92+
},
8193
}
8294

8395
func init() {
84-
rootCmd.AddCommand(daemonCmd)
96+
rootCmd.AddCommand(daemonCmd)
8597
}

cmd/ps.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var psCmd = &cobra.Command{
1818
}
1919

2020
func psRPC() {
21+
s.Start()
2122
startDaemon()
2223
c, err := getClient()
2324
if err != nil {
@@ -26,7 +27,6 @@ func psRPC() {
2627
defer c.Close()
2728
req := new(daemon.PSRequest)
2829
rep := new(daemon.PSReply)
29-
s.Start()
3030
err = c.Call("Server.PS", req, rep)
3131
if err != nil {
3232
exit(err)
@@ -37,8 +37,9 @@ func psRPC() {
3737
tbl.AppendHeader(table.Row{"Name", "Target Address", "Remote URI", "Status", "Uptime"})
3838
for _, c := range rep.Connections {
3939
uptime := "-"
40-
if c.Status == daemon.ConnectionStatusStatusOnline {
41-
uptime = durafmt.ParseShort(time.Since(c.UpdatedAt)).String()
40+
since := time.Since(c.ConnectedAt)
41+
if c.Status == daemon.ConnectionStatusStatusOnline && since > 0 {
42+
uptime = durafmt.ParseShort(since).String()
4243
}
4344
tbl.AppendRow([]interface{}{c.Name, c.TargetAddress, c.RemoteURI, c.Status, uptime})
4445
}

cmd/rm.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ var rmCmd = &cobra.Command{
1818
return nil
1919
},
2020
Run: func(cmd *cobra.Command, args []string) {
21+
s.Start()
2122
startDaemon()
2223
c, err := getClient()
2324
if err != nil {
2425
exit(err)
2526
}
2627
defer c.Close()
2728
rep := new(daemon.RMReply)
28-
s.Start()
2929
defer s.Stop()
3030
err = c.Call("Server.RM", daemon.RMRequest{
3131
Name: args[0],
@@ -40,5 +40,4 @@ var rmCmd = &cobra.Command{
4040

4141
func init() {
4242
rootCmd.AddCommand(rmCmd)
43-
rmCmd.PersistentFlags().BoolVarP(&force, "force", "f", false, "force remove a connection")
4443
}

cmd/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ var rootCmd = &cobra.Command{
2828
return nil
2929
},
3030
Run: func(cmd *cobra.Command, args []string) {
31+
s.Start()
3132
startDaemon()
3233
c, err := getClient()
3334
if err != nil {
3435
exit(err)
3536
}
3637
defer c.Close()
3738
rep := new(daemon.ConnectReply)
38-
s.Start()
3939
addr := args[0]
4040
_, _, err = net.SplitHostPort(addr)
4141
if err != nil && strings.Contains(err.Error(), "missing port") {
@@ -84,7 +84,7 @@ func initialize() {
8484
viper.Set("log_file", filepath.Join(root, "daemon.log"))
8585
viper.Set("daemon_pid", filepath.Join(root, "daemon.pid"))
8686
viper.Set("daemon_addr", filepath.Join(root, "daemon.addr"))
87-
viper.Set("host", "labstack.me:22222")
87+
viper.Set("hostname", "labstack.me")
8888
viper.Set("api_url", "https://tunnel.labstack.com/api/v1")
8989
if dev := viper.GetString("DC") == "dev"; dev {
9090
viper.Set("host", "labstack.d:22222")

cmd/start.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

cmd/stop.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)