Class: OpenVPN::Client
- Inherits:
-
Object
- Object
- OpenVPN::Client
- Defined in:
- lib/openvpn.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#emitter ⇒ Object
readonly
Returns the value of attribute emitter.
Instance Method Summary collapse
- #authorize(auth) ⇒ Object
- #cmd(command) ⇒ Object
- #connect(params = {}) ⇒ Object
- #connect_and_kill(params = {}) ⇒ Object
- #destroy ⇒ Object
- #disconnect ⇒ Object
- #emit(event, *args) ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #on(event, &block) ⇒ Object
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
9 10 11 12 |
# File 'lib/openvpn.rb', line 9 def initialize @connection = nil @emitter = Hash.new { |h, k| h[k] = [] } end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
7 8 9 |
# File 'lib/openvpn.rb', line 7 def connection @connection end |
#emitter ⇒ Object (readonly)
Returns the value of attribute emitter.
7 8 9 |
# File 'lib/openvpn.rb', line 7 def emitter @emitter end |
Instance Method Details
#authorize(auth) ⇒ Object
63 64 65 66 |
# File 'lib/openvpn.rb', line 63 def (auth) open_vpn_management(%(username "Auth" "#{auth[:user]}")) open_vpn_management(%(password "Auth" "#{auth[:pass]}")) end |
#cmd(command) ⇒ Object
72 73 74 |
# File 'lib/openvpn.rb', line 72 def cmd(command) open_vpn_management(command) end |
#connect(params = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/openvpn.rb', line 29 def connect(params = {}) host = params[:host] || '127.0.0.1' port = params[:port] || 1337 timeout = params[:timeout] || 2 Thread.new do begin @connection = Net::Telnet.new( 'Host' => host, 'Port' => port, 'Timeout' => timeout, 'Prompt' => // ) open_vpn_log open_vpn_management('pid') open_vpn_management('bytecount 1') open_vpn_management('hold release') emit(:connected) rescue StandardError => e emit(:error, e.) end end self end |
#connect_and_kill(params = {}) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/openvpn.rb', line 56 def connect_and_kill(params = {}) connect(params) sleep(1) disconnect_open_vpn self end |
#destroy ⇒ Object
22 23 24 25 26 27 |
# File 'lib/openvpn.rb', line 22 def destroy if @connection @connection.close rescue nil @connection = nil end end |
#disconnect ⇒ Object
68 69 70 |
# File 'lib/openvpn.rb', line 68 def disconnect disconnect_open_vpn end |
#emit(event, *args) ⇒ Object
18 19 20 |
# File 'lib/openvpn.rb', line 18 def emit(event, *args) @emitter[event].each { |block| block.call(*args) } end |
#on(event, &block) ⇒ Object
14 15 16 |
# File 'lib/openvpn.rb', line 14 def on(event, &block) @emitter[event] << block if block_given? end |