copper-cti 2.2.0
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/src/code/CTI/Builder/FileBuilder.rb +36 -0
- data/src/code/CTI/Builder/NullBuilder.rb +29 -0
- data/src/code/CTI/Builder/StreamBuilder.rb +212 -0
- data/src/code/CTI/CTIP2/CTIP2.rb +396 -0
- data/src/code/CTI/CTIP2/Utils.rb +119 -0
- data/src/code/CTI/Driver.rb +70 -0
- data/src/code/CTI/Results/DirectoryResults.rb +31 -0
- data/src/code/CTI/Results/SingleResult.rb +36 -0
- data/src/code/CTI/Session.rb +566 -0
- data/src/code/CTI.rb +53 -0
- metadata +52 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
module CTI
|
|
2
|
+
module CTIP2
|
|
3
|
+
=begin rdoc
|
|
4
|
+
32ビット数値をビッグインディアンで書き出します。
|
|
5
|
+
|
|
6
|
+
a:: 数値
|
|
7
|
+
戻り値:: 書き込んだバイト数
|
|
8
|
+
=end
|
|
9
|
+
def write_int(a)
|
|
10
|
+
self.write([a].pack('N'))
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
=begin rdoc
|
|
14
|
+
64ビット数値をビッグインディアンで書き出します。
|
|
15
|
+
|
|
16
|
+
a:: 数値
|
|
17
|
+
戻り値:: 書き込んだバイト数
|
|
18
|
+
=end
|
|
19
|
+
|
|
20
|
+
def write_long(a)
|
|
21
|
+
self.write([a >> 32, a & 0xFFFFFFFF].pack('NN'))
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
=begin rdoc
|
|
25
|
+
8ビット数値を書き出します。
|
|
26
|
+
|
|
27
|
+
a:: 数値
|
|
28
|
+
戻り値:: 書き込んだバイト数
|
|
29
|
+
=end
|
|
30
|
+
|
|
31
|
+
def write_byte(b)
|
|
32
|
+
self.write([b].pack('C'))
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
=begin rdoc
|
|
36
|
+
バイト数を16ビットビッグインディアンで書き出した後、バイト列を書き出します。
|
|
37
|
+
|
|
38
|
+
b:: バイト列
|
|
39
|
+
戻り値:: 書き込んだバイト数
|
|
40
|
+
=end
|
|
41
|
+
|
|
42
|
+
def write_bytes(b)
|
|
43
|
+
self.write([b.bytesize].pack('n'))
|
|
44
|
+
self.write(b)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
=begin rdoc
|
|
48
|
+
16ビットビッグインディアン数値を読み込みます。
|
|
49
|
+
|
|
50
|
+
戻り値:: 数値、エラーであればfalse
|
|
51
|
+
=end
|
|
52
|
+
|
|
53
|
+
def read_short
|
|
54
|
+
b = self.read(2)
|
|
55
|
+
a = b.unpack('n')
|
|
56
|
+
return a[0]
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
=begin rdoc
|
|
60
|
+
32ビットビッグインディアン数値を読み込みます。
|
|
61
|
+
|
|
62
|
+
戻り値:: 数値、エラーであればfalse
|
|
63
|
+
=end
|
|
64
|
+
|
|
65
|
+
def read_int
|
|
66
|
+
b = self.read(4)
|
|
67
|
+
a = b.unpack('N')
|
|
68
|
+
return a[0]
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
=begin rdoc
|
|
72
|
+
64ビットビッグインディアン数値を読み込みます。
|
|
73
|
+
|
|
74
|
+
戻り値:: 数値、エラーであればfalse
|
|
75
|
+
=end
|
|
76
|
+
|
|
77
|
+
def read_long
|
|
78
|
+
b = self.read(8)
|
|
79
|
+
a = b.unpack('NN')
|
|
80
|
+
h = a[0]
|
|
81
|
+
l = a[1]
|
|
82
|
+
if h >> 31 != 0 then
|
|
83
|
+
h ^= 0xFFFFFFFF
|
|
84
|
+
l ^= 0xFFFFFFFF
|
|
85
|
+
b = (h << 32) | l
|
|
86
|
+
b = -(b + 1)
|
|
87
|
+
else
|
|
88
|
+
b = (h << 32) | l
|
|
89
|
+
end
|
|
90
|
+
return b;
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
=begin rdoc
|
|
94
|
+
8ビット数値を読み込みます。
|
|
95
|
+
|
|
96
|
+
戻り値:: 数値、エラーであればfalse
|
|
97
|
+
=end
|
|
98
|
+
|
|
99
|
+
def read_byte
|
|
100
|
+
b = self.read(1)
|
|
101
|
+
b = b.unpack('C')
|
|
102
|
+
return b[0]
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
=begin rdoc
|
|
106
|
+
16ビットビッグインディアン数値を読み込み、そのバイト数だけバイト列を読み込みます。
|
|
107
|
+
|
|
108
|
+
戻り値:: バイト列、エラーであればfalse
|
|
109
|
+
=end
|
|
110
|
+
|
|
111
|
+
def read_bytes
|
|
112
|
+
b = self.read(2)
|
|
113
|
+
a = b.unpack('n')
|
|
114
|
+
len = a[0]
|
|
115
|
+
b = self.read(len);
|
|
116
|
+
return b;
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
require File.expand_path('Session', File.dirname(__FILE__))
|
|
2
|
+
require 'socket'
|
|
3
|
+
|
|
4
|
+
module CTI
|
|
5
|
+
=begin rdoc
|
|
6
|
+
Version:: $Id: Driver.rb 902 2013-04-23 05:07:04Z miyabe $
|
|
7
|
+
|
|
8
|
+
ドライバクラスです。通常は直接使用する必要はありません。
|
|
9
|
+
代わりに CTI#get_session メソッドを使用してください。
|
|
10
|
+
=end
|
|
11
|
+
class Driver
|
|
12
|
+
public
|
|
13
|
+
=begin rdoc
|
|
14
|
+
指定されたURIに接続し、セッションを返します。
|
|
15
|
+
|
|
16
|
+
uri:: 接続先アドレス
|
|
17
|
+
options:: 接続オプション
|
|
18
|
+
|
|
19
|
+
返り値:: CTI::Session オブジェクト
|
|
20
|
+
=end
|
|
21
|
+
def get_session(uri, options = {}, &block)
|
|
22
|
+
# uriの解析
|
|
23
|
+
host = 'localhost'
|
|
24
|
+
port = 8099
|
|
25
|
+
ssl = false
|
|
26
|
+
if /^ctips:\/\/([^:\/]+):([0-9]+)\/?$/ =~ uri then
|
|
27
|
+
ssl = true
|
|
28
|
+
host = $1
|
|
29
|
+
port = $2.to_i
|
|
30
|
+
elsif /^ctips:\/\/([^:\/]+)\/?$/ =~ uri then
|
|
31
|
+
ssl = true
|
|
32
|
+
host = $1
|
|
33
|
+
elsif /^ctip:\/\/([^:\/]+):([0-9]+)\/?$/ =~ uri then
|
|
34
|
+
host = $1
|
|
35
|
+
port = $2.to_i
|
|
36
|
+
elsif /^ctip:\/\/([^:\/]+)\/?$/ =~ uri then
|
|
37
|
+
host = $1
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
io = TCPSocket.open(host, port)
|
|
41
|
+
if ssl
|
|
42
|
+
# TLSを使う場合
|
|
43
|
+
#
|
|
44
|
+
# **証明書を検証し、SNIを送る。**素の SSLSocket.new(io) は
|
|
45
|
+
# 検証を一切しない(VERIFY_NONE)ので、TLSにしても相手が誰かを
|
|
46
|
+
# 確かめないまま話すことになる。SNIも送らないため、名前で証明書を
|
|
47
|
+
# 選ぶ中継(Traefik など)からは既定の自己署名証明書が返ってくる。
|
|
48
|
+
require 'openssl'
|
|
49
|
+
ctx = OpenSSL::SSL::SSLContext.new
|
|
50
|
+
ctx.set_params # 既定のCAと VERIFY_PEER を入れる
|
|
51
|
+
io = OpenSSL::SSL::SSLSocket.new(io, ctx)
|
|
52
|
+
io.hostname = host # SNI
|
|
53
|
+
io.sync_close = true
|
|
54
|
+
io.connect
|
|
55
|
+
io.post_connection_check(host) # 証明書の名前がホストと一致するか
|
|
56
|
+
end
|
|
57
|
+
session = Session.new(io, options)
|
|
58
|
+
if block
|
|
59
|
+
# ブロック実行
|
|
60
|
+
begin
|
|
61
|
+
block.call(session)
|
|
62
|
+
ensure
|
|
63
|
+
session.close()
|
|
64
|
+
end
|
|
65
|
+
else
|
|
66
|
+
return session
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require File.expand_path('../Builder/FileBuilder', File.dirname(__FILE__))
|
|
2
|
+
|
|
3
|
+
module CTI
|
|
4
|
+
module Results
|
|
5
|
+
=begin rdoc
|
|
6
|
+
Version:: $Id: DirectoryResults.rb 902 2013-04-23 05:07:04Z miyabe $
|
|
7
|
+
|
|
8
|
+
ディレクトリに複数のファイルとして結果を得るためのオブジェクトです。
|
|
9
|
+
=end
|
|
10
|
+
class DirectoryResults
|
|
11
|
+
=begin rdoc
|
|
12
|
+
結果オブジェクトを作成します。
|
|
13
|
+
|
|
14
|
+
dir:: 出力先ディレクトリ
|
|
15
|
+
prefix:: 結果ファイル名の先頭に付ける文字列
|
|
16
|
+
suffix:: 結果ファイル名の末尾に付ける文字列
|
|
17
|
+
=end
|
|
18
|
+
def initialize(dir, prefix = '', suffix = '')
|
|
19
|
+
@dir = dir
|
|
20
|
+
@prefix = prefix
|
|
21
|
+
@suffix = suffix
|
|
22
|
+
@counter = 0
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def next_builder(opts = {})
|
|
26
|
+
@counter += 1
|
|
27
|
+
return CTI::Builder::FileBuilder.new("#{@dir}/#{@prefix}#{@counter}#{@suffix}")
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
require File.expand_path('../Builder/NullBuilder', File.dirname(__FILE__))
|
|
2
|
+
|
|
3
|
+
module CTI
|
|
4
|
+
=begin rdoc
|
|
5
|
+
このモジュールのクラスは、 CTI::Session#set_results に渡して結果を得るために使います。
|
|
6
|
+
=end
|
|
7
|
+
module Results
|
|
8
|
+
=begin rdoc
|
|
9
|
+
Version:: $Id: SingleResult.rb 902 2013-04-23 05:07:04Z miyabe $
|
|
10
|
+
|
|
11
|
+
単一の結果を得るためのオブジェクトです。
|
|
12
|
+
=end
|
|
13
|
+
class SingleResult
|
|
14
|
+
=begin rdoc
|
|
15
|
+
結果オブジェクトを作成します。
|
|
16
|
+
|
|
17
|
+
builder:: CTI::Builder オブジェクト
|
|
18
|
+
&block:: 結果が出力される直前に呼び出されるブロックです。引数にハッシュ型として結果に関する情報が渡されます。
|
|
19
|
+
ハッシュには'uri', 'mime_type', 'encoding', 'length'というキーでそれぞれURI, MIME型, 文字コード, 結果長さが格納されます。
|
|
20
|
+
ただし、'encoding', 'length'は必ずしも提供されません。
|
|
21
|
+
=end
|
|
22
|
+
def initialize(builder, &block)
|
|
23
|
+
@builder = builder
|
|
24
|
+
@block = block
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def next_builder(opts = {})
|
|
28
|
+
return CTI::Builder::NullBuilder.new unless @builder
|
|
29
|
+
@block.call(opts) if @block
|
|
30
|
+
builder = @builder
|
|
31
|
+
@builder = nil
|
|
32
|
+
return builder
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|