chat_xdk 0.7.10-x86_64-linux
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 +7 -0
- data/LICENSE +21 -0
- data/README.md +150 -0
- data/THIRD_PARTY_NOTICES.md +570 -0
- data/lib/chat_xdk/chat.rb +1253 -0
- data/lib/chat_xdk/errors.rb +83 -0
- data/lib/chat_xdk/feature_fetch.rb +249 -0
- data/lib/chat_xdk/feature_http.rb +416 -0
- data/lib/chat_xdk/file_decryptor.rb +102 -0
- data/lib/chat_xdk/file_encryptor.rb +100 -0
- data/lib/chat_xdk/native/linux-x64/libchat_xdk_ffi.so +0 -0
- data/lib/chat_xdk/native.rb +266 -0
- data/lib/chat_xdk/types.rb +1860 -0
- data/lib/chat_xdk/utilities.rb +82 -0
- data/lib/chat_xdk/version.rb +7 -0
- data/lib/chat_xdk.rb +12 -0
- metadata +79 -0
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "ipaddr"
|
|
4
|
+
require "openssl"
|
|
5
|
+
require "socket"
|
|
6
|
+
require "uri"
|
|
7
|
+
|
|
8
|
+
module ChatXdk
|
|
9
|
+
# Anonymous HTTP/1.1 GET for feature switches over raw sockets.
|
|
10
|
+
# Sends no cookies or credentials, stores nothing from responses, follows no
|
|
11
|
+
# redirects, honors HTTP(S)_PROXY / ALL_PROXY / NO_PROXY, and enforces a
|
|
12
|
+
# strict total deadline and body byte cap.
|
|
13
|
+
module FeatureHttp
|
|
14
|
+
EMPTY = "".b.freeze
|
|
15
|
+
MAX_HEAD_BYTES = 64 * 1024
|
|
16
|
+
MAX_CHUNK_LINE_BYTES = 1024
|
|
17
|
+
STATUS_LINE = %r{\AHTTP/1\.[01] (\d{3})(?: .*)?\z}.freeze
|
|
18
|
+
CHUNK_SIZE = /\A([0-9A-Fa-f]{1,8})\s*(?:;.*)?\z/.freeze
|
|
19
|
+
|
|
20
|
+
Answer = Struct.new(:status, :body)
|
|
21
|
+
|
|
22
|
+
module_function
|
|
23
|
+
|
|
24
|
+
# Perform GET url within timeout_ms total milliseconds, reading the body up
|
|
25
|
+
# to max_bytes + 1 bytes. Raises on any I/O, TLS, framing, or timeout error.
|
|
26
|
+
# Optional register_socket/unregister_socket procs let shutdown abort an
|
|
27
|
+
# in-flight request.
|
|
28
|
+
def get(url, timeout_ms, max_bytes, register_socket: nil, unregister_socket: nil)
|
|
29
|
+
uri = URI.parse(url)
|
|
30
|
+
scheme = uri.scheme&.downcase
|
|
31
|
+
secure = scheme == "https"
|
|
32
|
+
raise IOError, "not an http or https URL" unless (secure || scheme == "http") && uri.host && !uri.host.empty?
|
|
33
|
+
|
|
34
|
+
deadline = monotonic + (timeout_ms.to_f / 1000.0)
|
|
35
|
+
authority_host = uri.host
|
|
36
|
+
host = authority_host.start_with?("[") && authority_host.end_with?("]") ? authority_host[1..-2] : authority_host
|
|
37
|
+
default_port = secure ? 443 : 80
|
|
38
|
+
port = uri.port || default_port
|
|
39
|
+
authority = uri.port && uri.port != default_port ? "#{authority_host}:#{port}" : authority_host
|
|
40
|
+
path = uri.path.nil? || uri.path.empty? ? "/" : uri.path
|
|
41
|
+
target = uri.query ? "#{path}?#{uri.query}" : path
|
|
42
|
+
|
|
43
|
+
proxy = proxy_for(scheme, host, port)
|
|
44
|
+
connect_host, connect_port, proxy_auth = proxy ? [proxy[:host], proxy[:port], proxy[:auth]] : [host, port, nil]
|
|
45
|
+
|
|
46
|
+
sock = connect_tcp(connect_host, connect_port, deadline)
|
|
47
|
+
register_socket&.call(sock)
|
|
48
|
+
io = sock
|
|
49
|
+
begin
|
|
50
|
+
if secure
|
|
51
|
+
open_tunnel(sock, "#{authority_host}:#{port}", proxy_auth, deadline) if proxy
|
|
52
|
+
io = start_tls(sock, host, deadline)
|
|
53
|
+
register_socket&.call(io)
|
|
54
|
+
elsif proxy
|
|
55
|
+
target = "http://#{authority}#{target}"
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
request = +"GET #{target} HTTP/1.1\r\n" \
|
|
59
|
+
"Host: #{authority}\r\n" \
|
|
60
|
+
"Accept: application/json\r\n" \
|
|
61
|
+
"Accept-Encoding: identity\r\n" \
|
|
62
|
+
"Connection: close\r\n"
|
|
63
|
+
request << "Proxy-Authorization: #{proxy_auth}\r\n" if proxy && !secure && proxy_auth
|
|
64
|
+
request << "\r\n"
|
|
65
|
+
write_all(io, request.b, deadline)
|
|
66
|
+
|
|
67
|
+
reader = BufferedReader.new(io, deadline)
|
|
68
|
+
head = nil
|
|
69
|
+
status = 0
|
|
70
|
+
loop do
|
|
71
|
+
head = read_head(reader)
|
|
72
|
+
status = parse_status(head)
|
|
73
|
+
break if status >= 200
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
body = read_body(reader, head, status, max_bytes + 1)
|
|
77
|
+
Answer.new(status, body)
|
|
78
|
+
ensure
|
|
79
|
+
unregister_socket&.call
|
|
80
|
+
close_quietly(io)
|
|
81
|
+
close_quietly(sock) unless io.equal?(sock)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def monotonic
|
|
86
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def remaining_time(deadline)
|
|
90
|
+
rem = deadline - monotonic
|
|
91
|
+
raise IOError, "timed out" if rem <= 0
|
|
92
|
+
|
|
93
|
+
rem
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def connect_tcp(host, port, deadline)
|
|
97
|
+
Socket.tcp(host, port, connect_timeout: remaining_time(deadline))
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def open_tunnel(sock, target, proxy_auth, deadline)
|
|
101
|
+
request = +"CONNECT #{target} HTTP/1.1\r\nHost: #{target}\r\n"
|
|
102
|
+
request << "Proxy-Authorization: #{proxy_auth}\r\n" if proxy_auth
|
|
103
|
+
request << "\r\n"
|
|
104
|
+
write_all(sock, request.b, deadline)
|
|
105
|
+
|
|
106
|
+
# Read one byte at a time so no TLS bytes are buffered past the CONNECT response.
|
|
107
|
+
head = read_head_unbuffered(sock, deadline)
|
|
108
|
+
status = parse_status(head)
|
|
109
|
+
raise IOError, "proxy answered CONNECT with #{status}" if status < 200 || status > 299
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def start_tls(sock, host, deadline)
|
|
113
|
+
ctx = OpenSSL::SSL::SSLContext.new
|
|
114
|
+
ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
|
115
|
+
store = OpenSSL::X509::Store.new
|
|
116
|
+
store.set_default_paths
|
|
117
|
+
ctx.cert_store = store
|
|
118
|
+
|
|
119
|
+
ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
|
|
120
|
+
ssl.sync_close = true
|
|
121
|
+
ssl.hostname = host if ssl.respond_to?(:hostname=)
|
|
122
|
+
|
|
123
|
+
loop do
|
|
124
|
+
case ssl.connect_nonblock(exception: false)
|
|
125
|
+
when :wait_readable
|
|
126
|
+
wait_io(ssl, :read, deadline)
|
|
127
|
+
when :wait_writable
|
|
128
|
+
wait_io(ssl, :write, deadline)
|
|
129
|
+
else
|
|
130
|
+
break
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
ssl.post_connection_check(host)
|
|
135
|
+
ssl
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def wait_io(io, mode, deadline)
|
|
139
|
+
rem = remaining_time(deadline)
|
|
140
|
+
ready = mode == :read ? IO.select([io], nil, nil, rem) : IO.select(nil, [io], nil, rem)
|
|
141
|
+
raise IOError, "timed out" unless ready
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def write_all(io, data, deadline)
|
|
145
|
+
offset = 0
|
|
146
|
+
while offset < data.bytesize
|
|
147
|
+
result = io.write_nonblock(data.byteslice(offset, data.bytesize - offset), exception: false)
|
|
148
|
+
case result
|
|
149
|
+
when :wait_readable
|
|
150
|
+
wait_io(io, :read, deadline)
|
|
151
|
+
when :wait_writable
|
|
152
|
+
wait_io(io, :write, deadline)
|
|
153
|
+
else
|
|
154
|
+
offset += result
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def read_head_unbuffered(sock, deadline)
|
|
160
|
+
head = +"".b
|
|
161
|
+
line_ends = 0
|
|
162
|
+
while line_ends < 2
|
|
163
|
+
byte = read_one_byte(sock, deadline)
|
|
164
|
+
raise IOError, "no complete status line and headers" if byte.nil? || head.bytesize >= MAX_HEAD_BYTES
|
|
165
|
+
|
|
166
|
+
if byte == "\n".b
|
|
167
|
+
line_ends += 1
|
|
168
|
+
elsif byte != "\r".b
|
|
169
|
+
line_ends = 0
|
|
170
|
+
end
|
|
171
|
+
head << byte
|
|
172
|
+
end
|
|
173
|
+
head.force_encoding(Encoding::ISO_8859_1).split(/\r?\n/)
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def read_one_byte(io, deadline)
|
|
177
|
+
loop do
|
|
178
|
+
chunk = io.read_nonblock(1, exception: false)
|
|
179
|
+
case chunk
|
|
180
|
+
when :wait_readable
|
|
181
|
+
wait_io(io, :read, deadline)
|
|
182
|
+
when :wait_writable
|
|
183
|
+
wait_io(io, :write, deadline)
|
|
184
|
+
when nil
|
|
185
|
+
return nil
|
|
186
|
+
else
|
|
187
|
+
return chunk
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def read_head(reader)
|
|
193
|
+
head = +"".b
|
|
194
|
+
line_ends = 0
|
|
195
|
+
while line_ends < 2
|
|
196
|
+
byte = reader.read_byte
|
|
197
|
+
raise IOError, "no complete status line and headers" if byte.nil? || head.bytesize >= MAX_HEAD_BYTES
|
|
198
|
+
|
|
199
|
+
if byte == 10 # \n
|
|
200
|
+
line_ends += 1
|
|
201
|
+
elsif byte != 13 # \r
|
|
202
|
+
line_ends = 0
|
|
203
|
+
end
|
|
204
|
+
head << byte
|
|
205
|
+
end
|
|
206
|
+
head.force_encoding(Encoding::ISO_8859_1).split(/\r?\n/)
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def parse_status(head)
|
|
210
|
+
m = STATUS_LINE.match(head[0].to_s)
|
|
211
|
+
raise IOError, "not an HTTP/1 status line" unless m
|
|
212
|
+
|
|
213
|
+
m[1].to_i
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def header_value(head, name)
|
|
217
|
+
value = nil
|
|
218
|
+
down = name.downcase
|
|
219
|
+
head[1..].each do |line|
|
|
220
|
+
colon = line.index(":")
|
|
221
|
+
next unless colon && colon > 0
|
|
222
|
+
next unless line[0, colon].downcase == down
|
|
223
|
+
|
|
224
|
+
raise IOError, "more than one #{name}" unless value.nil?
|
|
225
|
+
|
|
226
|
+
value = line[(colon + 1)..].strip
|
|
227
|
+
end
|
|
228
|
+
value
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
def read_body(reader, head, status, limit)
|
|
232
|
+
return EMPTY if status == 204 || status == 304
|
|
233
|
+
|
|
234
|
+
body = +"".b
|
|
235
|
+
coding = header_value(head, "transfer-encoding")
|
|
236
|
+
length = header_value(head, "content-length")
|
|
237
|
+
|
|
238
|
+
if coding
|
|
239
|
+
raise IOError, "unknown transfer coding" unless coding.downcase == "chunked"
|
|
240
|
+
|
|
241
|
+
while body.bytesize < limit
|
|
242
|
+
size = read_chunk_size(reader)
|
|
243
|
+
break if size == 0
|
|
244
|
+
|
|
245
|
+
reader.copy_into(body, size, limit)
|
|
246
|
+
if body.bytesize < limit
|
|
247
|
+
trail = read_line(reader)
|
|
248
|
+
raise IOError, "no line end after a chunk" unless trail.empty?
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
elsif length
|
|
252
|
+
raise IOError, "not a content length" unless /\A\d{1,18}\z/.match?(length)
|
|
253
|
+
|
|
254
|
+
reader.copy_into(body, length.to_i, limit)
|
|
255
|
+
else
|
|
256
|
+
reader.copy_into(body, -1, limit)
|
|
257
|
+
end
|
|
258
|
+
body
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
def read_chunk_size(reader)
|
|
262
|
+
m = CHUNK_SIZE.match(read_line(reader))
|
|
263
|
+
raise IOError, "not a chunk size" unless m
|
|
264
|
+
|
|
265
|
+
m[1].to_i(16)
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
def read_line(reader)
|
|
269
|
+
line = +"".b
|
|
270
|
+
loop do
|
|
271
|
+
b = reader.read_byte
|
|
272
|
+
raise IOError, "no complete line" if b.nil? || line.bytesize >= MAX_CHUNK_LINE_BYTES
|
|
273
|
+
break if b == 10 # \n
|
|
274
|
+
|
|
275
|
+
line << b
|
|
276
|
+
end
|
|
277
|
+
text = line.force_encoding(Encoding::ISO_8859_1)
|
|
278
|
+
text.end_with?("\r") ? text[0..-2] : text
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
def proxy_for(scheme, host, port)
|
|
282
|
+
return nil if proxy_bypass?(host, port)
|
|
283
|
+
|
|
284
|
+
raw = env_ci("#{scheme}_proxy") || env_ci("all_proxy")
|
|
285
|
+
return nil if raw.nil? || raw.empty?
|
|
286
|
+
|
|
287
|
+
raw = "http://#{raw}" unless raw.include?("://")
|
|
288
|
+
uri = URI.parse(raw)
|
|
289
|
+
return nil unless uri.scheme&.downcase == "http" && uri.host && !uri.host.empty?
|
|
290
|
+
|
|
291
|
+
auth = nil
|
|
292
|
+
if uri.user
|
|
293
|
+
creds = "#{URI.decode_www_form_component(uri.user)}:#{URI.decode_www_form_component(uri.password.to_s)}"
|
|
294
|
+
auth = "Basic #{[creds.b].pack('m0')}"
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
proxy_host = uri.host
|
|
298
|
+
proxy_host = proxy_host[1..-2] if proxy_host.start_with?("[") && proxy_host.end_with?("]")
|
|
299
|
+
{ host: proxy_host, port: uri.port || 80, auth: auth }
|
|
300
|
+
rescue URI::InvalidURIError
|
|
301
|
+
nil
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
def proxy_bypass?(host, port)
|
|
305
|
+
no_proxy = env_ci("no_proxy")
|
|
306
|
+
return false if no_proxy.nil? || no_proxy.empty?
|
|
307
|
+
|
|
308
|
+
host_down = host.downcase
|
|
309
|
+
no_proxy.split(",").each do |raw_entry|
|
|
310
|
+
entry = raw_entry.strip.downcase
|
|
311
|
+
next if entry.empty?
|
|
312
|
+
return true if entry == "*"
|
|
313
|
+
|
|
314
|
+
entry_host, entry_port = split_host_port(entry)
|
|
315
|
+
next if entry_port && entry_port != port
|
|
316
|
+
|
|
317
|
+
clean = entry_host.sub(/\A\./, "")
|
|
318
|
+
return true if host_down == clean || host_down.end_with?(".#{clean}")
|
|
319
|
+
end
|
|
320
|
+
false
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
def split_host_port(entry)
|
|
324
|
+
if entry.start_with?("[")
|
|
325
|
+
bracket = entry.index("]")
|
|
326
|
+
return [entry, nil] unless bracket
|
|
327
|
+
|
|
328
|
+
h = entry[1...bracket]
|
|
329
|
+
rest = entry[(bracket + 1)..]
|
|
330
|
+
p = rest.start_with?(":") ? rest[1..].to_i : nil
|
|
331
|
+
[h, p]
|
|
332
|
+
elsif entry.count(":") == 1
|
|
333
|
+
h, p = entry.split(":", 2)
|
|
334
|
+
[h, p.to_i]
|
|
335
|
+
else
|
|
336
|
+
[entry, nil]
|
|
337
|
+
end
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
def env_ci(name)
|
|
341
|
+
ENV[name.downcase] || ENV[name.upcase]
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
def close_quietly(io)
|
|
345
|
+
return unless io
|
|
346
|
+
|
|
347
|
+
io.close unless io.closed?
|
|
348
|
+
rescue StandardError
|
|
349
|
+
nil
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
# Deadline-bounded buffered reader over a non-blocking socket or SSLSocket.
|
|
353
|
+
class BufferedReader
|
|
354
|
+
def initialize(io, deadline)
|
|
355
|
+
@io = io
|
|
356
|
+
@deadline = deadline
|
|
357
|
+
@buf = +"".b
|
|
358
|
+
@pos = 0
|
|
359
|
+
@eof = false
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
def read_byte
|
|
363
|
+
return nil unless ensure_bytes
|
|
364
|
+
|
|
365
|
+
b = @buf.getbyte(@pos)
|
|
366
|
+
@pos += 1
|
|
367
|
+
b
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
def copy_into(dest, count, limit)
|
|
371
|
+
remaining = count
|
|
372
|
+
while remaining != 0 && dest.bytesize < limit
|
|
373
|
+
unless ensure_bytes
|
|
374
|
+
return if remaining < 0
|
|
375
|
+
|
|
376
|
+
raise EOFError, "the body ended early"
|
|
377
|
+
end
|
|
378
|
+
|
|
379
|
+
avail = @buf.bytesize - @pos
|
|
380
|
+
want = limit - dest.bytesize
|
|
381
|
+
want = remaining if remaining > 0 && remaining < want
|
|
382
|
+
take = avail < want ? avail : want
|
|
383
|
+
dest << @buf.byteslice(@pos, take)
|
|
384
|
+
@pos += take
|
|
385
|
+
remaining -= take if remaining > 0
|
|
386
|
+
end
|
|
387
|
+
end
|
|
388
|
+
|
|
389
|
+
private
|
|
390
|
+
|
|
391
|
+
def ensure_bytes
|
|
392
|
+
return true if @pos < @buf.bytesize
|
|
393
|
+
return false if @eof
|
|
394
|
+
|
|
395
|
+
@buf = +"".b
|
|
396
|
+
@pos = 0
|
|
397
|
+
loop do
|
|
398
|
+
chunk = @io.read_nonblock(4096, exception: false)
|
|
399
|
+
case chunk
|
|
400
|
+
when :wait_readable
|
|
401
|
+
FeatureHttp.wait_io(@io, :read, @deadline)
|
|
402
|
+
when :wait_writable
|
|
403
|
+
FeatureHttp.wait_io(@io, :write, @deadline)
|
|
404
|
+
when nil
|
|
405
|
+
@eof = true
|
|
406
|
+
return false
|
|
407
|
+
else
|
|
408
|
+
@buf = chunk.b
|
|
409
|
+
return true
|
|
410
|
+
end
|
|
411
|
+
end
|
|
412
|
+
end
|
|
413
|
+
end
|
|
414
|
+
end
|
|
415
|
+
private_constant :FeatureHttp
|
|
416
|
+
end
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ChatXdk
|
|
4
|
+
# Incremental decryptor for one encrypted file.
|
|
5
|
+
#
|
|
6
|
+
# Feed the encrypted bytes in chunks with {#push}, then call {#finish} once
|
|
7
|
+
# at end of input. {#finish} raises if the file ended before its final frame
|
|
8
|
+
# (truncation), so do not treat bytes from {#push} as complete until {#finish}
|
|
9
|
+
# succeeds. Always {#close} the instance when finished (or use the block form
|
|
10
|
+
# of {Chat#file_decryptor}).
|
|
11
|
+
#
|
|
12
|
+
# Not thread-safe: do not share one instance across threads without external
|
|
13
|
+
# synchronization.
|
|
14
|
+
class FileDecryptor
|
|
15
|
+
# @api private
|
|
16
|
+
HandleState = Struct.new(:handle, :closed, :mutex)
|
|
17
|
+
private_constant :HandleState
|
|
18
|
+
|
|
19
|
+
# @api private
|
|
20
|
+
def self.create(chat_handle, params_json)
|
|
21
|
+
out = FFI::MemoryPointer.new(:pointer)
|
|
22
|
+
begin
|
|
23
|
+
Native.consume(Native.chat_xdk_file_decryptor_new(chat_handle, Native.utf8_ptr(params_json), out))
|
|
24
|
+
new(out.read_pointer)
|
|
25
|
+
ensure
|
|
26
|
+
out.free
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# @api private
|
|
31
|
+
def self.finalizer_for(state)
|
|
32
|
+
proc do
|
|
33
|
+
state.mutex.synchronize do
|
|
34
|
+
unless state.closed
|
|
35
|
+
state.closed = true
|
|
36
|
+
h = state.handle
|
|
37
|
+
state.handle = nil
|
|
38
|
+
Native.chat_xdk_file_decryptor_free(h) if h && !h.null?
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# @api private
|
|
45
|
+
def initialize(handle)
|
|
46
|
+
@state = HandleState.new(handle, false, Mutex.new)
|
|
47
|
+
ObjectSpace.define_finalizer(self, self.class.finalizer_for(@state))
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Decrypt a chunk of encrypted bytes, returning the plaintext bytes
|
|
51
|
+
# available so far (may be empty until enough input has arrived).
|
|
52
|
+
#
|
|
53
|
+
# @param ciphertext [String] encrypted binary bytes
|
|
54
|
+
# @return [String] decrypted binary bytes
|
|
55
|
+
def push(ciphertext)
|
|
56
|
+
raise ArgumentError, "expected String, got #{ciphertext.class}" unless ciphertext.is_a?(String)
|
|
57
|
+
|
|
58
|
+
b64 = Native.b64_encode(ciphertext)
|
|
59
|
+
result_b64 = with_handle { |h| Native.consume(Native.chat_xdk_file_decryptor_push(h, Native.utf8_ptr(b64))) }
|
|
60
|
+
Native.b64_decode(result_b64)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Decrypt the final frame and consume the decryptor. Raises if truncated.
|
|
64
|
+
#
|
|
65
|
+
# @return [String] final decrypted binary bytes
|
|
66
|
+
def finish
|
|
67
|
+
result_b64 = with_handle { |h| Native.consume(Native.chat_xdk_file_decryptor_finish(h)) }
|
|
68
|
+
Native.b64_decode(result_b64)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# @return [Boolean] whether this decryptor has been closed
|
|
72
|
+
def closed?
|
|
73
|
+
@state.mutex.synchronize { @state.closed }
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Free the native decryptor handle. Idempotent.
|
|
77
|
+
def close
|
|
78
|
+
h = nil
|
|
79
|
+
@state.mutex.synchronize do
|
|
80
|
+
return if @state.closed
|
|
81
|
+
|
|
82
|
+
@state.closed = true
|
|
83
|
+
h = @state.handle
|
|
84
|
+
@state.handle = nil
|
|
85
|
+
end
|
|
86
|
+
ObjectSpace.undefine_finalizer(self)
|
|
87
|
+
Native.chat_xdk_file_decryptor_free(h) if h && !h.null?
|
|
88
|
+
nil
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
private
|
|
92
|
+
|
|
93
|
+
def with_handle
|
|
94
|
+
h = @state.mutex.synchronize do
|
|
95
|
+
raise InvalidStateError.new("FileDecryptor is closed", kind: :invalid_state) if @state.closed || @state.handle.nil?
|
|
96
|
+
|
|
97
|
+
@state.handle
|
|
98
|
+
end
|
|
99
|
+
yield(h)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ChatXdk
|
|
4
|
+
# Incremental encryptor for one file of an outgoing message.
|
|
5
|
+
#
|
|
6
|
+
# Feed the file in chunks with {#push}, then call {#finish} once to emit the
|
|
7
|
+
# final frame. Always {#close} the instance when finished (or use the block
|
|
8
|
+
# form of {Chat#file_encryptor}) to release native resources.
|
|
9
|
+
#
|
|
10
|
+
# Not thread-safe: do not share one instance across threads without external
|
|
11
|
+
# synchronization.
|
|
12
|
+
class FileEncryptor
|
|
13
|
+
# @api private
|
|
14
|
+
HandleState = Struct.new(:handle, :closed, :mutex)
|
|
15
|
+
private_constant :HandleState
|
|
16
|
+
|
|
17
|
+
# @api private
|
|
18
|
+
def self.create(chat_handle, params_json)
|
|
19
|
+
out = FFI::MemoryPointer.new(:pointer)
|
|
20
|
+
begin
|
|
21
|
+
Native.consume(Native.chat_xdk_file_encryptor_new(chat_handle, Native.utf8_ptr(params_json), out))
|
|
22
|
+
new(out.read_pointer)
|
|
23
|
+
ensure
|
|
24
|
+
out.free
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# @api private
|
|
29
|
+
def self.finalizer_for(state)
|
|
30
|
+
proc do
|
|
31
|
+
state.mutex.synchronize do
|
|
32
|
+
unless state.closed
|
|
33
|
+
state.closed = true
|
|
34
|
+
h = state.handle
|
|
35
|
+
state.handle = nil
|
|
36
|
+
Native.chat_xdk_file_encryptor_free(h) if h && !h.null?
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# @api private
|
|
43
|
+
def initialize(handle)
|
|
44
|
+
@state = HandleState.new(handle, false, Mutex.new)
|
|
45
|
+
ObjectSpace.define_finalizer(self, self.class.finalizer_for(@state))
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Encrypt a chunk of plaintext bytes, returning the encrypted bytes
|
|
49
|
+
# available so far (may be empty for small inputs).
|
|
50
|
+
#
|
|
51
|
+
# @param plaintext [String] binary bytes
|
|
52
|
+
# @return [String] encrypted binary bytes
|
|
53
|
+
def push(plaintext)
|
|
54
|
+
raise ArgumentError, "expected String, got #{plaintext.class}" unless plaintext.is_a?(String)
|
|
55
|
+
|
|
56
|
+
b64 = Native.b64_encode(plaintext)
|
|
57
|
+
result_b64 = with_handle { |h| Native.consume(Native.chat_xdk_file_encryptor_push(h, Native.utf8_ptr(b64))) }
|
|
58
|
+
Native.b64_decode(result_b64)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Emit the final frame and consume the encryptor.
|
|
62
|
+
#
|
|
63
|
+
# @return [String] final encrypted binary bytes
|
|
64
|
+
def finish
|
|
65
|
+
result_b64 = with_handle { |h| Native.consume(Native.chat_xdk_file_encryptor_finish(h)) }
|
|
66
|
+
Native.b64_decode(result_b64)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# @return [Boolean] whether this encryptor has been closed
|
|
70
|
+
def closed?
|
|
71
|
+
@state.mutex.synchronize { @state.closed }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Free the native encryptor handle. Idempotent.
|
|
75
|
+
def close
|
|
76
|
+
h = nil
|
|
77
|
+
@state.mutex.synchronize do
|
|
78
|
+
return if @state.closed
|
|
79
|
+
|
|
80
|
+
@state.closed = true
|
|
81
|
+
h = @state.handle
|
|
82
|
+
@state.handle = nil
|
|
83
|
+
end
|
|
84
|
+
ObjectSpace.undefine_finalizer(self)
|
|
85
|
+
Native.chat_xdk_file_encryptor_free(h) if h && !h.null?
|
|
86
|
+
nil
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
private
|
|
90
|
+
|
|
91
|
+
def with_handle
|
|
92
|
+
h = @state.mutex.synchronize do
|
|
93
|
+
raise InvalidStateError.new("FileEncryptor is closed", kind: :invalid_state) if @state.closed || @state.handle.nil?
|
|
94
|
+
|
|
95
|
+
@state.handle
|
|
96
|
+
end
|
|
97
|
+
yield(h)
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
Binary file
|