├── showcase_linux_armv5.gif ├── showcase_linux_x86-64.gif ├── go.mod ├── .gitignore ├── package.json ├── go.sum ├── README.md ├── index.html ├── LICENSE └── main.go /showcase_linux_armv5.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NaisuXu/web-terminal-demo-with-golang-and-xterm/HEAD/showcase_linux_armv5.gif -------------------------------------------------------------------------------- /showcase_linux_x86-64.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NaisuXu/web-terminal-demo-with-golang-and-xterm/HEAD/showcase_linux_x86-64.gif -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module webterminal 2 | 3 | go 1.20 4 | 5 | require ( 6 | github.com/creack/pty v1.1.18 7 | github.com/olahol/melody v1.1.3 8 | ) 9 | 10 | require github.com/gorilla/websocket v1.5.0 // indirect 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/* 2 | !/node_modules/xterm 3 | /node_modules/xterm/* 4 | !/node_modules/xterm/css 5 | /node_modules/xterm/css/* 6 | !/node_modules/xterm/css/xterm.css 7 | !/node_modules/xterm/lib 8 | /node_modules/xterm/lib/* 9 | !/node_modules/xterm/lib/xterm.js 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "web-terminal-demo-with-golang-and-xterm", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "xterm": "^5.1.0" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY= 2 | github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= 3 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 4 | github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= 5 | github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= 6 | github.com/olahol/melody v1.1.3 h1:7Eo8egmejdrhdCM64uPgWj7NLSAVKl7Iv9NloFlzb60= 7 | github.com/olahol/melody v1.1.3/go.mod h1:GgkTl6Y7yWj/HtfD48Q5vLKPVoZOH+Qqgfa7CvJgJM4= 8 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 9 | github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= 10 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WebTerminal demo with Golang and Xterm.js 2 | 3 | ## 简介 / Brief 4 | 5 | 这是一个 `WebTerminal` 的项目演示,使用 `Golang` 和 `Xterm.js` 实现。This is a demo for `WebTerminal`, based on `Golang` and `Xterm. js` . 6 | 7 | 8 | 9 | ## 用法 / Usage 10 | 11 | 下载可执行文件到服务器设备中,然后启动服务。Download the executable file to the server device, and then start the service. 12 | 13 | ```shell 14 | chmod +x webterminal_xxxx 15 | ./webterminal_xxxx 16 | ``` 17 | 18 | 在客户端浏览器中打开 `http://server_ip_address:22333/` 。Navigate your browser to `http://server_ip_address:22333/` . 19 | 20 | 21 | 22 | ## 演示 / Showcase 23 | 24 | **linux_x86-64 (Ununtu 22.04 AMD Ryzen 5 PRO 4650U):** 25 | 26 | ![showcase_linux_x86-64](showcase_linux_x86-64.gif) 27 | 28 | 29 | 30 | **linux_armv5 (NUC980 Linux buildroot 5.10.103+ armv5tejl GNU/Linux):** 31 | 32 | ![showcase_linux_armv5](showcase_linux_armv5.gif) 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | WebTerminal 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 |
14 |
15 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 NaisuXu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "embed" 5 | "net/http" 6 | "os/exec" 7 | 8 | "github.com/creack/pty" 9 | "github.com/olahol/melody" 10 | ) 11 | 12 | //go:embed index.html node_modules/xterm/css/xterm.css node_modules/xterm/lib/xterm.js 13 | var content embed.FS 14 | 15 | func main() { 16 | c := exec.Command("sh") // 系统默认shell交互程序 17 | f, err := pty.Start(c) // pty用于调用系统自带的虚拟终端 18 | if err != nil { 19 | panic(err) 20 | } 21 | 22 | m := melody.New() // melody用于实现WebSocket功能 23 | 24 | go func() { // 处理来自虚拟终端的消息 25 | for { 26 | buf := make([]byte, 1024) 27 | read, err := f.Read(buf) 28 | if err != nil { 29 | return 30 | } 31 | // fmt.Println("f.Read: ", string(buf[:read])) 32 | m.Broadcast(buf[:read]) // 将数据发送给网页 33 | } 34 | }() 35 | 36 | m.HandleMessage(func(s *melody.Session, msg []byte) { // 处理来自WebSocket的消息 37 | // fmt.Println("m.HandleMessage: ", string(msg)) 38 | f.Write(msg) // 将消息写到虚拟终端 39 | }) 40 | 41 | http.HandleFunc("/webterminal", func(w http.ResponseWriter, r *http.Request) { 42 | m.HandleRequest(w, r) // 访问 /webterminal 时将转交给melody处理 43 | }) 44 | 45 | fs := http.FileServer(http.FS(content)) 46 | http.Handle("/", http.StripPrefix("/", fs)) // 设置静态文件服务 47 | 48 | http.ListenAndServe("0.0.0.0:22333", nil) // 启动服务器,访问 http://本机(服务器)IP地址:22333/ 进行测试 49 | } 50 | --------------------------------------------------------------------------------