From 288840167f69b1ad2bae9bad286fab26a072a4a5 Mon Sep 17 00:00:00 2001 From: Charlie Moog Date: Fri, 3 Jul 2020 13:32:28 -0500 Subject: [PATCH] Improve docs --- .gitignore | 1 + README.md | 52 ++++++++++++++++++++-------------------------------- 2 files changed, 21 insertions(+), 32 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57f1cb2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.idea/ \ No newline at end of file diff --git a/README.md b/README.md index a5756df..c27b42d 100644 --- a/README.md +++ b/README.md @@ -4,36 +4,30 @@ web socket command execution protocol. 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") ``` @@ -41,17 +35,11 @@ conn.Close(websocket.StatusNormalClosure, "normal closure") ```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") } ``` @@ -59,13 +47,13 @@ func (s server) ServeHTTP(w http.ResponseWriter, r *http.Request) { 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 ``` @@ -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] [ <=> ]