package main import ( "flag" "fmt" "io" "math" "os" "strconv" "strings" ) type config struct { inbound uint16 outbound uint16 proxyUID uint32 exclude []uint16 } func parse(args []string) (config, error) { flags := flag.NewFlagSet("mesh-init", flag.ContinueOnError) flags.SetOutput(io.Discard) inbound := flags.Uint("inbound", 4143, "inbound proxy port") outbound := flags.Uint("outbound", 4140, "outbound proxy port") uid := flags.Uint64("proxy-uid", 2102, "proxy user ID") exclude := flags.String("ignore-inbound", "4190,4191", "inbound ports excluded from interception") if err := flags.Parse(args); err != nil { return config{}, err } if flags.NArg() != 0 || *inbound == 0 || *inbound > math.MaxUint16 || *outbound == 0 || *outbound > math.MaxUint16 || *uid > math.MaxUint32 { return config{}, fmt.Errorf("invalid proxy configuration") } result := config{inbound: uint16(*inbound), outbound: uint16(*outbound), proxyUID: uint32(*uid)} if *exclude != "" { for _, item := range strings.Split(*exclude, ",") { port, err := strconv.ParseUint(item, 10, 16) if err != nil || port == 0 { return config{}, fmt.Errorf("invalid inbound exclusion %q", item) } result.exclude = append(result.exclude, uint16(port)) } } return result, nil } func main() { value, err := parse(os.Args[1:]) if err == nil { err = install(value) } if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } fmt.Println("NAT_READY") }