Skip to content

Commit 9ac46dd

Browse files
authored
Implementing Stub Solver but not exposing (#598)
* stub core implementing * validating structures * address mapper tested * testing and refactoring * refactoring * testing ipv4 validator * testing * new test * refactoring * clean code * docs * clean code * release notes * [Gradle Release Plugin] - new version commit: '3.32.0-snapshot'. * clearing todos * clean code * adjusting version
1 parent 9565b8d commit 9ac46dd

File tree

16 files changed

+367
-3
lines changed

16 files changed

+367
-3
lines changed

RELEASE-NOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 3.32.0
2+
* Implementing Stub Solver but not exposing. #545
3+
14
## 3.31.1
25
* Adjusting Canary Rate Threshold Circuit Breaker to mark all circuits as open at the start. #533
36

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ buildscript {
55
mavenLocal()
66
mavenCentral()
77
gradlePluginPortal()
8+
maven { url "https://oss.sonatype.org/service/local/repositories/releases/content" }
89
}
910
}
1011

@@ -56,7 +57,7 @@ dependencies {
5657

5758
implementation('jakarta.enterprise:jakarta.enterprise.cdi-api:2.0.2')
5859
implementation('jakarta.ws.rs:jakarta.ws.rs-api:2.1.6')
59-
implementation('com.mageddo.commons:commons-lang:0.1.16')
60+
implementation('com.mageddo.commons:commons-lang:0.1.21')
6061
implementation('org.apache.commons:commons-exec:1.3')
6162

6263
implementation('ch.qos.logback:logback-classic:1.5.6')

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=3.31.1-snapshot
1+
version=3.32.0-snapshot
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.mageddo.dnsproxyserver.solver.stub;
2+
3+
import com.mageddo.dnsproxyserver.solver.stub.addressexpression.AddressExpressions;
4+
import com.mageddo.dnsproxyserver.solver.stub.addressexpression.ParseException;
5+
import com.mageddo.net.IP;
6+
import org.apache.commons.lang3.Validate;
7+
8+
public class HostnameIpExtractor {
9+
10+
public static IP extract(String hostname, String domain) {
11+
12+
hostname = removeDomainFrom(hostname, domain);
13+
Validate.notBlank(hostname, "Hostname is empty");
14+
15+
RuntimeException lastException = null;
16+
for (int i = 0; i < hostname.length(); i++) {
17+
try {
18+
return AddressExpressions.toIp(hostname.substring(i));
19+
} catch (ParseException e) {
20+
lastException = e;
21+
}
22+
}
23+
24+
throw lastException;
25+
}
26+
27+
static String removeDomainFrom(String hostname, String domain) {
28+
final var idx = hostname.indexOf(domain);
29+
if (idx < 0) {
30+
return hostname;
31+
}
32+
return hostname.substring(0, idx - 1);
33+
}
34+
35+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.mageddo.dnsproxyserver.solver.stub;
2+
3+
import com.mageddo.dnsproxyserver.solver.Response;
4+
import com.mageddo.dnsproxyserver.solver.Solver;
5+
import org.xbill.DNS.Message;
6+
7+
/**
8+
* Extract the address from the hostname then answer.
9+
* Inspired at nip.io and sslip.io, see #545.
10+
*/
11+
public class SolverStub implements Solver {
12+
@Override
13+
public Response handle(Message query) {
14+
return null;
15+
}
16+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.mageddo.dnsproxyserver.solver.stub.addressexpression;
2+
3+
import com.mageddo.net.IP;
4+
5+
import java.util.List;
6+
7+
public class AddressExpressions {
8+
9+
public static IP toIp(String addressExpression) {
10+
RuntimeException lastException = null;
11+
for (final var parser : buildParsers()) {
12+
try {
13+
return parser.parse(addressExpression);
14+
} catch (ParseException e) {
15+
lastException = e;
16+
}
17+
}
18+
throw lastException;
19+
}
20+
21+
static List<Parser> buildParsers() {
22+
return List.of(
23+
new Ipv6Parser(),
24+
new Ipv4Parser(),
25+
new HexadecimalParser()
26+
);
27+
}
28+
29+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.mageddo.dnsproxyserver.solver.stub.addressexpression;
2+
3+
import com.mageddo.net.IP;
4+
import org.apache.commons.codec.DecoderException;
5+
import org.apache.commons.codec.binary.Hex;
6+
7+
public class HexadecimalParser implements Parser {
8+
@Override
9+
public IP parse(String addressExpression) {
10+
try {
11+
return IP.of(Hex.decodeHex(addressExpression));
12+
} catch (DecoderException e) {
13+
throw new ParseException("not a hexadecimal address: " + addressExpression, e);
14+
}
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.mageddo.dnsproxyserver.solver.stub.addressexpression;
2+
3+
import com.mageddo.dnsproxyserver.utils.Ips;
4+
import com.mageddo.net.IP;
5+
6+
public class Ipv4Parser implements Parser {
7+
@Override
8+
public IP parse(String addressExpression) {
9+
final var normalizedStr = addressExpression.replaceAll("-", ".");
10+
if (Ips.isIpv4(normalizedStr)) {
11+
return IP.of(normalizedStr);
12+
}
13+
throw new ParseException("invalid ipv4 address expression: " + addressExpression);
14+
}
15+
16+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.mageddo.dnsproxyserver.solver.stub.addressexpression;
2+
3+
import com.mageddo.net.IP;
4+
import org.apache.commons.lang3.StringUtils;
5+
6+
public class Ipv6Parser implements Parser {
7+
@Override
8+
public IP parse(String addressExpression) {
9+
if (isIpv6(addressExpression)) {
10+
try {
11+
return IP.of(addressExpression.replaceAll("-", ":"));
12+
} catch (RuntimeException e){
13+
throw throwError(addressExpression);
14+
}
15+
}
16+
throw throwError(addressExpression);
17+
}
18+
19+
RuntimeException throwError(String addressExpression) {
20+
throw new ParseException("Not ipv6 address: " + addressExpression);
21+
}
22+
23+
static boolean isIpv6(String addressExpression) {
24+
return (addressExpression.contains("--") || StringUtils.countMatches(addressExpression, "-") >= IP.IPV4_BYTES)
25+
&& !addressExpression.contains(".");
26+
}
27+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.mageddo.dnsproxyserver.solver.stub.addressexpression;
2+
3+
public class ParseException extends RuntimeException {
4+
5+
public ParseException(String message) {
6+
super(message);
7+
}
8+
9+
public ParseException(String message, Throwable cause) {
10+
super(message, cause);
11+
}
12+
}

0 commit comments

Comments
 (0)