From b40cab15f4d01a4e8455e5808ca82e40ff291a92 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Wed, 6 Jan 2021 12:12:39 +0100 Subject: Renaming VPN stuff to wireguard. --- .../main/java/io/trygvis/rules/acme/IpCalc.java | 67 ---------------------- .../main/java/io/trygvis/rules/network/IpCalc.java | 51 ++++++++++++++++ .../main/resources/io/trygvis/rules/acme/acme.drl | 10 +--- 3 files changed, 54 insertions(+), 74 deletions(-) delete mode 100644 module/ri-engine/src/main/java/io/trygvis/rules/acme/IpCalc.java create mode 100644 module/ri-engine/src/main/java/io/trygvis/rules/network/IpCalc.java (limited to 'module/ri-engine/src/main') diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/acme/IpCalc.java b/module/ri-engine/src/main/java/io/trygvis/rules/acme/IpCalc.java deleted file mode 100644 index 5369d62..0000000 --- a/module/ri-engine/src/main/java/io/trygvis/rules/acme/IpCalc.java +++ /dev/null @@ -1,67 +0,0 @@ -package io.trygvis.rules.acme; - -import io.trygvis.rules.network.Ipv4Cidr; - -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.List; -import java.util.regex.Pattern; - -public class IpCalc { - private static final Pattern pattern = Pattern.compile("([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})/([0-9]{1,3})"); - - public static class FirstLast { - public final int first; - public final int last; - - public FirstLast(int first, int last) { - this.first = first; - this.last = last; - } - } - - public static Ipv4Cidr cidr(String cidr) { - var matcher = pattern.matcher(cidr); - if (!matcher.matches()) { - throw new IllegalArgumentException("Not a CIDR: " + cidr); - } - - var b1 = matcher.group(1); - var b2 = matcher.group(2); - var b3 = matcher.group(3); - var b4 = matcher.group(4); - - int network = parse(b1) << 24 | - parse(b2) << 16 | - parse(b3) << 8 | - parse(b4); - -// System.out.printf("network = %x%n", network); - - var l = matcher.group(5); - var bits = Integer.parseInt(l); - var hostBits = 32 - bits; - int size = 1 << hostBits; - - int netmask = (-1 >> hostBits) << hostBits; -// System.out.printf("netmask = %08x%n", netmask); - - int x = network & ~netmask; - - if (x != 0) { - throw new IllegalArgumentException("Not a CIDR: " + cidr); - } - - return new Ipv4Cidr(network, netmask, size, bits); - } - - private static int parse(String s) { - var i = Integer.parseInt(s); - if (i > 255) { - throw new IllegalArgumentException("Not a CIDR"); - } - - return i; - } -} diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/network/IpCalc.java b/module/ri-engine/src/main/java/io/trygvis/rules/network/IpCalc.java new file mode 100644 index 0000000..e40e169 --- /dev/null +++ b/module/ri-engine/src/main/java/io/trygvis/rules/network/IpCalc.java @@ -0,0 +1,51 @@ +package io.trygvis.rules.network; + +import java.util.regex.Pattern; + +public class IpCalc { + private static final Pattern pattern = Pattern.compile("([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})/([0-9]{1,3})"); + + public static Ipv4Cidr cidr(String cidr) { + var matcher = pattern.matcher(cidr); + if (!matcher.matches()) { + throw new IllegalArgumentException("Not a CIDR: " + cidr); + } + + var b1 = matcher.group(1); + var b2 = matcher.group(2); + var b3 = matcher.group(3); + var b4 = matcher.group(4); + + int network = parse(b1) << 24 | + parse(b2) << 16 | + parse(b3) << 8 | + parse(b4); + +// System.out.printf("network = %x%n", network); + + var l = matcher.group(5); + var bits = Integer.parseInt(l); + var hostBits = 32 - bits; + int size = 1 << hostBits; + + int netmask = (-1 >> hostBits) << hostBits; +// System.out.printf("netmask = %08x%n", netmask); + + int x = network & ~netmask; + + if (x != 0) { + throw new IllegalArgumentException("Not a CIDR: " + cidr); + } + + return new Ipv4Cidr(network, netmask, size, bits); + } + + private static int parse(String s) { + var i = Integer.parseInt(s); + if (i > 255) { + throw new IllegalArgumentException("Not a CIDR"); + } + + return i; + } +} diff --git a/module/ri-engine/src/main/resources/io/trygvis/rules/acme/acme.drl b/module/ri-engine/src/main/resources/io/trygvis/rules/acme/acme.drl index 0465343..7d53763 100644 --- a/module/ri-engine/src/main/resources/io/trygvis/rules/acme/acme.drl +++ b/module/ri-engine/src/main/resources/io/trygvis/rules/acme/acme.drl @@ -4,16 +4,13 @@ import io.trygvis.rules.machine.Machine; import io.trygvis.rules.dba.Cluster; import io.trygvis.rules.dba.Container; +dialect "mvel" + declare AcmeServer name : String machine : Machine end -//declare MachinePublicName -// machine : Machine -// fqdn : String -//end - rule "Ops" when $ops: AcmeOps() @@ -56,7 +53,6 @@ when $m : Machine(fqdn == null) $s : AcmeServer(machine == $m) then - var fqdn = "%s.machine.acme.com".formatted($s.machine.name); - $s.machine.fqdn = fqdn; + $s.machine.fqdn = "%s.machine.acme.com".formatted($s.machine.name); update($s.machine) end -- cgit v1.2.3