ruby-openvpn 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.md +10 -0
  4. data/lib/openvpn.rb +128 -0
  5. metadata +60 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bc5a1d684ad198abca47192a0adc879d94975764343f7393286fb848ee024243
4
+ data.tar.gz: 64ceea86ab532364592779498701c84f40118dd49fae8cf6696576ad89a3b8ed
5
+ SHA512:
6
+ metadata.gz: cf38839d156be89b67fd94f438173cc57b8a6d6a604b57aa8966f5d39545899d8a8b1fb259ffe57ddba3502a66e1f51c835817e8c477cfe574233e9b8d7b6e9c
7
+ data.tar.gz: 49335ef54fa9abf7adc1321f663dfb0756be85db493d3d4d716e621b365e200731370fd07975081f3e4487b0a42a92116309d4a03f6e5c1664a4278c3ee48c16
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 jjjm03299-wq
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,10 @@
1
+ # Ruby OpenVPN Management Client
2
+
3
+ `ruby-openvpn` is an event-driven Ruby library built to interact with the OpenVPN Management Interface using pure Ruby standard libraries (`net/telnet`).
4
+
5
+ ## Installation
6
+
7
+ Install the gem directly via your terminal:
8
+
9
+ ```bash
10
+ $ gem install ruby-openvpn
data/lib/openvpn.rb ADDED
@@ -0,0 +1,128 @@
1
+ # lib/openvpn.rb
2
+ require 'net/telnet'
3
+ require 'socket'
4
+
5
+ module OpenVPN
6
+ class Client
7
+ attr_reader :connection, :emitter
8
+
9
+ def initialize
10
+ @connection = nil
11
+ @emitter = Hash.new { |h, k| h[k] = [] }
12
+ end
13
+
14
+ def on(event, &block)
15
+ @emitter[event] << block if block_given?
16
+ end
17
+
18
+ def emit(event, *args)
19
+ @emitter[event].each { |block| block.call(*args) }
20
+ end
21
+
22
+ def destroy
23
+ if @connection
24
+ @connection.close rescue nil
25
+ @connection = nil
26
+ end
27
+ end
28
+
29
+ def connect(params = {})
30
+ host = params[:host] || '127.0.0.1'
31
+ port = params[:port] || 1337
32
+ timeout = params[:timeout] || 2
33
+
34
+ Thread.new do
35
+ begin
36
+ @connection = Net::Telnet.new(
37
+ 'Host' => host,
38
+ 'Port' => port,
39
+ 'Timeout' => timeout,
40
+ 'Prompt' => //
41
+ )
42
+
43
+ open_vpn_log
44
+ open_vpn_management('pid')
45
+ open_vpn_management('bytecount 1')
46
+ open_vpn_management('hold release')
47
+ emit(:connected)
48
+ rescue StandardError => e
49
+ emit(:error, e.message)
50
+ end
51
+ end
52
+
53
+ self
54
+ end
55
+
56
+ def connect_and_kill(params = {})
57
+ connect(params)
58
+ sleep(1)
59
+ disconnect_open_vpn
60
+ self
61
+ end
62
+
63
+ def authorize(auth)
64
+ open_vpn_management(%(username "Auth" "#{auth[:user]}"))
65
+ open_vpn_management(%(password "Auth" "#{auth[:pass]}"))
66
+ end
67
+
68
+ def disconnect
69
+ disconnect_open_vpn
70
+ end
71
+
72
+ def cmd(command)
73
+ open_vpn_management(command)
74
+ end
75
+
76
+ private
77
+
78
+ def disconnect_open_vpn
79
+ open_vpn_management('signal SIGTERM')
80
+ end
81
+
82
+ def open_vpn_management(command)
83
+ sleep(1)
84
+ if @connection
85
+ @connection.cmd('String' => command) do |c|
86
+ parse_response(c)
87
+ end
88
+ end
89
+ end
90
+
91
+ def open_vpn_log
92
+ @connection.cmd('String' => 'log on all')
93
+ @connection.cmd('String' => 'state on')
94
+
95
+ Thread.new do
96
+ loop do
97
+ break unless @connection
98
+ @connection.waitfor('Match' => /\n/) do |output|
99
+ parse_response(output) if output
100
+ end
101
+ end
102
+ end
103
+ end
104
+
105
+ def parse_response(response)
106
+ response.each_line do |res|
107
+ res = res.strip
108
+ next if res.empty?
109
+
110
+ if res.start_with?('STATE:')
111
+ emit(:state_change, res.sub('STATE:', '').strip.split(','))
112
+ elsif res.start_with?('HOLD:')
113
+ emit(:hold_waiting)
114
+ elsif res.start_with?('FATAL:', 'ERROR:')
115
+ emit(:error, res.sub(/^(FATAL|ERROR):\s*/, ''))
116
+ elsif res.start_with?('BYTECOUNT:')
117
+ emit(:bytecount, res.sub('BYTECOUNT:', '').strip.split(','))
118
+ elsif res.start_with?('SUCCESS:')
119
+ if res.include?('pid')
120
+ emit(:pid, res.split.last)
121
+ end
122
+ else
123
+ emit(:console_output, res) unless res.empty?
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-openvpn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - jjjm03299-wq
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2026-08-23 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: net-telnet
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: 0.2.0
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: 0.2.0
26
+ description: An event-driven Ruby library for managing and interacting with OpenVPN
27
+ instances.
28
+ email:
29
+ - your.email@example.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE.txt
35
+ - README.md
36
+ - lib/openvpn.rb
37
+ homepage: https://github.com/jjjm03299-wq/ruby-openvpn
38
+ licenses:
39
+ - MIT
40
+ metadata:
41
+ homepage_uri: https://github.com/jjjm03299-wq/ruby-openvpn
42
+ source_code_uri: https://github.com/jjjm03299-wq/ruby-openvpn
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 3.0.0
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.6.2
58
+ specification_version: 4
59
+ summary: A pure Ruby OpenVPN management client.
60
+ test_files: []