piper

package module
v0.0.0-...-8bf560e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2016 License: Apache-2.0 Imports: 16 Imported by: 2

README

piper

piper is a small library to aide in running a process locally, but actually run it remotely over a WebSocket connection.

Here is an example of its usage in a tool used on Gondor:

package main

import (
	"flag"
	"fmt"
	"log"
	"net/http"
	"os"
	"os/exec"
	"strings"

	"github.com/eldarion-gondor/piper"
	"github.com/gorilla/websocket"
)

var secret = flag.String("secret", "", "")
var buildSHA string

var upgrader = websocket.Upgrader{
	ReadBufferSize:  8192,
	WriteBufferSize: 8192,
}

func main() {
	flag.Parse()
	if len(flag.Args()) == 0 {
		fmt.Println("no program to exec")
		os.Exit(1)
	}
	done := make(chan bool)
	http.HandleFunc("/ok", func(rw http.ResponseWriter, req *http.Request) {
		rw.WriteHeader(http.StatusOK)
	})
	http.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
		defer func() { done <- true }()
		log.Println("connection recieved")
		ws, err := upgrader.Upgrade(rw, req, nil)
		if err != nil {
			log.Printf("upgrade error: %s", err)
			return
		}
		log.Println("upgraded to websockets; ready to pipe")
		pipe, err := piper.NewServerPipe(req, ws)
		if err != nil {
			log.Printf("pipe error: %s", err)
			ws.Close()
			return
		}
		log.Printf("exec: %s", strings.Join(flag.Args(), " "))
		cmd := exec.Command(flag.Arg(0), flag.Args()[1:]...)
		if err := pipe.RunCmd(cmd); err != nil {
			log.Printf("exec error: %s", err)
			pipe.Close("error")
			return
		}
	})
	go func() {
		if err := http.ListenAndServe(":8000", nil); err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
	}()
	<-done
}

This program will start an HTTP server and expose a process via a WebSocket connection.

Documentation

Index

Constants

View Source
const (
	READY = iota
	EXIT
	STDOUT
	STDERR
	STDIN
	EOF
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Message

type Message struct {
	Kind     int
	Payload  []byte
	ExitCode uint32
}

func DecodeMessage

func DecodeMessage(r io.Reader) (*Message, error)

func (*Message) Prepare

func (m *Message) Prepare() ([]byte, error)

type Opts

type Opts struct {
	Tty    bool `json:"tty"`
	Width  int  `json:"width,omitempty"`
	Height int  `json:"height,omitempty"`
}

type Pipe

type Pipe struct {
	// contains filtered or unexported fields
}

func NewClientPipe

func NewClientPipe(host string, opts Opts, tlsConfig *tls.Config, logger *log.Logger) (*Pipe, error)

func NewPipe

func NewPipe(conn *websocket.Conn, opts Opts, logger *log.Logger) *Pipe

func NewServerPipe

func NewServerPipe(req *http.Request, conn *websocket.Conn, logger *log.Logger) (*Pipe, error)

func (*Pipe) Close

func (pipe *Pipe) Close(msg string)

func (*Pipe) Interact

func (pipe *Pipe) Interact() (int, error)

func (*Pipe) RunCmd

func (pipe *Pipe) RunCmd(cmd *exec.Cmd) error

func (*Pipe) Send

func (pipe *Pipe) Send(payload []byte)

type Winsize

type Winsize struct {
	Height uint16
	Width  uint16
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL