1515 * limitations under the License.
1616 */
1717
18- import * as crypto from 'crypto'
18+ import * as Crypto from 'crypto'
1919import * as Async from 'async'
2020import * as Stream from 'stream'
2121import * as Net from 'net'
22+ import HttpHeader from './httpProxy'
2223const EOF = Buffer . from ( '\r\n\r\n' , 'utf8' )
2324import * as fs from 'fs'
2425export interface packetBuffer {
@@ -36,20 +37,20 @@ export interface pairConnect {
3637export const encrypt = ( text : Buffer , masterkey : string , CallBack ) => {
3738 let salt = null
3839 Async . waterfall ( [
39- next => crypto . randomBytes ( 64 , next ) ,
40+ next => Crypto . randomBytes ( 64 , next ) ,
4041 ( _salt , next ) => {
4142 salt = _salt
42- crypto . pbkdf2 ( masterkey , salt , 2145 , 32 , 'sha512' , next )
43+ Crypto . pbkdf2 ( masterkey , salt , 2145 , 32 , 'sha512' , next )
4344 }
4445 ] , ( err , derivedKey ) => {
4546 if ( err )
4647 return CallBack ( err )
4748
48- crypto . randomBytes ( 12 , ( err1 , iv ) => {
49+ Crypto . randomBytes ( 12 , ( err1 , iv ) => {
4950 if ( err1 )
5051 return CallBack ( err1 )
5152
52- const cipher = crypto . createCipheriv ( 'aes-256-gcm' , derivedKey , iv ) ;
53+ const cipher = Crypto . createCipheriv ( 'aes-256-gcm' , derivedKey , iv ) ;
5354
5455 let _text = Buffer . concat ( [ Buffer . alloc ( 4 , 0 ) , text ] )
5556 _text . writeUInt32BE ( text . length , 0 )
@@ -82,13 +83,13 @@ export const decrypt = ( data: Buffer, masterkey, CallBack ) => {
8283 const tag = data . slice ( 76 , 92 ) ;
8384 const text = data . slice ( 92 ) ;
8485 // derive key using; 32 byte key length
85- crypto . pbkdf2 ( masterkey , salt , 2145 , 32 , 'sha512' , ( err , derivedKey ) => {
86+ Crypto . pbkdf2 ( masterkey , salt , 2145 , 32 , 'sha512' , ( err , derivedKey ) => {
8687
8788 if ( err )
8889 return CallBack ( err )
8990 // AES 256 GCM Mode
9091 try {
91- const decipher = crypto . createDecipheriv ( 'aes-256-gcm' , derivedKey , iv )
92+ const decipher = Crypto . createDecipheriv ( 'aes-256-gcm' , derivedKey , iv )
9293 decipher . setAuthTag ( tag )
9394 const decrypted = Buffer . concat ( [ decipher . update ( text ) , decipher . final ( ) ] )
9495 const leng = decrypted . slice ( 4 , 4 + decrypted . readUInt32BE ( 0 ) )
@@ -134,6 +135,27 @@ const HTTP_HEADER = Buffer.from (
134135 `HTTP/1.1 200 OK\r\nDate: ${ new Date ( ) . toUTCString ( ) } \r\nContent-Type: text/html\r\nTransfer-Encoding: chunked\r\nConnection: keep-alive\r\nVary: Accept-Encoding\r\n\r\n` , 'utf8' )
135136const HTTP_EOF = Buffer . from ( '\r\n\r\n' , 'utf8' )
136137
138+ const getHeaderString = ( headers , host : string , port : number ) => {
139+ //console.trace (`getHeaderString header[${ !headers ? 'null': Object.keys ( headers ).forEach ( n => { return `${ n }: [${ + headers [ n ]}] ` }) }]`)
140+ const ret = `Host: ${ host } ${ port !== 80 ? ':' + port : '' } \r\n` +
141+ ( headers && headers [ 'accept' ] ? headers [ 'accept' ] : 'Accept: */*' ) + '\r\n' +
142+ ( headers && headers [ 'accept-language' ] ? headers [ 'accept-language' ] : 'Accept-Language: en-ca' ) + '\r\n' +
143+ ( headers && headers [ 'connection' ] ? headers [ 'Connection' ] : 'Connection: keep-alive' ) + '\r\n' +
144+ ( headers && headers [ 'accept-encoding' ] ? headers [ 'accept-encoding' ] : 'Accept-Encoding: gzip, deflate' ) + '\r\n' +
145+ ( headers && headers [ 'user-agent' ] ? headers [ 'user-agent' ] : 'User-Agent: Mozilla/5.0' ) + '\r\n'
146+ return ret
147+ }
148+
149+ export const otherRequestForNet = ( path : string , host : string , port : number , headers : HttpHeader ) => {
150+
151+ if ( path . length < 2048 )
152+ return `GET /${ path } HTTP/1.1\r\n` + getHeaderString ( headers && headers . headers ? headers : null , host , port ) + '\r\n'
153+
154+ return `POST /${ Crypto . randomBytes ( 4 + Math . random ( ) * 100 ) . toString ( 'base64' ) } HTTP/1.1\r\n` +
155+ getHeaderString ( headers && headers . headers ? headers . headers : null , host , port ) + `Content-Length: ${ path . length } \r\n\r\n` + path + '\r\n\r\n'
156+ }
157+
158+
137159export class encryptStream extends Stream . Transform {
138160 private salt : Buffer
139161 private iv : Buffer
@@ -143,16 +165,17 @@ export class encryptStream extends Stream.Transform {
143165 private BlockBuffer ( _buf : Buffer ) {
144166 return Buffer . from ( _buf . length . toString ( 16 ) . toUpperCase ( ) + '\r\n' , 'utf8' )
145167 }
168+
146169 private init ( callback ) {
147170 return Async . waterfall ( [
148- next => crypto . randomBytes ( 64 , next ) ,
171+ next => Crypto . randomBytes ( 64 , next ) ,
149172 ( _salt , next ) => {
150173 this . salt = _salt
151- crypto . randomBytes ( 12 , next )
174+ Crypto . randomBytes ( 12 , next )
152175 } ,
153176 ( _iv , next ) => {
154177 this . iv = _iv
155- crypto . pbkdf2 ( this . password , this . salt , 2145 , 32 , 'sha512' , next )
178+ Crypto . pbkdf2 ( this . password , this . salt , 2145 , 32 , 'sha512' , next )
156179 }
157180 ] , ( err , derivedKey ) => {
158181
@@ -161,8 +184,10 @@ export class encryptStream extends Stream.Transform {
161184 } )
162185 }
163186
164- constructor ( private password : string , private random : number , private httpHeader : ( str : string ) => Buffer ) {
187+ constructor ( private password : string , private random : number , private host : string , private port : number , private httpRequest : HttpHeader ) {
188+
165189 super ( )
190+ console . trace ( `new encryptStream get httpRequest [${ httpRequest && httpRequest . headers ? JSON . stringify ( httpRequest . headers ) : 'null' } ]` )
166191 }
167192
168193 public _transform ( chunk : Buffer , encode , cb ) {
@@ -171,7 +196,7 @@ export class encryptStream extends Stream.Transform {
171196 return this . _transform ( chunk , encode , cb )
172197 } )
173198 }
174- const cipher = crypto . createCipheriv ( 'aes-256-gcm' , this . derivedKey , this . iv )
199+ const cipher = Crypto . createCipheriv ( 'aes-256-gcm' , this . derivedKey , this . iv )
175200
176201 let _text = Buffer . concat ( [ Buffer . alloc ( 4 , 0 ) , chunk ] )
177202
@@ -186,22 +211,13 @@ export class encryptStream extends Stream.Transform {
186211 if ( this . first ) {
187212 this . first = false
188213 const black = Buffer . concat ( [ this . salt , this . iv , _buf1 ] ) . toString ( 'base64' )
189- if ( ! this . httpHeader ) {
190- const _buf4 = Buffer . from ( black , 'base64' )
191- return cb ( null , Buffer . concat ( [ HTTP_HEADER , this . BlockBuffer ( _buf4 ) , _buf4 , EOF ] ) )
192- }
193- const _buf2 = this . httpHeader ( black )
194-
195- return cb ( null , _buf2 )
214+
215+ otherRequestForNet ( black , this . host , this . port , this . httpRequest )
216+ return cb ( null , otherRequestForNet ( black , this . host , this . port , this . httpRequest ) )
196217 }
197218
198- const _buf2 = _buf1 . toString ( 'base64' )
199-
200- if ( this . httpHeader ) {
201- return cb ( null , this . httpHeader ( _buf2 ) )
202- }
203- const _buf3 = Buffer . from ( _buf2 , 'base64' )
204- return cb ( null , Buffer . concat ( [ this . BlockBuffer ( _buf3 ) , _buf3 , EOF ] ) )
219+ const _buf2 = _buf1 . toString ( 'base64' )
220+ return cb ( null , otherRequestForNet ( _buf2 , this . host , this . port , this . httpRequest ) )
205221 }
206222}
207223
@@ -213,7 +229,7 @@ export class decryptStream extends Stream.Transform {
213229 private derivedKey : Buffer = null
214230 private _decrypt ( _text : Buffer ) {
215231
216- const decipher = crypto . createDecipheriv ( 'aes-256-gcm' , this . derivedKey , this . iv )
232+ const decipher = Crypto . createDecipheriv ( 'aes-256-gcm' , this . derivedKey , this . iv )
217233 decipher . setAuthTag ( _text . slice ( 0 , 16 ) )
218234 try {
219235 const _buf = Buffer . concat ( [ decipher . update ( _text . slice ( 16 ) ) , decipher . final ( ) ] )
@@ -233,7 +249,7 @@ export class decryptStream extends Stream.Transform {
233249 public _First ( chunk : Buffer , CallBack : ( err ?: Error , text ?: Buffer ) => void ) {
234250 this . salt = chunk . slice ( 0 , 64 ) ;
235251 this . iv = chunk . slice ( 64 , 76 ) ;
236- return crypto . pbkdf2 ( this . password , this . salt , 2145 , 32 , 'sha512' , ( err , derivedKey ) => {
252+ return Crypto . pbkdf2 ( this . password , this . salt , 2145 , 32 , 'sha512' , ( err , derivedKey ) => {
237253 if ( err ) {
238254 console . log ( `decryptStream crypto.pbkdf2 ERROR: ${ err . message } ` )
239255 return CallBack ( err )
0 commit comments