Make client request base64 encoding

This makes gotty-client still work.
This commit is contained in:
Søren L. Hansen 2022-03-31 10:46:59 -07:00
parent 1eed97f0f8
commit dd3603c341
8 changed files with 42 additions and 37 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

29
js/package-lock.json generated
View File

@ -1491,23 +1491,6 @@
"webpack": "^4.0.0 || ^5.0.0"
}
},
"node_modules/style-loader/node_modules/schema-utils": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-3.0.0.tgz",
"integrity": "sha512-6D82/xSzO094ajanoOSbe4YvXWMfn2A//8Y1+MUqFAJul5Bs+yn36xbK9OtNDcRVSBJ9jjeoXftM6CfztsjOAA==",
"dependencies": {
"@types/json-schema": "^7.0.6",
"ajv": "^6.12.5",
"ajv-keywords": "^3.5.2"
},
"engines": {
"node": ">= 10.13.0"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/webpack"
}
},
"node_modules/supports-color": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
@ -2970,18 +2953,6 @@
"requires": {
"loader-utils": "^2.0.0",
"schema-utils": "^3.0.0"
},
"dependencies": {
"schema-utils": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-3.0.0.tgz",
"integrity": "sha512-6D82/xSzO094ajanoOSbe4YvXWMfn2A//8Y1+MUqFAJul5Bs+yn36xbK9OtNDcRVSBJ9jjeoXftM6CfztsjOAA==",
"requires": {
"@types/json-schema": "^7.0.6",
"ajv": "^6.12.5",
"ajv-keywords": "^3.5.2"
}
}
}
},
"supports-color": {

View File

@ -149,7 +149,7 @@ export class WebTTY {
let maxChunkSize = Math.floor(effectiveBufferSize / 4)*3;
for (let i = 0; i < Math.ceil(dataString.length / maxChunkSize); i++) {
let inputChunk = dataString.substring(i * effectiveBufferSize, Math.min((i + 1) * effectiveBufferSize, dataString.length))
let inputChunk = dataString.substring(i * maxChunkSize, Math.min((i + 1) * maxChunkSize, dataString.length))
this.connection.send(msgInput + btoa(inputChunk));
}
}
@ -210,6 +210,9 @@ export class WebTTY {
this.term.onResize(resizeHandler);
resizeHandler(termInfo.columns, termInfo.rows);
// Set encoding to base64 (TODO: Fix this up)
connection.send("4base64");
this.term.onInput(
(input: string) => {
this.sendInput(input);

19
webtty/codecs.go Normal file
View File

@ -0,0 +1,19 @@
package webtty
type Decoder interface {
Decode(dst, src []byte) (int, error)
}
type Encoder interface {
Encode(dst, src []byte) (int, error)
}
type NullCodec struct{}
func (NullCodec) Encode(dst, src []byte) (int, error) {
return copy(dst, src), nil
}
func (NullCodec) Decode(dst, src []byte) (int, error) {
return copy(dst, src), nil
}

View File

@ -13,6 +13,8 @@ const (
Ping = '2'
// Notify that the browser size has been changed
ResizeTerminal = '3'
// Change encoding
SetEncoding = '4'
)
const (

View File

@ -24,6 +24,7 @@ type WebTTY struct {
rows int
reconnect int // in seconds
masterPrefs []byte
decoder Decoder
bufferSize int
writeMutex sync.Mutex
@ -43,6 +44,7 @@ func New(masterConn Master, slave Slave, options ...Option) (*WebTTY, error) {
rows: 0,
bufferSize: 1024,
decoder: &NullCodec{},
}
for _, option := range options {
@ -177,9 +179,9 @@ func (wt *WebTTY) handleMasterReadEvent(data []byte) error {
}
var decodedBuffer = make([]byte, len(data))
n, err := base64.StdEncoding.Decode(decodedBuffer, data[1:])
n, err := wt.decoder.Decode(decodedBuffer, data[1:])
if err != nil {
return errors.Wrapf(err, "failed to write received data to slave")
return errors.Wrapf(err, "failed to decode received data")
}
_, err = wt.slave.Write(decodedBuffer[:n])
@ -193,6 +195,14 @@ func (wt *WebTTY) handleMasterReadEvent(data []byte) error {
return errors.Wrapf(err, "failed to return Pong message to master")
}
case SetEncoding:
switch string(data[1:]) {
case "base64":
wt.decoder = base64.StdEncoding
case "null":
wt.decoder = NullCodec{}
}
case ResizeTerminal:
if wt.columns != 0 && wt.rows != 0 {
break

View File

@ -98,8 +98,8 @@ func TestWriteFromFrontend(t *testing.T) {
checkNextMsgType(t, mMaster.gottyToMasterReader, SetWindowTitle)
checkNextMsgType(t, mMaster.gottyToMasterReader, SetBufferSize)
// simulate input from frontend... ("hello" in base64)
message := []byte("1aGVsbG8=\n") // line buffered canonical mode
// simulate input from frontend...
message := []byte("1hello\n") // line buffered canonical mode
mMaster.masterToGottyWriter.Write(message)
// ...and make sure it makes it through to the slave intact
@ -108,7 +108,7 @@ func TestWriteFromFrontend(t *testing.T) {
if err != nil {
t.Fatalf("Unexpected error from Write(): %s", err)
}
if !bytes.Equal(readBuf[:n], []byte("hello")) {
if !bytes.Equal(readBuf[:n], message[1:]) {
t.Fatalf("Unexpected message received: `%s`", readBuf[:n])
}
}