rack-tctp 0.9.5 → 0.9.8
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.
- checksums.yaml +4 -4
- data/lib/rack/tctp/halec.rb +78 -20
- data/lib/rack/tctp.rb +5 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1fb5bcf11c0cacc00595f49b429d6810b646f62a
|
4
|
+
data.tar.gz: 469afac39fcc7dd72568c9c5f0c62c8309e5e7b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a47f449b3ddb7da03987b77624b936c88875a46d9d64ec5b6016d070bf287d1a6b7c868ab0e92f74070c115f5219bb06c79d84bfebb6ef201dadd4677572808
|
7
|
+
data.tar.gz: b8cfe98bbe089fbfa476146de64e8458e7b5e9e478190b8539588bbb5f0c83ecb770966074fc00be291ccddc86ec1b0fdbcaefddcfd36f69541c31b5519ba145
|
data/lib/rack/tctp/halec.rb
CHANGED
@@ -18,8 +18,37 @@ class Rack::TCTP::HALEC
|
|
18
18
|
# A server or client certificate (if any)
|
19
19
|
attr_reader :certificate
|
20
20
|
|
21
|
+
# The thread for encrypting and decrypting data of this HALEC
|
22
|
+
attr_reader :async_thread
|
23
|
+
|
24
|
+
# The queue for encrypting and decrypting data
|
25
|
+
# @return [Queue] The async queue
|
26
|
+
attr_reader :async_queue
|
27
|
+
|
21
28
|
def initialize(options = {})
|
22
29
|
@url = options[:url] || nil
|
30
|
+
|
31
|
+
@async_queue = Queue.new
|
32
|
+
|
33
|
+
Thread.new do
|
34
|
+
begin
|
35
|
+
while true
|
36
|
+
item = @async_queue.pop
|
37
|
+
|
38
|
+
case item[0]
|
39
|
+
when :encrypt
|
40
|
+
item[2].call encrypt_data(item[1])
|
41
|
+
when :decrypt
|
42
|
+
item[2].call decrypt_data(item[1])
|
43
|
+
when :call
|
44
|
+
item[2].call
|
45
|
+
end
|
46
|
+
end
|
47
|
+
rescue Exception => e
|
48
|
+
#TODO Handle HALEC encryption thread shutdown
|
49
|
+
puts e
|
50
|
+
end
|
51
|
+
end
|
23
52
|
end
|
24
53
|
|
25
54
|
# Encrypts +plaintext+ data and either returns the encrypted data or calls a block with it.
|
@@ -28,21 +57,33 @@ class Rack::TCTP::HALEC
|
|
28
57
|
# @yield Gives the encrypted data to the block
|
29
58
|
# @yieldparam [String] The encrypted data
|
30
59
|
def encrypt_data(plaintext, &encrypted)
|
31
|
-
@engine.write plaintext
|
60
|
+
written = @engine.write plaintext
|
61
|
+
|
62
|
+
if written < plaintext.length
|
63
|
+
exit -1
|
64
|
+
end
|
65
|
+
|
66
|
+
read_data = []
|
32
67
|
|
33
68
|
while(read_chunk = @engine.extract)
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
read_data = StringIO.new(read_chunk)
|
41
|
-
end
|
69
|
+
read_data << read_chunk
|
70
|
+
end
|
71
|
+
|
72
|
+
if block_given?
|
73
|
+
read_data.each do |data|
|
74
|
+
encrypted.call data
|
42
75
|
end
|
76
|
+
else
|
77
|
+
read_data.join
|
43
78
|
end
|
79
|
+
end
|
44
80
|
|
45
|
-
|
81
|
+
# Encrypts +plaintext+ data asynchronously, yielding data to the block when done.
|
82
|
+
# @param [String] plaintext The plaintext
|
83
|
+
# @yield Gives the encrypted data to the block
|
84
|
+
# @yieldparam [String] The encrypted data
|
85
|
+
def encrypt_data_async(plaintext, &encrypted)
|
86
|
+
async_queue.push [:encrypt, plaintext, encrypted]
|
46
87
|
end
|
47
88
|
|
48
89
|
# Decrypts +encrypted+ data and either returns the plaintext or calls a block with it.
|
@@ -51,21 +92,38 @@ class Rack::TCTP::HALEC
|
|
51
92
|
# @yield Gives the plaintext to the block
|
52
93
|
# @yieldparam [String] The plaintext
|
53
94
|
def decrypt_data(encrypted, &decrypted)
|
54
|
-
@engine.inject encrypted
|
95
|
+
injected = @engine.inject encrypted
|
96
|
+
|
97
|
+
if injected < encrypted.length
|
98
|
+
exit -1
|
99
|
+
end
|
100
|
+
|
101
|
+
read_data = []
|
55
102
|
|
56
103
|
while(read_chunk = @engine.read)
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
read_data = StringIO.new(read_chunk)
|
64
|
-
end
|
104
|
+
read_data << read_chunk
|
105
|
+
end
|
106
|
+
|
107
|
+
if block_given? then
|
108
|
+
read_data.each do |data|
|
109
|
+
decrypted.call data
|
65
110
|
end
|
111
|
+
else
|
112
|
+
read_data.join
|
66
113
|
end
|
114
|
+
end
|
115
|
+
|
116
|
+
# Decrypts +encrypted+ data asynchronously, yielding data to the block when done.
|
117
|
+
# @param [String] encrypted The encrypted data
|
118
|
+
# @yield Gives the decrypted data to the block
|
119
|
+
# @yieldparam [String] the decrypted data
|
120
|
+
def decrypt_data_async(encrypted, &decrypted)
|
121
|
+
async_queue.push [:decrypt, encrypted, decrypted]
|
122
|
+
end
|
67
123
|
|
68
|
-
|
124
|
+
# Calls a given block asynchronously, considering the order of all calls to async methods.
|
125
|
+
def call_async(&block)
|
126
|
+
async_queue.push [:call, nil, block]
|
69
127
|
end
|
70
128
|
end
|
71
129
|
|
data/lib/rack/tctp.rb
CHANGED
@@ -102,7 +102,11 @@ module Rack
|
|
102
102
|
|
103
103
|
read_body = req.body.read
|
104
104
|
|
105
|
-
|
105
|
+
begin
|
106
|
+
decrypted_body.write halec.decrypt_data(read_body)
|
107
|
+
rescue Exception => e
|
108
|
+
error(e.message + e.backtrace.join("<br/>\n"))
|
109
|
+
end
|
106
110
|
|
107
111
|
req.body.string = decrypted_body.string
|
108
112
|
end
|