Skip to content

Commit

Permalink
[SERVER] Create escape function, and test
Browse files Browse the repository at this point in the history
  • Loading branch information
jc authored and jc committed Jan 8, 2019
1 parent bfa2d1e commit 2caf499
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 112 deletions.
29 changes: 21 additions & 8 deletions server/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { readFileSync } from "fs";
import { resolve } from "path";
import sha1 from "sha1";
import { escape } from "./torrent-tools";
import { decode , encode} from "bencode";
import {pipe} from "lodash/fp";


interface Sha1Options {
asBytes?: boolean;
asString?: boolean;
}

interface Metadata {
info: object;
'url-list'?: Array<Buffer>;
}

const root = resolve('.');

/**
Expand All @@ -14,22 +26,23 @@ const root = resolve('.');
*/

const torrentFile = readFileSync(root + '/torrent-files/pic.torrent');
const metadata = decode(torrentFile);
const metadata: Metadata = decode(torrentFile);

if (!metadata) {
// handle error
}

const {info, files} = metadata;
const {info} = metadata;
// const url_list = metadata['url-list'].toString().split(',');

if (!info || !files) {
if (!info) {
// handle error
}

const info_hash = pipe([encode, sha1, escape])(info);
console.log(metadata);

function escape(l: string) {
console.log(l)
return l;
}
const sha = (options: Sha1Options) => (msg: Buffer) => sha1(msg, options);

const info_hash: string = pipe([encode, sha({})])(info);

console.log('INFO_HASH : ', info_hash);
2 changes: 0 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"@types/bencode": "^2.0.0",
"@types/jest": "^23.3.10",
"@types/node-fetch": "^2.1.4",
"@types/parse-torrent": "^5.8.2",
"bencode": "^2.0.0",
"jest": "^23.6.0",
"ts-jest": "^23.10.5",
Expand All @@ -23,7 +22,6 @@
"bencode": "^2.0.0",
"lodash": "^4.17.11",
"node-fetch": "^2.3.0",
"parse-torrent": "^6.1.2",
"sha1": "^1.1.1"
}
}
18 changes: 10 additions & 8 deletions server/torrent-tools.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { toBytes } from "./torrent-tools";
import { escape } from "./torrent-tools";

