-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
85 lines (72 loc) · 1.98 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"crypto/tls"
"errors"
"log"
"net"
"os"
"os/signal"
"syscall"
"time"
"github.com/databus23/guttle"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
func ClientCommand() *cobra.Command {
o := ClientOptions{}
c := &cobra.Command{
Use: "client",
Short: "Starts the guttle client",
RunE: func(c *cobra.Command, args []string) (err error) {
if err = o.Validate(); err != nil {
return
}
if o.ClientCert != "" {
tlsConfig, err2 := newTLSConfig(o.ClientCert, o.ClientKey)
if err2 != nil {
return err2
}
if o.CA != "" {
if tlsConfig.RootCAs, err = loadCAFile(o.CA); err != nil {
return err
}
}
o.ClientOptions.Dial = func(network, addr string) (net.Conn, error) {
return tls.DialWithDialer(&net.Dialer{Timeout: 10 * time.Second}, network, addr, tlsConfig)
}
}
client := guttle.NewClient(&o.ClientOptions)
go func() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM) // Push signals into channel
<-sigs
log.Print("Signal received, closing.")
client.Stop()
<-sigs //second signal terminates right away
os.Exit(1)
}()
return client.Start()
},
}
o.BindFlags(c.Flags())
return c
}
type ClientOptions struct {
guttle.ClientOptions
ClientCert string
ClientKey string
CA string
}
func (o *ClientOptions) BindFlags(flags *pflag.FlagSet) {
flags.StringVar(&o.ServerAddr, "server", "", "Tunnel server address (ip:port)")
flags.StringVar(&o.ListenAddr, "listen-addr", "", "Listen for incoming connections to send through the tunnel")
flags.StringVar(&o.ClientCert, "cert", "", "TLS client cert for tunnel connection")
flags.StringVar(&o.ClientKey, "key", "", "TLS client key for tunnel connection")
flags.StringVar(&o.CA, "ca", "", "CA for validating tunnel server (default: system chain)")
}
func (o *ClientOptions) Validate() error {
if o.ServerAddr == "" {
return errors.New("--server is required")
}
return nil
}