func main() { if len(os.Args) < 2 { usage() } var addr string flag.StringVar(&addr, addressFlagName, addressFlagDefaultValue, addressFlagDescription) flag.Parse() if !flag.Parsed() { log.Printf("%s: invalid argument(s)\n", os.Args[0]) usage() } var client net.Conn var err error if os.Getenv("USE_TLS") != "" { client, err = tls.Dial("tcp", addr, &tls.Config{InsecureSkipVerify: true}) } else { client, err = net.Dial("tcp", addr) } if err != nil { log.Fatal(err) } p, err := spdy.NewSpdyStreamProvider(client, false) if err != nil { log.Fatal(err) } transport := spdy.NewTransport(p) sender, err := transport.NewSendChannel() if err != nil { log.Fatal(err) } receiver, remoteSender := libchan.Pipe() command := &RemoteCommand{ Cmd: flag.Args()[0], Args: flag.Args()[1:], Stdin: os.Stdin, Stdout: os.Stdout, Stderr: os.Stderr, StatusChan: remoteSender, } err = sender.Send(command) if err != nil { log.Fatal(err) } response := &CommandResponse{} err = receiver.Receive(response) if err != nil { log.Fatal(err) } os.Exit(response.Status) }
func main() { if len(os.Args) < 2 { log.Fatal("usage: <command> [<arg> ]") } var client net.Conn var err error if os.Getenv("USE_TLS") != "" { client, err = tls.Dial("tcp", "127.0.0.1:9323", &tls.Config{InsecureSkipVerify: true}) } else { client, err = net.Dial("tcp", "127.0.0.1:9323") } if err != nil { log.Fatal(err) } p, err := spdy.NewSpdyStreamProvider(client, false) if err != nil { log.Fatal(err) } transport := spdy.NewTransport(p) sender, err := transport.NewSendChannel() if err != nil { log.Fatal(err) } receiver, remoteSender := libchan.Pipe() command := &RemoteCommand{ Cmd: os.Args[1], Args: os.Args[2:], Stdin: os.Stdin, Stdout: os.Stdout, Stderr: os.Stderr, StatusChan: remoteSender, } err = sender.Send(command) if err != nil { log.Fatal(err) } response := &CommandResponse{} err = receiver.Receive(response) if err != nil { log.Fatal(err) } os.Exit(response.Status) }
func NewRemoteRepository(remoteURL string) ThingeyRepository { receiver, remoteSender := libchan.Pipe() var client net.Conn var err error client, err = net.Dial("tcp", remoteURL) if err != nil { log.Fatal(err) } p, err := spdy.NewSpdyStreamProvider(client, false) if err != nil { log.Fatal(err) } transport := spdy.NewTransport(p) return NewThingeyRepository(transport.NewSendChannel, receiver, remoteSender) }
func TapClient(client net.Conn, name string, stderr bool) error { provider, err := spdy.NewSpdyStreamProvider(client, false) if err != nil { return err } transport := spdy.NewTransport(provider) sender, err := transport.NewSendChannel() if err != nil { return err } defer sender.Close() remoteDone, done := libchan.Pipe() errPipe, remoteErrPipe := libchan.Pipe() sm := tapStreamMessage{ Done: remoteDone, Err: remoteErrPipe, Name: name, Stdout: !stderr, W: os.Stdout, } if err := sender.Send(&sm); err != nil { return err } signalChan := make(chan os.Signal) signal.Notify(signalChan, os.Interrupt, os.Kill) go func() { <-signalChan if err := done.Close(); err != nil { logrus.Errorf("Error closing done channel") } }() var em errStreamMessage if err := errPipe.Receive(&em); err != nil && err != io.EOF { return err } if em.Message != "" { return fmt.Errorf("remote error: %s", em.Message) } return nil }
func main() { remote := flag.String("remote", "localhost", "Where to find that there server there") port := flag.String("port", "8080", "The port to listen on") flag.Parse() fmt.Println("Config: ", *remote, *port) client, err := net.Dial("tcp", *remote+":"+*port) check(err) p, err := spdy.NewSpdyStreamProvider(client, false) check(err) transport := spdy.NewTransport(p) sender, err := transport.NewSendChannel() check(err) receiver, remoteSender := libchan.Pipe() closeReceiver, _ := libchan.Pipe() command := &RemoteCommand{ Cmd: "attach", Args: make([]string, 3), OutChan: remoteSender, Closer: closeReceiver, } err = sender.Send(command) check(err) for { rLine := &RemoteLine{} err = receiver.Receive(rLine) fmt.Println(rLine.Line) } }
func listen(listener net.Listener, clients *[]libchan.Sender) { for { c, err := listener.Accept() check(err) p, err := spdy.NewSpdyStreamProvider(c, true) check(err) t := spdy.NewTransport(p) go func() { for { receiver, err := t.WaitReceiveChannel() check(err) command := &RemoteCommand{} err = receiver.Receive(command) check(err) *clients = append(*clients, command.OutChan) } }() } }
func main() { cert := os.Getenv("TLS_CERT") key := os.Getenv("TLS_KEY") var listener net.Listener if cert != "" && key != "" { tlsCert, err := tls.LoadX509KeyPair(cert, key) if err != nil { log.Fatal(err) } tlsConfig := &tls.Config{ InsecureSkipVerify: true, Certificates: []tls.Certificate{tlsCert}, } listener, err = tls.Listen("tcp", "localhost:9323", tlsConfig) if err != nil { log.Fatal(err) } } else { var err error listener, err = net.Listen("tcp", "localhost:9323") if err != nil { log.Fatal(err) } } for { c, err := listener.Accept() if err != nil { log.Print(err) break } p, err := spdy.NewSpdyStreamProvider(c, true) if err != nil { log.Print(err) break } t := spdy.NewTransport(p) go func() { for { receiver, err := t.WaitReceiveChannel() if err != nil { log.Print(err) break } go func() { for { command := &RemoteCommand{} err := receiver.Receive(command) if err != nil { log.Print(err) break } cmd := exec.Command(command.Cmd, command.Args...) cmd.Stdout = command.Stdout cmd.Stderr = command.Stderr stdin, err := cmd.StdinPipe() if err != nil { log.Print(err) break } go func() { io.Copy(stdin, command.Stdin) stdin.Close() }() res := cmd.Run() command.Stdout.Close() command.Stderr.Close() returnResult := &CommandResponse{} if res != nil { if exiterr, ok := res.(*exec.ExitError); ok { returnResult.Status = exiterr.Sys().(syscall.WaitStatus).ExitStatus() } else { log.Print(res) returnResult.Status = 10 } } err = command.StatusChan.Send(returnResult) if err != nil { log.Print(err) } } }() } }() } }
func xmain() { var listener net.Listener var err error listener, err = net.Listen("tcp", "localhost:9323") if err != nil { log.Fatal(err) } for { c, err := listener.Accept() if err != nil { log.Print(err) break } p, err := spdy.NewSpdyStreamProvider(c, true) if err != nil { log.Print(err) break } t := spdy.NewTransport(p) go func() { for { receiver, err := t.WaitReceiveChannel() if err != nil { log.Print(err) break } go func() { for { command := &RemoteCommand{} err := receiver.Receive(command) if err != nil { log.Print(err) break } cmd := exec.Command(command.Cmd, command.Args...) cmd.Stdout = command.Stdout cmd.Stderr = command.Stderr stdin, err := cmd.StdinPipe() if err != nil { log.Print(err) break } go func() { io.Copy(stdin, command.Stdin) stdin.Close() }() res := cmd.Run() command.Stdout.Close() command.Stderr.Close() returnResult := &CommandResponse{} if res != nil { if exiterr, ok := res.(*exec.ExitError); ok { returnResult.Status = exiterr.Sys().(syscall.WaitStatus).ExitStatus() } else { log.Print(res) returnResult.Status = 10 } } err = command.StatusChan.Send(returnResult) if err != nil { log.Print(err) } } }() } }() } }
func TapServer(l net.Listener, lr *LogRouter) { for { c, err := l.Accept() if err != nil { if err != io.EOF { logrus.Errorf("Listen error: %#v", err) } return } p, err := spdy.NewSpdyStreamProvider(c, true) if err != nil { logrus.Errorf("Error creating stream provider: %#v", err) continue } t := spdy.NewTransport(p) go func() { r, err := t.WaitReceiveChannel() if err != nil { logrus.Errorf("Error receiving channel, ending libchan transport: %s", err) return } for { var tm tapStreamMessage if err := r.Receive(&tm); err != nil { if err != io.EOF { logrus.Errorf("Error receiving message, ending libchan transport: %s", err) } return } ts, ok := lr.logStreams[tm.Name] if !ok { tm.Err.Send(errStreamMessage{Message: "missing named stream"}) // TODO: Check send error tm.Err.Close() continue } var tap io.ReadCloser if tm.Stdout { tap = ts.TapStdout() } else { tap = ts.TapStderr() } go func() { defer tm.Err.Close() _, err := io.Copy(tm.W, tap) if err != nil { logrus.Errorf("Error copying tap: %v", err) tm.Err.Send(errStreamMessage{Message: err.Error()}) } }() go func() { defer tap.Close() var s struct{} if err := tm.Done.Receive(&s); err != nil && err != io.EOF { logrus.Errorf("Error reading from done: %s", err) } }() } }() } }