Skip to content

Commit 1879333

Browse files
committed
Remove table cipher.
1 parent 66f06ac commit 1879333

File tree

5 files changed

+24
-168
lines changed

5 files changed

+24
-168
lines changed

README.md

+6-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ shadowsocks-heroku is a lightweight tunnel proxy which can help you get through
55

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

8-
Notice that the protocol is INCOMPATIBLE with the origin shadowsocks.
8+
Notice that the protocol is INCOMPATIBLE with shadowsocks.
99

1010
Heroku
1111
------
@@ -34,10 +34,10 @@ To [email protected]:still-tor-8707.git
3434
Set a few configs:
3535

3636
```
37-
$ heroku config:set METHOD=table KEY=foobar
37+
$ heroku config:set METHOD=aes-128-cfb KEY=foobar
3838
Setting config vars and restarting still-tor-8707... done, v11
3939
KEY: foobar
40-
METHOD: table
40+
METHOD: aes-128-cfb
4141
```
4242

4343
Install project dependencies with `npm install`:
@@ -50,7 +50,7 @@ $ npm install
5050
Then run:
5151

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

@@ -71,16 +71,9 @@ $ heroku logs -t --app still-tor-8707
7171
Supported Ciphers
7272
-----------------
7373

74-
- table
75-
- bf-cfb
76-
- des-cfb
77-
- rc2-cfb
78-
- idea-cfb
79-
- seed-cfb
80-
- cast5-cfb
8174
- aes-128-cfb
8275
- aes-192-cfb
8376
- aes-256-cfb
84-
- camellia-256-cfb
85-
- camellia-192-cfb
8677
- camellia-128-cfb
78+
- camellia-192-cfb
79+
- camellia-256-cfb

encrypt.js

+18-95
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,4 @@
11
import crypto from 'crypto';
2-
const int32Max = Math.pow(2, 32);
3-
4-
const cachedTables = {}; // password: [encryptTable, decryptTable]
5-
6-
const getTable = function (key) {
7-
if (cachedTables[key]) {
8-
return cachedTables[key];
9-
}
10-
console.log('calculating ciphers');
11-
let table = new Array(256);
12-
const decrypt_table = new Array(256);
13-
const md5sum = crypto.createHash('md5');
14-
md5sum.update(key);
15-
const hash = Buffer.from(md5sum.digest(), 'binary');
16-
const al = hash.readUInt32LE(0);
17-
const ah = hash.readUInt32LE(4);
18-
let i = 0;
19-
20-
while (i < 256) {
21-
table[i] = i;
22-
i++;
23-
}
24-
i = 1;
25-
26-
while (i < 1024) {
27-
table.sort(
28-
(x, y) =>
29-
(((ah % (x + i)) * int32Max + al) % (x + i)) -
30-
(((ah % (y + i)) * int32Max + al) % (y + i)),
31-
);
32-
i++;
33-
}
34-
i = 0;
35-
while (i < 256) {
36-
decrypt_table[table[i]] = i;
37-
++i;
38-
}
39-
const result = [table, decrypt_table];
40-
cachedTables[key] = result;
41-
return result;
42-
};
43-
44-
const substitute = function (table, buf) {
45-
let i = 0;
46-
47-
while (i < buf.length) {
48-
buf[i] = table[buf[i]];
49-
i++;
50-
}
51-
return buf;
52-
};
532

543
const bytes_to_key_results = {};
554

@@ -83,35 +32,22 @@ const method_supported = {
8332
'aes-128-cfb': [16, 16],
8433
'aes-192-cfb': [24, 16],
8534
'aes-256-cfb': [32, 16],
86-
'bf-cfb': [16, 8],
8735
'camellia-128-cfb': [16, 16],
8836
'camellia-192-cfb': [24, 16],
8937
'camellia-256-cfb': [32, 16],
90-
'cast5-cfb': [16, 8],
91-
'des-cfb': [8, 8],
92-
'idea-cfb': [16, 8],
93-
'rc2-cfb': [16, 8],
94-
'seed-cfb': [16, 16],
9538
};
9639

97-
class Encryptor {
40+
export class Encryptor {
9841
constructor(key, method) {
9942
this.key = key;
10043
this.method = method;
10144
this.iv_sent = false;
102-
if (this.method === 'table') {
103-
this.method = null;
104-
}
105-
if (this.method) {
106-
this.cipher = this.get_cipher(
107-
this.key,
108-
this.method,
109-
1,
110-
crypto.randomBytes(32),
111-
);
112-
} else {
113-
[this.encryptTable, this.decryptTable] = getTable(this.key);
114-
}
45+
this.cipher = this.get_cipher(
46+
this.key,
47+
this.method,
48+
1,
49+
crypto.randomBytes(32),
50+
);
11551
}
11652

11753
get_cipher_len(method) {
@@ -141,36 +77,23 @@ class Encryptor {
14177
}
14278

14379
encrypt(buf) {
144-
if (this.method) {
145-
const result = this.cipher.update(buf);
146-
if (this.iv_sent) {
147-
return result;
148-
} else {
149-
this.iv_sent = true;
150-
return Buffer.concat([this.cipher_iv, result]);
151-
}
80+
const result = this.cipher.update(buf);
81+
if (this.iv_sent) {
82+
return result;
15283
} else {
153-
return substitute(this.encryptTable, buf);
84+
this.iv_sent = true;
85+
return Buffer.concat([this.cipher_iv, result]);
15486
}
15587
}
15688

15789
decrypt(buf) {
158-
if (this.method) {
159-
let result;
160-
if (!this.decipher) {
161-
const decipher_iv_len = this.get_cipher_len(this.method)[1];
162-
const decipher_iv = buf.slice(0, decipher_iv_len);
163-
this.decipher = this.get_cipher(this.key, this.method, 0, decipher_iv);
164-
result = this.decipher.update(buf.slice(decipher_iv_len));
165-
return result;
166-
} else {
167-
result = this.decipher.update(buf);
168-
return result;
169-
}
90+
if (!this.decipher) {
91+
const decipher_iv_len = this.get_cipher_len(this.method)[1];
92+
const decipher_iv = buf.slice(0, decipher_iv_len);
93+
this.decipher = this.get_cipher(this.key, this.method, 0, decipher_iv);
94+
return this.decipher.update(buf.slice(decipher_iv_len));
17095
} else {
171-
return substitute(this.decryptTable, buf);
96+
return this.decipher.update(buf);
17297
}
17398
}
17499
}
175-
176-
export {Encryptor, getTable};

local.js

-5
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ const PORT = config.local_port;
5858
const KEY = config.password;
5959
let METHOD = config.method;
6060
const timeout = Math.floor(config.timeout * 1000);
61-
62-
if (['', 'null', 'table'].includes(METHOD.toLowerCase())) {
63-
METHOD = null;
64-
}
65-
6661
const HTTPPROXY = process.env.http_proxy;
6762

6863
if (HTTPPROXY) {

server.js

-4
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@ const KEY = config.password;
5959
let METHOD = config.method;
6060
const highWaterMark = +process.env.HIGH_WATER_MARK || 64 * 1024;
6161

62-
if (['', 'null', 'table'].includes(METHOD.toLowerCase())) {
63-
METHOD = null;
64-
}
65-
6662
const server = http.createServer(function (req, res) {
6763
res.writeHead(200, {'Content-Type': 'text/plain'});
6864
res.end('asdf.');

test.js

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

543
import child_process from 'child_process';

0 commit comments

Comments
 (0)