Skip to content

Commit ea9b9c4

Browse files
committed
Improve usage message and random seed
* The usage message for --help has been made much more comphrensive * rules.yaml is now the default, removing one argument from running the tool * The method for random seeds has been improved according to a deprecation Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
1 parent a2c5e5f commit ea9b9c4

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

main.go

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,63 @@ func main() {
3333
dialTimeout time.Duration
3434
)
3535

36-
flag.StringVar(&file, "f", "", "Job to run or leave blank for job.yaml in current directory")
36+
flag.Usage = func() {
37+
fmt.Fprintf(os.Stderr, `mixctl - TCP L4 load-balancer
38+
39+
GitHub: https://github.com/inlets/mixctl
40+
41+
Usage:
42+
43+
mixctl -f rules.yaml
44+
45+
Example config file:
46+
47+
version: 0.1
48+
49+
rules:
50+
- name: rpi-k3s
51+
from: 127.0.0.1:6443
52+
to:
53+
- 192.168.1.19:6443
54+
- 192.168.1.21:6443
55+
- 192.168.1.20:6443
56+
57+
rules:
58+
- name: remap-local-ssh-port
59+
from: 127.0.0.1:2222
60+
to:
61+
- 127.0.0.1:22
62+
63+
Flags:
64+
65+
`)
66+
67+
flag.PrintDefaults()
68+
}
69+
70+
flag.StringVar(&file, "f", "rules.yaml", "Job to run or leave blank for job.yaml in current directory")
3771
flag.BoolVar(&verbose, "v", true, "Verbose output for opened and closed connections")
3872
flag.DurationVar(&dialTimeout, "t", time.Millisecond*1500, "Dial timeout")
3973
flag.Parse()
4074

4175
if len(file) == 0 {
42-
fmt.Fprintf(os.Stderr, "usage: mixctl -f rules.yaml\n")
76+
flag.Usage()
4377
os.Exit(1)
4478
}
4579

4680
set := ForwardingSet{}
4781
data, err := os.ReadFile(file)
4882
if err != nil {
49-
fmt.Fprintf(os.Stderr, "error reading file %s %s", file, err.Error())
83+
fmt.Fprintf(os.Stderr, "Error reading %s %s\n\nRun mixctl --help for usage\n", file, err.Error())
5084
os.Exit(1)
5185
}
5286
if err = yaml.Unmarshal(data, &set); err != nil {
53-
fmt.Fprintf(os.Stderr, "error parsing file %s %s", file, err.Error())
87+
fmt.Fprintf(os.Stderr, "Error parsing file %s %s\n", file, err.Error())
5488
os.Exit(1)
5589
}
5690

5791
if len(set.Rules) == 0 {
58-
fmt.Fprintf(os.Stderr, "no rules found in file %s", file)
92+
fmt.Fprintf(os.Stderr, "No rules found in file %s\n", file)
5993
os.Exit(1)
6094
}
6195

@@ -64,7 +98,7 @@ func main() {
6498
wg := sync.WaitGroup{}
6599
wg.Add(len(set.Rules))
66100
for _, rule := range set.Rules {
67-
fmt.Printf("Forward (%s) from: %s to: %s\n", rule.Name, rule.From, rule.To)
101+
fmt.Printf("Forwarding (%s) from: %s to: %s\n", rule.Name, rule.From, rule.To)
68102
}
69103
fmt.Println()
70104

@@ -85,7 +119,8 @@ func main() {
85119

86120
func forward(name, from string, to []string, verbose bool, dialTimeout time.Duration) error {
87121
seed := time.Now().UnixNano()
88-
rand.Seed(seed)
122+
123+
localRand := rand.New(rand.NewSource(seed))
89124

90125
fmt.Printf("Listening on: %s\n", from)
91126
l, err := net.Listen("tcp", from)
@@ -104,7 +139,7 @@ func forward(name, from string, to []string, verbose bool, dialTimeout time.Dura
104139

105140
// pick randomly from the list of upstream servers
106141
// available
107-
index := rand.Intn(len(to))
142+
index := localRand.Intn(len(to))
108143
upstream := to[index]
109144

110145
// A separate Goroutine means the loop can accept another

0 commit comments

Comments
 (0)