Beispiel #1
0
// ServeChan accepts connections and starts-up a handler goroutine for each one
func ServeChan(listener net.Listener) {
	tl, err := spdy.NewTransportListener(listener, spdy.NoAuthenticator)
	if err != nil {
		glog.Fatal(err)
	}
	defer func() {
		tl.Close()
		glog.Info("Done serving libchan connections")
	}()
	for {
		t, err := tl.AcceptTransport()
		if err != nil {
			glog.Error(err)
			return
		}
		go handleRequests(t)
	}
}
Beispiel #2
0
func Rserver(proto *string, socket *string) {
	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(*proto, *socket, tlsConfig)
		if err != nil {
			log.Fatal(err)
		}
	} else {
		var err error
		listener, err = net.Listen(*proto, *socket)
		if err != nil {
			log.Fatal(err)
		}
	}

	tl, err := spdy.NewTransportListener(listener, spdy.NoAuthenticator)
	if err != nil {
		log.Fatal(err)
	}

	for {
		t, err := tl.AcceptTransport()
		if err != nil {
			log.Print(err)
			break
		}

		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
						//cmd.Stdout = os.Stdout
						//cmd.Stderr = os.Stderr

						stdin, err := cmd.StdinPipe()
						if err != nil {
							log.Print(err)
							break
						}
						if err != nil {
							log.Fatal(err)
						}
						go func() {
							log.Println("Copying back to stdin")
							io.Copy(stdin, command.Stdin)
							//log.Println("Closing stdin")
							//stdin.Close()
						}()
						log.Println("Running the command")
						res := cmd.Run()
						log.Printf("Command finished with error: %v", err)
						log.Println("Done")
						//log.Println("Closing the Stdout")
						//command.Stdout.Close()
						//log.Println("Closing the Stderr")
						//command.Stderr.Close()
						log.Println("Assigning returnResult")
						returnResult := &CommandResponse{}
						if res != nil {
							log.Println("Res is not null")
							if exiterr, ok := res.(*exec.ExitError); ok {
								log.Println("Treating the error code")
								returnResult.Status = exiterr.Sys().(syscall.WaitStatus).ExitStatus()
								log.Println("We have the result")
							} else {
								log.Print(res)
								returnResult.Status = 10
							}
						}
						log.Println("Res is not null")
						err = command.StatusChan.Send(returnResult)
						log.Println("Finished")
						if err != nil {
							log.Print(err)
						}
					}
				}()
			}
		}()
	}
}
Beispiel #3
0
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)
		}
	}

	tl, err := spdy.NewTransportListener(listener, spdy.NoAuthenticator)
	if err != nil {
		log.Fatal(err)
	}

	for {
		t, err := tl.AcceptTransport()
		if err != nil {
			log.Print(err)
			break
		}

		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)
						}
					}
				}()
			}
		}()
	}
}