summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--Gemfile3
-rw-r--r--Gemfile.lock16
-rw-r--r--LICENSE9
-rw-r--r--Rakefile5
-rw-r--r--wireguardmeshgenerator.gemspec13
-rw-r--r--wireguardmeshgenerator.rb61
7 files changed, 108 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..849ddff
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+dist/
diff --git a/Gemfile b/Gemfile
new file mode 100644
index 0000000..0db8860
--- /dev/null
+++ b/Gemfile
@@ -0,0 +1,3 @@
+source 'https://rubygems.org'
+
+gem 'erb'
diff --git a/Gemfile.lock b/Gemfile.lock
new file mode 100644
index 0000000..016db3d
--- /dev/null
+++ b/Gemfile.lock
@@ -0,0 +1,16 @@
+GEM
+ remote: https://rubygems.org/
+ specs:
+ cgi (0.4.2)
+ erb (4.0.4)
+ cgi (>= 0.3.3)
+
+PLATFORMS
+ ruby
+ x86_64-linux
+
+DEPENDENCIES
+ erb
+
+BUNDLED WITH
+ 2.5.23
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..c4f0ff5
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,9 @@
+Copyright (c) 2025 snonux
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/Rakefile b/Rakefile
new file mode 100644
index 0000000..ad70746
--- /dev/null
+++ b/Rakefile
@@ -0,0 +1,5 @@
+task :generate_mesh do
+ ruby 'wireguardmeshgenerator.rb'
+end
+
+task default: :generate_mesh
diff --git a/wireguardmeshgenerator.gemspec b/wireguardmeshgenerator.gemspec
new file mode 100644
index 0000000..d73e89a
--- /dev/null
+++ b/wireguardmeshgenerator.gemspec
@@ -0,0 +1,13 @@
+Gem::Specification.new do |s|
+ s.required_ruby_version = '>= 3.3.0'
+ s.name = 'wireguardmeshgenerator'
+ s.version = '0.0.0'
+ s.licenses = ['BSD3']
+ s.summary = 'WireGuard Mesh generator'
+ s.description = 'To configure my stuff'
+ s.authors = ['Paul Buetow']
+ s.email = 'wireguardmeshgenerator@dev.buetow.org'
+ s.files = ['wireguardmeshgenerator.rb']
+ s.homepage = 'https://codeberg.org/snonux/wireguardmeshgenerator'
+ s.metadata = { 'source_code_uri' => 'https://codeberg.org/snonux/wireguardmeshgenerator' }
+end
diff --git a/wireguardmeshgenerator.rb b/wireguardmeshgenerator.rb
new file mode 100644
index 0000000..656586a
--- /dev/null
+++ b/wireguardmeshgenerator.rb
@@ -0,0 +1,61 @@
+#!/usr/bin/ruby
+
+require 'fileutils'
+
+HOSTS = {
+ f0: { lan: { domain: 'lan.buetow.org', ip: '192.168.1.130' },
+ wg0: { domain: 'wg0.buetow.org', ip: '192.168.2.130' } },
+ f1: { lan: { domain: 'lan.buetow.org', ip: '192.168.1.131' },
+ wg0: { domain: 'wg0.buetow.org', ip: '192.168.2.131' } },
+ f2: { lan: { domain: 'lan.buetow.org', ip: '192.168.1.132' },
+ wg0: { domain: 'wg0.buetow.org', ip: '192.168.2.132' } },
+ r0: { lan: { domain: 'lan.buetow.org', ip: '192.168.1.120' },
+ wg0: { domain: 'wg0.buetow.org', ip: '192.168.2.120' } },
+ r1: { lan: { domain: 'lan.buetow.org', ip: '192.168.1.121' },
+ wg0: { domain: 'wg0.buetow.org', ip: '192.168.2.121' } },
+ r2: { lan: { domain: 'lan.buetow.org', ip: '192.168.1.122' },
+ wg0: { domain: 'wg0.buetow.org', ip: '192.168.2.122' } }
+}.freeze
+
+PeerSnippet = Struct.new(:description, :public_key, :preshared_key, :allowed_ips) do
+ def to_s
+ <<~PEER_CONFIG
+ [Peer]
+ # #{description}
+ PublicKey = #{public_key}
+ PresharedKey = #{preshared_key}
+ AllowedIPs = #{allowed_ips}
+ PEER_CONFIG
+ end
+end
+
+WireguardConfig = Struct.new(:myself, :hosts) do
+ def to_s
+ peers = hosts.map do |name, data|
+ PeerSnippet.new("#{name}.#{data[:wg0][:domain]}",
+ :PUB_KEY, :PRESHARED_KEY, "#{data[:wg0][:ip]}/32")
+ end
+
+ <<~CONFIG
+ [Interface]
+ Address = #{hosts[myself][:wg0][:ip]}
+ PrivateKey = #{private_key}
+
+ #{peers.map(&:to_s).join("\n")}
+ CONFIG
+ end
+
+ private
+
+ def private_key = 'PRIVATE_KEY'
+end
+
+HOSTS.each_key do |name|
+ config_dir = "dist/#{name}/etc/wireguard"
+ config_path = "#{config_dir}/wg0.conf"
+ FileUtils.mkdir_p(config_dir) unless Dir.exist?(config_dir)
+
+ wg0 = WireguardConfig.new(name, HOSTS)
+ puts "Generating config for #{name} at #{config_path}"
+ File.write(config_path, wg0.to_s)
+end