openVPNServer 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README +68 -0
  2. data/lib/openVPNServer.rb +188 -0
  3. data/test.rb +17 -0
  4. metadata +62 -0
data/README ADDED
@@ -0,0 +1,68 @@
1
+ openVPNServer Management - README File
2
+ ---------------------------------------
3
+
4
+ NOTE: Please check your openVPN server version because there are some commands that aren't implemented in the older versions.
5
+
6
+ Versions:
7
+ 0.0.1 Creation of project. Basic set of commands implemented.
8
+
9
+
10
+ Commands List and Examples of usage
11
+ -----------------------------------
12
+
13
+ new
14
+ Create a new openvpn telnet session. Need host and port of server and optionally password for login.
15
+
16
+ o = Net::OpenVPN::Manage.new("Host" => "myHost", "Port" => 1234, "Timeout" => 10, "Password" => "myPassword")
17
+
18
+ close
19
+ Destroy an openvpn telnet session.
20
+
21
+ o.close
22
+
23
+ status
24
+ Get information about clients connected list and routing table. Return two arrays of arrays with lists inside.
25
+ For each client in client_list array there is: Common Name, Addredding Infos, Bytes in/out, Uptime.
26
+ Insteed for each route entry there is: IP/Eth Address (depend on tun/tap mode), Addressing, Uptime.
27
+
28
+ client_list, routing_list = o.status
29
+ p client_list
30
+ [["Foo", "1.2.3.4:5678", "4.3.2.1", "67264", "87264", "Fri Jul 7 14:20:51 2006", "1152300051"],
31
+ ["Foo2", "2.3.4.5:6789", "5.4.3.2", "12347101", "19043721", "Tue Jul 3 12:10:05 2006", "1150000050"]
32
+ ... ]
33
+ p routing_list
34
+ [["4.3.2.1", "John Doe" ,"1.2.3.4:5678", "Fri Jul 7 14:41:35 2006", "1152301295"],
35
+ ["5.4.3.2", "Jane Doe" ,"2.3.4.5:6789", "Tue Jul 3 12:10:05 2006", "1150000050"]
36
+ ... ]
37
+
38
+ load_stats
39
+ 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.
40
+
41
+ stats_info = o.load_stats
42
+
43
+ kill
44
+ Kill the client instance(s) by common name of host:port combination.
45
+
46
+ o.kill("CommonName" => "myCN") OR o.kill("Host" => "myHost", "Port" => 1234)
47
+
48
+ signal
49
+ Send signal s to daemon, where s can be SIGHUP, SIGTERM, SIGUSR1, SIGUSR2.
50
+
51
+ o.signal("SIGHUP")
52
+
53
+ version
54
+ Returns a string showing the processes and management interface's version.
55
+
56
+ pid
57
+ Show process ID of the current OpenVPN process.
58
+
59
+ mute
60
+ Set log mute level to n, or show level if n is absent.
61
+
62
+ o.mute(n)
63
+
64
+ verb
65
+ Set log verbosity level to n, or show if n is absent.
66
+
67
+ o.verb(n)
68
+
@@ -0,0 +1,188 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Authors:: Alessandro Celestini <a.celestini@gmail.com>, Antonio Davoli <antonio.davoli@gmail.com>, Davide Guerri <d.guerri@caspur.it>
4
+ # Copyright:: Copyright (c) 2010
5
+ # License:: Ruby License
6
+ #
7
+
8
+ # This class was written to simplify the managing of a openVPN
9
+ # server through apposite command sent by a telnet client.
10
+
11
+ require 'net/telnet'
12
+
13
+ class OpenVPNServer
14
+
15
+ @cmd_prompt = /(SUCCESS:.*\n|ERROR:.*\n|END.*\n)/
16
+
17
+ # Create a new openvpn telnet session. Need host and port of server and optionally password for login.
18
+ def initialize(options)
19
+ pass = nil
20
+
21
+ # Parsing Options - Set to default values if missing
22
+ if !options.has_key?("Host")
23
+ options["Host"] = "localhost"
24
+ end
25
+
26
+ if !options.has_key?("Port")
27
+ options["Port"] = 1234
28
+ end
29
+
30
+ if !options.has_key?("Timeout")
31
+ options["Timeout"] = 10
32
+ end
33
+
34
+ if options.has_key?("Password")
35
+ pass = options["Password"]
36
+ options.delete("Password")
37
+ end
38
+
39
+ # Add Prompt to options
40
+ options["Prompt"] = />INFO:OpenVPN.*\n/
41
+
42
+ # Create Socket Telnet Connection
43
+ @sock = Net::Telnet::new(options)
44
+
45
+ # Password Management
46
+ # ----------------------
47
+ # This is just a little trick.
48
+ # The openvpn telnet server for management requests just password without username.
49
+ # The Net::Telnet client wait first for username prompt indeed, so we have to deceive it
50
+ # that there is a user without pass, and this is made inverting the prompt values and
51
+ # sending just pass prompt and pass value :)
52
+
53
+ if !pass.nil?
54
+ @sock.login("LoginPrompt" => /ENTER PASSWORD:/, "Name" => pass)
55
+ end
56
+ end
57
+
58
+ # Destroy an openVPNServer telnet session.
59
+ def destroy
60
+ @sock.close
61
+ end
62
+
63
+ # Get information about clients connected list and routing table. Return two arrays of arrays with lists inside.
64
+ # For each client in client_list array there is: Common Name, Addredding Infos, Bytes in/out, Uptime.
65
+ # Insteed for each route entry there is: IP/Eth Address (depend on tun/tap mode), Addressing, Uptime.
66
+ def status
67
+ client_list_flag = 0, routing_list_flag = 0
68
+ client_list = []
69
+ routing_list = []
70
+
71
+ c = @sock.cmd("String" => "status", "Match" => /(SUCCESS:.*\n|ERROR:.*\n|END.*\n)/)
72
+ c.each do |l|
73
+
74
+ # End Information Markers
75
+ if (l == "ROUTING TABLE\n")
76
+ client_list_flag = 0
77
+ end
78
+
79
+ if (l == "GLOBAL STATS\n")
80
+ routing_list_flag = 0
81
+ end
82
+
83
+ # Update Clients Connected List
84
+ if client_list_flag == 1
85
+ client_list << l.split(',')
86
+ client_list[-1][-1].chop!
87
+ end
88
+
89
+ # Update Routing Info List
90
+ if routing_list_flag == 1
91
+ routing_list << l.split(',')
92
+ routing_list[-1][-1].chop!
93
+ end
94
+
95
+ # Start Information Markers
96
+ if (l == "Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since\n")
97
+ client_list_flag = 1
98
+ end
99
+
100
+ if (l == "Virtual Address,Common Name,Real Address,Last Ref\n")
101
+ routing_list_flag = 1
102
+ end
103
+ end
104
+
105
+ return client_list, routing_list
106
+ end
107
+
108
+ # Get information about number of clients connected and traffic statistic (byte in & byte out).
109
+ #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.
110
+
111
+ def load_stats
112
+ stats_info = []
113
+ c = @sock.cmd("String" => "load-stats", "Match" => /(SUCCESS:.*\n|ERROR:.*\n|END.*\n)/)
114
+ stats_info = c.split(',')
115
+ # Number of clients
116
+ stats_info[0] = stats_info[0].gsub("SUCCESS: nclients=", "").to_i
117
+ # Bytes Input
118
+ stats_info[1] = stats_info[1].gsub("bytesin=", "").to_i
119
+ # Bytes Output
120
+ stats_info[2] = stats_info[2].chop!.gsub("bytesout=", "").to_i
121
+ return stats_info
122
+ end
123
+
124
+ # Returns a string showing the processes and management interface's version.
125
+ def version
126
+ @sock.cmd("String" => "version", "Match" => /(SUCCESS:.*\n|ERROR:.*\n|END.*\n)/)
127
+ end
128
+
129
+ # Show process ID of the current OpenVPN process.
130
+ def pid
131
+ @sock.cmd("String" => "pid", "Match" => /(SUCCESS:.*\n|ERROR:.*\n|END.*\n)/)
132
+ end
133
+
134
+ # Send signal s to daemon, where s can be SIGHUP, SIGTERM, SIGUSR1, SIGUSR2.
135
+ def signal(s)
136
+ msg = "signal"
137
+ if s == "SIGHUP" || s == "SIGTERM" || s == "SIGUSR1" || s == "SIGUSR2"
138
+ msg.concat(" #{s}")
139
+ @sock.cmd("String" => msg , "Match" => /(SUCCESS:.*\n|ERROR:.*\n|END.*\n)/)
140
+ else
141
+ puts "openVPNServer Signal Error (Supported: SIGHUP, SIGTERM, SIGUSR1, SIGUSR2)"
142
+ end
143
+ end
144
+
145
+ # Set log verbosity level to n, or show if n is absent.
146
+ def verb(n=-1)
147
+ verb = "verb"
148
+ verb.concat(" #{n}") if n >= 0
149
+ @sock.cmd("String" => verb , "Match" => /(SUCCESS:.*\n|ERROR:.*\n|END.*\n)/)
150
+ end
151
+
152
+ # Set log mute level to n, or show level if n is absent.
153
+ def mute(n=-1)
154
+ mute = "mute"
155
+ mute.concat(" #{n}") if n >= 0
156
+ @sock.cmd("String" => mute , "Match" => /(SUCCESS:.*\n|ERROR:.*\n|END.*\n)/)
157
+ end
158
+
159
+ # Kill the client instance(s) by common name of host:port combination.
160
+ def kill(options)
161
+
162
+ msg = "kill"
163
+ cn = nil
164
+ host = nil
165
+ port = nil
166
+
167
+ # Searching Options
168
+ cn = options["CommonName"] if options.has_key?("CommonName")
169
+ host = options["Host"] if options.has_key?("Host")
170
+ port = options["Port"] if options.has_key?("Port")
171
+
172
+ if !cn.nil?
173
+ msg.concat(" #{cn}")
174
+ @sock.cmd("String" => msg , "Match" => /(SUCCESS:.*\n|ERROR:.*\n|END.*\n)/) do |c|
175
+ print c
176
+ end
177
+ else
178
+ if !host.nil? && !port.nil?
179
+ msg.concat(" #{host}:#{port}")
180
+ @sock.cmd("String" => msg , "Match" => /(SUCCESS:.*\n|ERROR:.*\n|END.*\n)/)
181
+ else
182
+ puts "Net::OpenVPN Kill Error (Common Name or Host:Port Combination needed)"
183
+ end
184
+ end
185
+ end
186
+
187
+ end
188
+
data/test.rb ADDED
@@ -0,0 +1,17 @@
1
+
2
+ # OpenVPNServer Test Example
3
+
4
+ require 'Rubygems'
5
+ require 'openVPNServer'
6
+
7
+ # openVPNServer creation (Modify your fields)
8
+ s = OpenVPNServer.new("Host" => "localhost", "Port" => 1234, "Timeout" => 10, "Password" => "hi")
9
+ # status command
10
+ c,r = s.status
11
+ p c
12
+ p r
13
+ # load_stats command
14
+ s_info = s.load_stats
15
+ p s_info
16
+ s.destroy
17
+
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: openVPNServer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Alessandro Celestini
8
+ - Antonio Davoli
9
+ - Davide Guerri
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2010-01-13 00:00:00 +01:00
15
+ default_executable:
16
+ dependencies: []
17
+
18
+ description: openVPNServer provides simple interfacing with openVPN server management console through a telnet client.
19
+ email:
20
+ - a.celestini@gmail.com
21
+ - antonio.davoli@gmail.com
22
+ - d.guerri@caspur.it
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - README
31
+ - lib/openVPNServer.rb
32
+ - test.rb
33
+ has_rdoc: true
34
+ homepage: http://openvpnserver.rubyforge.org
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project: openVPNServer
57
+ rubygems_version: 1.3.5
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: openVPNServer Management Ruby Class
61
+ test_files: []
62
+