2024-11-12 13:59:37 +00:00
|
|
|
package main
|
|
|
|
|
2024-11-15 15:03:42 +00:00
|
|
|
import "flag"
|
2024-11-12 13:59:37 +00:00
|
|
|
import "fmt"
|
2024-11-16 15:47:15 +00:00
|
|
|
import "io"
|
2024-11-12 13:59:37 +00:00
|
|
|
import "os"
|
|
|
|
import "strings"
|
|
|
|
|
2024-11-19 15:31:14 +00:00
|
|
|
|
2024-11-12 13:59:37 +00:00
|
|
|
func main() {
|
|
|
|
var err error
|
2024-11-15 15:03:42 +00:00
|
|
|
var flgs *flag.FlagSet
|
|
|
|
|
2024-11-12 13:59:37 +00:00
|
|
|
if len(os.Args) < 2 {
|
|
|
|
goto wrong_usage
|
|
|
|
}
|
|
|
|
if strings.EqualFold(os.Args[1], "server") {
|
2024-11-15 15:03:42 +00:00
|
|
|
var la []string
|
2024-11-16 15:47:15 +00:00
|
|
|
|
2024-11-15 15:03:42 +00:00
|
|
|
la = make([]string, 0)
|
|
|
|
|
|
|
|
flgs = flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
flgs.Func("listen-on", "specify a listening address", func(v string) error {
|
|
|
|
la = append(la, v)
|
|
|
|
return nil
|
|
|
|
})
|
2024-11-16 15:47:15 +00:00
|
|
|
flgs.SetOutput(io.Discard) // prevent usage output
|
2024-11-15 15:03:42 +00:00
|
|
|
err = flgs.Parse(os.Args[2:])
|
|
|
|
if err != nil {
|
2024-11-16 15:47:15 +00:00
|
|
|
fmt.Printf ("ERROR: %s\n", err.Error())
|
2024-11-15 15:03:42 +00:00
|
|
|
goto wrong_usage
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(la) < 0 || flgs.NArg() > 0 {
|
2024-11-12 13:59:37 +00:00
|
|
|
goto wrong_usage
|
|
|
|
}
|
2024-11-15 15:03:42 +00:00
|
|
|
|
|
|
|
err = server_main(la)
|
2024-11-12 13:59:37 +00:00
|
|
|
if err != nil {
|
2024-11-15 15:03:42 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "ERROR: server error - %s\n", err.Error())
|
|
|
|
goto oops
|
2024-11-12 13:59:37 +00:00
|
|
|
}
|
|
|
|
} else if strings.EqualFold(os.Args[1], "client") {
|
2024-11-15 15:03:42 +00:00
|
|
|
var la []string
|
|
|
|
var sa []string
|
|
|
|
|
|
|
|
la = make([]string, 0)
|
|
|
|
sa = make([]string, 0)
|
|
|
|
|
|
|
|
flgs = flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
flgs.Func("listen-on", "specify a control channel address", func(v string) error {
|
|
|
|
la = append(la, v)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
flgs.Func("server", "specify a server address", func(v string) error {
|
|
|
|
sa = append(sa, v)
|
|
|
|
return nil
|
|
|
|
})
|
2024-11-16 15:47:15 +00:00
|
|
|
flgs.SetOutput(io.Discard)
|
2024-11-15 15:03:42 +00:00
|
|
|
err = flgs.Parse(os.Args[2:])
|
|
|
|
if err != nil {
|
2024-11-16 15:47:15 +00:00
|
|
|
fmt.Printf ("ERROR: %s\n", err.Error())
|
2024-11-12 13:59:37 +00:00
|
|
|
goto wrong_usage
|
|
|
|
}
|
2024-11-15 15:03:42 +00:00
|
|
|
|
|
|
|
if len(la) != 1 || len(sa) != 1 || flgs.NArg() < 1 {
|
|
|
|
goto wrong_usage
|
|
|
|
}
|
|
|
|
err = client_main(la[0], sa[0], flgs.Args())
|
2024-11-12 13:59:37 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "ERROR: client error - %s\n", err.Error())
|
2024-11-15 15:03:42 +00:00
|
|
|
goto oops
|
2024-11-12 13:59:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
goto wrong_usage
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
|
|
|
wrong_usage:
|
2024-11-15 15:03:42 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "USAGE: %s server --listen-on=addr:port\n", os.Args[0])
|
|
|
|
fmt.Fprintf(os.Stderr, " %s client --listen-on=addr:port --server=addr:port peer-addr:peer-port\n", os.Args[0])
|
|
|
|
os.Exit(1)
|
|
|
|
|
|
|
|
oops:
|
2024-11-12 13:59:37 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|