#!/usr/bin/env ruby require_relative "utils" require_relative "bird" require "netaddr" require "optparse" require "set" class OpenvpnRegistry < Registry def initialize super @host = data["host"] @v4_tunnel_ip = @host["v4_tunnel"] or die("v4_tunnel not set for host") @start_port = @host["start_port"].to_i @end_port = @host["end_port"].to_i @openvpn_path = Pathname.new(File.expand_path("../../openvpn", __FILE__)) end def add_peer(name, peer) save_config(name, generate_config(name, peer)) peer_template = generate_config(name, peer_params(peer)) puts "==== openvpn.conf for #{name} =====" puts peer_template puts "===================================\n" end def remove_peer(peer) if data["network"].delete(peer).nil? die("failed to remove '#{peer}': no such peer found in registry.json") end key = @openvpn_path.join("#{name}.key") unless File.exists?(key) FileUtils.rm_f(key) end end def update_configurations data["network"].each do |name, peer| next unless peer["type"] == "openvpn" save_config(name, generate_config(name, peer)) end end def generate_config(name, peer) key = @openvpn_path.join("#{name}.key") unless File.exists?(key) FileUtils.mkdir_p(File.dirname(key)) sh("openvpn", "--genkey", "--secret", key.to_s) puts "===== shared key for #{name} ======" puts File.read(key) puts "===================================\n" end context = template_params(name, peer) template_path = Pathname.new(File.expand_path("../../templates", __FILE__)) openvpn_template = Template.new(template_path.join("openvpn.conf.erb")) openvpn_template.render(context) end private def save_config(peer, template) atomic_write(@openvpn_path.join("#{peer}.conf"), template) end def peer_params(peer) params = peer.dup params["rport"] = peer["lport"] if peer["rport"] params["lport"] = peer["rport"] end params["remote"] = if peer["proto"] == "udp6" @host["v6_public"] else @host["v4_public"] end params end def template_params(name, params) unless params["proto"] die "proto not set for peer #{name}" end unless params["v4_tunnel"] # TODO die "v4_tunnel not set for peer #{name}" end params["lport"] ||= next_free_port if params["remote"] == "float" params["rport"] ||= params["lport"] end params.merge(own_v4_tunnel: @v4_tunnel_ip) end def next_free_port ports = Set.new(data["network"].map { |name, peer| peer["lport"] }) (@start_port..@end_port).each do |port| unless ports.include?(port) return port end end die "no free local ports in range #{@start_port}:#{@end_port}" end end GLOBAL_OPTIONS = OptionParser.new do |opts| opts.banner = "Usage: openvpn [options] [subcommand [options]]" opts.separator "" opts.separator <