Skip to content

Instantly share code, notes, and snippets.

@rm77
Forked from Humerus/index.html
Created May 24, 2023 04:31
Show Gist options
  • Save rm77/3a71f5f0f66d11844ff50e769594ac1d to your computer and use it in GitHub Desktop.
Save rm77/3a71f5f0f66d11844ff50e769594ac1d to your computer and use it in GitHub Desktop.
Docker Exec Web Console
<html>
<head>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" />
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<script src="//cdn.rawgit.com/chjj/term.js/0b10f6c55d5113d50d0ff94b6c38a46375a5f9a5/src/term.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<style>
body {
background-color: #000;
}
.terminal {
border: #000 solid 5px;
font-family: "DejaVu Sans Mono", "Liberation Mono", monospace;
font-size: 11px;
color: #f0f0f0;
background: #000;
}
.terminal-cursor {
color: #000;
background: #f0f0f0;
}
</style>
<script type="text/javascript">
$(function() {
var websocket = new WebSocket("ws://" + window.location.hostname + ":" + window.location.port + "/exec/" + prompt("cid"));
websocket.onopen = function(evt) {
var term = new Terminal({
cols: 100,
rows: 30,
screenKeys: true,
useStyle: true,
cursorBlink: true,
});
term.on('data', function(data) {
websocket.send(data);
});
term.on('title', function(title) {
document.title = title;
});
term.open(document.getElementById('container-terminal'));
websocket.onmessage = function(evt) {
term.write(evt.data);
}
websocket.onclose = function(evt) {
term.write("Session terminated");
term.destroy();
}
websocket.onerror = function(evt) {
if (typeof console.log == "function") {
console.log(evt)
}
}
}
});
</script>
</head>
<div id="container-terminal"></div>
</html>
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httputil"
"time"
"github.com/davecgh/go-spew/spew"
"golang.org/x/net/websocket"
)
var port = flag.String("port", "8080", "Port for server")
var host = flag.String("host", "192.168.0.126:2735", "Docker host")
func main() {
flag.Parse()
http.Handle("/exec/", websocket.Handler(ExecContainer))
http.Handle("/", http.FileServer(http.Dir("./")))
if err := http.ListenAndServe(":"+*port, nil); err != nil {
panic(err)
}
}
func ExecContainer(ws *websocket.Conn) {
container := ws.Request().URL.Path[len("/exec/"):]
if container == "" {
ws.Write([]byte("Container does not exist"))
return
}
type stuff struct {
Id string
}
var s stuff
params := bytes.NewBufferString("{\"AttachStdin\":true,\"AttachStdout\":true,\"AttachStderr\":true,\"Tty\":true,\"Cmd\":[\"/bin/bash\"]}")
resp, err := http.Post("http://"+*host+"/containers/"+container+"/exec", "application/json", params)
if err != nil {
panic(err)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
json.Unmarshal([]byte(data), &s)
if err := hijack(*host, "POST", "/exec/"+s.Id+"/start", true, ws, ws, ws, nil, nil); err != nil {
panic(err)
}
fmt.Println("Connection!")
fmt.Println(ws)
spew.Dump(ws)
}
func hijack(addr, method, path string, setRawTerminal bool, in io.ReadCloser, stdout, stderr io.Writer, started chan io.Closer, data interface{}) error {
params := bytes.NewBufferString("{\"Detach\": false, \"Tty\": true}")
req, err := http.NewRequest(method, path, params)
if err != nil {
return err
}
req.Header.Set("User-Agent", "Docker-Client")
req.Header.Set("Content-Type", "text/plain")
req.Header.Set("Connection", "Upgrade")
req.Header.Set("Upgrade", "tcp")
req.Host = addr
dial, err := net.Dial("tcp", addr)
// When we set up a TCP connection for hijack, there could be long periods
// of inactivity (a long running command with no output) that in certain
// network setups may cause ECONNTIMEOUT, leaving the client in an unknown
// state. Setting TCP KeepAlive on the socket connection will prohibit
// ECONNTIMEOUT unless the socket connection truly is broken
if tcpConn, ok := dial.(*net.TCPConn); ok {
tcpConn.SetKeepAlive(true)
tcpConn.SetKeepAlivePeriod(30 * time.Second)
}
if err != nil {
return err
}
clientconn := httputil.NewClientConn(dial, nil)
defer clientconn.Close()
// Server hijacks the connection, error 'connection closed' expected
clientconn.Do(req)
rwc, br := clientconn.Hijack()
defer rwc.Close()
if started != nil {
started <- rwc
}
var receiveStdout chan error
if stdout != nil || stderr != nil {
go func() (err error) {
if setRawTerminal && stdout != nil {
_, err = io.Copy(stdout, br)
}
return err
}()
}
go func() error {
if in != nil {
io.Copy(rwc, in)
}
if conn, ok := rwc.(interface {
CloseWrite() error
}); ok {
if err := conn.CloseWrite(); err != nil {
}
}
return nil
}()
if stdout != nil || stderr != nil {
if err := <-receiveStdout; err != nil {
return err
}
}
spew.Dump(br)
go func() {
for {
fmt.Println(br)
spew.Dump(br)
}
}()
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment