@bbk47/toolbox 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -11,5 +11,6 @@ A tool library for bbk/abc
11
11
  - aes encrypt
12
12
  - logger util
13
13
  - md5 encrypt
14
- - socks5 addr parse
14
+ - socks5 addr parse and build
15
15
  - uuid util
16
+ - merge object
package/lib/encrypt.js CHANGED
@@ -63,7 +63,7 @@ Encryptor.prototype.get_cipher_len = function (method) {
63
63
 
64
64
  Encryptor.prototype.getKeyIV = function (password, method) {
65
65
  method = method.toLowerCase();
66
- password = Buffer.from(password, "binary");
66
+ password = Buffer.from(password, "ascii");
67
67
  var m = this.get_cipher_len(method);
68
68
  var _ref = EVP_BytesToKey(password, m[0], m[1]);
69
69
  return { key: _ref[0], iv: _ref[1] };
package/lib/merge.js ADDED
@@ -0,0 +1,29 @@
1
+ function factory(overide) {
2
+ return function assign(target) {
3
+ target = target || {};
4
+ var args = [].slice.call(arguments, 1);
5
+ args.forEach((temp) => {
6
+ if (temp) {
7
+ Object.keys(temp).forEach((key) => {
8
+ if (overide) {
9
+ target[key] = temp[key];
10
+ } else if (temp[key]) {
11
+ target[key] = temp[key];
12
+ }
13
+ });
14
+ }
15
+ });
16
+ return target;
17
+ };
18
+ }
19
+
20
+ function merge(target) {
21
+ return factory(false).apply(target, arguments);
22
+ }
23
+
24
+ function assign(target) {
25
+ return factory(true).apply(target, arguments);
26
+ }
27
+
28
+ exports.objectMerge = merge;
29
+ exports.objectAssign = assign;
package/lib/socks5.js CHANGED
@@ -1,4 +1,4 @@
1
- exports.parseAddrInfo = function (buf, offset) {
1
+ function parseSocks5Addr(buf, offset) {
2
2
  if (offset !== 0) {
3
3
  buf = buf.slice(offset);
4
4
  }
@@ -20,4 +20,30 @@ exports.parseAddrInfo = function (buf, offset) {
20
20
  dstPort = port[0] * 256 + port[1];
21
21
  }
22
22
  return { dstAddr, dstPort };
23
- };
23
+ }
24
+
25
+ function buildSocks5Addr(hostname, port) {
26
+ var socksAddrInfoBuf;
27
+ if (/^(\d+\.){3}\d+$/.test(hostname)) {
28
+ const parts = hostname.split('.').map((p) => Number(p));
29
+ const portBuf = Buffer.from([parseInt(port / 256), port % 256]);
30
+ const preBuf = Buffer.from([0x01, parts[0], parts[1], parts[2], parts[3]]);
31
+ socksAddrInfoBuf = Buffer.concat([preBuf, portBuf]);
32
+ } else {
33
+ const domain = hostname;
34
+ const preBuf = Buffer.from([0x03, domain.length]);
35
+ const domainBuf = Buffer.from(domain);
36
+ const portBuf = Buffer.from([parseInt(port / 256), port % 256]);
37
+ socksAddrInfoBuf = Buffer.concat([preBuf, domainBuf, portBuf]);
38
+ }
39
+ return socksAddrInfoBuf;
40
+ }
41
+
42
+ exports.parseAddrInfo = parseSocks5Addr;
43
+ exports.parseSocks5Addr = parseSocks5Addr;
44
+ exports.buildSocks5Addr = buildSocks5Addr;
45
+
46
+ // console.log(buildSocks5Addr('1.2.4.8', 8899).toString('hex'));
47
+ // console.log(buildSocks5Addr('181.58.189.23', 5478).toString('hex'));
48
+ // console.log(buildSocks5Addr('www.baidu.com', 1099).toString('hex'));
49
+ // console.log(buildSocks5Addr('kefdka.1231.a123baidu.com', 65534).toString('hex'));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbk47/toolbox",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "toolbox for bbk",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
package/test/encrypt.js CHANGED
@@ -7,9 +7,6 @@ describe('encrypt', function () {
7
7
  const worker = new encrypt.Encryptor('csii2019', 'aes-256-cfb');
8
8
 
9
9
  const result1 = worker.encrypt(Buffer.from('hello,world'));
10
- const result12 = worker.encrypt(Buffer.from('hello,world'));
11
- console.log(result1)
12
- console.log(result2)
13
10
  const result2 = worker.encrypt(Buffer.from('818c9ba2109b4c505155aebf6d3647a3'));
14
11
  const result3 = worker.encrypt(Buffer.from('818c9ba2109b4c505155aebf6d3647a2'));
15
12
 
package/test/merge.js ADDED
@@ -0,0 +1,17 @@
1
+ const assert = require('assert');
2
+ const merge = require('../lib/merge');
3
+ const { describe } = require('mocha');
4
+
5
+ describe('socks5', function () {
6
+ it('test object merge', function () {
7
+ var obj = merge.objectMerge({ sss: 1 }, { sss: 123 }, { bb: 456 }, { sss: null });
8
+ assert.equal(obj.sss, 123);
9
+ assert.equal(obj.bb, 456);
10
+ });
11
+
12
+ it('test object assign', function () {
13
+ var obj = merge.objectAssign({ sss: 12 }, { sss: 123 }, { bb: 456 }, { sss: null ,bb:789});
14
+ assert.equal(obj.sss, null);
15
+ assert.equal(obj.bb, 789);
16
+ });
17
+ });
package/test/socks5.js CHANGED
@@ -3,21 +3,40 @@ const socks5 = require('../lib/socks5');
3
3
  const { describe } = require('mocha');
4
4
 
5
5
  describe('socks5', function () {
6
- it('test socks5 ipv4 example', function () {
7
- const obj1 = { type: 1, addr: '1.2.4.8' };
8
- const buf = Buffer.from([0x01, 0x01, 0x02, 0x04, 0x08, 0x00, 0x50]);
9
- const obj = socks5.parseAddrInfo(buf);
10
- assert.equal(obj.dstAddr, '1.2.4.8');
11
- assert.equal(obj.dstPort, 80);
6
+
7
+ it('test socks5 parse ip example', function () {
8
+ const obj1 = socks5.parseSocks5Addr(Buffer.from('010102040822c3', 'hex'));
9
+ assert.equal(obj1.dstAddr, '1.2.4.8');
10
+ assert.equal(obj1.dstPort, 8899);
11
+
12
+ const obj2 = socks5.parseSocks5Addr(Buffer.from('01b53abd171566', 'hex'));
13
+ assert.equal(obj2.dstAddr, '181.58.189.23');
14
+ assert.equal(obj2.dstPort, 5478);
15
+ });
16
+
17
+ it('test socks5 parse domain example', function () {
18
+ const obj1 = socks5.parseSocks5Addr(Buffer.from('030d7777772e62616964752e636f6d044b', 'hex'));
19
+ assert.equal(obj1.dstAddr, 'www.baidu.com');
20
+ assert.equal(obj1.dstPort, 1099);
21
+
22
+ const obj2 = socks5.parseSocks5Addr(Buffer.from('03196b6566646b612e313233312e6131323362616964752e636f6dfffe', 'hex'));
23
+ assert.equal(obj2.dstAddr, 'kefdka.1231.a123baidu.com');
24
+ assert.equal(obj2.dstPort, 65534);
25
+ });
26
+
27
+ it('test socks5 build ipv4 example', function () {
28
+ const buf1 = socks5.buildSocks5Addr("1.2.4.8",8899);
29
+ assert.equal(buf1.toString('hex'), '010102040822c3');
30
+ const buf2 = socks5.buildSocks5Addr("181.58.189.23",5478);
31
+ assert.equal(buf2.toString('hex'), '01b53abd171566');
12
32
  });
13
33
 
14
- it('test socks5 domain example', function () {
15
- const obj1 = { type: 3, addr: 'www.baidu.com' };
16
- const buf1 = Buffer.from([0x03, 0x0d]);
17
- const buf = Buffer.concat([buf1, Buffer.from('www.baidu.com'), Buffer.from([0x00, 0x17])]);
18
- const obj = socks5.parseAddrInfo(buf);
19
- // console.log(obj);
20
- assert.equal(obj.dstAddr, 'www.baidu.com');
21
- assert.equal(obj.dstPort, 23);
34
+ it('test socks5 build domain example', function () {
35
+ const buf1 = socks5.buildSocks5Addr("www.baidu.com",1099);
36
+ assert.equal(buf1.toString('hex'), '030d7777772e62616964752e636f6d044b');
37
+ const buf2 = socks5.buildSocks5Addr("kefdka.1231.a123baidu.com",65534);
38
+ assert.equal(buf2.toString('hex'), '03196b6566646b612e313233312e6131323362616964752e636f6dfffe');
22
39
  });
40
+
41
+
23
42
  });
package/ss.js DELETED
@@ -1,8 +0,0 @@
1
- const encrypt = require('./lib/encrypt')
2
-
3
- const worker = new encrypt.Encryptor('csii2019', 'aes-256-cfb');
4
-
5
- const result1 = worker.encrypt(Buffer.from('hello,world'));
6
- const result12 = worker.encrypt(Buffer.from('hello,world'));
7
- console.log(result1)
8
- console.log(result12)