Coder Social home page Coder Social logo

tcp-local-tunnel's Introduction

tcp-local-tunnel

Expose localhost to the Internet using TCP sockets!


tcp-local-tunnel version tcp-local-tunnel license tcp-local-tunnel PRs welcome

About

Simple module that allows to expose server from local network to the Internet. It works similarly to localtunnel, but is more stable and simpler.

I'm not providing a service where you can access your exposed local server, you will need to have some VPS or another machine connected to the internet with at least two open ports.

Module creates connection from your local server to your remote server via TCP and transmits data in both ways with established tunnel.

This is extremely useful when you need to access an IoT device (Raspberry PI i.e) running in your home network. For example you can turn off light (that you forgot to turn off, or just to make sure) in home from your workplace or any other place on Earth.

I'm also working on NodeMCU implementation of client side module (ESP8266 devices)

Module supports any protocol that uses TCP, eg. HTTP, Websocket.

The stability mostly depends on your local internet connection quality.

Security caveats

If somebody knows host and port of your remote server which local machine connects to, then he also can creates tunnel and redirect remote requests to his machine instead of yours.

Tunnel connection is not encrypted, so potential attacker can read data you are sending.

See alternatives for more secure solutions.

Basic-usage

npm install tcp-local-tunnel

Let's say your local server is listening on 3000

Code to run on local machine

const { client } = require('tcp-local-tunnel');
client(
  {
    host: '255.255.255.255', // remote server ip or domain
    port: 8010 // tunnel port
  },
  {
    host: 'localhost',
    port: 3000
  }
);

Code to run on remote machine

const { proxyServer } = require('tcp-local-tunnel');

proxyServer({
  proxyPort: 80, // remote port to access exposed local machine
  tunnelPort: 8010 // tunnel port
});

Run server and expose it immediately (code in ./example)

Code to run on local machine

const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');

const { client } = require('../index.js');

/* simple express server showcase */

const serverPort = 3000;

const server = express();

server.use(bodyParser.json());
server.use(bodyParser.urlencoded());

server.get('/someurl', function(req, res, next) {
  res.send('some url page');
});
server.get('/video.mp4', (req, res) => {
  res.header('Content-Type', 'video/mp4');
  fs.createReadStream('neverGonna.mp4').pipe(res);
});
server.get('/long.json', (req, res) => {
  const file = fs.readFileSync('long.json');
  res.header('Content-Length', Buffer.byteLength(file));
  console.log('contetn length', Buffer.byteLength(file));
  res.json(file);
});
server.get('/', function(req, res, next) {
  res.send('Main page');
});

server.listen(serverPort);

/* tcp tunnel config */

client(
  {
    host: '255.255.255.255',
    port: 8010
  },
  {
    host: 'localhost',
    port: serverPort
  },
  40 // number of concurrent open tunnels
);

Code to run on remote machine

const { proxyServer } = require('../index.js');

/* internet server proxy configuration */

proxyServer({
  proxyPort: 80,
  tunnelPort: 8010
});

API

Client (local side)

const { client } = require('tcp-local-tunnel');

client(
  {
    host: '255.255.255', // remote server host or domain
    port: 8010 // remote server tunnel port
  },
  {
    host: 'localhost', // local server host or ip
    port: 3000 // local server port
  },
  10 // number of concurent open tunnels, default is 10
);

ProxyServer (remote side)

const { proxyServer } = require('tcp-local-tunnel')

proxyServer({
  proxyPort: 80, // remote port to access exposed local machine
    tunnelPort: 8010, // tunnel port
    timeout : 5000 // time after request is rejected when there are no tunnel connections
})

Alternatives

You can use SSH remote port forwarding to achieve the same. It's encrypted and hence more secure.

See Remote Forwarding chapter https://www.ssh.com/ssh/tunneling/example

Contributing

Project is open to contributions, just rise an issue if you have some ideas about features or you noticed a bug. After discussion we can approach implementation :)

Made with ๐Ÿง  by @jayu

I hope that this small piece of software will help you build fancy IoT systems. If this tool was useful, don't hesitate to give it a ๐ŸŒŸ!

License

MIT

tcp-local-tunnel's People

Contributors

jayu avatar jpacora avatar uriva avatar deluxor avatar aniruddha-chakraborty avatar thiagopromano avatar dependabot[bot] avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.