routeros-api 0.2.0 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f63f8077dd7855c9c67297abc2801872eb3de689f0fce401d37adf4edbac1d3b
4
- data.tar.gz: 43e84dd99ef9e1ee7b8e83fddb14a730bfb35c715564c8a0bc8fc5030e9fdc65
3
+ metadata.gz: 14c6115c02446080bc47d5ad9b98e15e19cb34771adfce52ebd0919d2c61cac6
4
+ data.tar.gz: 63287c6efaba8a6d654d5ca939f6aa2cb1f301077561bce58a5db7e7b43a01cd
5
5
  SHA512:
6
- metadata.gz: d2233021b902cb11e23c911dcf35112d9f2b31845b6760f140bc473498cb460e9efbbbe691ff0ac91145cfb6b76b8615fb90484ec0ee9e0d09975192eab06d2f
7
- data.tar.gz: 3ca944848e4d942aea06f6ec8ad32c3ed2cf8d932e89ed172bda0ea6a0236f9673a80cb8afcf1e34eaa2e753633cdc17b92431c3b1f88579feae03875b0ba7d0
6
+ metadata.gz: 8f9ccae171b00da8b4dc1216bede71a1608ba252264e708140fc757a4fd14e35c832d6829192996c2859c7376f8f0e540f7c48ede314a88887366ec2490f6b43
7
+ data.tar.gz: e72a2576902ab545c98daf4336b16b1d1c9e236f28397fe3af7fed022609981b62a7abeef9857f82b154ce2f49f25ca76badcdd05e410cf29b3c0c1068ac17f8
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.0] - 2024-10-16
4
+
5
+ - SSL support
6
+
3
7
  ## [0.2.0] - 2024-10-16
4
8
 
5
9
  - Parsing of known number and boolean values
data/README.md CHANGED
@@ -26,7 +26,6 @@ end
26
26
 
27
27
  ## Planned Features
28
28
 
29
- - [ ] SSL support
30
29
  - [ ] Async support
31
30
  - [ ] Auto deploy and auto changelog
32
31
 
data/examples/simple.rb CHANGED
@@ -3,11 +3,13 @@
3
3
  require "bundler/setup"
4
4
  require "routeros/api"
5
5
 
6
- api = RouterOS::API.new("192.168.1.1", 8728)
7
- response = api.login("lupadmin", "M0N3frHsuS)6:")
6
+ api = RouterOS::API.new("192.168.1.1", 8728, ssl: false)
7
+ response = api.login("test", "test")
8
8
  puts(response) # (OK) []
9
9
 
10
10
  if response.ok?
11
11
  response = api.command("ip/route/getall")
12
12
  puts(response.data) # (OK) [{ ... }]
13
13
  end
14
+
15
+ api.close
data/lib/routeros/api.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "socket"
4
+ require "openssl"
4
5
  require_relative "request"
5
6
  require_relative "response"
6
7
  require_relative "word_stream"
@@ -8,12 +9,23 @@ require_relative "word_stream"
8
9
  module RouterOS
9
10
  # Main abstraction to connect to RouterOS
10
11
  class API
11
- VERSION = "0.2.0"
12
+ VERSION = "0.3.0"
12
13
 
13
14
  class Error < StandardError; end
14
15
 
15
- def initialize(host, port)
16
- @stream = RouterOS::WordStream.new(TCPSocket.new(host, port))
16
+ def initialize(host, port, ssl: false, ssl_ctx: nil)
17
+ raw_socket = TCPSocket.new(host, port)
18
+
19
+ if ssl
20
+ ssl_socket = OpenSSL::SSL::SSLSocket.new(raw_socket, ssl_ctx || OpenSSL::SSL::SSLContext.new)
21
+ ssl_socket.connect
22
+ socket = ssl_socket
23
+ else
24
+ socket = raw_socket
25
+ end
26
+
27
+ @ssl = ssl
28
+ @stream = RouterOS::WordStream.new(socket)
17
29
  @tag = 0
