npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tcp-local-tunnel

v1.0.6

Published

Expose localhost to the Internet using TCP sockets

Downloads

136

Readme


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