sparoid 1.0.7 → 1.0.12

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: 2a97b0d586778f3f851e5c42039bffaf461bb2e8927d9fea86cd69c8cfa0e1e9
4
- data.tar.gz: b66ff9a2c806838408c546d38c3698c7bc5334d3b11aba00d1e4de8f9e848f05
3
+ metadata.gz: 12dad3dbab45bb38650465dab2b26f356c051569b06d20ba1bcfd6af11eb9701
4
+ data.tar.gz: 0b17fe457b36978f1b682153cb14578192cb657a846fadcbb3abb2cf82e07bb6
5
5
  SHA512:
6
- metadata.gz: 1f2643b8599ab7c78e30b3e6927ad79e405becebfa8cfe617a0d7245e82ec4d0b11c9fcbf9cff2365403b59d2472260b4ab8b290e897991d8f3e6b1b19736e1e
7
- data.tar.gz: '048ecd5b85d177c044626fb11cf304fbbc14b906d366566e34c7afa535ec419ba4b8a9369108ab67fc121496908e4a3ac0f42a2821db723bb7771a46ab26ab7e'
6
+ metadata.gz: 1c31f7b1fa30e2d064b0e92e79b2b567bdfa4021995d7f52c25b6b4373d08062ed091aab8187aeb42051c11e0c7c1b994d158878e64311eed28e68d1e3fe4008
7
+ data.tar.gz: cacd2adf45ed9fa8e31b063809e01cf0371c6d13670e24b5bdb17f7c2623c2f9592c7e38b6df18a5f529db9628d4197baf323a9d58124b8eb6ea75762c3a6d78
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.4
2
+ TargetRubyVersion: 2.5
3
3
  NewCops: enable
4
4
 
5
5
  Style/StringLiterals:
data/CHANGELOG.md CHANGED
@@ -1,3 +1,24 @@
1
+ ## [1.0.12] - 2021-06-14
2
+
3
+ - Use file locking to prevent multiple processes/threads to write to the public ip cache file
4
+
5
+ ## [1.0.11] - 2021-06-14
6
+
7
+ - Log hostname when reporting unhandled exceptions
8
+
9
+ ## [1.0.10] - 2021-06-09
10
+
11
+ - Cache public IP in /tmp/.sparoid_public_ip for 1 min
12
+
13
+ ## [1.0.9] - 2021-05-23
14
+
15
+ - Exit gracefully on abort (ctrl-c) instead of dumping huge stacktrace
16
+ - Sleep 20ms aftering sending UDP package to allow for remote host to open its firewall
17
+
18
+ ## [1.0.8] - 2021-04-27
19
+
20
+ - Get ENV variables if config file is missing
21
+
1
22
  ## [1.0.7] - 2021-04-27
2
23
 
3
24
  - Get key and hmac key from ENV variables
data/README.md CHANGED
@@ -26,7 +26,7 @@ Can be used with OpenSSH's ProxyCommand/ProxyUseFdpass to send the packet before
26
26
 
27
27
  ```
28
28
  Host *.example.com
29
- ProxyCommand sparoid send %h --passfd %p
29
+ ProxyCommand sparoid connect %h %p
30
30
  ProxyUseFdpass yes
31
31
  ```
32
32
 
data/exe/sparoid CHANGED
@@ -1,5 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- require_relative "../lib/sparoid/cli"
5
- Sparoid::CLI.start
4
+ begin
5
+ require_relative "../lib/sparoid/cli"
6
+ Sparoid::CLI.start
7
+ rescue Interrupt
8
+ exit 1
9
+ end
data/lib/sparoid.rb CHANGED
@@ -11,9 +11,14 @@ module Sparoid
11
11
 
12
12
  # Send an authorization packet
13
13
  def auth(key, hmac_key, host, port)
14
- msg = message(public_ip)
14
+ msg = message(cached_public_ip)
15
15
  data = prefix_hmac(hmac_key, encrypt(key, msg))
16
16
  sendmsg(host, port, data)
17
+
18
+ # wait some time for the server to actually open the port
19
+ # if we don't wait the next SYN package will be dropped
20
+ # and it have to be redelivered, adding 1 second delay
21
+ sleep 0.02
17
22
  end
18
23
 
19
24
  # Generate new aes and hmac keys, print to stdout
@@ -67,7 +72,42 @@ module Sparoid
67
72
  version = 1
68
73
  ts = (Time.now.utc.to_f * 1000).floor
69
74
  nounce = OpenSSL::Random.random_bytes(16)
