Skip to content

Instantly share code, notes, and snippets.

@davistran86
Forked from mandulaj/index.html
Created September 18, 2019 07:47
Show Gist options
  • Save davistran86/5132d024cadcc5d67082d9d3a40b76bd to your computer and use it in GitHub Desktop.
Save davistran86/5132d024cadcc5d67082d9d3a40b76bd to your computer and use it in GitHub Desktop.
Webssh
<!doctype html>
<html>
<head>
<title>SSH Client</title>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.5/socket.io.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<style>
body {
margin: 0;
padding: 0;
}
.terminal {
font-family: monospace;
color: white;
background: black;
}
</style>
</head>
<body>
<h1>SSH</h1>
<div class="terminal">
</div>
<script>
// Connect to the socket.io server
var socket = io.connect('http://localhost:8080');
// Wait for data from the server
socket.on('output', function (data) {
// Insert some line breaks where they belong
data = data.replace("\n", "<br>");
data = data.replace("\r", "<br>");
// Append the data to our terminal
$('.terminal').append(data);
});
// Listen for user input and pass it to the server
$(document).on("keypress",function(e){
var char = "";
char = String.fromCharCode(e.which);
socket.emit("input", char);
});
</script>
</body>
</html>
{
"name": "WebSSH",
"version": "1.0.0",
"description": "",
"main": "server.js",
"dependencies": {
"express": "^4.12.3",
"pty.js": "^0.2.7-1",
"socket.io": "^1.3.5"
},
"devDependencies": {},
"scripts": {
"start": "node server.js"
},
"author": "Jakub Mandula<[email protected]",
"license": "MIT"
}
var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');
var pty = require('pty.js');
// Setup the express app
var app = express();
// HTTPS key and certificate files
var options = {
key: fs.readFileSync('keys/key.pem'),
cert: fs.readFileSync('keys/cert.pem')
};
// Create Server using the app and bind it to a port
//https.createServer(options, app).listen(4000)
var server = http.createServer(app).listen(8080);
// Static file serving
app.use("/",express.static("./"));
// Bind socket.io to the server
var io = require('socket.io')(server);
// When a new socket connects
io.on('connection', function(socket){
// Create terminal
var term = pty.spawn('sh', [], {
name: 'xterm-color',
cols: 80,
rows: 30,
cwd: process.env.HOME,
env: process.env
});
// Listen on the terminal for output and send it to the client
term.on('data', function(data){
socket.emit('output', data);
});
// Listen on the client and send any input to the terminal
socket.on('input', function(data){
term.write(data);
});
// Listen for a resize request and update the terminal size
socket.on('resize', function(data){
term.resize(data[0], data[1]);
});
// When socket disconnects, destroy the terminal
socket.on("disconnect", function(){
term.destroy();
console.log("bye");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment