send files over tcp

This commit is contained in:
AJ ONeal 2018-07-01 01:18:10 -06:00
parent 9a1e4e3b06
commit f0fa9c8615
1 changed files with 56 additions and 8 deletions

View File

@ -141,6 +141,40 @@ module.exports.assign = function (state, tun, cb) {
return conn; return conn;
} }
function errorTcp(conf, cb) {
var socketPair = require('socket-pair');
var conn = socketPair.create(function (err, other) {
if (err) { cb(err); return; }
cb(null, conn);
other.write("\n" +
[ "[Telebit Error Server]"
, "Could not load '" + conf.handler + "' as a module, file, or directory."
].join("\n") + "\n\n");
other.end();
});
//if (tun.data) { conn.write(tun.data); }
return conn;
}
function fileDirTcp(conf, cb) {
var socketPair = require('socket-pair');
var conn = socketPair.create(function (err, other) {
if (err) { cb(err); return; }
if (conf._stat.isFile()) {
fs.createReadStream(conf.handler).pipe(other);
} else {
fs.readdir(conf.handler, function (err, nodes) {
other.write('\n' + nodes.join('\n') + '\n\n');
other.end();
});
}
cb(null, conn);
});
//if (tun.data) { conn.write(tun.data); }
return conn;
}
function echoTcp(cb) { function echoTcp(cb) {
var socketPair = require('socket-pair'); var socketPair = require('socket-pair');
var conn = socketPair.create(function (err, other) { var conn = socketPair.create(function (err, other) {
@ -228,10 +262,12 @@ module.exports.assign = function (state, tun, cb) {
console.error("Failed to require('" + handlerpath + "'):", e1.message); console.error("Failed to require('" + handlerpath + "'):", e1.message);
console.error("Failed to require('" + path.join(localshare, handlerpath) + "'):", e2.message); console.error("Failed to require('" + path.join(localshare, handlerpath) + "'):", e2.message);
console.warn("Trying static and index handlers for '" + handle + ":" + id + "'"); console.warn("Trying static and index handlers for '" + handle + ":" + id + "'");
echoTcp(cb); handler = null;
return; // fallthru
} }
} }
if (handler) {
var socketPair = require('socket-pair'); var socketPair = require('socket-pair');
conn = socketPair.create(function (err, other) { conn = socketPair.create(function (err, other) {
handler(other, tun, id); handler(other, tun, id);
@ -239,6 +275,18 @@ module.exports.assign = function (state, tun, cb) {
}); });
return conn; return conn;
} }
fs.access(conf.handler, fs.constants.R_OK, function (err1) {
fs.stat(conf.handler, function (err2, stat) {
if (err1 || err2 && (stat.isFile() || stat.isDirectory())) {
errorTcp(conf, cb);
return;
}
conf._stat = stat;
fileDirTcp(conf, cb);
});
});
}
var handlerservers = {}; var handlerservers = {};
function invokeHandler(conf, tlsSocket, tun, id) { function invokeHandler(conf, tlsSocket, tun, id) {
if (parseInt(conf.handler, 10)) { if (parseInt(conf.handler, 10)) {
@ -289,7 +337,7 @@ module.exports.assign = function (state, tun, cb) {
} catch(e2) { } catch(e2) {
console.error("Failed to require('" + handlerpath + "'):", e1.message); console.error("Failed to require('" + handlerpath + "'):", e1.message);
console.error("Failed to require('" + path.join(localshare, handlerpath) + "'):", e2.message); console.error("Failed to require('" + path.join(localshare, handlerpath) + "'):", e2.message);
console.warn("Using default handler for '" + handle + ":" + id + "'"); console.warn("Trying static and index handlers for '" + handle + ":" + id + "'");
handler = null; handler = null;
// fallthru // fallthru
} }