network-facade 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/network-facade-ssl +23 -106
- data/lib/network-facade.rb +0 -123
- data/lib/network-facade/base.rb +71 -45
- data/lib/network-facade/cert.rb +90 -0
- data/lib/network-facade/config.rb +1 -1
- data/lib/network-facade/rest.rb +29 -7
- data/lib/network-facade/ssl.rb +1 -0
- data/lib/network-facade/tcp.rb +8 -2
- data/lib/network-facade/unix.rb +7 -2
- metadata +3 -2
data/bin/network-facade-ssl
CHANGED
@@ -1,117 +1,34 @@
|
|
1
1
|
#!/usr/bin/ruby
|
2
|
-
require 'network-facade'
|
2
|
+
require 'network-facade/cert'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
raise "Missing option #{o}" if opts[o].nil?
|
7
|
-
end
|
4
|
+
if $0 == __FILE__
|
5
|
+
# Authority
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
[ 'CN', 'CA' ]
|
14
|
-
]
|
15
|
-
|
16
|
-
cert_opts = {
|
17
|
-
:is_ca => true,
|
18
|
-
:name => name,
|
19
|
-
:comment => 'NetworkFacade Certification Authority',
|
20
|
-
:period => 3650
|
7
|
+
CA = {
|
8
|
+
:country => 'FR',
|
9
|
+
:hostname => 'demo',
|
10
|
+
:domainname => 'example.com'
|
21
11
|
}
|
22
12
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
def create_cert(opts)
|
27
|
-
[ :country, :hostname, :domainname, :ca_cert, :ca_key ].each do |o|
|
28
|
-
raise "Missing option #{o}" if opts[o].nil?
|
29
|
-
end
|
13
|
+
cert = NetworkFacade::SSL.create_ca(CA)
|
14
|
+
File.open('ca.key', 'w') { |fd| fd.puts cert[:key] }
|
15
|
+
File.open('ca.cert', 'w') { |fd| fd.puts cert[:cert].to_pem }
|
30
16
|
|
31
|
-
|
32
|
-
[ 'C', opts[:country], OpenSSL::ASN1::PRINTABLESTRING ],
|
33
|
-
[ 'O', opts[:domainname], OpenSSL::ASN1::UTF8STRING ],
|
34
|
-
[ 'OU', opts[:hostname], OpenSSL::ASN1::UTF8STRING ]
|
35
|
-
]
|
17
|
+
# Client / Server
|
36
18
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
:
|
42
|
-
:
|
43
|
-
:ca_key => opts[:ca_key],
|
44
|
-
:comment => opts[:comment] || 'NetworkFacade Generated Certificate',
|
45
|
-
:period => 365
|
19
|
+
OPTS = {
|
20
|
+
:country => CA[:country],
|
21
|
+
:hostname => CA[:hostname],
|
22
|
+
:domainname => CA[:domainname],
|
23
|
+
:ca_cert => 'ca.cert',
|
24
|
+
:ca_key => 'ca.key'
|
46
25
|
}
|
47
26
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
def create(opts)
|
52
|
-
key = OpenSSL::PKey::RSA.new(1024)
|
53
|
-
|
54
|
-
cert = OpenSSL::X509::Certificate.new
|
55
|
-
cert.subject = ::OpenSSL::X509::Name.new(opts[:name])
|
56
|
-
cert.issuer = opts[:is_ca] ? cert.subject : opts[:ca_cert].subject
|
57
|
-
cert.not_before = Time.now
|
58
|
-
cert.not_after = Time.now + (opts[:period] * 24 * 60 * 60)
|
59
|
-
cert.public_key = key.public_key
|
60
|
-
cert.serial = opts[:is_ca] ? 0x0 : 0x2
|
61
|
-
cert.version = 2
|
62
|
-
|
63
|
-
ef = OpenSSL::X509::ExtensionFactory.new
|
64
|
-
ef.subject_certificate = cert
|
65
|
-
ef.issuer_certificate = opts[:is_ca] ? cert : opts[:ca_cert]
|
66
|
-
|
67
|
-
if opts[:is_ca]
|
68
|
-
cert.extensions = [
|
69
|
-
ef.create_extension('basicConstraints','CA:TRUE', true),
|
70
|
-
ef.create_extension('nsComment', opts[:comment]),
|
71
|
-
ef.create_extension('subjectKeyIdentifier', 'hash'),
|
72
|
-
ef.create_extension('keyUsage', 'cRLSign,keyCertSign', true)
|
73
|
-
]
|
74
|
-
cert.add_extension(ef.create_extension('authorityKeyIdentifier', 'keyid:always,issuer:always'))
|
75
|
-
else
|
76
|
-
cert.extensions = [
|
77
|
-
ef.create_extension('basicConstraints','CA:FALSE', true),
|
78
|
-
ef.create_extension('nsComment', opts[:comment]),
|
79
|
-
ef.create_extension('subjectKeyIdentifier', 'hash'),
|
80
|
-
]
|
81
|
-
cert.add_extension(ef.create_extension('authorityKeyIdentifier', 'keyid:always,issuer:always'))
|
82
|
-
end
|
27
|
+
cert = NetworkFacade::SSL.create_cert(OPTS)
|
28
|
+
File.open('client.key', 'w') { |fd| fd.puts cert[:key] }
|
29
|
+
File.open('client.cert', 'w') { |fd| fd.puts cert[:cert].to_pem }
|
83
30
|
|
84
|
-
cert
|
85
|
-
|
86
|
-
{
|
31
|
+
cert = NetworkFacade::SSL.create_cert(OPTS)
|
32
|
+
File.open('server.key', 'w') { |fd| fd.puts cert[:key] }
|
33
|
+
File.open('server.cert', 'w') { |fd| fd.puts cert[:cert].to_pem }
|
87
34
|
end
|
88
|
-
|
89
|
-
# Authority
|
90
|
-
|
91
|
-
CA = {
|
92
|
-
:country => 'FR',
|
93
|
-
:hostname => 'demo',
|
94
|
-
:domainname => 'example.com'
|
95
|
-
}
|
96
|
-
|
97
|
-
cert = create_ca(CA)
|
98
|
-
File.open('ca.key', 'w') { |fd| fd.puts cert[:key] }
|
99
|
-
File.open('ca.cert', 'w') { |fd| fd.puts cert[:cert].to_pem }
|
100
|
-
|
101
|
-
# Client / Server
|
102
|
-
|
103
|
-
OPTS = {
|
104
|
-
:country => CA[:country],
|
105
|
-
:hostname => CA[:hostname],
|
106
|
-
:domainname => CA[:domainname],
|
107
|
-
:ca_cert => 'ca.cert',
|
108
|
-
:ca_key => 'ca.key'
|
109
|
-
}
|
110
|
-
|
111
|
-
cert = create_cert(OPTS)
|
112
|
-
File.open('client.key', 'w') { |fd| fd.puts cert[:key] }
|
113
|
-
File.open('client.cert', 'w') { |fd| fd.puts cert[:cert].to_pem }
|
114
|
-
|
115
|
-
cert = create_cert(OPTS)
|
116
|
-
File.open('server.key', 'w') { |fd| fd.puts cert[:key] }
|
117
|
-
File.open('server.cert', 'w') { |fd| fd.puts cert[:cert].to_pem }
|
data/lib/network-facade.rb
CHANGED
@@ -1,8 +1,3 @@
|
|
1
|
-
require 'socket'
|
2
|
-
require 'uri'
|
3
|
-
require 'fcntl'
|
4
|
-
require 'thread'
|
5
|
-
require 'logger'
|
6
1
|
require 'network-facade/config'
|
7
2
|
require 'network-facade/base'
|
8
3
|
require 'network-facade/tcp'
|
@@ -10,121 +5,3 @@ require 'network-facade/unix'
|
|
10
5
|
require 'network-facade/ssl'
|
11
6
|
require 'network-facade/rest'
|
12
7
|
require 'network-facade/defaults'
|
13
|
-
|
14
|
-
if __FILE__ == $0
|
15
|
-
NetworkFacade.log = STDERR
|
16
|
-
|
17
|
-
case ARGV.shift
|
18
|
-
#=======================<client>==============================#
|
19
|
-
when 'client-tcp-uri'
|
20
|
-
|
21
|
-
class Foo < NetworkFacade::Client 'nf://localhost:5042'
|
22
|
-
end
|
23
|
-
f = Foo.new
|
24
|
-
p f.bar
|
25
|
-
|
26
|
-
when 'client-tcp-class'
|
27
|
-
|
28
|
-
class Foo < NetworkFacade::Client
|
29
|
-
end
|
30
|
-
f = Foo.new(:host => 'localhost', :port => 5042)
|
31
|
-
p f.big
|
32
|
-
|
33
|
-
when 'client-unix'
|
34
|
-
|
35
|
-
class Foo < NetworkFacade::Unix::Client
|
36
|
-
end
|
37
|
-
f = Foo.new
|
38
|
-
p f.bar
|
39
|
-
|
40
|
-
when 'client-ssl'
|
41
|
-
|
42
|
-
class Foo < NetworkFacade::SSL::Client 'nf://localhost:5044'
|
43
|
-
end
|
44
|
-
f = Foo.new(:ca => '../cert/ca.cert',
|
45
|
-
:cert => '../cert/client.cert',
|
46
|
-
:key => '../cert/client.key')
|
47
|
-
p f.bar
|
48
|
-
|
49
|
-
when 'client-tcp-compress'
|
50
|
-
|
51
|
-
class Foo < NetworkFacade::Client
|
52
|
-
end
|
53
|
-
p Foo.new(:compress => true).big
|
54
|
-
|
55
|
-
when 'client-rest-rtm'
|
56
|
-
|
57
|
-
class Foo < NetworkFacade::REST::Client 'http://www.rememberthemilk.com/services/rest/'
|
58
|
-
end
|
59
|
-
|
60
|
-
f = Foo.new
|
61
|
-
p f.call(:method => 'rtm.test.echo', :api_key => ARGV.first, :foo => 'bar')
|
62
|
-
|
63
|
-
f = Foo.new(:method => :param, :method_param => 'method')
|
64
|
-
p f.send('rtm.test.echo', :api_key => ARGV.first, :foo => 'bar')
|
65
|
-
|
66
|
-
when 'client-rest-netvibes'
|
67
|
-
|
68
|
-
class Foo < NetworkFacade::REST::Client 'http://www.netvibes.com'
|
69
|
-
end
|
70
|
-
f = Foo.new(:mapping => {
|
71
|
-
:login => '/user/signIn.php',
|
72
|
-
:load => '/get/userData.php',
|
73
|
-
:save => '/save/userData.php'
|
74
|
-
}, :post => [:login, :save])
|
75
|
-
|
76
|
-
p f.login(:email => 'paillerosse@lescampeurs.org', :password => ARGV.first)
|
77
|
-
p f.load
|
78
|
-
|
79
|
-
when 'client-rest-flickr'
|
80
|
-
|
81
|
-
class Foo < NetworkFacade::REST::Client 'http://api.flickr.com/services/rest/'
|
82
|
-
end
|
83
|
-
|
84
|
-
f = Foo.new
|
85
|
-
p f.call(:method => 'flickr.test.echo', :api_key => ARGV.first, :foo => 'bar')
|
86
|
-
|
87
|
-
when 'client-rest-digg'
|
88
|
-
|
89
|
-
class Foo < NetworkFacade::REST::Client 'http://services.digg.com'
|
90
|
-
end
|
91
|
-
f = Foo.new(:mapping => {
|
92
|
-
:apple_stories => '/stories/topic/apple',
|
93
|
-
:me => '/user/pyros'
|
94
|
-
})
|
95
|
-
p f.apple_stories(:appkey => 'http://network-facade.rubyforge.org', :count => 3)
|
96
|
-
p f.apple_stories(:appkey => 'http://network-facade.rubyforge.org', :count => 3, :type => :json)
|
97
|
-
p f.me(:appkey => 'http://network-facade.rubyforge.org')
|
98
|
-
|
99
|
-
#=======================</client>=============================#
|
100
|
-
else
|
101
|
-
class Foo
|
102
|
-
def bar
|
103
|
-
42
|
104
|
-
end
|
105
|
-
def big
|
106
|
-
(Time.now.to_s + "\n") * 10
|
107
|
-
end
|
108
|
-
def error
|
109
|
-
raise "Error"
|
110
|
-
end
|
111
|
-
end
|
112
|
-
case ARGV.shift
|
113
|
-
#=======================<server>==============================#
|
114
|
-
when 'server-tcp'
|
115
|
-
NetworkFacade::Server.new.add(Foo.new).start
|
116
|
-
when 'server-tcp-compress'
|
117
|
-
NetworkFacade::Server.new(:compress => true).add(Foo.new).start
|
118
|
-
when 'server-unix'
|
119
|
-
NetworkFacade::Unix::Server.new.add(Foo.new).start
|
120
|
-
when 'server-ssl'
|
121
|
-
s = NetworkFacade::SSL::Server.new(
|
122
|
-
:port => 5044,
|
123
|
-
:ca => '../cert/ca.cert',
|
124
|
-
:cert => '../cert/server.cert',
|
125
|
-
:key => '../cert/server.key')
|
126
|
-
s.add(Foo.new).start
|
127
|
-
#=======================</server>=============================#
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
data/lib/network-facade/base.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'logger'
|
1
3
|
module NetworkFacade
|
2
4
|
def self.log=(log)
|
3
5
|
@@log = log.is_a?(IO) ? Logger.new(log) : log
|
@@ -51,8 +53,8 @@ module Base
|
|
51
53
|
@uri.path = @options[:path] if @options[:path]
|
52
54
|
@uri.query = @options[:query] if @options[:query]
|
53
55
|
@uri.userinfo = @options[:userinfo] if @options[:userinfo]
|
54
|
-
@mutex = Mutex.new
|
55
56
|
require 'zlib' if @options[:compress]
|
57
|
+
require 'thread' if @options[:mode] == :thread
|
56
58
|
end
|
57
59
|
|
58
60
|
def method_missing(name, *args)
|
@@ -69,9 +71,9 @@ module Base
|
|
69
71
|
end
|
70
72
|
result
|
71
73
|
rescue Errno::EPIPE, EOFError, Errno::EINVAL, Errno::ECONNRESET
|
72
|
-
NetworkFacade.log(:warn, "#{$!.inspect} occured
|
73
|
-
connect
|
74
|
-
retry
|
74
|
+
NetworkFacade.log(:warn, "#{$!.inspect} occured", "#{self.class}##{name}")
|
75
|
+
# connect
|
76
|
+
# retry
|
75
77
|
end
|
76
78
|
end
|
77
79
|
|
@@ -96,16 +98,19 @@ module Base
|
|
96
98
|
end
|
97
99
|
|
98
100
|
class Server
|
101
|
+
attr_reader :thread
|
102
|
+
|
99
103
|
def initialize(options = {})
|
100
104
|
@options = options
|
101
105
|
require 'zlib' if @options[:compress]
|
102
|
-
@
|
106
|
+
@options[:mode] ||= :select
|
107
|
+
@clients = (@options[:mode] == :select ? [@options[:server]] : [])
|
103
108
|
@objs = {}
|
109
|
+
@thread = nil
|
104
110
|
end
|
105
111
|
|
106
|
-
def
|
107
|
-
|
108
|
-
raise "Default Server#accept method"
|
112
|
+
def client_id(client)
|
113
|
+
"0x%08x" % client.object_id
|
109
114
|
end
|
110
115
|
|
111
116
|
def add(obj)
|
@@ -114,51 +119,72 @@ module Base
|
|
114
119
|
@objs[id] = obj
|
115
120
|
self
|
116
121
|
end
|
117
|
-
|
118
|
-
def client_id(client)
|
119
|
-
"0x%08x" % client.object_id
|
120
|
-
end
|
122
|
+
alias_method :<<, :add
|
121
123
|
|
122
124
|
def start
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
if readable.include?(@options[:server])
|
127
|
-
accept
|
128
|
-
readable.delete(@options[:server])
|
129
|
-
end
|
130
|
-
rescue Exception
|
131
|
-
NetworkFacade.log(:warn, "An error occured when accapting new client")
|
132
|
-
NetworkFacade.log(:warn, $!)
|
133
|
-
next
|
134
|
-
end
|
135
|
-
|
136
|
-
readable.each do |client|
|
137
|
-
size = nil
|
138
|
-
data = nil
|
125
|
+
if @options[:mode] == :select
|
126
|
+
loop do
|
127
|
+
readable, writable, errors, timeout = IO.select(@clients)
|
139
128
|
begin
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
if @objs[data[0]].respond_to? data[1]
|
144
|
-
NetworkFacade.log(:info, "Call method #{data[1].inspect} with #{data[2].inspect}", client_id(client))
|
145
|
-
result = @objs[data[0]].send(data[1], *data[2])
|
146
|
-
else
|
147
|
-
NetworkFacade.log(:info, "Call unknown method #{data[1].inspect}", client_id(client))
|
148
|
-
end
|
149
|
-
rescue Exception
|
150
|
-
result = $!
|
151
|
-
NetworkFacade.log(:info, "Error occured when executing #{data[1].inspect} with #{data[2].inspect}", client_id(client))
|
152
|
-
NetworkFacade.log(:info, $!)
|
129
|
+
if readable.include?(@options[:server])
|
130
|
+
@clients << accept
|
131
|
+
readable.delete(@options[:server])
|
153
132
|
end
|
154
|
-
write(client, result)
|
155
133
|
rescue Exception
|
156
|
-
NetworkFacade.log(:
|
157
|
-
|
158
|
-
|
134
|
+
NetworkFacade.log(:warn, "An error occured when accapting new client")
|
135
|
+
NetworkFacade.log(:warn, $!)
|
136
|
+
next
|
137
|
+
end
|
138
|
+
|
139
|
+
readable.each do |client|
|
140
|
+
process(client)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
elsif @options[:mode] == :thread
|
144
|
+
@thread = Thread.new do
|
145
|
+
loop do
|
159
146
|
GC.start
|
147
|
+
Thread.new(accept) do |client|
|
148
|
+
process(client)
|
149
|
+
end
|
160
150
|
end
|
161
151
|
end
|
152
|
+
else
|
153
|
+
raise "Unknown mode #{@options[:mode]}"
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
protected
|
158
|
+
|
159
|
+
def accept
|
160
|
+
NetworkFacade.log(:info, "Accepting new client")
|
161
|
+
raise "Default Server#accept method"
|
162
|
+
end
|
163
|
+
|
164
|
+
def process(client)
|
165
|
+
size = nil
|
166
|
+
data = nil
|
167
|
+
begin
|
168
|
+
data = read(client)
|
169
|
+
result = nil
|
170
|
+
begin
|
171
|
+
if @objs[data[0]].respond_to? data[1]
|
172
|
+
NetworkFacade.log(:info, "Call method #{data[1].inspect} for /#{data[0]} with #{data[2].inspect}", client_id(client))
|
173
|
+
result = @objs[data[0]].send(data[1], *data[2])
|
174
|
+
else
|
175
|
+
NetworkFacade.log(:info, "Call unknown method #{data[1].inspect} for /#{data[0]}", client_id(client))
|
176
|
+
end
|
177
|
+
rescue Exception
|
178
|
+
result = $!
|
179
|
+
NetworkFacade.log(:info, "Error occured when executing #{data[1].inspect} with #{data[2].inspect}", client_id(client))
|
180
|
+
NetworkFacade.log(:info, $!)
|
181
|
+
end
|
182
|
+
write(client, result)
|
183
|
+
rescue Exception
|
184
|
+
NetworkFacade.log(:info, "Close connection", client_id(client))
|
185
|
+
client.close
|
186
|
+
@clients.delete(client)
|
187
|
+
GC.start
|
162
188
|
end
|
163
189
|
end
|
164
190
|
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
|
3
|
+
module NetworkFacade
|
4
|
+
module SSL
|
5
|
+
def self.create_ca(opts = {})
|
6
|
+
[ :country, :hostname, :domainname ].each do |o|
|
7
|
+
raise "Missing option #{o}" if opts[o].nil?
|
8
|
+
end
|
9
|
+
|
10
|
+
name = [
|
11
|
+
[ 'C', opts[:country], OpenSSL::ASN1::PRINTABLESTRING ],
|
12
|
+
[ 'O', opts[:domainname], OpenSSL::ASN1::UTF8STRING ],
|
13
|
+
[ 'OU', opts[:hostname], OpenSSL::ASN1::UTF8STRING ],
|
14
|
+
[ 'CN', 'CA' ]
|
15
|
+
]
|
16
|
+
|
17
|
+
cert_opts = {
|
18
|
+
:is_ca => true,
|
19
|
+
:name => name,
|
20
|
+
:comment => 'NetworkFacade Certification Authority',
|
21
|
+
:period => 3650
|
22
|
+
}
|
23
|
+
|
24
|
+
create(cert_opts)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.create_cert(opts)
|
28
|
+
[ :country, :hostname, :domainname, :ca_cert, :ca_key ].each do |o|
|
29
|
+
raise "Missing option #{o}" if opts[o].nil?
|
30
|
+
end
|
31
|
+
|
32
|
+
name = [
|
33
|
+
[ 'C', opts[:country], OpenSSL::ASN1::PRINTABLESTRING ],
|
34
|
+
[ 'O', opts[:domainname], OpenSSL::ASN1::UTF8STRING ],
|
35
|
+
[ 'OU', opts[:hostname], OpenSSL::ASN1::UTF8STRING ]
|
36
|
+
]
|
37
|
+
|
38
|
+
opts[:ca_cert] = OpenSSL::X509::Certificate.new(File.read(opts[:ca_cert])) if opts[:ca_cert].is_a?(String)
|
39
|
+
opts[:ca_key] = OpenSSL::PKey::RSA.new(File.read(opts[:ca_key])) if opts[:ca_key].is_a?(String)
|
40
|
+
|
41
|
+
cert_opts = {
|
42
|
+
:name => name,
|
43
|
+
:ca_cert => opts[:ca_cert],
|
44
|
+
:ca_key => opts[:ca_key],
|
45
|
+
:comment => opts[:comment] || 'NetworkFacade Generated Certificate',
|
46
|
+
:period => 365
|
47
|
+
}
|
48
|
+
|
49
|
+
create(cert_opts)
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.create(opts)
|
53
|
+
key = OpenSSL::PKey::RSA.new(1024)
|
54
|
+
|
55
|
+
cert = OpenSSL::X509::Certificate.new
|
56
|
+
cert.subject = ::OpenSSL::X509::Name.new(opts[:name])
|
57
|
+
cert.issuer = opts[:is_ca] ? cert.subject : opts[:ca_cert].subject
|
58
|
+
cert.not_before = Time.now
|
59
|
+
cert.not_after = Time.now + (opts[:period] * 24 * 60 * 60)
|
60
|
+
cert.public_key = key.public_key
|
61
|
+
cert.serial = opts[:is_ca] ? 0x0 : 0x2
|
62
|
+
cert.version = 2
|
63
|
+
|
64
|
+
ef = OpenSSL::X509::ExtensionFactory.new
|
65
|
+
ef.subject_certificate = cert
|
66
|
+
ef.issuer_certificate = opts[:is_ca] ? cert : opts[:ca_cert]
|
67
|
+
|
68
|
+
if opts[:is_ca]
|
69
|
+
cert.extensions = [
|
70
|
+
ef.create_extension('basicConstraints','CA:TRUE', true),
|
71
|
+
ef.create_extension('nsComment', opts[:comment]),
|
72
|
+
ef.create_extension('subjectKeyIdentifier', 'hash'),
|
73
|
+
ef.create_extension('keyUsage', 'cRLSign,keyCertSign', true)
|
74
|
+
]
|
75
|
+
cert.add_extension(ef.create_extension('authorityKeyIdentifier', 'keyid:always,issuer:always'))
|
76
|
+
else
|
77
|
+
cert.extensions = [
|
78
|
+
ef.create_extension('basicConstraints','CA:FALSE', true),
|
79
|
+
ef.create_extension('nsComment', opts[:comment]),
|
80
|
+
ef.create_extension('subjectKeyIdentifier', 'hash'),
|
81
|
+
]
|
82
|
+
cert.add_extension(ef.create_extension('authorityKeyIdentifier', 'keyid:always,issuer:always'))
|
83
|
+
end
|
84
|
+
|
85
|
+
cert.sign(opts[:is_ca] ? key : opts[:ca_key], OpenSSL::Digest::SHA1.new)
|
86
|
+
|
87
|
+
{ :key => key, :cert => cert }
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/lib/network-facade/rest.rb
CHANGED
@@ -2,6 +2,7 @@ require 'net/http'
|
|
2
2
|
require 'cgi'
|
3
3
|
require 'rubygems'
|
4
4
|
begin
|
5
|
+
raise LoadError if ENV['FORCE_REXML']
|
5
6
|
require 'xml/libxml'
|
6
7
|
rescue LoadError
|
7
8
|
require 'rexml/document'
|
@@ -11,6 +12,8 @@ begin
|
|
11
12
|
rescue
|
12
13
|
# Do nothing
|
13
14
|
end
|
15
|
+
require 'network-facade/base'
|
16
|
+
require 'network-facade/config'
|
14
17
|
|
15
18
|
|
16
19
|
module NetworkFacade
|
@@ -22,11 +25,30 @@ module REST
|
|
22
25
|
end
|
23
26
|
|
24
27
|
class Client < Base::Client
|
28
|
+
@@post = {}
|
29
|
+
@@mapping = {}
|
30
|
+
|
25
31
|
def initialize(options = {})
|
26
32
|
super
|
27
33
|
@cookies = {}
|
28
34
|
end
|
29
35
|
|
36
|
+
def self.post(*method)
|
37
|
+
@@post[self.name] ||= []
|
38
|
+
@@post[self.name] += method
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.mapping(name, uri = nil)
|
42
|
+
if name.is_a? Hash
|
43
|
+
@@mapping[self.name] = name
|
44
|
+
elsif name.is_a? Symbol and uri.is_a? String
|
45
|
+
@@mapping[self.name] ||= {}
|
46
|
+
@@mapping[self.name][name] = uri
|
47
|
+
else
|
48
|
+
raise 'Bad mapping params'
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
30
52
|
def method_missing(name, *args)
|
31
53
|
|
32
54
|
# Build query string
|
@@ -37,8 +59,8 @@ module REST
|
|
37
59
|
end.join('&')
|
38
60
|
|
39
61
|
# Cutom method mapping defined ?
|
40
|
-
if
|
41
|
-
@uri.path =
|
62
|
+
if @@mapping[self.class.name] and @@mapping[self.class.name][name]
|
63
|
+
@uri.path = @@mapping[self.class.name][name]
|
42
64
|
end
|
43
65
|
|
44
66
|
format = @options[:append_slash] ? '%s/?%s' : '%s?%s'
|
@@ -63,12 +85,12 @@ module REST
|
|
63
85
|
end
|
64
86
|
|
65
87
|
# POST or GET
|
66
|
-
if
|
88
|
+
if @@post[self.class.name].is_a? Array and @@post[self.class.name].include? name
|
67
89
|
req = Net::HTTP::Post.new(path, header)
|
68
90
|
req.form_data = params
|
69
91
|
else
|
70
92
|
req = Net::HTTP::Get.new(path, header)
|
71
|
-
NetworkFacade.log(:info, "GET #{req.path}")
|
93
|
+
NetworkFacade.log(:info, "GET #{@uri.scheme}://#{@uri.host}#{req.path}")
|
72
94
|
end
|
73
95
|
res = http.request(req)
|
74
96
|
|
@@ -82,7 +104,7 @@ module REST
|
|
82
104
|
case res.code
|
83
105
|
when '200'
|
84
106
|
case res.content_type
|
85
|
-
when /xml/
|
107
|
+
when /xml/i
|
86
108
|
if defined? XML
|
87
109
|
parser = XML::Parser.new
|
88
110
|
parser.string = res.body
|
@@ -90,13 +112,13 @@ module REST
|
|
90
112
|
else
|
91
113
|
data = REXML::Document.new(res.body)
|
92
114
|
end
|
93
|
-
when /json/
|
115
|
+
when /json/i
|
94
116
|
if defined? JSON
|
95
117
|
data = JSON.parse res.body
|
96
118
|
else
|
97
119
|
data = res.body
|
98
120
|
end
|
99
|
-
when /php/
|
121
|
+
when /php/i
|
100
122
|
#TODO: http://php.net/serialize, http://hurring.com/code/perl/serialize/
|
101
123
|
raise "Not implemented yet"
|
102
124
|
else
|
data/lib/network-facade/ssl.rb
CHANGED
data/lib/network-facade/tcp.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'socket'
|
2
|
+
require 'network-facade/base'
|
3
|
+
require 'fcntl'
|
1
4
|
module NetworkFacade
|
2
5
|
module TCP
|
3
6
|
|
@@ -11,8 +14,10 @@ module TCP
|
|
11
14
|
class Client < Base::Client
|
12
15
|
def initialize(options = {})
|
13
16
|
super
|
17
|
+
@uri.host = 'localhost' if @uri.host.nil? or @uri.host.empty?
|
18
|
+
@uri.port = PORT if @uri.port.nil?
|
14
19
|
@options[:no_delay] ||= true
|
15
|
-
@client ||= TCPSocket.new(@uri.host
|
20
|
+
@client ||= TCPSocket.new(@uri.host, @uri.port)
|
16
21
|
@client.setsockopt(Socket::SOL_TCP, Socket::TCP_NODELAY, 1) if @options[:no_delay]
|
17
22
|
end
|
18
23
|
end
|
@@ -32,7 +37,7 @@ module TCP
|
|
32
37
|
NetworkFacade.log(:info, "Accept", client.peeraddr[2])
|
33
38
|
client.setsockopt(Socket::SOL_TCP, Socket::TCP_NODELAY, 1) if @options[:no_delay]
|
34
39
|
client.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) if @options[:close_exec]
|
35
|
-
|
40
|
+
client
|
36
41
|
end
|
37
42
|
|
38
43
|
def client_id(client)
|
@@ -42,3 +47,4 @@ module TCP
|
|
42
47
|
|
43
48
|
end
|
44
49
|
end
|
50
|
+
require 'network-facade/defaults'
|
data/lib/network-facade/unix.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'socket'
|
2
|
+
require 'fcntl'
|
3
|
+
require 'network-facade/base'
|
1
4
|
module NetworkFacade
|
2
5
|
module Unix
|
3
6
|
|
@@ -7,6 +10,8 @@ module Unix
|
|
7
10
|
def initialize(options = {})
|
8
11
|
super
|
9
12
|
options[:path] ||= PATH
|
13
|
+
@uri.scheme = 'unix'
|
14
|
+
@uri.path = '/' + self.class.name.downcase
|
10
15
|
@client = UNIXSocket.open(options[:path])
|
11
16
|
end
|
12
17
|
end
|
@@ -15,7 +20,7 @@ module Unix
|
|
15
20
|
def initialize(options = {})
|
16
21
|
options[:path] ||= PATH
|
17
22
|
options[:close_exec] ||= true
|
18
|
-
File.unlink(
|
23
|
+
File.unlink(options[:path]) if File.exists?(options[:path])
|
19
24
|
options[:server] ||= UNIXServer.open(options[:path])
|
20
25
|
super(options)
|
21
26
|
end
|
@@ -23,7 +28,7 @@ module Unix
|
|
23
28
|
def accept
|
24
29
|
client = @options[:server].accept
|
25
30
|
client.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) if @options[:close_exec]
|
26
|
-
|
31
|
+
client
|
27
32
|
end
|
28
33
|
end
|
29
34
|
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
|
|
3
3
|
specification_version: 1
|
4
4
|
name: network-facade
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: "0.
|
7
|
-
date: 2007-05-
|
6
|
+
version: "0.3"
|
7
|
+
date: 2007-05-04 00:00:00 +02:00
|
8
8
|
summary: Object-oriented netwotk facade
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -36,6 +36,7 @@ files:
|
|
36
36
|
- lib/network-facade/defaults.rb
|
37
37
|
- lib/network-facade/ssl.rb
|
38
38
|
- lib/network-facade/rest.rb
|
39
|
+
- lib/network-facade/cert.rb
|
39
40
|
- lib/network-facade.rb
|
40
41
|
test_files: []
|
41
42
|
|