Class: OpenVPNServer
- Inherits:
-
Object
- Object
- OpenVPNServer
- Defined in:
- lib/openVPNServer.rb
Instance Method Summary collapse
-
#destroy ⇒ Object
Destroy an openVPNServer telnet session.
-
#initialize(options) ⇒ OpenVPNServer
constructor
Create a new openvpn telnet session.
-
#kill(options) ⇒ Object
Kill the client instance(s) by common name of host:port combination.
-
#load_stats ⇒ Object
Get information about number of clients connected and traffic statistic (byte in & byte out).
-
#mute(n = -1)) ⇒ Object
Set log mute level to n, or show level if n is absent.
-
#pid ⇒ Object
Show process ID of the current OpenVPN process.
-
#signal(s) ⇒ Object
Send signal s to daemon, where s can be SIGHUP, SIGTERM, SIGUSR1, SIGUSR2.
-
#status ⇒ Object
Insteed for each route entry there is: IP/Eth Address (depend on tun/tap mode), Addressing, Uptime.
-
#verb(n = -1)) ⇒ Object
Set log verbosity level to n, or show if n is absent.
-
#version ⇒ Object
Returns a string showing the processes and management interface’s version.
Constructor Details
#initialize(options) ⇒ OpenVPNServer
Create a new openvpn telnet session. Need host and port of server and optionally password for login.
18 19 20 21 22 23 24 25 26 27 28 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 55 56 |
# File 'lib/openVPNServer.rb', line 18 def initialize() pass = nil # Parsing Options - Set to default values if missing if !.has_key?("Host") ["Host"] = "localhost" end if !.has_key?("Port") ["Port"] = 1234 end if !.has_key?("Timeout") ["Timeout"] = 10 end if .has_key?("Password") pass = ["Password"] .delete("Password") end # Add Prompt to options ["Prompt"] = />INFO:OpenVPN.*\n/ # Create Socket Telnet Connection @sock = Net::Telnet::new() # Password Management # ---------------------- # This is just a little trick. # The openvpn telnet server for management requests just password without username. # The Net::Telnet client wait first for username prompt indeed, so we have to deceive it # that there is a user without pass, and this is made inverting the prompt values and # sending just pass prompt and pass value :) if !pass.nil? @sock.login("LoginPrompt" => /ENTER PASSWORD:/, "Name" => pass) end end |
Instance Method Details
#destroy ⇒ Object
Destroy an openVPNServer telnet session.
59 60 61 |
# File 'lib/openVPNServer.rb', line 59 def destroy @sock.close end |
#kill(options) ⇒ Object
Kill the client instance(s) by common name of host:port combination.
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 |
# File 'lib/openVPNServer.rb', line 160 def kill() msg = "kill" cn = nil host = nil port = nil # Searching Options cn = ["CommonName"] if .has_key?("CommonName") host = ["Host"] if .has_key?("Host") port = ["Port"] if .has_key?("Port") if !cn.nil? msg.concat(" #{cn}") @sock.cmd("String" => msg , "Match" => /(SUCCESS:.*\n|ERROR:.*\n|END.*\n)/) do |c| print c end else if !host.nil? && !port.nil? msg.concat(" #{host}:#{port}") @sock.cmd("String" => msg , "Match" => /(SUCCESS:.*\n|ERROR:.*\n|END.*\n)/) else puts "Net::OpenVPN Kill Error (Common Name or Host:Port Combination needed)" end end end |
#load_stats ⇒ Object
Get information about number of clients connected and traffic statistic (byte in & byte out). Return an array of three element, the first is the number of client, second the number of byte in input and third the number of byte in output.
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/openVPNServer.rb', line 111 def load_stats stats_info = [] c = @sock.cmd("String" => "load-stats", "Match" => /(SUCCESS:.*\n|ERROR:.*\n|END.*\n)/) stats_info = c.split(',') # Number of clients stats_info[0] = stats_info[0].gsub("SUCCESS: nclients=", "").to_i # Bytes Input stats_info[1] = stats_info[1].gsub("bytesin=", "").to_i # Bytes Output stats_info[2] = stats_info[2].chop!.gsub("bytesout=", "").to_i return stats_info end |
#mute(n = -1)) ⇒ Object
Set log mute level to n, or show level if n is absent.
153 154 155 156 157 |
# File 'lib/openVPNServer.rb', line 153 def mute(n=-1) mute = "mute" mute.concat(" #{n}") if n >= 0 @sock.cmd("String" => mute , "Match" => /(SUCCESS:.*\n|ERROR:.*\n|END.*\n)/) end |
#pid ⇒ Object
Show process ID of the current OpenVPN process.
130 131 132 |
# File 'lib/openVPNServer.rb', line 130 def pid @sock.cmd("String" => "pid", "Match" => /(SUCCESS:.*\n|ERROR:.*\n|END.*\n)/) end |
#signal(s) ⇒ Object
Send signal s to daemon, where s can be SIGHUP, SIGTERM, SIGUSR1, SIGUSR2.
135 136 137 138 139 140 141 142 143 |
# File 'lib/openVPNServer.rb', line 135 def signal(s) msg = "signal" if s == "SIGHUP" || s == "SIGTERM" || s == "SIGUSR1" || s == "SIGUSR2" msg.concat(" #{s}") @sock.cmd("String" => msg , "Match" => /(SUCCESS:.*\n|ERROR:.*\n|END.*\n)/) else puts "openVPNServer Signal Error (Supported: SIGHUP, SIGTERM, SIGUSR1, SIGUSR2)" end end |
#status ⇒ Object
Insteed for each route entry there is: IP/Eth Address (depend on tun/tap mode), Addressing, Uptime.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/openVPNServer.rb', line 66 def status client_list_flag = 0, routing_list_flag = 0 client_list = [] routing_list = [] c = @sock.cmd("String" => "status", "Match" => /(SUCCESS:.*\n|ERROR:.*\n|END.*\n)/) c.each do |l| # End Information Markers if (l == "ROUTING TABLE\n") client_list_flag = 0 end if (l == "GLOBAL STATS\n") routing_list_flag = 0 end # Update Clients Connected List if client_list_flag == 1 client_list << l.split(',') client_list[-1][-1].chop! end # Update Routing Info List if routing_list_flag == 1 routing_list << l.split(',') routing_list[-1][-1].chop! end # Start Information Markers if (l == "Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since\n") client_list_flag = 1 end if (l == "Virtual Address,Common Name,Real Address,Last Ref\n") routing_list_flag = 1 end end return client_list, routing_list end |
#verb(n = -1)) ⇒ Object
Set log verbosity level to n, or show if n is absent.
146 147 148 149 150 |
# File 'lib/openVPNServer.rb', line 146 def verb(n=-1) verb = "verb" verb.concat(" #{n}") if n >= 0 @sock.cmd("String" => verb , "Match" => /(SUCCESS:.*\n|ERROR:.*\n|END.*\n)/) end |
#version ⇒ Object
Returns a string showing the processes and management interface’s version.
125 126 127 |
# File 'lib/openVPNServer.rb', line 125 def version @sock.cmd("String" => "version", "Match" => /(SUCCESS:.*\n|ERROR:.*\n|END.*\n)/) end |