@@ -14,6 +14,7 @@ import (
14
14
"syscall"
15
15
"time"
16
16
17
+ "github.com/pkg/errors"
17
18
"github.com/sirupsen/logrus"
18
19
"github.com/spf13/cobra"
19
20
)
@@ -27,7 +28,9 @@ var rootCmd = &cobra.Command{
27
28
func init () {
28
29
rootCmd .Flags ().BoolP ("quiet" , "q" , false , "Quiet mode" )
29
30
rootCmd .Flags ().StringP ("source" , "s" , "" , "Source address" )
31
+ rootCmd .MarkFlagRequired ("source" )
30
32
rootCmd .Flags ().StringP ("destination" , "d" , "" , "Destination address" )
33
+ rootCmd .MarkFlagRequired ("destination" )
31
34
}
32
35
33
36
func Execute () {
@@ -47,14 +50,18 @@ var (
47
50
SIGTERM = syscall .Signal (0xf )
48
51
)
49
52
50
- func listen (addr string ) (net.Listener , error ) {
51
- //listen
52
- network := "tcp"
53
- if strings .HasPrefix (addr , "unix:" ) {
54
- network = "unix"
55
- addr = strings .TrimPrefix (addr , "unix:" )
53
+ func listen (url string ) (net.Listener , error ) {
54
+ parts := strings .SplitN (url , "://" , 2 )
55
+ if len (parts ) != 2 {
56
+ return nil , errors .Errorf ("invalid url: %s" , url )
56
57
}
57
- return net .Listen (network , addr )
58
+ proto := parts [0 ]
59
+ addr := parts [1 ]
60
+ listener , err := net .Listen (proto , addr )
61
+ if err != nil {
62
+ return nil , errors .WithStack (err )
63
+ }
64
+ return listener , err
58
65
}
59
66
60
67
func runAction (cmd * cobra.Command , args []string ) error {
@@ -120,13 +127,14 @@ var pool = sync.Pool{
120
127
},
121
128
}
122
129
123
- func dial (destination string ) (net.Conn , error ) {
124
- network := "tcp"
125
- if strings .HasPrefix (destination , "unix:" ) {
126
- network = "unix"
127
- destination = strings .TrimPrefix (destination , "unix:" )
130
+ func dial (url string ) (net.Conn , error ) {
131
+ parts := strings .SplitN (url , "://" , 2 )
132
+ if len (parts ) != 2 {
133
+ return nil , errors .Errorf ("invalid url: %s" , url )
128
134
}
129
- return net .Dial (network , destination )
135
+ proto := parts [0 ]
136
+ addr := parts [1 ]
137
+ return net .Dial (proto , addr )
130
138
}
131
139
132
140
func fwd (uconn net.Conn , destination string , quiet bool ) {
0 commit comments