func main() { if len(os.Args) < 2 { fmt.Println("Not enough args") os.Exit(2) } uri := os.Args[1] peer1 := duplex.NewPeer() peer1.SetOption(duplex.OptName, "peer1") err := peer1.Bind(uri) if err != nil { panic(err) } go func() { meta, ch := peer1.Accept() fmt.Println("accepted", meta.Service(), meta.Headers()) for { frame, err := ch.ReadFrame() if err != nil { panic(err) } println("peer1 recv:", string(frame)) err = ch.WriteFrame(frame) if err != nil { panic(err) } } }() peer2 := duplex.NewPeer() peer2.SetOption(duplex.OptName, "peer2") err = peer2.Connect(uri) if err != nil { panic(err) } println("connected") fmt.Println(peer1.Peers(), peer2.Peers()) target := peer2.Peers()[0] ch, err := peer2.Open(target, "foobar", []string{"abc=123"}) if err != nil { panic(err) } go func() { for { frame, err := ch.ReadFrame() if err != nil { panic(err) } println("peer2 recv:", string(frame)) } }() scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { ch.WriteFrame(scanner.Bytes()) } }
func NewPeer() *Peer { return &Peer{ Peer: duplex.NewPeer(), contextType: reflect.TypeOf(""), serviceMap: make(map[string]*service), } }
func server(backendListen, frontendListen string) { // bind frontend frontend, err := net.Listen("tcp", frontendListen) if err != nil { log.Fatal(err) } log.Println("frontend listening on", frontendListen, "...") mux, err := vhost.NewHTTPMuxer(frontend, 1*time.Second) if err != nil { log.Fatal(err) } go func() { for { conn, err := mux.NextError() log.Println("vhost:", err) if conn != nil { conn.Write([]byte("error: " + err.Error())) conn.Close() } } }() // bind backend backend := duplex.NewPeer() err = backend.Bind("tcp://" + backendListen) if err != nil { log.Fatal(err) } log.Println("backend listening on", backendListen, "...") for { meta, ch := backend.Accept() if meta == nil { break } // register vhost if meta.Service() == "tunnel" { log.Println("registering", meta.RemotePeer()) vhostListener, err := mux.Listen(meta.RemotePeer()) if err != nil { log.Println(err) } go func() { for { conn, err := vhostListener.Accept() if err != nil { log.Println(err) } tunnelCh, err := ch.Open("", nil) if err != nil { log.Println(err) } go tunnelCh.Join(conn) } }() continue } } }
func ReloadGateway(args []string) { gatewaySock := PluginPath + "/gateway.sock" if _, err := os.Stat(gatewaySock); os.IsNotExist(err) { return } trigger := duplex.NewPeer() defer trigger.Shutdown() trigger.SetOption(duplex.OptName, "plugn:reload") err := trigger.Connect("unix://" + gatewaySock) if err != nil { panic(err) } _, err = trigger.Open("plugn:gateway", "reload", nil) if err != nil { panic(err) } }
func TriggerGateway(args []string) { gatewaySock := PluginPath + "/gateway.sock" if _, err := os.Stat(gatewaySock); os.IsNotExist(err) { return } trigger := duplex.NewPeer() defer trigger.Shutdown() trigger.SetOption(duplex.OptName, "plugn:trigger") err := trigger.Connect("unix://" + gatewaySock) if err != nil { panic(err) } ch, err := trigger.Open("plugn:gateway", args[0], args[1:]) if err != nil { panic(err) } io.Copy(os.Stdout, ch) ch.Close() }
func main() { plugin := duplex.NewPeer() defer plugin.Shutdown() plugin.SetOption(duplex.OptName, "remote") err := plugin.Connect("unix://" + os.Getenv("PLUGIN_PATH") + "/gateway.sock") if err != nil { panic(err) } log.Println("remote: starting...") for { meta, ch := plugin.Accept() log.Println("remote: triggered:", meta.Service(), meta.Headers()) switch meta.Service() { case "items": io.WriteString(ch, "Fake Remote, Item 1:5.00\n") io.WriteString(ch, "Fake Remote, Item 2:5.00\n") } ch.Close() } }
func startGateway(wg *sync.WaitGroup, plugins *coproc.Host) *duplex.Peer { gateway := duplex.NewPeer() gateway.SetOption(duplex.OptName, "plugn:gateway") err := gateway.Bind("unix://" + PluginPath + "/gateway.sock") if err != nil { panic(err) } wg.Add(1) go func() { for { meta, ch := gateway.Accept() if meta == nil { break } if meta.Service() == "reload" { log.Println("reload received...") plugins.RestartWith(findPlugins()) ch.Close() continue } go func() { for _, peer := range gateway.Peers() { if !strings.HasPrefix(peer, "plugn") { hook, err := gateway.Open(peer, meta.Service(), meta.Headers()) if err != nil { log.Println("unable to trigger", peer) continue } io.Copy(ch, hook) hook.Close() } } ch.Close() }() } wg.Done() }() return gateway }