18
30
  end
19
31
 
@@ -23,7 +35,12 @@ module RouterOS
23
35
  end
24
36
 
25
37
  def login(name, password)
26
- command("/login", { name: name, password: password })
38
+ warn("non encrypted connection, be careful") unless @ssl
39
+ command("/login", { name:, password: })
40
+ end
41
+
42
+ def close
43
+ @stream.close
27
44
  end
28
45
 
29
46
  private
@@ -49,7 +66,7 @@ module RouterOS
49
66
 
50
67
  response << sentence
51
68
 
52
- break if sentence.include? "!done"
69
+ break if sentence.include?("!done")
53
70
  end
54
71
 
55
72
  RouterOS::Response.new(response)
@@ -3,8 +3,8 @@
3
3
  module RouterOS
4
4
  # Represents a parsed RouterOS response
5
5
  class Response
6
- KNOWN_NUMBER_FIELDS = %i[scope distance target-scope]
7
- KNOWN_BOOLEAN_VALUES = %w[true false]
6
+ KNOWN_NUMBER_FIELDS = %i[scope distance target-scope].freeze
7
+ KNOWN_BOOLEAN_VALUES = %w[true false].freeze
8
8
 
9
9
  attr_reader :raw_sentences, :data, :tag
10
10
 
@@ -19,6 +19,10 @@ module RouterOS
19
19
  @stream.read(read_length)
20
20
  end
21
21
 
22
+ def close
23
+ @stream.close
24
+ end
25
+
22
26
  # utility to encode the length
23
27
  def self.encode_length(length)
24
28
  case length
@@ -49,9 +53,14 @@ module RouterOS
49
53
 
50
54
  private
51
55
 
56
+ def readbyte
57
+ bytes = @stream.read(1).bytes
58
+ bytes[0]
59
+ end
60
+
52
61
  # reads the length from the underlying stream
53
62
  def read_length
54
- length = @stream.readbyte
63
+ length = readbyte
55
64
  mask = 0xFF
56
65
 
57
66
  if length & 0x80 == 0x00 # first bit is 0
@@ -66,7 +75,7 @@ module RouterOS
66
75
  byte_amount = 4
67
76
  mask = 0x1F
68
77
  elsif length & 0xF8 == 0xF0 # first, second, third and fourth bits are 1, fifth is 0
69
- length = @stream.readbyte
78
+ length = readbyte
70
79
  byte_amount = 3
71
80
  end
72
81
 
@@ -74,7 +83,7 @@ module RouterOS
74
83
 
75
84
  (byte_amount - 1).times do
76
85
  length <<= 8
77
- length += @stream.readbyte
86
+ length += readbyte
78
87
  end
79
88
 
80
89
  length
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: routeros-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Barros
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-10-16 00:00:00.000000000 Z
11
+ date: 2024-10-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description:
13
+ description:
14
14
  email:
15
15
  - mcbarros@gmail.com
16
16
  executables: []
@@ -33,10 +33,9 @@ homepage: https://github.com/mcbarros/routeros-api
33
33
  licenses:
34
34
  - MIT
35
35
  metadata:
36
- homepage_uri: https://github.com/mcbarros/routeros-api
37
36
  source_code_uri: https://github.com/mcbarros/routeros-api
38
37
  changelog_uri: https://github.com/mcbarros/routeros-api/blob/main/CHANGELOG.md
39
- post_install_message:
38
+ post_install_message:
40
39
  rdoc_options: []
41
40
  require_paths:
42
41
  - lib
@@ -52,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
51
  version: '0'
53
52
  requirements: []
54
53
  rubygems_version: 3.5.16
55
- signing_key:
54
+ signing_key:
56
55
  specification_version: 4
57
56
  summary: Ruby lib to access RouterOS API.
58
57
  test_files: []