func main() { flag.Parse() if *target == 0 && *path == "" { fmt.Fprintln(os.Stderr, "error: a target pid or path is required") flag.PrintDefaults() return } if *target != 0 { p, err := namespace.ProcessPath(*target, namespace.CLONE_NEWNET) if err != nil { panic(err) } *path = p } fmt.Printf("PROXY: targetPath:%v targetAddr:%v remoteAddr:%v\n", *path, *targetAddr, *remoteAddr) listener, err := nameNet.ListenNamespace(*path, "tcp", *targetAddr) if err != nil { panic(err) } for { // Wait for a connection. conn, err := listener.Accept() if err != nil { log.Fatal(err) } go proxyConn(&conn) } }
func proxy(c *cli.Context) { addr := c.String("addr") namespacePid := c.Int("namespace-pid") namespacePath := c.String("namespace-path") if namespacePid == 0 && namespacePath == "" { fmt.Fprintln(os.Stderr, "warn: a namespace pid or path is required to setns, listening in this namespace") } path := c.String("path") if path == "" { fmt.Fprintln(os.Stderr, "error: an etcd path is required") return } if namespacePid != 0 { p, err := namespace.ProcessPath(namespacePid, namespace.CLONE_NEWNET) if err != nil { panic(err) } namespacePath = p } peers := trimsplit(c.String("peers"), ",") client := etcd.NewClient(peers) log.Printf("Proxying for keys in %s on host %s", path, addr) // Keep an eye on the backend path b := backends{path: path} err := b.Sync(client) if err != nil { log.Fatal(err) } go b.Watch(client) log.Printf("Proxying for keys in %s on host %s in namespace %d", path, addr, namespacePath) listener, err := nameNet.ListenNamespace(namespacePath, "tcp", addr) if err != nil { panic(err) } for { // Wait for a connection. conn, err := listener.Accept() if err != nil { log.Fatal(err) } next := b.Next() if next == "" { conn.Close() log.Printf("No backends! Closing the connection") continue } log.Printf("PROXY: targetAddr:%v addr:%v\n", addr, next) go proxyConn(&conn, next) } }