Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Style code to match existing format.
- Loading branch information
Showing
7 changed files
with
95 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters