Skip to content

Commit

Permalink
Style code to match existing format.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericbarch committed Apr 23, 2018
1 parent 72461af commit 6b4656c
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 83 deletions.
32 changes: 20 additions & 12 deletions README.md
Expand Up @@ -4,7 +4,7 @@ Tunnel HTTP (and websocket!) connections via socket.io streams. Inspired by [loc

## Blog Post

https://ericbarch.com/post/sockettunnel/
[Read all about it](https://ericbarch.com/post/sockettunnel/)

## Server Usage

Expand All @@ -29,27 +29,35 @@ Assuming a web server running on 127.0.0.1:8000

1. Clone this repo into your project
2. `npm i`
3. In your project file require socket-tunnel api and run connect():
3. In your project file, require the socket-tunnel api and call connect():

```JavaScript
const socketTunnel = require('./socket-tunnel/lib/api')
const socketTunnel = require('./socket-tunnel/lib/api');

socketTunnel.connect("http://YOURDOMAIN.com", "YOURSUBDOMAIN", "8000",)
.then(console.log)
.catch(console.log)
socketTunnel.connect('http://YOURDOMAIN.com', 'YOURSUBDOMAIN', '8000')
.then(console.log)
.catch(console.log);
```
4. Browse to http://YOURSUBDOMAIN.YOURDOMAIN.com to see your local service available on the public internet

## Client API Parameters

`socketTunnel.connect()` returns a promise which resolves to the requested URL/subdomain

```Javascript
// running socketTunnel.connect() returns a promise which resolves to the requested url or catches any errors. Both return strings.
// options:
// your server domain name, subdomain to request, port of local server, hostname of local server (optional. defaults to 127.0.0.1)
socketTunnel.connect(server, subdomain, port)
```JavaScript
socketTunnel.connect(remoteServer, desiredSubdomain, localPort, localHostname);
```

| Property | Default | Description |
|------------------|-------------|----------------------------------------------------|
| remoteServer | n/a | IP address or hostname of the socket-tunnel server |
| desiredSubdomain | n/a | Subdomain to request for this client |
| localPort | n/a | Local port to tunnel to |
| localHostname | '127.0.0.1' | Local host to tunnel to |

## Credits

Created by Eric Barch.
Created by Eric Barch. Additional code provided by [these generous contributors](https://github.com/ericbarch/socket-tunnel/graphs/contributors).

## License

Expand Down
5 changes: 0 additions & 5 deletions api-example.js

This file was deleted.

98 changes: 48 additions & 50 deletions client.js
@@ -1,63 +1,61 @@
'use strict';

module.exports = (options, success = console.log, err = console.log) => {
// require the things we need
const net = require('net');
const ss = require('socket.io-stream');
let socket = require('socket.io-client')(options['server']);

socket.on('connect', () => {
console.log(new Date() + ': connected');
console.log(new Date() + ': requesting subdomain ' + options['subdomain'] + ' via ' + options['server']);

socket.emit('createTunnel', options['subdomain'], (err) => {
if (err) {
console.log(new Date() + ': [error] ' + err);

// send error to callback
err(err);
} else {
console.log(new Date() + ': registered with server successfully');

// clean and concat requested url. send url success to callback
let url = ()=>{
let subdomain = options['subdomain'].toString()
let server = options['server'].toString()

if (server.includes('https://')){
return `https://${subdomain}.${server.slice(8)}`
} else if (server.includes('http://')){
return `http://${subdomain}.${server.slice(7)}`
module.exports = (options) => {
return new Promise((resolve, reject) => {
// require the things we need
const net = require('net');
const ss = require('socket.io-stream');
let socket = require('socket.io-client')(options['server']);

socket.on('connect', () => {
console.log(new Date() + ': connected');
console.log(new Date() + ': requesting subdomain ' + options['subdomain'] + ' via ' + options['server']);

socket.emit('createTunnel', options['subdomain'], (err) => {
if (err) {
console.log(new Date() + ': [error] ' + err);

reject(err);
} else {
console.log(new Date() + ': registered with server successfully');

// clean and concat requested url
let url;
let subdomain = options['subdomain'].toString();
let server = options['server'].toString();

if (server.includes('https://')) {
url = `https://${subdomain}.${server.slice(8)}`;
} else if (server.includes('http://')) {
url = `http://${subdomain}.${server.slice(7)}`;
} else {
return `https://${subdomain}.${server}`
url = `https://${subdomain}.${server}`;
}

};

success(url());
};

// resolve promise with requested URL
resolve(url);
}
});
});
});

socket.on('incomingClient', (clientId) => {
let ref;
let client = net.connect(options['port'], options['hostname'], () => {
let s = ss.createStream();
s.pipe(client).pipe(s);
socket.on('incomingClient', (clientId) => {
let client = net.connect(options['port'], options['hostname'], () => {
let s = ss.createStream();
s.pipe(client).pipe(s);

s.on('end', () => {
client.destroy();
});
s.on('end', () => {
client.destroy();
});

ss(socket).emit(clientId, s);
});
ss(socket).emit(clientId, s);
});

client.on('error', () => {
// handle connection refusal (create a stream and immediately close it)
let s = ss.createStream();
ss(socket).emit(clientId, s);
s.end();
client.on('error', () => {
// handle connection refusal (create a stream and immediately close it)
let s = ss.createStream();
ss(socket).emit(clientId, s);
s.end();
});
});
});
};
9 changes: 9 additions & 0 deletions examples/api.js
@@ -0,0 +1,9 @@
const socketTunnel = require('../lib/api');

socketTunnel.connect('https://domain.example', 'deviceSubdomain', '2222')
.then((url) => {
console.log(url);
})
.catch((err) => {
console.error(err);
});
30 changes: 16 additions & 14 deletions lib/api.js
@@ -1,20 +1,22 @@
// client api

const client = require("../client")
const client = require('../client');

let api = {
connect: (server, subdomain, port, hostname = '127.0.0.1' )=>{
return new Promise((res, rej)=>{
if (!server || !subdomain || !port || !hostname) rej('0ne or more options were not provided');
let options = {
server: server,
subdomain: subdomain.toString(),
port: port.toString(),
hostname: hostname
};
client(options, res, rej);
})
connect: (server, subdomain, port, hostname = '127.0.0.1') => {
if (!server || !subdomain || !port || !hostname) {
return Promise.reject(new Error('One or more options were not provided'));
}

let options = {
server: server,
subdomain: subdomain.toString(),
port: port.toString(),
hostname: hostname
};

// client returns a promise
return client(options);
}
};

module.exports = api;
module.exports = api;
2 changes: 1 addition & 1 deletion nginx.conf.sample
Expand Up @@ -3,7 +3,7 @@
server {
listen *:80;
server_name subdomain.example.com *.subdomain.example.com;
rewrite ^ https://$host$request_uri? permanent;
rewrite ^ https://$host$request_uri? permanent;
}

server {
Expand Down
2 changes: 1 addition & 1 deletion server.js
Expand Up @@ -146,7 +146,7 @@ module.exports = (options) => {
}

// domains are case insensitive
let reqNameNormalized = requestedName.toString().toLowerCase().replace(/[^0-9a-z-]/g,"");
let reqNameNormalized = requestedName.toString().toLowerCase().replace(/[^0-9a-z-]/g, '');

// make sure the client is requesting a valid subdomain
if (reqNameNormalized.length === 0 || !isValidDomain(`${reqNameNormalized}.example.com`)) {
Expand Down

0 comments on commit 6b4656c

Please sign in to comment.