parent
a3b2e0026d
commit
a5e826e284
|
@ -57,7 +57,10 @@ func main() {
|
||||||
Addr: flagAddr,
|
Addr: flagAddr,
|
||||||
Handler: Middleware(g),
|
Handler: Middleware(g),
|
||||||
}
|
}
|
||||||
server.ListenAndServe()
|
err := server.ListenAndServe()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Middleware(next http.Handler) http.Handler {
|
func Middleware(next http.Handler) http.Handler {
|
||||||
|
|
14
gterm.go
14
gterm.go
|
@ -9,7 +9,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/creack/pty"
|
"github.com/creack/pty"
|
||||||
|
@ -78,7 +77,6 @@ func New() *GTerm {
|
||||||
log.Info("current connections: ", cons)
|
log.Info("current connections: ", cons)
|
||||||
time.Sleep(5 * time.Minute)
|
time.Sleep(5 * time.Minute)
|
||||||
}
|
}
|
||||||
|
|
||||||
}()
|
}()
|
||||||
return >erm{
|
return >erm{
|
||||||
AppName: "GTERM",
|
AppName: "GTERM",
|
||||||
|
@ -165,8 +163,7 @@ func (g *GTerm) WS(w http.ResponseWriter, r *http.Request) {
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var connectionClosed bool
|
var connectionClosed bool
|
||||||
var waiter sync.WaitGroup
|
waiter := make(chan struct{}, 1)
|
||||||
waiter.Add(1)
|
|
||||||
|
|
||||||
// this is a keep-alive loop that ensures connection does not hang-up itself
|
// this is a keep-alive loop that ensures connection does not hang-up itself
|
||||||
lastPongTime := time.Now()
|
lastPongTime := time.Now()
|
||||||
|
@ -183,7 +180,7 @@ func (g *GTerm) WS(w http.ResponseWriter, r *http.Request) {
|
||||||
time.Sleep(g.Timeout / 2)
|
time.Sleep(g.Timeout / 2)
|
||||||
if time.Since(lastPongTime) > g.Timeout {
|
if time.Since(lastPongTime) > g.Timeout {
|
||||||
l.Warn("failed to get response from ping, triggering disconnect now...")
|
l.Warn("failed to get response from ping, triggering disconnect now...")
|
||||||
waiter.Done()
|
waiter <- struct{}{}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
l.Debug("received response from ping successfully")
|
l.Debug("received response from ping successfully")
|
||||||
|
@ -198,7 +195,7 @@ func (g *GTerm) WS(w http.ResponseWriter, r *http.Request) {
|
||||||
// can be terminated - this frees up memory so the service doesn't get
|
// can be terminated - this frees up memory so the service doesn't get
|
||||||
// overloaded
|
// overloaded
|
||||||
if errorCounter > g.ErrorLimit {
|
if errorCounter > g.ErrorLimit {
|
||||||
waiter.Done()
|
waiter <- struct{}{}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
buffer := make([]byte, g.BufferSize)
|
buffer := make([]byte, g.BufferSize)
|
||||||
|
@ -208,7 +205,7 @@ func (g *GTerm) WS(w http.ResponseWriter, r *http.Request) {
|
||||||
if err := conn.WriteMessage(websocket.TextMessage, []byte("bye!")); err != nil {
|
if err := conn.WriteMessage(websocket.TextMessage, []byte("bye!")); err != nil {
|
||||||
l.Warn("failed to send termination message from tty to xterm.js: ", err)
|
l.Warn("failed to send termination message from tty to xterm.js: ", err)
|
||||||
}
|
}
|
||||||
waiter.Done()
|
waiter <- struct{}{}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := conn.WriteMessage(websocket.BinaryMessage, buffer[:readLength]); err != nil {
|
if err := conn.WriteMessage(websocket.BinaryMessage, buffer[:readLength]); err != nil {
|
||||||
|
@ -276,7 +273,8 @@ func (g *GTerm) WS(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
waiter.Wait()
|
<-waiter
|
||||||
|
close(waiter)
|
||||||
l.Info("closing connection...")
|
l.Info("closing connection...")
|
||||||
connectionClosed = true
|
connectionClosed = true
|
||||||
}
|
}
|
||||||
|
|
19
main.js
19
main.js
|
@ -8,13 +8,13 @@ import 'xterm/css/xterm.css'
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
const terminal = new Terminal({
|
const terminal = new Terminal({
|
||||||
screenKeys: true,
|
// screenKeys: true,
|
||||||
useStyle: true,
|
// useStyle: true,
|
||||||
cursorBlink: true,
|
// cursorBlink: true,
|
||||||
fullscreenWin: true,
|
// fullscreenWin: true,
|
||||||
maximizeWin: true,
|
// maximizeWin: true,
|
||||||
screenReaderMode: true,
|
// screenReaderMode: true,
|
||||||
cols: 128,
|
// cols: 128,
|
||||||
});
|
});
|
||||||
const fitAddon = new FitAddon();
|
const fitAddon = new FitAddon();
|
||||||
terminal.loadAddon(fitAddon);
|
terminal.loadAddon(fitAddon);
|
||||||
|
@ -29,7 +29,6 @@ import 'xterm/css/xterm.css'
|
||||||
const serializeAddon = new SerializeAddon();
|
const serializeAddon = new SerializeAddon();
|
||||||
terminal.loadAddon(serializeAddon);
|
terminal.loadAddon(serializeAddon);
|
||||||
terminal.open(document.getElementById("terminal"));
|
terminal.open(document.getElementById("terminal"));
|
||||||
fitAddon.fit();
|
|
||||||
ws.onclose = function(event) {
|
ws.onclose = function(event) {
|
||||||
console.log(event);
|
console.log(event);
|
||||||
terminal.write('\r\n\nconnection has been terminated from the server-side (hit refresh to restart)\n')
|
terminal.write('\r\n\nconnection has been terminated from the server-side (hit refresh to restart)\n')
|
||||||
|
@ -39,6 +38,9 @@ import 'xterm/css/xterm.css'
|
||||||
terminal._initialized = true;
|
terminal._initialized = true;
|
||||||
terminal.focus();
|
terminal.focus();
|
||||||
setTimeout(function() {fitAddon.fit()});
|
setTimeout(function() {fitAddon.fit()});
|
||||||
|
document.addEventListener('keypress',(e)=>{
|
||||||
|
e.preventDefault();
|
||||||
|
})
|
||||||
terminal.onResize(function(event) {
|
terminal.onResize(function(event) {
|
||||||
var rows = event.rows;
|
var rows = event.rows;
|
||||||
var cols = event.cols;
|
var cols = event.cols;
|
||||||
|
@ -54,5 +56,6 @@ import 'xterm/css/xterm.css'
|
||||||
console.log("resize")
|
console.log("resize")
|
||||||
fitAddon.fit();
|
fitAddon.fit();
|
||||||
};
|
};
|
||||||
|
fitAddon.fit();
|
||||||
};
|
};
|
||||||
})();
|
})();
|
File diff suppressed because one or more lines are too long
|
@ -1 +1 @@
|
||||||
<!DOCTYPE html><html><head><link rel="stylesheet" href="index.6999253a.css"><title>{{.AppName}}</title><style>body::-webkit-scrollbar,div::-webkit-scrollbar,html::-webkit-scrollbar{display:none;width:0}body,html{margin:0;overflow:hidden;padding:0}div#terminal{height:100%;left:0;position:absolute;top:0;width:100%}div#terminal div{height:100%}.xterm-screen,.xterm-viewport{height:100%;margin:0;padding:0}</style></head><body> <div id="terminal"></div> <script type="module" src="index.b28ecb90.js"></script> </body></html>
|
<!DOCTYPE html><html><head><link rel="stylesheet" href="index.6999253a.css"><title>{{.AppName}}</title><style>body::-webkit-scrollbar,div::-webkit-scrollbar,html::-webkit-scrollbar{display:none;width:0}body,html{margin:0;overflow:hidden;padding:0}div#terminal{height:100%;left:0;position:absolute;top:0;width:100%}div#terminal div{height:100%}.xterm-screen,.xterm-viewport{height:100%;margin:0;padding:0}</style></head><body> <div id="terminal"></div> <script type="module" src="index.52560dc1.js"></script> </body></html>
|
Loading…
Reference in New Issue