I’m working on a project using AWS Lightsail instance. The goal is to allow users to expose their local applications to the web without requiring SSH key authentication, Just like Serveo.net and localhost.run.
What I have :
domain name
Server: AWS Lightsail instance running Ubuntu 22.04 LTS
What I want :
When the user runs ssh -R 80:localhost:3000 mydomain.com
He should get a random Public Url (id.domain.com)
Any links wheere I can start ?
I tried Installing nginx
sudo apt update
sudo apt install nginx
sudo nano /etc/nginx/sites-available/default
server {
listen 80;
server_name *.mydomain.com;
location / {
proxy_pass http://localhost:3000; # Change to your internal service port
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
And nodejs express to handle the requests
const express = require('express');
const { NodeSSH } = require('node-ssh');
const app = express();
const ssh = new NodeSSH();
app.use(express.json());
app.post('/create-tunnel', (req, res) => {
const { user, remotePort } = req.body;
const localPort = 80; // The port your application runs on
ssh.connect({
host: 'localhost',
username: user,
// Use password authentication (you may need to handle this securely)
password: 'user_password',
}).then(() => {
ssh.forwardOut(
'127.0.0.1',
localPort,
'127.0.0.1',
remotePort,
(err, stream) => {
if (err) return res.status(500).send('Error creating tunnel.');
res.send(`Tunnel created for user: ${user}`);
}
);
}).catch(err => {
res.status(500).send('Error connecting via SSH: ' + err);
});
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
Is there any good tutorials to achieve this goal ?
6