2020-10-26 12:30:30 +00:00
|
|
|
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
2019-09-18 05:54:16 +00:00
|
|
|
import { ITerminalOptions, Terminal } from 'xterm';
|
2020-10-14 06:34:53 +00:00
|
|
|
import { FitAddon } from 'xterm-addon-fit';
|
|
|
|
import { SearchAddon } from 'xterm-addon-search';
|
|
|
|
import { SearchBarAddon } from 'xterm-addon-search-bar';
|
2019-06-29 23:14:32 +00:00
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
2019-09-06 06:05:24 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
2020-07-03 21:19:05 +00:00
|
|
|
import styled from 'styled-components/macro';
|
2020-03-30 05:12:50 +00:00
|
|
|
import { usePermissions } from '@/plugins/usePermissions';
|
2020-10-17 20:54:34 +00:00
|
|
|
import tw, { theme as th } from 'twin.macro';
|
2020-10-14 06:34:53 +00:00
|
|
|
import 'xterm/css/xterm.css';
|
2020-10-04 02:21:11 +00:00
|
|
|
import useEventListener from '@/plugins/useEventListener';
|
|
|
|
import { debounce } from 'debounce';
|
2020-10-26 12:30:30 +00:00
|
|
|
import { usePersistedState } from '@/plugins/usePersistedState';
|
2019-06-29 23:14:32 +00:00
|
|
|
|
|
|
|
const theme = {
|
2020-10-17 20:54:34 +00:00
|
|
|
background: th`colors.black`.toString(),
|
2019-06-29 23:14:32 +00:00
|
|
|
cursor: 'transparent',
|
2020-10-17 20:54:34 +00:00
|
|
|
black: th`colors.black`.toString(),
|
2019-06-29 23:14:32 +00:00
|
|
|
red: '#E54B4B',
|
|
|
|
green: '#9ECE58',
|
|
|
|
yellow: '#FAED70',
|
|
|
|
blue: '#396FE2',
|
|
|
|
magenta: '#BB80B3',
|
|
|
|
cyan: '#2DDAFD',
|
|
|
|
white: '#d0d0d0',
|
|
|
|
brightBlack: 'rgba(255, 255, 255, 0.2)',
|
|
|
|
brightRed: '#FF5370',
|
|
|
|
brightGreen: '#C3E88D',
|
|
|
|
brightYellow: '#FFCB6B',
|
|
|
|
brightBlue: '#82AAFF',
|
|
|
|
brightMagenta: '#C792EA',
|
|
|
|
brightCyan: '#89DDFF',
|
|
|
|
brightWhite: '#ffffff',
|
2020-10-14 06:34:53 +00:00
|
|
|
selection: '#FAF089',
|
2019-06-29 23:14:32 +00:00
|
|
|
};
|
|
|
|
|
2019-09-18 05:54:16 +00:00
|
|
|
const terminalProps: ITerminalOptions = {
|
2019-09-06 06:05:24 +00:00
|
|
|
disableStdin: true,
|
|
|
|
cursorStyle: 'underline',
|
|
|
|
allowTransparency: true,
|
|
|
|
fontSize: 12,
|
|
|
|
fontFamily: 'Menlo, Monaco, Consolas, monospace',
|
|
|
|
rows: 30,
|
|
|
|
theme: theme,
|
2019-09-18 05:54:16 +00:00
|
|
|
};
|
2019-06-29 23:14:32 +00:00
|
|
|
|
2019-12-22 22:33:08 +00:00
|
|
|
const TerminalDiv = styled.div`
|
|
|
|
&::-webkit-scrollbar {
|
|
|
|
width: 8px;
|
|
|
|
}
|
|
|
|
|
|
|
|
&::-webkit-scrollbar-thumb {
|
|
|
|
${tw`bg-neutral-900`};
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2019-09-06 06:05:24 +00:00
|
|
|
export default () => {
|
2020-01-18 23:26:15 +00:00
|
|
|
const TERMINAL_PRELUDE = '\u001b[1m\u001b[33mcontainer@pterodactyl~ \u001b[0m';
|
2020-10-04 02:21:11 +00:00
|
|
|
const ref = useRef<HTMLDivElement>(null);
|
2019-09-18 05:54:16 +00:00
|
|
|
const terminal = useMemo(() => new Terminal({ ...terminalProps }), []);
|
2020-10-14 06:34:53 +00:00
|
|
|
const fitAddon = new FitAddon();
|
|
|
|
const searchAddon = new SearchAddon();
|
2020-10-15 20:41:11 +00:00
|
|
|
const searchBar = new SearchBarAddon({ searchAddon });
|
2019-12-07 20:13:46 +00:00
|
|
|
const { connected, instance } = ServerContext.useStoreState(state => state.socket);
|
2020-07-04 22:40:41 +00:00
|
|
|
const [ canSendCommands ] = usePermissions([ 'control.console' ]);
|
2020-10-26 12:30:30 +00:00
|
|
|
const serverId = ServerContext.useStoreState(state => state.server.data!.id);
|
|
|
|
const [ history, setHistory ] = usePersistedState<string[]>(`${serverId}:command_history`, []);
|
|
|
|
const [ historyIndex, setHistoryIndex ] = useState(-1);
|
2019-06-30 00:18:17 +00:00
|
|
|
|
2020-01-18 23:26:15 +00:00
|
|
|
const handleConsoleOutput = (line: string, prelude = false) => terminal.writeln(
|
|
|
|
(prelude ? TERMINAL_PRELUDE : '') + line.replace(/(?:\r\n|\r|\n)$/im, '') + '\u001b[0m',
|
2019-09-06 06:05:24 +00:00
|
|
|
);
|
|
|
|
|
2019-09-28 20:09:47 +00:00
|
|
|
const handleDaemonErrorOutput = (line: string) => terminal.writeln(
|
2020-01-18 23:26:15 +00:00
|
|
|
TERMINAL_PRELUDE + '\u001b[1m\u001b[41m' + line.replace(/(?:\r\n|\r|\n)$/im, '') + '\u001b[0m',
|
2019-09-28 20:09:47 +00:00
|
|
|
);
|
|
|
|
|
2019-09-28 20:45:09 +00:00
|
|
|
const handlePowerChangeEvent = (state: string) => terminal.writeln(
|
2020-01-18 23:26:15 +00:00
|
|
|
TERMINAL_PRELUDE + 'Server marked as ' + state + '...\u001b[0m',
|
2019-09-28 20:45:09 +00:00
|
|
|
);
|
|
|
|
|
2020-11-09 07:34:56 +00:00
|
|
|
const handleCommandKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
2020-10-26 12:30:30 +00:00
|
|
|
if (e.key === 'ArrowUp') {
|
|
|
|
const newIndex = Math.min(historyIndex + 1, history!.length - 1);
|
|
|
|
|
|
|
|
setHistoryIndex(newIndex);
|
|
|
|
e.currentTarget.value = history![newIndex] || '';
|
2020-11-09 07:34:56 +00:00
|
|
|
|
|
|
|
// By default up arrow will also bring the cursor to the start of the line,
|
|
|
|
// so we'll preventDefault to keep it at the end.
|
|
|
|
e.preventDefault();
|
2020-10-26 12:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (e.key === 'ArrowDown') {
|
|
|
|
const newIndex = Math.max(historyIndex - 1, -1);
|
|
|
|
|
|
|
|
setHistoryIndex(newIndex);
|
|
|
|
e.currentTarget.value = history![newIndex] || '';
|
2019-09-18 05:54:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 12:30:30 +00:00
|
|
|
const command = e.currentTarget.value;
|
|
|
|
if (e.key === 'Enter' && command.length > 0) {
|
2020-10-27 10:19:33 +00:00
|
|
|
setHistory(prevHistory => [ command, ...prevHistory! ].slice(0, 32));
|
2020-10-26 12:30:30 +00:00
|
|
|
setHistoryIndex(-1);
|
|
|
|
|
|
|
|
instance && instance.send('send command', command);
|
|
|
|
e.currentTarget.value = '';
|
|
|
|
}
|
2019-09-18 05:54:23 +00:00
|
|
|
};
|
|
|
|
|
2019-09-06 06:05:24 +00:00
|
|
|
useEffect(() => {
|
2020-10-04 02:21:11 +00:00
|
|
|
if (connected && ref.current && !terminal.element) {
|
|
|
|
terminal.open(ref.current);
|
2020-10-14 06:34:53 +00:00
|
|
|
terminal.loadAddon(fitAddon);
|
|
|
|
terminal.loadAddon(searchAddon);
|
2020-10-15 20:41:11 +00:00
|
|
|
terminal.loadAddon(searchBar);
|
2020-10-14 06:34:53 +00:00
|
|
|
fitAddon.fit();
|
2019-06-30 00:18:17 +00:00
|
|
|
|
2020-10-14 06:34:53 +00:00
|
|
|
// Add support for capturing keys
|
2020-02-11 17:37:12 +00:00
|
|
|
terminal.attachCustomKeyEventHandler((e: KeyboardEvent) => {
|
2020-10-14 06:34:53 +00:00
|
|
|
// Ctrl + C ( Copy )
|
2020-10-14 15:38:59 +00:00
|
|
|
if (e.ctrlKey && e.key === 'c') {
|
2020-02-11 17:37:12 +00:00
|
|
|
document.execCommand('copy');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-14 15:38:59 +00:00
|
|
|
if (e.ctrlKey && e.key === 'f') {
|
2020-10-15 20:41:11 +00:00
|
|
|
searchBar.show();
|
2020-10-14 06:34:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-14 11:23:02 +00:00
|
|
|
if (e.key === 'Escape') {
|
2020-10-15 20:41:11 +00:00
|
|
|
searchBar.hidden();
|
2020-10-14 11:23:02 +00:00
|
|
|
}
|
2020-02-11 17:37:12 +00:00
|
|
|
return true;
|
|
|
|
});
|
2019-06-30 01:28:23 +00:00
|
|
|
}
|
2020-10-04 02:21:11 +00:00
|
|
|
}, [ terminal, connected ]);
|
|
|
|
|
|
|
|
const fit = debounce(() => {
|
2020-10-14 06:34:53 +00:00
|
|
|
fitAddon.fit();
|
2020-10-04 02:21:11 +00:00
|
|
|
}, 100);
|
|
|
|
|
|
|
|
useEventListener('resize', () => fit());
|
2019-06-29 23:14:32 +00:00
|
|
|
|
2019-09-06 06:05:24 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (connected && instance) {
|
|
|
|
terminal.clear();
|
2019-06-29 23:14:32 +00:00
|
|
|
|
2019-09-28 20:45:09 +00:00
|
|
|
instance.addListener('status', handlePowerChangeEvent);
|
2019-09-18 05:54:23 +00:00
|
|
|
instance.addListener('console output', handleConsoleOutput);
|
2020-01-18 23:26:15 +00:00
|
|
|
instance.addListener('install output', handleConsoleOutput);
|
|
|
|
instance.addListener('daemon message', line => handleConsoleOutput(line, true));
|
2019-09-28 20:09:47 +00:00
|
|
|
instance.addListener('daemon error', handleDaemonErrorOutput);
|
2019-09-06 06:05:24 +00:00
|
|
|
instance.send('send logs');
|
2019-06-30 00:18:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 06:05:24 +00:00
|
|
|
return () => {
|
2019-09-28 20:09:47 +00:00
|
|
|
instance && instance.removeListener('console output', handleConsoleOutput)
|
2020-01-18 23:26:15 +00:00
|
|
|
.removeListener('install output', handleConsoleOutput)
|
|
|
|
.removeListener('daemon message', line => handleConsoleOutput(line, true))
|
2019-09-28 20:45:09 +00:00
|
|
|
.removeListener('daemon error', handleDaemonErrorOutput)
|
|
|
|
.removeListener('status', handlePowerChangeEvent);
|
2019-09-06 06:05:24 +00:00
|
|
|
};
|
2019-12-07 20:13:46 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2019-09-06 06:05:24 +00:00
|
|
|
}, [ connected, instance ]);
|
2019-06-30 01:28:23 +00:00
|
|
|
|
2019-09-06 06:05:24 +00:00
|
|
|
return (
|
2020-07-04 22:40:41 +00:00
|
|
|
<div css={tw`text-xs font-mono relative`}>
|
2019-09-06 06:05:24 +00:00
|
|
|
<SpinnerOverlay visible={!connected} size={'large'}/>
|
|
|
|
<div
|
2020-07-04 22:40:41 +00:00
|
|
|
css={[
|
|
|
|
tw`rounded-t p-2 bg-black w-full`,
|
|
|
|
!canSendCommands && tw`rounded-b`,
|
|
|
|
]}
|
2019-09-06 06:05:24 +00:00
|
|
|
style={{
|
|
|
|
minHeight: '16rem',
|
2019-09-18 04:59:35 +00:00
|
|
|
maxHeight: '32rem',
|
2019-09-06 06:05:24 +00:00
|
|
|
}}
|
|
|
|
>
|
2020-10-04 02:21:11 +00:00
|
|
|
<TerminalDiv id={'terminal'} ref={ref}/>
|
2019-09-06 06:05:24 +00:00
|
|
|
</div>
|
2020-03-30 05:12:50 +00:00
|
|
|
{canSendCommands &&
|
2020-07-04 22:40:41 +00:00
|
|
|
<div css={tw`rounded-b bg-neutral-900 text-neutral-100 flex`}>
|
|
|
|
<div css={tw`flex-shrink-0 p-2 font-bold`}>$</div>
|
|
|
|
<div css={tw`w-full`}>
|
2019-09-18 05:54:23 +00:00
|
|
|
<input
|
|
|
|
type={'text'}
|
|
|
|
disabled={!instance || !connected}
|
2020-07-04 22:40:41 +00:00
|
|
|
css={tw`bg-transparent text-neutral-100 p-2 pl-0 w-full`}
|
2020-11-09 07:34:56 +00:00
|
|
|
onKeyDown={handleCommandKeyDown}
|
2019-09-18 05:54:23 +00:00
|
|
|
/>
|
2019-06-29 23:14:32 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2020-03-30 05:12:50 +00:00
|
|
|
}
|
2019-09-06 06:05:24 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|