mirror of
				https://git.proxmox.com/git/mirror_xterm.js
				synced 2025-10-31 22:47:18 +00:00 
			
		
		
		
	 f57a40eeb3
			
		
	
	
		f57a40eeb3
		
			
		
	
	
	
	
		
			
			- Used fetch tentatively - Implemented resize endpoint - Create terminals with the appropriate size
		
			
				
	
	
		
			86 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			86 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| var express = require('express');
 | |
| var app = express();
 | |
| var expressWs = require('express-ws')(app);
 | |
| var os = require('os');
 | |
| var pty = require('pty.js');
 | |
| 
 | |
| var terminals = {},
 | |
|     logs = {};
 | |
| 
 | |
| app.use('/src', express.static(__dirname + '/../src'));
 | |
| app.use('/addons', express.static(__dirname + '/../addons'));
 | |
| 
 | |
| app.get('/', function(req, res){
 | |
|   res.sendFile(__dirname + '/index.html');
 | |
| });
 | |
| 
 | |
| app.get('/style.css', function(req, res){
 | |
|   res.sendFile(__dirname + '/style.css');
 | |
| });
 | |
| 
 | |
| app.get('/main.js', function(req, res){
 | |
|   res.sendFile(__dirname + '/main.js');
 | |
| });
 | |
| 
 | |
| app.post('/terminals', function (req, res) {
 | |
|   var cols = parseInt(req.query.cols),
 | |
|       rows = parseInt(req.query.rows),
 | |
|       term = pty.spawn(process.platform === 'win32' ? 'cmd.exe' : 'bash', [], {
 | |
|         name: 'xterm-color',
 | |
|         cols: cols || 80,
 | |
|         rows: rows || 24,
 | |
|         cwd: process.env.PWD,
 | |
|         env: process.env
 | |
|       });
 | |
| 
 | |
|   console.log('Created terminal with PID: ' + term.pid);
 | |
|   terminals[term.pid] = term;
 | |
|   logs[term.pid] = '';
 | |
|   term.on('data', function(data) {
 | |
|     logs[term.pid] += data;
 | |
|   });
 | |
|   res.send(term.pid.toString());
 | |
|   res.end();
 | |
| });
 | |
| 
 | |
| app.post('/terminals/:pid/size', function (req, res) {
 | |
|   var pid = parseInt(req.params.pid),
 | |
|       cols = parseInt(req.query.cols),
 | |
|       rows = parseInt(req.query.rows),
 | |
|       term = terminals[pid];
 | |
| 
 | |
|   term.resize(cols, rows);
 | |
|   console.log('Resized terminal ' + pid + ' to ' + cols + ' cols and ' + rows + ' rows.');
 | |
|   res.end();
 | |
| });
 | |
| 
 | |
| app.ws('/terminals/:pid', function (ws, req) {
 | |
|   var term = terminals[parseInt(req.params.pid)];
 | |
|   console.log('Connected to terminal ' + term.pid);
 | |
|   ws.send(logs[term.pid]);
 | |
| 
 | |
|   term.on('data', function(data) {
 | |
|     try {
 | |
|       ws.send(data);
 | |
|     } catch (ex) {
 | |
|       // The WebSocket is not open, ignore
 | |
|     }
 | |
|   });
 | |
|   ws.on('message', function(msg) {
 | |
|     term.write(msg);
 | |
|   });
 | |
|   ws.on('close', function () {
 | |
|     process.kill(term.pid);
 | |
|     console.log('Closed terminal ' + term.pid);
 | |
|     // Clean things up
 | |
|     delete terminals[term.pid];
 | |
|     delete logs[term.pid];
 | |
|   });
 | |
| });
 | |
| 
 | |
| var port = process.env.PORT || 3000,
 | |
|     host = os.platform() === 'win32' ? '127.0.0.1' : '0.0.0.0';
 | |
| 
 | |
| console.log('App listening to http://' + host + ':' + port);
 | |
| app.listen(port, host);
 |