Skip to content

Commit

Permalink
Improve usage message and random seed
Browse files Browse the repository at this point in the history
* 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) <[email protected]>
  • Loading branch information
alexellis committed Oct 27, 2023
1 parent a2c5e5f commit ea9b9c4
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,63 @@ func main() {
dialTimeout time.Duration
)

flag.StringVar(&file, "f", "", "Job to run or leave blank for job.yaml in current directory")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `mixctl - TCP L4 load-balancer
GitHub: https://github.com/inlets/mixctl
Usage:
mixctl -f rules.yaml
Example config file:
version: 0.1
rules:
- name: rpi-k3s
from: 127.0.0.1:6443
to:
- 192.168.1.19:6443
- 192.168.1.21:6443
- 192.168.1.20:6443
rules:
- name: remap-local-ssh-port
from: 127.0.0.1:2222
to:
- 127.0.0.1:22
Flags:
`)

flag.PrintDefaults()
}

flag.StringVar(&file, "f", "rules.yaml", "Job to run or leave blank for job.yaml in current directory")
flag.BoolVar(&verbose, "v", true, "Verbose output for opened and closed connections")
flag.DurationVar(&dialTimeout, "t", time.Millisecond*1500, "Dial timeout")
flag.Parse()

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

set := ForwardingSet{}
data, err := os.ReadFile(file)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading file %s %s", file, err.Error())
fmt.Fprintf(os.Stderr, "Error reading %s %s\n\nRun mixctl --help for usage\n", file, err.Error())
os.Exit(1)
}
if err = yaml.Unmarshal(data, &set); err != nil {
fmt.Fprintf(os.Stderr, "error parsing file %s %s", file, err.Error())
fmt.Fprintf(os.Stderr, "Error parsing file %s %s\n", file, err.Error())
os.Exit(1)
}

if len(set.Rules) == 0 {
fmt.Fprintf(os.Stderr, "no rules found in file %s", file)
fmt.Fprintf(os.Stderr, "No rules found in file %s\n", file)
os.Exit(1)
}

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

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

func forward(name, from string, to []string, verbose bool, dialTimeout time.Duration) error {
seed := time.Now().UnixNano()
rand.Seed(seed)

localRand := rand.New(rand.NewSource(seed))

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

// pick randomly from the list of upstream servers
// available
index := rand.Intn(len(to))
index := localRand.Intn(len(to))
upstream := to[index]

// A separate Goroutine means the loop can accept another
Expand Down

0 comments on commit ea9b9c4

Please sign in to comment.