FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScriptSolidServer/src/terminal/index.js at gh-pages · JavaScriptSolidServer/JavaScriptSolidServer · GitHub
JavaScriptSolidServer
JavaScriptSolidServer
Repository navigation
Code
Issues
164
(164)
Pull requests
14
(14)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScriptSolidServer
/
src
/
terminal
/
index.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
128 lines (110 loc) · 3.86 KB
Breadcrumbs
JavaScriptSolidServer
/
src
/
terminal
/
index.js
Copy path
File metadata and controls
128 lines (110 loc) · 3.86 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* Terminal Plugin — WebSocket Shell Access
*
* Provides a remote shell over WebSocket for authenticated pod owners.
* Spawns /bin/sh on connection and pipes stdin/stdout/stderr between
* the WebSocket and the shell process.
*
* SECURITY: Requires authentication. The connecting user's webId must
* be present (verified via token). This is a privileged endpoint.
*
* Usage: jss start --terminal
* Endpoint: wss://your.pod/.terminal
*
* Protocol (binary/text over WebSocket):
* -> (text/binary) stdin data sent to shell
* <- (text/binary) stdout/stderr data from shell
* <- JSON { type: "exit", code: <n> } shell exited
* <- JSON { type: "error", message: "..." }
*/
import
websocket
from
'@fastify/websocket'
;
import
{
getWebIdFromRequestAsync
}
from
'../auth/token.js'
;
import
{
spawn
}
from
'child_process'
;
/**
* Register terminal WebSocket route on Fastify instance
*
*
@param
{
object
} fastify - Fastify instance
*
@param
{
object
} options - Options
*
@param
{
string
} options.path - WebSocket path (default: '/.terminal')
*/
export
async
function
terminalPlugin
(
fastify
,
options
=
{
}
)
{
const
wsPath
=
options
.
path
||
'/.terminal'
;
// Track active shell processes for cleanup
const
shells
=
new
Set
(
)
;
if
(
!
fastify
.
websocketServer
)
{
await
fastify
.
register
(
websocket
)
;
}
// Clean up all shells on server close
fastify
.
addHook
(
'onClose'
,
async
(
)
=>
{
for
(
const
proc
of
shells
)
{
try
{
proc
.
kill
(
)
;
}
catch
{
/* already dead */
}
}
shells
.
clear
(
)
;
}
)
;
fastify
.
get
(
wsPath
,
{
websocket
:
true
}
,
async
(
connection
,
request
)
=>
{
const
socket
=
connection
.
socket
||
connection
;
// Authenticate — query param token support for browser WebSocket
const
queryToken
=
request
.
query
?.
token
;
if
(
queryToken
&&
!
request
.
headers
.
authorization
)
{
request
.
headers
.
authorization
=
`Bearer
${
queryToken
}
`
;
}
const
{
webId
}
=
await
getWebIdFromRequestAsync
(
request
)
;
if
(
!
webId
&&
!
options
.
public
)
{
socket
.
send
(
JSON
.
stringify
(
{
type
:
'error'
,
message
:
'Authentication required'
}
)
)
;
socket
.
close
(
)
;
return
;
}
// Spawn shell
const
shellCommand
=
process
.
env
.
SHELL
||
'bash'
;
const
shell
=
spawn
(
shellCommand
,
[
'-i'
]
,
{
stdio
:
[
'pipe'
,
'pipe'
,
'pipe'
]
,
env
:
{
...
process
.
env
,
TERM
:
'xterm-256color'
}
,
}
)
;
shells
.
add
(
shell
)
;
// Pipe shell stdout to WebSocket
shell
.
stdout
.
on
(
'data'
,
(
data
)
=>
{
if
(
socket
.
readyState
===
1
)
{
try
{
socket
.
send
(
data
.
toString
(
)
.
replace
(
/
\r
?
\n
/
g
,
'\r\n'
)
)
;
}
catch
{
/* socket closed */
}
}
}
)
;
// Pipe shell stderr to WebSocket
shell
.
stderr
.
on
(
'data'
,
(
data
)
=>
{
if
(
socket
.
readyState
===
1
)
{
try
{
socket
.
send
(
data
.
toString
(
)
.
replace
(
/
\r
?
\n
/
g
,
'\r\n'
)
)
;
}
catch
{
/* socket closed */
}
}
}
)
;
// Shell exited
shell
.
on
(
'close'
,
(
code
)
=>
{
shells
.
delete
(
shell
)
;
if
(
socket
.
readyState
===
1
)
{
try
{
socket
.
send
(
JSON
.
stringify
(
{
type
:
'exit'
,
code
:
code
??
1
}
)
)
;
socket
.
close
(
)
;
}
catch
{
/* socket already closed */
}
}
}
)
;
shell
.
on
(
'error'
,
(
err
)
=>
{
shells
.
delete
(
shell
)
;
if
(
socket
.
readyState
===
1
)
{
try
{
socket
.
send
(
JSON
.
stringify
(
{
type
:
'error'
,
message
:
err
.
message
}
)
)
;
socket
.
close
(
)
;
}
catch
{
/* socket already closed */
}
}
}
)
;
// Pipe WebSocket messages to shell stdin
socket
.
on
(
'message'
,
(
data
)
=>
{
if
(
shell
.
stdin
.
writable
)
{
const
buf
=
Buffer
.
isBuffer
(
data
)
?
data
:
Buffer
.
from
(
data
)
;
try
{
shell
.
stdin
.
write
(
buf
)
;
}
catch
{
/* stdin closed */
}
}
}
)
;
// WebSocket closed — kill the shell
socket
.
on
(
'close'
,
(
)
=>
{
shells
.
delete
(
shell
)
;
try
{
shell
.
kill
(
)
;
}
catch
{
/* already dead */
}
}
)
;
socket
.
on
(
'error'
,
(
)
=>
{
}
)
;
}
)
;
}
export
default
terminalPlugin
;
Back
|
FazBrowse Home
|
New Git URL