You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
4.4 KiB

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = _default;
exports.resizeTerminal = exports.getDefaultShell = void 0;
function _react() {
const data = _interopRequireDefault(require("react"));
_react = function _react() {
return data;
};
return data;
}
function _sockjs() {
const data = _interopRequireDefault(require("sockjs"));
_sockjs = function _sockjs() {
return data;
};
return data;
}
function _get() {
const data = _interopRequireDefault(require("lodash/get"));
_get = function _get() {
return data;
};
return data;
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
let term;
/**
* get user default shell
* /bin/zsh /bin/bash
*/
const getDefaultShell = () => {
if (process.platform === 'darwin') {
return process.env.SHELL || '/bin/bash';
}
if (process.platform === 'win32') {
return process.env.COMSPEC || 'cmd.exe';
}
return process.env.SHELL || '/bin/sh';
};
/**
* Security Check
*
*/
exports.getDefaultShell = getDefaultShell;
const securityCheck = conn => {
if (process.env.HOST === '0.0.0.0') {
conn.write('The current environment is not safe.');
return false;
}
return true;
};
const resizeTerminal = opts => {
const cols = opts.cols,
rows = opts.rows;
if (term && cols && rows) {
term.resize(cols, rows);
}
};
/**
* export terminal socket init needs bind express app server
*/
exports.resizeTerminal = resizeTerminal;
function _default(server) {
const terminalSS = _sockjs().default.createServer();
terminalSS.on('connection', conn => {
const _this$config$data = this.config.data,
currentProject = _this$config$data.currentProject,
projectsByKey = _this$config$data.projectsByKey;
const currentProjectCwd = (0, _get().default)(projectsByKey, `${currentProject}.path`);
const cwd = currentProjectCwd || this.cwd || process.cwd(); // insecurity env to run shell
const safe = securityCheck(conn);
let spawn;
try {
// eslint-disable-next-line prefer-destructuring
spawn = require('node-pty').spawn;
} catch (e) {
conn.write('Failed to install or prebuild node-pty module, please see docs: https://umijs.org/guide/faq.html#terminal-need-node-pty-module');
return false;
}
if (safe) {
const defaultShell = getDefaultShell();
const defaultShellArgs = ['--login'];
term = spawn(defaultShell, defaultShellArgs, {
name: 'xterm-color',
cols: 180,
rows: 30,
cwd,
env: _objectSpread(_objectSpread({}, process.env), {}, {
// LANG: `${osLocaleSync()}.UTF-8`,
TERM: 'xterm-256color',
COLORTERM: 'truecolor'
})
});
/**
* stringify command shell string
* @param command ls/... shell commands
*/
term.onData(chunk => {
// _log('ptyProcess data', chunk);
conn.write(chunk);
}); // === socket listener ===
conn.on('data', data => {
// _log('terminal conn message', data);
term.write(data);
});
conn.on('close', () => {
// maybe change the pty cwd
term.kill();
});
}
});
terminalSS.installHandlers(server, {
prefix: '/terminal-socket',
log: () => {}
});
}