Module: OverSIP::Config
- Extended by:
- Validators, Logger
- Defined in:
- lib/oversip/config.rb,
lib/oversip/config_validators.rb
Defined Under Namespace
Modules: Validators
Constant Summary
collapse
- DEFAULT_CONFIG_DIR =
"/etc/oversip"
- DEFAULT_TLS_DIR =
"tls"
- DEFAULT_TLS_CA_DIR =
"tls/ca"
- DEFAULT_CONFIG_FILE =
"oversip.conf"
- PROXIES_FILE =
"proxies.conf"
- SERVER_FILE =
"server.rb"
- CONFIG_VALIDATIONS =
{
:core => {
:nameservers => [ :ipv4, :multi_value ],
:syslog_facility => [
[ :choices,
%w{ kern user daemon local0 local1 local2 local3 local4 local5 local6 local7 } ]
],
:syslog_level => [
[ :choices,
%w{ debug info notice warn error crit } ]
],
},
:sip => {
:sip_udp => :boolean,
:sip_tcp => :boolean,
:sip_tls => :boolean,
:enable_ipv4 => :boolean,
:listen_ipv4 => :ipv4,
:advertised_ipv4 => :ipv4,
:enable_ipv6 => :boolean,
:listen_ipv6 => :ipv6,
:advertised_ipv6 => :ipv6,
:listen_port => :port,
:listen_port_tls => :port,
:use_tls_tunnel => :boolean,
:listen_port_tls_tunnel => :port,
:callback_on_client_tls_handshake => :boolean,
:local_domains => [ :domain, :multi_value ],
:tcp_keepalive_interval => [ :fixnum, [ :greater_equal_than, 180 ] ],
:record_route_hostname_tls_ipv4 => :domain,
:record_route_hostname_tls_ipv6 => :domain,
},
:websocket => {
:sip_ws => :boolean,
:sip_wss => :boolean,
:enable_ipv4 => :boolean,
:listen_ipv4 => :ipv4,
:advertised_ipv4 => :ipv4,
:enable_ipv6 => :boolean,
:listen_ipv6 => :ipv6,
:advertised_ipv6 => :ipv6,
:listen_port => :port,
:listen_port_tls => :port,
:use_tls_tunnel => :boolean,
:listen_port_tls_tunnel => :port,
:callback_on_client_tls_handshake => :boolean,
:max_ws_message_size => [ :fixnum, [ :minor_than, 1048576 ] ],
:max_ws_frame_size => [ :fixnum, [ :minor_than, 1048576 ] ],
:ws_keepalive_interval => [ :fixnum, [ :greater_equal_than, 180 ] ]
},
:tls => {
:public_cert => [ :readable_file, :tls_pem_chain ],
:private_cert => [ :readable_file, :tls_pem_private ],
:ca_dir => :readable_dir
}
}
Constants included
from Validators
Validators::DOMAIN_REGEXP, Validators::TLS_PEM_CHAIN_REGEXP
Class Method Summary
collapse
Methods included from Logger
close, fg_system_msg2str, init_logger_mq, load_methods, log_id, syslog_system_msg2str, syslog_user_msg2str
Methods included from Validators
boolean, choices, domain, fixnum, greater_equal_than, greater_than, ipv4, ipv4_any, ipv6, ipv6_any, minor_equal_than, minor_than, port, readable_dir, readable_file, string, tls_pem_chain, tls_pem_private
Class Method Details
.discover_local_ip(type) ⇒ Object
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
|
# File 'lib/oversip/config.rb', line 538
def self.discover_local_ip(type)
begin
if type == :ipv4
socket = ::UDPSocket.new ::Socket::AF_INET
socket.connect("1.2.3.4", 1)
ip = socket.local_address.ip_address
socket.close
socket = ::UDPSocket.new ::Socket::AF_INET
elsif type == :ipv6
socket = ::UDPSocket.new ::Socket::AF_INET6
socket.connect("2001::1", 1)
ip = socket.local_address.ip_address
socket.close
socket = ::UDPSocket.new ::Socket::AF_INET6
end
begin
socket.bind ip, 0
rescue => e
log_system_warn "cannot bind in autodiscovered local #{type == :ipv4 ? "IPv4" : "IPv6"} '#{ip}': #{e.message} (#{e.class})"
return false
ensure
socket.close
end
return ip
rescue => e
log_system_warn "cannot autodiscover local #{type == :ipv4 ? "IPv4" : "IPv6"}: #{e.message} (#{e.class})"
return false
end
end
|
.humanize_value(value) ⇒ Object
525
526
527
528
529
530
531
532
533
534
535
536
|
# File 'lib/oversip/config.rb', line 525
def self.humanize_value value
case value
when ::TrueClass ; "yes"
when ::FalseClass ; "no"
when ::NilClass ; "null"
when ::String ; value
when ::Symbol ; value.to_s
when ::Array ; value.join(", ")
when ::Fixnum, ::Float ; value.to_s
else ; value.to_s
end
end
|
.load(config_dir = nil, config_file = nil) ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
# File 'lib/oversip/config.rb', line 132
def self.load config_dir=nil, config_file=nil
@config_dir = (::File.expand_path(config_dir) if config_dir) || DEFAULT_CONFIG_DIR
@config_file = ::File.join(@config_dir, config_file || DEFAULT_CONFIG_FILE)
@proxies_file = ::File.join(@config_dir, PROXIES_FILE)
@server_file = ::File.join(@config_dir, SERVER_FILE)
begin
conf_yaml = ::YAML.load_file @config_file
rescue ::Exception => e
log_system_crit "error loading Main Configuration file '#{@config_file}':"
::OverSIP::Launcher.fatal e
end
begin
proxies_yaml = ::YAML.load_file @proxies_file
rescue ::Exception => e
log_system_crit "error loading Proxies Configuration file '#{@proxies_file}':"
::OverSIP::Launcher.fatal e
end
begin
require @server_file
rescue ::Exception => e
log_system_crit "error loading Server file '#{@server_file}':"
::OverSIP::Launcher.fatal e
end
begin
pre_check(conf_yaml)
CONFIG_VALIDATIONS.each_key do |section|
CONFIG_VALIDATIONS[section].each do |parameter, validations|
values = conf_yaml[section.to_s][parameter.to_s] rescue nil
validations = [ validations ] unless validations.is_a?(Array)
if values == nil
if validations.include? :required
::OverSIP::Launcher.fatal "#{section}[#{parameter}] requires a value"
end
next
end
if values.is_a? ::Array
unless validations.include? :multi_value
::OverSIP::Launcher.fatal "#{section}[#{parameter}] does not allow multiple values"
end
if validations.include? :non_empty and values.empty?
::OverSIP::Launcher.fatal "#{section}[#{parameter}] does not allow empty values"
end
end
values = ( values.is_a?(::Array) ? values : [ values ] )
values.each do |value|
validations.each do |validation|
if validation.is_a? ::Symbol
args = []
elsif validation.is_a? ::Array
args = validation[1..-1]
validation = validation[0]
end
next if [:required, :multi_value, :non_empty].include? validation
unless send validation, value, *args
::OverSIP::Launcher.fatal "#{section}[#{parameter}] has invalid value '#{humanize_value value}' (does not satisfy '#{validation}' validation requirement)"
end
end
@configuration[section][parameter] = ( validations.include?(:multi_value) ? values : values[0] )
end
end end
post_process
post_check
rescue ::OverSIP::ConfigurationError => e
::OverSIP::Launcher.fatal "configuration error: #{e.message}"
rescue => e
::OverSIP::Launcher.fatal e
end
::OverSIP.configuration = @configuration
begin
::OverSIP::ProxiesConfig.load proxies_yaml
rescue ::OverSIP::ConfigurationError => e
::OverSIP::Launcher.fatal "error loading Proxies Configuration file '#{@proxies_file}': #{e.message}"
rescue ::Exception => e
log_system_crit "error loading Proxies Configuration file '#{@proxies_file}':"
::OverSIP::Launcher.fatal e
end
end
|
.log_id ⇒ Object
18
19
20
|
# File 'lib/oversip/config.rb', line 18
def self.log_id
@log_id ||= "Config"
end
|
.post_check ⇒ Object
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
# File 'lib/oversip/config.rb', line 371
def self.post_check
binds = { :udp => [], :tcp => [] }
if @configuration[:sip][:enable_ipv4]
ipv4 = @configuration[:sip][:listen_ipv4]
if @configuration[:sip][:sip_udp]
binds[:udp] << [ ipv4, @configuration[:sip][:listen_port] ]
end
if @configuration[:sip][:sip_tcp]
binds[:tcp] << [ ipv4, @configuration[:sip][:listen_port] ]
end
if @configuration[:sip][:sip_tls]
unless @configuration[:sip][:use_tls_tunnel]
binds[:tcp] << [ ipv4, @configuration[:sip][:listen_port_tls] ]
else
binds[:tcp] << [ "127.0.0.1", @configuration[:sip][:listen_port_tls_tunnel] ]
end
end
end
if @configuration[:sip][:enable_ipv6]
ipv6 = @configuration[:sip][:listen_ipv6]
if @configuration[:sip][:sip_udp]
binds[:udp] << [ ipv6, @configuration[:sip][:listen_port] ]
end
if @configuration[:sip][:sip_tcp]
binds[:tcp] << [ ipv6, @configuration[:sip][:listen_port] ]
end
if @configuration[:sip][:sip_tls]
unless @configuration[:sip][:use_tls_tunnel]
binds[:tcp] << [ ipv6, @configuration[:sip][:listen_port_tls] ]
else
binds[:tcp] << [ "::1", @configuration[:sip][:listen_port_tls_tunnel] ]
end
end
end
if @configuration[:websocket][:enable_ipv4]
ipv4 = @configuration[:websocket][:listen_ipv4]
if @configuration[:websocket][:sip_ws]
binds[:tcp] << [ ipv4, @configuration[:websocket][:listen_port] ]
end
if @configuration[:websocket][:sip_wss]
unless @configuration[:sip][:use_tls_tunnel]
binds[:tcp] << [ ipv4, @configuration[:websocket][:listen_port_tls] ]
else
binds[:tcp] << [ "127.0.0.1", @configuration[:websocket][:listen_port_tls_tunnel] ]
end
end
end
if @configuration[:websocket][:enable_ipv6]
ipv6 = @configuration[:websocket][:listen_ipv6]
if @configuration[:websocket][:sip_ws]
binds[:tcp] << [ ipv6, @configuration[:websocket][:listen_port] ]
end
if @configuration[:websocket][:sip_wss]
unless @configuration[:sip][:use_tls_tunnel]
binds[:tcp] << [ ipv6, @configuration[:websocket][:listen_port_tls] ]
else
binds[:tcp] << [ "::1", @configuration[:websocket][:listen_port_tls_tunnel] ]
end
end
end
unless @configuration[:sip][:use_tls_tunnel]
@configuration[:sip][:listen_port_tls_tunnel] = nil
end
unless @configuration[:websocket][:use_tls_tunnel]
@configuration[:websocket][:listen_port_tls_tunnel] = nil
end
[:udp, :tcp].each do |transport|
transport_str = transport.to_s.upcase
binds[transport].each do |ip, port|
begin
unless (ip_type = ::OverSIP::Utils.ip_type(ip))
raise ::OverSIP::ConfigurationError, "given IP '#{ip}' is not IPv4 nor IPv6"
end
case transport
when :udp
case ip_type
when :ipv4
socket = ::UDPSocket.new ::Socket::AF_INET
when :ipv6
socket = ::UDPSocket.new ::Socket::AF_INET6
end
socket.bind ip, port
when :tcp
socket = ::TCPServer.open ip, port
end
socket.close
rescue ::Errno::EADDRNOTAVAIL
raise ::OverSIP::ConfigurationError, "cannot bind in #{transport_str} IP '#{ip}', address not available"
rescue ::Errno::EADDRINUSE
raise ::OverSIP::ConfigurationError, "#{transport_str} IP '#{ip}' and port #{port} already in use"
rescue ::Errno::EACCES
raise ::OverSIP::ConfigurationError, "no permission to bind in #{transport_str} IP '#{ip}' and port #{port}"
rescue => e
raise e.class, "error binding in #{transport_str} IP '#{ip}' and port #{port} (#{e.class}: #{e.message})"
end
end
end
end
|
.post_process ⇒ Object
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
# File 'lib/oversip/config.rb', line 256
def self.post_process
if @configuration[:tls][:public_cert] and @configuration[:tls][:private_cert]
@use_tls = true
full_cert = ::Tempfile.new("oversip_full_cert_")
full_cert.puts ::File.read(@configuration[:tls][:public_cert])
full_cert.puts ::File.read(@configuration[:tls][:private_cert])
@configuration[:tls][:full_cert] = full_cert.path
full_cert.close
else
@configuration[:sip][:sip_tls] = false
@configuration[:websocket][:sip_wss] = false
end
if @configuration[:sip][:sip_udp] or @configuration[:sip][:sip_tcp]
@use_sip_udp_or_tcp = true
else
@configuration[:sip][:listen_port] = nil
end
if @configuration[:sip][:sip_tls] and @use_tls
@use_sip_tls = true
else
@configuration[:sip][:listen_port_tls] = nil
end
unless @use_sip_udp_or_tcp or @use_sip_tls
@configuration[:sip][:listen_ipv4] = nil
@configuration[:sip][:listen_ipv6] = nil
@configuration[:sip][:enable_ipv4] = nil
@configuration[:sip][:enable_ipv6] = nil
end
unless @configuration[:sip][:enable_ipv4]
@configuration[:sip][:listen_ipv4] = nil
@configuration[:sip][:advertised_ipv4] = nil
@configuration[:sip][:record_route_hostname_tls_ipv4] = nil
end
unless @configuration[:sip][:enable_ipv6]
@configuration[:sip][:listen_ipv6] = nil
@configuration[:sip][:advertised_ipv6] = nil
@configuration[:sip][:record_route_hostname_tls_ipv6] = nil
end
if @configuration[:websocket][:sip_ws]
@use_sip_ws = true
else
@configuration[:websocket][:listen_port] = nil
end
if @configuration[:websocket][:sip_wss] and @use_tls
@use_sip_wss = true
else
@configuration[:websocket][:listen_port_tls] = nil
end
unless @use_sip_ws or @use_sip_wss
@configuration[:websocket][:listen_ipv4] = nil
@configuration[:websocket][:listen_ipv6] = nil
@configuration[:websocket][:enable_ipv4] = nil
@configuration[:websocket][:enable_ipv6] = nil
end
unless @configuration[:websocket][:enable_ipv4]
@configuration[:websocket][:listen_ipv4] = nil
@configuration[:websocket][:advertised_ipv4] = nil
end
unless @configuration[:websocket][:enable_ipv6]
@configuration[:websocket][:listen_ipv6] = nil
@configuration[:websocket][:advertised_ipv6] = nil
end
if ( @use_sip_udp_or_tcp or @use_sip_tls ) and @configuration[:sip][:listen_ipv4] == nil and @configuration[:sip][:enable_ipv4]
unless (@configuration[:sip][:listen_ipv4] = discover_local_ip(:ipv4))
log_system_warn "disabling IPv4 for SIP"
@configuration[:sip][:listen_ipv4] = nil
@configuration[:sip][:enable_ipv4] = false
end
end
if ( @use_sip_udp_or_tcp or @use_sip_tls ) and @configuration[:sip][:listen_ipv6] == nil and @configuration[:sip][:enable_ipv6]
unless (@configuration[:sip][:listen_ipv6] = discover_local_ip(:ipv6))
log_system_warn "disabling IPv6 for SIP"
@configuration[:sip][:listen_ipv6] = nil
@configuration[:sip][:enable_ipv6] = false
end
end
if ( @use_sip_ws or @use_sip_wss ) and @configuration[:websocket][:listen_ipv4] == nil and @configuration[:websocket][:enable_ipv4]
unless (@configuration[:websocket][:listen_ipv4] = discover_local_ip(:ipv4))
log_system_warn "disabling IPv4 for WebSocket"
@configuration[:websocket][:listen_ipv4] = nil
@configuration[:websocket][:enable_ipv4] = false
end
end
if ( @use_sip_ws or @use_sip_wss ) and @configuration[:websocket][:listen_ipv6] == nil and @configuration[:websocket][:enable_ipv6]
unless (@configuration[:websocket][:listen_ipv6] = discover_local_ip(:ipv6))
log_system_warn "disabling IPv6 for WebSocket"
@configuration[:websocket][:listen_ipv6] = nil
@configuration[:websocket][:enable_ipv6] = false
end
end
if @configuration[:sip][:local_domains]
if @configuration[:sip][:local_domains].is_a? ::String
@configuration[:sip][:local_domains] = [ @configuration[:sip][:local_domains].downcase ]
end
@configuration[:sip][:local_domains].each {|local_domain| local_domain.downcase!}
end
end
|
.pre_check(conf_yaml) ⇒ Object
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
# File 'lib/oversip/config.rb', line 236
def self.pre_check conf_yaml
tls_public_cert = conf_yaml["tls"]["public_cert"] rescue nil
tls_private_cert = conf_yaml["tls"]["private_cert"] rescue nil
tls_ca_dir = conf_yaml["tls"]["ca_dir"] rescue nil
if tls_public_cert.is_a?(::String) and tls_public_cert[0] != "/"
conf_yaml["tls"]["public_cert"] = ::File.join(@config_dir, DEFAULT_TLS_DIR, tls_public_cert)
end
if tls_private_cert.is_a?(::String) and tls_private_cert[0] != "/"
conf_yaml["tls"]["private_cert"] = ::File.join(@config_dir, DEFAULT_TLS_DIR, tls_private_cert)
end
if tls_ca_dir.is_a?(::String) and tls_ca_dir[0] != "/"
conf_yaml["tls"]["ca_dir"] = ::File.join(@config_dir, DEFAULT_TLS_DIR, tls_ca_dir)
end
end
|
.print(colorize = true) ⇒ Object
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
|
# File 'lib/oversip/config.rb', line 491
def self.print colorize=true
color = ::Term::ANSIColor if colorize
puts
@configuration.each_key do |section|
if colorize
puts " #{color.bold(section.to_s)}:"
else
puts " #{section.to_s}:"
end
@configuration[section].each do |parameter, value|
humanized_value = humanize_value value
color_value = case value
when ::TrueClass
colorize ? color.bold(color.green(humanized_value)) : humanized_value
when ::FalseClass
colorize ? color.bold(color.red(humanized_value)) : humanized_value
when ::NilClass
humanized_value
when ::String, ::Symbol
colorize ? color.yellow(humanized_value) : humanized_value
when ::Array
colorize ? color.yellow(humanized_value) : humanized_value
when ::Fixnum, ::Float
colorize ? color.bold(color.blue(humanized_value)) : humanized_value
else
humanized_value
end
printf(" %-32s: %s\n", parameter, color_value)
end
puts
end
end
|
.system_reload ⇒ Object
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
|
# File 'lib/oversip/config.rb', line 570
def self.system_reload
log_system_notice "reloading OverSIP..."
begin
proxies_yaml = ::YAML.load_file @proxies_file
::OverSIP::ProxiesConfig.load proxies_yaml, reload=true
log_system_notice "Proxies Configuration file '#{@proxies_file}' reloaded"
rescue ::OverSIP::ConfigurationError => e
log_system_crit "error reloading Proxies Configuration file '#{@proxies_file}': #{e.message}"
rescue ::Exception => e
log_system_crit "error reloading Proxies Configuration file '#{@proxies_file}':"
log_system_crit e
end
begin
::Kernel.load @server_file
log_system_notice "Server file '#{@server_file}' reloaded"
rescue ::Exception => e
log_system_crit "error reloading Server file '#{@server_file}':"
log_system_crit e
end
end
|