Skip to content

Commit

Permalink
Improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
cmoog committed Jul 3, 2020
1 parent dedbab5 commit 2888401
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea/
52 changes: 20 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,56 @@
<strong style="font-size: 1.5em; text-decoration: underline;">w</strong>eb <strong style="font-size: 1.5em;text-decoration: underline;">s</strong>ocket command <strong style="font-size: 1.5em;text-decoration: underline;">e</strong>xecution <strong style="font-size: 1.5em;text-decoration: underline;">p</strong>rotocol. It can be thought of as SSH without encryption.

It's useful in cases where you want to provide a command exec interface into a remote environment. It's implemented
with WebSocket so it may be used directly by a browser frontend.
with WebSocket so it may be used directly by a browser frontend. Its symmetric design satisfies
`wsep.Execer` for local and remote execution.

## Examples

Error handling is omitted for brevity.

### Client

```golang
ws, _, err := websocket.Dial(ctx, "ws://remote.exec.addr", nil)
if err != nil {
// handle error
}
conn, _, _ := websocket.Dial(ctx, "ws://remote.exec.addr", nil)
defer conn.Close(websocket.StatusAbnormalClosure, "terminate process")

executor := wsep.RemoteExecer(ws)
process, err := executor.Start(ctx, proto.Command{
execer := wsep.RemoteExecer(conn)
process, _ := execer.Start(ctx, wsep.Command{
Command: "cat",
Args: []string{"go.mod"},
Args: []string{"go.mod"},
Stdin: false,
})
if err != nil {
// handle error
}

io.Copy(os.Stderr, process.Stderr())
go io.Copy(os.Stderr, process.Stderr())
go io.Copy(os.Stdout, process.Stdout())

err = process.Wait()
if err != nil {
// process failed
} else {
// process succeeded
}
process.Wait()
conn.Close(websocket.StatusNormalClosure, "normal closure")
```

### Server

```golang
func (s server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ws, err := websocket.Accept(w, r, nil)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
err = wsep.Serve(r.Context(), ws, wsep.LocalExecer{})
if err != nil {
ws.Close(websocket.StatusAbnormalClosure, "failed to serve execer")
return
}
ws.Close(websocket.StatusNormalClosure, "normal closure")
conn, _ := websocket.Accept(w, r, nil)

wsep.Serve(r.Context(), conn, wsep.LocalExecer{})

ws.Close(websocket.StatusNormalClosure, "normal closure")
}
```

### Development / Testing

Start a local executor:

```
```sh
go run ./dev/server
```

Start a client:

```
```sh
go run ./dev/client tty bash
go run ./dev/client notty ls
```
Expand All @@ -74,7 +62,7 @@ go run ./dev/client notty ls

Local `sh` through a local `wsep` connection

```shellscript
```shell script
$ head -c 100000000 /dev/urandom > /tmp/random; cat /tmp/random | pv | time ./bin/client notty sh -c "cat > /dev/null"

95.4MiB 0:00:00 [ 269MiB/s] [ <=> ]
Expand Down

0 comments on commit 2888401

Please sign in to comment.