Skip to content

Commit

Permalink
add --file-type to select cwmp type
Browse files Browse the repository at this point in the history
  • Loading branch information
BoLaMN committed Jan 15, 2020
1 parent 7ce4f3f commit 8232ac2
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 41 deletions.
10 changes: 9 additions & 1 deletion dist/args.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
var snakeToCamel;

snakeToCamel = function(s) {
return s.replace(/(\-\w)/g, function(m) {
return m[1].toUpperCase();
});
};

module.exports = process.argv.slice(2, process.argv.length).reduce(function(args, str) {
var arg, flag, value;
arg = str.split('=');
flag = arg[0].slice(2, arg[0].length);
value = arg[1] || true;
args[flag] = value;
args[snakeToCamel(flag)] = value;
return args;
}, {});
23 changes: 18 additions & 5 deletions dist/http/cwmp/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict';
var createSoapEnv, device, env, file, finished, methods, parse, ref, request, response, set, stage;
var args, createSoapEnv, device, env, file, fileTypes, finished, methods, parse, ref, request, response, set, stage;

ref = require('./xml'), parse = ref.parse, methods = ref.methods, createSoapEnv = ref.createSoapEnv;
ref = require('./xml'), parse = ref.parse, methods = ref.methods, createSoapEnv = ref.createSoapEnv, fileTypes = ref.fileTypes;

file = require('../file');

args = require('../../args');

stage = null;

device = {};
Expand Down Expand Up @@ -75,9 +77,20 @@ request = function(url, req, res) {
console.log('>>> EMPTY REQUEST');
console.dir([req.headers, req.body]);
res.name = 'cwmp:Download';
res.fileType = '3 Vendor Configuration File';
res.fileSize = file.sts.length;
res.url = url + "file.sts";
if (args.fileType && (fileTypes[args.fileType] != null)) {
res.fileType = fileTypes[args.fileType];
} else {
res.fileType = (function() {
switch (file.ext) {
case '.rbi':
return '1 Firmware Upgrade Image';
case '.sts':
return '3 Vendor Configuration File';
}
})();
}
res.fileSize = file.data.length;
res.url = "" + url + file.name;
}
res.env = env.join(' ');
return response(res);
Expand Down
8 changes: 8 additions & 0 deletions dist/http/cwmp/xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,11 @@ exports.methods = methods = {
exports.createSoapEnv = function(res, headers) {
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<soap-env:Envelope " + (res.env || '') + ">\n <soap-env:Header>\n <cwmp:ID soap-env:mustUnderstand=\"1\">" + res.id + "</cwmp:ID>\n </soap-env:Header>\n <soap-env:Body>\n " + (methods[res.name](res, headers)) + "\n </soap-env:Body>\n</soap-env:Envelope>";
};

exports.fileTypes = {
1: '1 Firmware Upgrade Image',
2: '2 Web Content',
3: '3 Vendor Configuration File',
4: '4 Tone File',
5: '5 Ringer File'
};
3 changes: 2 additions & 1 deletion dist/http/file.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 13 additions & 28 deletions dist/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,30 @@ cwmp = require('./cwmp');

module.exports = function(ip, port, url) {
var e, srv;
if (args.sts) {
if (args.file) {
file.name = path.basename(args.file);
try {
file.sts = readFileSync(args.sts);
file.data = readFileSync(args.file);
} catch (error) {
e = error;
throw e;
}
}
file.sts = Buffer.from(file.sts).toString('utf8').replace('{{url}}', url, 'utf8');
route.get('/file.sts', function(req, res) {
var headers, stream;
console.log('>>> STS REQUEST');
file.data = Buffer.from(file.data).toString('utf8').replace('{{url}}', url, 'utf8');
file.ext = path.extname(file.name);
route.get("/" + file.name, function(req, res) {
var ext, headers, stream;
ext = file.ext.toUpperCase();
console.log(">>> " + ext + " REQUEST");
headers = {
'Content-Type': 'text/plain',
'Content-Length': file.sts.length
'Content-Length': file.data.length
};
console.log('>>> STS RESPONSE');
console.dir([headers, file.sts.toString('utf8')]);
console.log('>>> #{ ext } RESPONSE');
console.dir([headers, file.data.toString('utf8')]);
res.writeHead(200, headers);
stream = new Duplex();
stream.push(file.sts);
stream.push(file.data);
stream.push(null);
return stream.pipe(res);
}).get('/done', function(req, res) {
Expand All @@ -49,24 +52,6 @@ module.exports = function(ip, port, url) {
}, 20000);
res.writeHead(200);
return res.end();
}).get('/{rbi}(.*?).rbi', function(req, res) {
var fp, headers, stats, stream;
console.log('>>> RBI REQUEST');
fp = path.join(process.cwd(), req.params.rbi, '.rbi');
if (existsSync(fp)) {
stats = statSync(fp);
headers = {
'Content-Type': 'text/plain',
'Content-Length': stats.size
};
stream = createReadStream(fp);
console.log('>>> RBI RESPONSE');
console.dir([headers, fp]);
res.writeHead(200, headers);
return stream.pipe(res);
} else {
return res.writeHead(404);
}
}).post('/', cwmp(url));
srv = createServer(route);
srv.keepAliveTimeout = 30000;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tch-exploit",
"version": "2.0.1-rc6",
"version": "2.0.1-rc7",
"main": "dist/index.js",
"bin": "dist/index.js",
"scripts": {
Expand Down
5 changes: 4 additions & 1 deletion src/args.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
snakeToCamel = (s) ->
s.replace /(\-\w)/g, (m) ->
m[1].toUpperCase()

module.exports = process.argv
.slice 2, process.argv.length
Expand All @@ -6,6 +9,6 @@ module.exports = process.argv
flag = arg[0].slice 2, arg[0].length
value = arg[1] or true

args[flag] = value
args[snakeToCamel(flag)] = value
args
, {}
12 changes: 8 additions & 4 deletions src/http/cwmp/index.coffee
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict'

{ parse, methods, createSoapEnv } = require './xml'
{ parse, methods, createSoapEnv, fileTypes } = require './xml'

file = require '../file'
args = require '../../args'

stage = null
device = {}
Expand Down Expand Up @@ -70,9 +71,12 @@ request = (url, req, res) ->

res.name = 'cwmp:Download'

res.fileType = switch file.ext
when '.rbi' then '1 Firmware Upgrade Image'
when '.sts' then '3 Vendor Configuration File'
if args.fileType and fileTypes[args.fileType]?
res.fileType = fileTypes[args.fileType]
else
res.fileType = switch file.ext
when '.rbi' then '1 Firmware Upgrade Image'
when '.sts' then '3 Vendor Configuration File'

res.fileSize = file.data.length
res.url = "#{ url }#{file.name}"
Expand Down
7 changes: 7 additions & 0 deletions src/http/cwmp/xml.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,10 @@ exports.createSoapEnv = (res, headers) ->
#{ methods[res.name] res, headers }
</soap-env:Body>
</soap-env:Envelope>"""

exports.fileTypes =
1: '1 Firmware Upgrade Image'
2: '2 Web Content'
3: '3 Vendor Configuration File'
4: '4 Tone File'
5: '5 Ringer File'

0 comments on commit 8232ac2

Please sign in to comment.