Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
leonovk committed Jul 17, 2024
1 parent da29fc5 commit ba6e601
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 5 deletions.
2 changes: 1 addition & 1 deletion config/settings/production.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ wg_default_address: <%= ENV['WG_DEFAULT_ADDRESS'] || '10.8.0.x' %>
wg_allowed_ips: <%= ENV['WG_ALLOWED_IPS'] || '0.0.0.0/0, ::/0' %>
wg_host: <%= ENV['WG_HOST'] %>
wg_port: <%= ENV['WG_PORT'] || '51820' %>
wg_default_dns: <%= ENV['WG_DEFAULT_DNS'] || '1.1.1.1' %>
wg_default_dns: <%= ENV['WG_DEFAULT_DNS'] %>
wg_pre_up: <%= ENV['WG_PRE_UP'] || '' %>
wg_pre_down: <%= ENV['WG_PRE_DOWN'] || '' %>
wg_post_up: <%= ENV['WG_POST_UP'] %>
Expand Down
17 changes: 13 additions & 4 deletions lib/utils/config_file_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,23 @@ def initialize(config)
end

def build
<<~TEXT
"#{interface}\n#{peer}"
end

private

def interface
result = <<~TEXT
[Interface]
PrivateKey = #{key['private_key']}
Address = #{key['address']}
DNS = #{key['dns']}
TEXT
result << "DNS = #{key['dns']}\n" if key['dns']
result
end

def peer
<<~TEXT
[Peer]
PublicKey = #{key['server_public_key']}
PresharedKey = #{key['preshared_key']}
Expand All @@ -27,8 +38,6 @@ def build
TEXT
end

private

attr_reader :key
end
end
74 changes: 74 additions & 0 deletions spec/lib/utils/config_file_builder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# frozen_string_literal: true

RSpec.describe Utils::ConfigFileBuilder do
subject(:build) { described_class.build(config.to_json) }

context 'when there is dns in the config' do
let(:config) do
{
private_key: '1',
address: '23.23.23.23',
dns: '1.1.1.1',
server_public_key: '2',
preshared_key: '3',
allowed_ips: '1.2.3.4',
persistent_keepalive: 4,
endpoint: '2.3.4.1'
}
end

let(:expected_result) do
<<~TEXT
[Interface]
PrivateKey = 1
Address = 23.23.23.23
DNS = 1.1.1.1
[Peer]
PublicKey = 2
PresharedKey = 3
AllowedIPs = 1.2.3.4
PersistentKeepalive = 4
Endpoint = 2.3.4.1
TEXT
end

it 'returns the expected result' do
expect(build).to eq(expected_result)
end
end

context 'when there is no dns in the config' do
let(:config) do
{
private_key: '1',
address: '23.23.23.23',
dns: nil,
server_public_key: '2',
preshared_key: '3',
allowed_ips: '1.2.3.4',
persistent_keepalive: 4,
endpoint: '2.3.4.1'
}
end

let(:expected_result) do
<<~TEXT
[Interface]
PrivateKey = 1
Address = 23.23.23.23
[Peer]
PublicKey = 2
PresharedKey = 3
AllowedIPs = 1.2.3.4
PersistentKeepalive = 4
Endpoint = 2.3.4.1
TEXT
end

it 'returns the expected result' do
expect(build).to eq(expected_result)
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'ipaddr'
require 'fileutils'
require 'ruby_units/namespaced'
require 'byebug'

Config.load_and_set_settings('config/settings/test.yaml')

Expand Down

0 comments on commit ba6e601

Please sign in to comment.