70
- [version, ts, nounce, ip.address].pack("Nq>a16a4")
75
+ [version, ts, nounce, ip.address].pack("N q> a16 a4")
76
+ end
77
+
78
+ def cached_public_ip
79
+ if up_to_date_cache?
80
+ read_cache
81
+ else
82
+ write_cache
83
+ end
84
+ rescue StandardError => e
85
+ warn "Sparoid: #{e.inspect}"
86
+ public_ip
87
+ end
88
+
89
+ def up_to_date_cache?
90
+ mtime = File.mtime("/tmp/.sparoid_public_ip")
91
+ (Time.now - mtime) <= 60 # cache is valid for 1 min
92
+ rescue Errno::ENOENT
93
+ false
94
+ end
95
+
96
+ def read_cache
97
+ File.open("/tmp/.sparoid_public_ip", "r") do |f|
98
+ f.flock(File::LOCK_SH)
99
+ Resolv::IPv4.create f.read
100
+ end
101
+ end
102
+
103
+ def write_cache
104
+ File.open("/tmp/.sparoid_public_ip", File::WRONLY | File::CREAT, 0o0644) do |f|
105
+ f.flock(File::LOCK_EX)
106
+ ip = public_ip
107
+ f.truncate(0)
108
+ f.write ip.to_s
109
+ ip
110
+ end
71
111
  end
72
112
 
73
113
  def public_ip
data/lib/sparoid/cli.rb CHANGED
@@ -13,7 +13,7 @@ module Sparoid
13
13
  rescue Errno::ENOENT
14
14
  abort "Sparoid: Config not found"
15
15
  rescue StandardError => e
16
- abort "Sparoid: #{e.message}"
16
+ abort "Sparoid: #{e.message} (#{host})"
17
17
  end
18
18
 
19
19
  desc "connect HOST PORT [SPA-PORT]", "Send a SPA, TCP connect, and then pass the FD back to the parent"
@@ -26,7 +26,7 @@ module Sparoid
26
26
  end
27
27
  Sparoid.fdpass(host, port)
28
28
  rescue StandardError => e
29
- abort "Sparoid: #{e.message}"
29
+ abort "Sparoid: #{e.message} (#{host})"
30
30
  end
31
31
 
32
32
  desc "keygen", "Generate an encryption key and a HMAC key"
@@ -41,14 +41,17 @@ module Sparoid
41
41
  private
42
42
 
43
43
  def send_auth(host, port, config)
44
- key = ENV["SPAROID_KEY"]
45
- hmac_key = ENV["SPAROID_HMAC_KEY"]
46
- key, hmac_key = get_keys(parse_ini(config)) if config
44
+ key, hmac_key = get_keys(parse_ini(config))
47
45
  Sparoid.auth(key, hmac_key, host, port.to_i)
48
46
  end
49
47
 
50
48
  def parse_ini(path)
51
49
  File.readlines(File.expand_path(path)).map! { |line| line.split("=", 2).map!(&:strip) }.to_h
50
+ rescue Errno::ENOENT
51
+ {
52
+ "key" => ENV["SPAROID_KEY"],
53
+ "hmac-key" => ENV["SPAROID_HMAC_KEY"]
54
+ }
52
55
  end
53
56
 
54
57
  def get_keys(config)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sparoid
4
- VERSION = "1.0.7"
4
+ VERSION = "1.0.12"
5
5
  end
data/sparoid.gemspec CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
  spec.summary = "Single Packet Authorisation client"
12
12
  spec.homepage = "https://github.com/84codes/sparoid.rb"
13
13
  spec.license = "MIT"
14
- spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
14
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
15
15
 
16
16
  spec.metadata["homepage_uri"] = spec.homepage
17
17
  spec.metadata["source_code_uri"] = spec.homepage
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sparoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.7
4
+ version: 1.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carl Hörberg
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-27 00:00:00.000000000 Z
11
+ date: 2021-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -62,14 +62,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
62
62
  requirements:
63
63
  - - ">="
64
64
  - !ruby/object:Gem::Version
65
- version: 2.4.0
65
+ version: 2.5.0
66
66
  required_rubygems_version: !ruby/object:Gem::Requirement
67
67
  requirements:
68
68
  - - ">="
69
69
  - !ruby/object:Gem::Version
70
70
  version: '0'
71
71
  requirements: []
72
- rubygems_version: 3.2.3
72
+ rubygems_version: 3.2.15
73
73
  signing_key:
74
74
  specification_version: 4
75
75
  summary: Single Packet Authorisation client