Skip to content

Commit

Permalink
Remove table cipher.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrluanma committed Mar 5, 2024
1 parent 66f06ac commit 1879333
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 168 deletions.
19 changes: 6 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ shadowsocks-heroku is a lightweight tunnel proxy which can help you get through

shadowsocks-heroku uses WebSocket instead of raw sockets, so it can be deployed on [Heroku](https://www.heroku.com/).

Notice that the protocol is INCOMPATIBLE with the origin shadowsocks.
Notice that the protocol is INCOMPATIBLE with shadowsocks.

Heroku
------
Expand Down Expand Up @@ -34,10 +34,10 @@ To [email protected]:still-tor-8707.git
Set a few configs:

```
$ heroku config:set METHOD=table KEY=foobar
$ heroku config:set METHOD=aes-128-cfb KEY=foobar
Setting config vars and restarting still-tor-8707... done, v11
KEY: foobar
METHOD: table
METHOD: aes-128-cfb
```

Install project dependencies with `npm install`:
Expand All @@ -50,7 +50,7 @@ $ npm install
Then run:

```
$ node local.js -s still-tor-8707.herokuapp.com -l 1080 -m table -k foobar -r 80
$ node local.js -s still-tor-8707.herokuapp.com -l 1080 -m aes-128-cfb -k foobar -r 80
server listening at { address: '127.0.0.1', family: 'IPv4', port: 1080 }
```

Expand All @@ -71,16 +71,9 @@ $ heroku logs -t --app still-tor-8707
Supported Ciphers
-----------------

- table
- bf-cfb
- des-cfb
- rc2-cfb
- idea-cfb
- seed-cfb
- cast5-cfb
- aes-128-cfb
- aes-192-cfb
- aes-256-cfb
- camellia-256-cfb
- camellia-192-cfb
- camellia-128-cfb
- camellia-192-cfb
- camellia-256-cfb
113 changes: 18 additions & 95 deletions encrypt.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,4 @@
import crypto from 'crypto';
const int32Max = Math.pow(2, 32);

const cachedTables = {}; // password: [encryptTable, decryptTable]

const getTable = function (key) {
if (cachedTables[key]) {
return cachedTables[key];
}
console.log('calculating ciphers');
let table = new Array(256);
const decrypt_table = new Array(256);
const md5sum = crypto.createHash('md5');
md5sum.update(key);
const hash = Buffer.from(md5sum.digest(), 'binary');
const al = hash.readUInt32LE(0);
const ah = hash.readUInt32LE(4);
let i = 0;

while (i < 256) {
table[i] = i;
i++;
}
i = 1;

while (i < 1024) {
table.sort(
(x, y) =>
(((ah % (x + i)) * int32Max + al) % (x + i)) -
(((ah % (y + i)) * int32Max + al) % (y + i)),
);
i++;
}
i = 0;
while (i < 256) {
decrypt_table[table[i]] = i;
++i;
}
const result = [table, decrypt_table];
cachedTables[key] = result;
return result;
};

const substitute = function (table, buf) {
let i = 0;

while (i < buf.length) {
buf[i] = table[buf[i]];
i++;
}
return buf;
};

const bytes_to_key_results = {};

Expand Down Expand Up @@ -83,35 +32,22 @@ const method_supported = {
'aes-128-cfb': [16, 16],
'aes-192-cfb': [24, 16],
'aes-256-cfb': [32, 16],
'bf-cfb': [16, 8],
'camellia-128-cfb': [16, 16],
'camellia-192-cfb': [24, 16],
'camellia-256-cfb': [32, 16],
'cast5-cfb': [16, 8],
'des-cfb': [8, 8],
'idea-cfb': [16, 8],
'rc2-cfb': [16, 8],
'seed-cfb': [16, 16],
};

class Encryptor {
export class Encryptor {
constructor(key, method) {
this.key = key;
this.method = method;
this.iv_sent = false;
if (this.method === 'table') {
this.method = null;
}
if (this.method) {
this.cipher = this.get_cipher(
this.key,
this.method,
1,
crypto.randomBytes(32),
);
} else {
[this.encryptTable, this.decryptTable] = getTable(this.key);
}
this.cipher = this.get_cipher(
this.key,
this.method,
1,
crypto.randomBytes(32),
);
}

get_cipher_len(method) {
Expand Down Expand Up @@ -141,36 +77,23 @@ class Encryptor {
}

encrypt(buf) {
if (this.method) {
const result = this.cipher.update(buf);
if (this.iv_sent) {
return result;
} else {
this.iv_sent = true;
return Buffer.concat([this.cipher_iv, result]);
}
const result = this.cipher.update(buf);
if (this.iv_sent) {
return result;
} else {
return substitute(this.encryptTable, buf);
this.iv_sent = true;
return Buffer.concat([this.cipher_iv, result]);
}
}

decrypt(buf) {
if (this.method) {
let result;
if (!this.decipher) {
const decipher_iv_len = this.get_cipher_len(this.method)[1];
const decipher_iv = buf.slice(0, decipher_iv_len);
this.decipher = this.get_cipher(this.key, this.method, 0, decipher_iv);
result = this.decipher.update(buf.slice(decipher_iv_len));
return result;
} else {
result = this.decipher.update(buf);
return result;
}
if (!this.decipher) {
const decipher_iv_len = this.get_cipher_len(this.method)[1];
const decipher_iv = buf.slice(0, decipher_iv_len);
this.decipher = this.get_cipher(this.key, this.method, 0, decipher_iv);
return this.decipher.update(buf.slice(decipher_iv_len));
} else {
return substitute(this.decryptTable, buf);
return this.decipher.update(buf);
}
}
}

export {Encryptor, getTable};
5 changes: 0 additions & 5 deletions local.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ const PORT = config.local_port;
const KEY = config.password;
let METHOD = config.method;
const timeout = Math.floor(config.timeout * 1000);

if (['', 'null', 'table'].includes(METHOD.toLowerCase())) {
METHOD = null;
}

const HTTPPROXY = process.env.http_proxy;

if (HTTPPROXY) {
Expand Down
4 changes: 0 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ const KEY = config.password;
let METHOD = config.method;
const highWaterMark = +process.env.HIGH_WATER_MARK || 64 * 1024;

if (['', 'null', 'table'].includes(METHOD.toLowerCase())) {
METHOD = null;
}

const server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('asdf.');
Expand Down
51 changes: 0 additions & 51 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,3 @@
// test encryption
import * as encrypt from './encrypt.js';

const target = [
[
60, 53, 84, 138, 217, 94, 88, 23, 39, 242, 219, 35, 12, 157, 165, 181, 255,
143, 83, 247, 162, 16, 31, 209, 190, 171, 115, 65, 38, 41, 21, 245, 236, 46,
121, 62, 166, 233, 44, 154, 153, 145, 230, 49, 128, 216, 173, 29, 241, 119,
64, 229, 194, 103, 131, 110, 26, 197, 218, 59, 204, 56, 27, 34, 141, 221,
149, 239, 192, 195, 24, 155, 170, 183, 11, 254, 213, 37, 137, 226, 75, 203,
55, 19, 72, 248, 22, 129, 33, 175, 178, 10, 198, 71, 77, 36, 113, 167, 48,
2, 117, 140, 142, 66, 199, 232, 243, 32, 123, 54, 51, 82, 57, 177, 87, 251,
150, 196, 133, 5, 253, 130, 8, 184, 14, 152, 231, 3, 186, 159, 76, 89, 228,
205, 156, 96, 163, 146, 18, 91, 132, 85, 80, 109, 172, 176, 105, 13, 50,
235, 127, 0, 189, 95, 98, 136, 250, 200, 108, 179, 211, 214, 106, 168, 78,
79, 74, 210, 30, 73, 201, 151, 208, 114, 101, 174, 92, 52, 120, 240, 15,
169, 220, 182, 81, 224, 43, 185, 40, 99, 180, 17, 212, 158, 42, 90, 9, 191,
45, 6, 25, 4, 222, 67, 126, 1, 116, 124, 206, 69, 61, 7, 68, 97, 202, 63,
244, 20, 28, 58, 93, 134, 104, 144, 227, 147, 102, 118, 135, 148, 47, 238,
86, 112, 122, 70, 107, 215, 100, 139, 223, 225, 164, 237, 111, 125, 207,
160, 187, 246, 234, 161, 188, 193, 249, 252,
],
[
151, 205, 99, 127, 201, 119, 199, 211, 122, 196, 91, 74, 12, 147, 124, 180,
21, 191, 138, 83, 217, 30, 86, 7, 70, 200, 56, 62, 218, 47, 168, 22, 107,
88, 63, 11, 95, 77, 28, 8, 188, 29, 194, 186, 38, 198, 33, 230, 98, 43, 148,
110, 177, 1, 109, 82, 61, 112, 219, 59, 0, 210, 35, 215, 50, 27, 103, 203,
212, 209, 235, 93, 84, 169, 166, 80, 130, 94, 164, 165, 142, 184, 111, 18,
2, 141, 232, 114, 6, 131, 195, 139, 176, 220, 5, 153, 135, 213, 154, 189,
238, 174, 226, 53, 222, 146, 162, 236, 158, 143, 55, 244, 233, 96, 173, 26,
206, 100, 227, 49, 178, 34, 234, 108, 207, 245, 204, 150, 44, 87, 121, 54,
140, 118, 221, 228, 155, 78, 3, 239, 101, 64, 102, 17, 223, 41, 137, 225,
229, 66, 116, 171, 125, 40, 39, 71, 134, 13, 193, 129, 247, 251, 20, 136,
242, 14, 36, 97, 163, 181, 72, 25, 144, 46, 175, 89, 145, 113, 90, 159, 190,
15, 183, 73, 123, 187, 128, 248, 252, 152, 24, 197, 68, 253, 52, 69, 117,
57, 92, 104, 157, 170, 214, 81, 60, 133, 208, 246, 172, 23, 167, 160, 192,
76, 161, 237, 45, 4, 58, 10, 182, 65, 202, 240, 185, 241, 79, 224, 132, 51,
42, 126, 105, 37, 250, 149, 32, 243, 231, 67, 179, 48, 9, 106, 216, 31, 249,
19, 85, 254, 156, 115, 255, 120, 75, 16,
],
];
const tables = encrypt.getTable('foobar!');
console.log(JSON.stringify(tables));
let i = 0;

while (i < 256) {
console.assert(tables[0][i] === target[0][i]);
console.assert(tables[1][i] === target[1][i]);
i++;
}

// test proxy

import child_process from 'child_process';
Expand Down

0 comments on commit 1879333

Please sign in to comment.