describe('Testing torrent-tools', () => {
describe('toBytes', () => {
test('should return 17', () => {
const received = toBytes('11');
const expected = '17'
expect(received).toEqual(expected);
describe('escape function', () => {
test('should return %7c%d3P%e5%a7%0f%0aaY%3eceC%f9%fcg%0f%fa%8aM', () => {
const buffer = new Buffer([0x7c ,0xd3 ,0x50 ,0xe5 ,0xa7 ,0x0f ,0x0a ,0x61 ,0x59 ,0x3e ,0x63 ,0x65 ,0x43 ,0xf9 ,0xfc ,0x67 ,0x0f ,0xfa ,0x8a ,0x4d])
const escapedB = escape(buffer);
expect(escapedB).toEqual('%7c%d3P%e5%a7%0f%0aaY%3eceC%f9%fcg%0f%fa%8aM');
})

test('should return \'31\'', () => {
const received = toBytes('1f')
test('should return %9a%813%3c%1b%16%e4%a8%3c%10%f3%05%2c%15%90%aa%df%5e.%20', () => {
const buffer = new Buffer([0x9a,0x81,0x33,0x3c,0x1b,0x16,0xe4,0xa8,0x3c,0x10,0xf3,0x05,0x2c,0x15,0x90,0xaa,0xdf,0x5e,0x2e,0x20]);
const escapedB = escape(buffer);
expect(escapedB).toEqual("%9a%813%3c%1b%16%e4%a8%3c%10%f3%05%2c%15%90%aa%df%5e.%20");
})
})
})
38 changes: 32 additions & 6 deletions server/torrent-tools.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
export const toBytes = (SHA1: string): string => {
return new Uint8Array(SHA1.length / 2).map((e, i) => {
const high = parseInt(SHA1[i], 16) << 4;
const low = parseInt(SHA1[i + 1], 16);
return high | low;
}).toString();
const A = 65;
const Z = 90;
const a = 97;
const z = 122;

const ZERO = 48;
const NINE = 57;

const UNDERSCORE = 95;
const DASH = 45;
const DOT = 46;
const TILDE = 126;

/**
https://wiki.theory.org/index.php/BitTorrentSpecification
Note that all binary data in the URL (particularly info_hash and peer_id) must be properly escaped.
this means any byte not in the set 0-9, a-z, A-Z, '.', '-', '_' and '~', must be encoded using the "%nn" format,
where nn is the hexadecimal value of the byte. (See RFC1738 for details.)
*/

export function escape(l: Buffer): string {
return l.reduce((acc: string, v) => {
const isAlpha = v >= A && v <= Z || v >= a && v <= z;
const isNumber = v >= ZERO && v <= NINE;
const isSpecialChars = v === UNDERSCORE || v === DOT || v === TILDE || v === DASH;
if (isAlpha || isNumber || isSpecialChars) {
return acc + String.fromCharCode(v);
} else {
return acc + `%${v.toString(16).padStart(2, '0')}`;
}
}, '');
}
2 changes: 1 addition & 1 deletion server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* Basic Options */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
"lib": ["es2017"], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
Expand Down
87 changes: 0 additions & 87 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@
version "4.14.119"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.119.tgz#be847e5f4bc3e35e46d041c394ead8b603ad8b39"

"@types/magnet-uri@*":
version "5.1.1"
resolved "https://registry.yarnpkg.com/@types/magnet-uri/-/magnet-uri-5.1.1.tgz#861aaf64c92a3137dd848fefc55cd352a8ea851a"
dependencies:
"@types/node" "*"

"@types/node-fetch@^2.1.4":
version "2.1.4"
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.1.4.tgz#093d1beae11541aef25999d70aa09286fd025b1a"
Expand All @@ -46,20 +40,6 @@
version "10.12.10"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.10.tgz#4fa76e6598b7de3f0cb6ec3abacc4f59e5b3a2ce"

"@types/parse-torrent-file@*":
version "4.0.1"
resolved "https://registry.yarnpkg.com/@types/parse-torrent-file/-/parse-torrent-file-4.0.1.tgz#056a6c18f3fac0cd7c6c74540f00496a3225976b"
dependencies:
"@types/node" "*"

"@types/parse-torrent@^5.8.2":
version "5.8.2"
resolved "https://registry.yarnpkg.com/@types/parse-torrent/-/parse-torrent-5.8.2.tgz#53ab880e38ced2005a79948f0df0c8762539323e"
dependencies:
"@types/magnet-uri" "*"
"@types/node" "*"
"@types/parse-torrent-file" "*"

"@types/prop-types@*":
version "15.5.6"
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.5.6.tgz#9c03d3fed70a8d517c191b7734da2879b50ca26c"
Expand Down Expand Up @@ -663,10 +643,6 @@ binary-extensions@^1.0.0:
version "1.12.0"
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-1.12.0.tgz#c2d780f53d45bba8317a8902d4ceeaf3a6385b14"

blob-to-buffer@^1.2.6:
version "1.2.8"
resolved "https://registry.yarnpkg.com/blob-to-buffer/-/blob-to-buffer-1.2.8.tgz#78eeeb332f1280ed0ca6fb2b60693a8c6d36903a"

bluebird@^3.5.1:
version "3.5.3"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.3.tgz#7d01c6f9616c9a51ab0f8c549a79dfe6ec33efa7"
Expand Down Expand Up @@ -1283,12 +1259,6 @@ decode-uri-component@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/decode-uri-component/-/decode-uri-component-0.2.0.tgz#eb3913333458775cb84cd1a1fae062106bb87545"

decompress-response@^3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/decompress-response/-/decompress-response-3.3.0.tgz#80a4dd323748384bfa248083622aedec982adff3"
dependencies:
mimic-response "^1.0.0"

deep-equal@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/deep-equal/-/deep-equal-1.0.1.tgz#f5d260292b660e084eff4cdbc9f08ad3247448b5"
Expand Down Expand Up @@ -2028,10 +1998,6 @@ get-caller-file@^1.0.1:
version "1.0.3"
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-1.0.3.tgz#f978fa4c90d1dfe7ff2d6beda2a515e713bdcf4a"

get-stdin@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/get-stdin/-/get-stdin-6.0.0.tgz#9e09bf712b360ab9225e812048f71fde9c89657b"

get-stream@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-3.0.0.tgz#8e943d1358dc37555054ecbe2edb05aa174ede14"
Expand Down Expand Up @@ -3269,13 +3235,6 @@ lru-cache@^4.1.3:
pseudomap "^1.0.2"
yallist "^3.0.2"

magnet-uri@^5.1.3:
version "5.2.4"
resolved "https://registry.yarnpkg.com/magnet-uri/-/magnet-uri-5.2.4.tgz#7afe5b736af04445aff744c93a890a3710077688"
dependencies:
thirty-two "^1.0.1"
uniq "^1.0.1"

make-dir@^1.0.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-1.3.0.tgz#79c1033b80515bd6d24ec9933e860ca75ee27f0c"
Expand Down Expand Up @@ -3432,10 +3391,6 @@ mimic-fn@^1.0.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-1.2.0.tgz#820c86a39334640e99516928bd03fca88057d022"

mimic-response@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-1.0.1.tgz#4923538878eef42063cb8a3e3b0798781487ab1b"

minimalistic-assert@^1.0.0, minimalistic-assert@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/minimalistic-assert/-/minimalistic-assert-1.0.1.tgz#2e194de044626d4a10e7f7fbc00ce73e83e4d5c7"
Expand Down Expand Up @@ -3948,18 +3903,6 @@ parse-json@^2.2.0:
dependencies:
error-ex "^1.2.0"

parse-torrent@^6.1.2:
version "6.1.2"
resolved "https://registry.yarnpkg.com/parse-torrent/-/parse-torrent-6.1.2.tgz#99da5bdd23435a1cb7e8e7a63847c4efb21b1956"
dependencies:
bencode "^2.0.0"
blob-to-buffer "^1.2.6"
get-stdin "^6.0.0"
magnet-uri "^5.1.3"
simple-get "^3.0.1"
simple-sha1 "^2.0.0"
uniq "^1.0.1"

[email protected]:
version "4.0.0"
resolved "https://registry.yarnpkg.com/parse5/-/parse5-4.0.0.tgz#6d78656e3da8d78b4ec0b906f7c08ef1dfe3f608"
Expand Down Expand Up @@ -4484,10 +4427,6 @@ run-queue@^1.0.0, run-queue@^1.0.3:
dependencies:
aproba "^1.1.1"

rusha@^0.8.1:
version "0.8.13"
resolved "https://registry.yarnpkg.com/rusha/-/rusha-0.8.13.tgz#9a084e7b860b17bff3015b92c67a6a336191513a"

[email protected], safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1:
version "5.1.2"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
Expand Down Expand Up @@ -4662,24 +4601,6 @@ signal-exit@^3.0.0, signal-exit@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"

simple-concat@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/simple-concat/-/simple-concat-1.0.0.tgz#7344cbb8b6e26fb27d66b2fc86f9f6d5997521c6"

simple-get@^3.0.1:
version "3.0.3"
resolved "https://registry.yarnpkg.com/simple-get/-/simple-get-3.0.3.tgz#924528ac3f9d7718ce5e9ec1b1a69c0be4d62efa"
dependencies:
decompress-response "^3.3.0"
once "^1.3.1"
simple-concat "^1.0.0"

simple-sha1@^2.0.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/simple-sha1/-/simple-sha1-2.1.1.tgz#93f3b7f2e8dfdc056c32793e5d47b58d311b140d"
dependencies:
rusha "^0.8.1"

sisteransi@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/sisteransi/-/sisteransi-0.1.1.tgz#5431447d5f7d1675aac667ccd0b865a4994cb3ce"
Expand Down Expand Up @@ -5043,10 +4964,6 @@ test-exclude@^4.2.1:
read-pkg-up "^1.0.1"
require-main-filename "^1.0.1"

thirty-two@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/thirty-two/-/thirty-two-1.0.2.tgz#4ca2fffc02a51290d2744b9e3f557693ca6b627a"

throat@^4.0.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/throat/-/throat-4.1.0.tgz#89037cbc92c56ab18926e6ba4cbb200e15672a6a"
Expand Down Expand Up @@ -5214,10 +5131,6 @@ union-value@^1.0.0:
is-extendable "^0.1.1"
set-value "^0.4.3"

uniq@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/uniq/-/uniq-1.0.1.tgz#b31c5ae8254844a3a8281541ce2b04b865a734ff"

unique-filename@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/unique-filename/-/unique-filename-1.1.1.tgz#1d69769369ada0583103a1e6ae87681b56573230"
Expand Down

0 comments on commit 2caf499

Please sign in to comment.