Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/594 2 #614

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
* Structure config.Config object as in #594 and fix tests
* [x] SolverRemote
* [x] SolverDocker - created the solver docker class, must fix compiling errors and tests
* How can I remove ConfigJson Object and continue to manage env entries?
186 changes: 162 additions & 24 deletions src/main/java/com/mageddo/dnsproxyserver/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.Value;
import org.apache.commons.lang3.Validate;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
Expand All @@ -29,54 +30,183 @@
* @see com.mageddo.dnsproxyserver.config.application.ConfigService
*/
@Value
@Builder(toBuilder = true)
@Builder(toBuilder = true, builderClassName = "ConfigBuilder")
public class Config {

private String version;

@Builder.Default
private List<IpAddr> remoteDnsServers = new ArrayList<>();
private Server server;

private Integer webServerPort;

private Integer dnsServerPort;

private Boolean defaultDns;
private DefaultDns defaultDns;

// tag:Log
private LogLevel logLevel;

private String logFile;
// end:Log

private Boolean registerContainerNames;
private Path configPath;

private String hostMachineHostname;
private SolverStub solverStub;

private String domain;
private SolverRemote solverRemote;

private Boolean mustConfigureDpsNetwork;
private SolverDocker solverDocker;

private Boolean dpsNetworkAutoConnect;
private SolverSystem solverSystem;

private Path configPath;
// tag:Local
private String activeEnv;
private List<Env> envs;
// end:Local

private String resolvConfPaths;
@NonNull
private Source source;

private SimpleServer.Protocol serverProtocol;

private URI dockerHost;
@JsonIgnore
public Boolean isDefaultDnsActive() {
if (this.defaultDns == null) {
return null;
}
return this.defaultDns.active;
}

private Boolean resolvConfOverrideNameServers;
@JsonIgnore
public String getDefaultDnsResolvConfPaths() {
if (this.getDefaultDnsResolvConf() == null) {
return null;
}
return this.getDefaultDnsResolvConf().paths;
}

private Integer noEntriesResponseCode;
@JsonIgnore
public Boolean isResolvConfOverrideNameServersActive() {
if (this.getDefaultDnsResolvConf() == null) {
return null;
}
return this.getDefaultDnsResolvConf().overrideNameServers;
}

private Boolean dockerSolverHostMachineFallbackActive;
@JsonIgnore
private DefaultDns.ResolvConf getDefaultDnsResolvConf() {
if (this.defaultDns == null) {
return null;
}
return this.defaultDns.resolvConf;
}

private SolverStub solverStub;
@Nonnull
@JsonIgnore
public List<IpAddr> getRemoteDnsServers() {
if (this.solverRemote == null) {
return Collections.emptyList();
}
return this.solverRemote.getDnsServers();
}

private SolverRemote solverRemote;
@JsonIgnore
public Boolean getRegisterContainerNames() {
if (this.solverDocker == null) {
return null;
}
return this.solverDocker.getRegisterContainerNames();
}

@NonNull
private Source source;
@JsonIgnore
public String getDockerDomain() {
if (this.solverDocker == null) {
return null;
}
return this.solverDocker.getDomain();
}

@JsonIgnore
public Boolean getDockerSolverMustConfigureDpsNetwork() {
if (this.solverDocker == null) {
return null;
}
return this.solverDocker.shouldAutoCreateDpsNetwork();
}

@JsonIgnore
public Boolean getDpsNetworkAutoConnect() {
if (this.solverDocker == null) {
return null;
}
return this.solverDocker.shouldAutoConnect();
}

@JsonIgnore
public URI getDockerDaemonUri() {
if (this.solverDocker == null) {
return null;
}
return this.solverDocker.getDockerDaemonUri();
}

@JsonIgnore
public SolverDocker.DpsNetwork getDockerSolverDpsNetwork() {
return this.solverDocker.getDpsNetwork();
}

@JsonIgnore
public Boolean getDockerSolverHostMachineFallbackActive() {
if (this.solverDocker == null) {
return null;
}
return this.solverDocker.shouldUseHostMachineFallback();
}

public String getHostMachineHostname() {
if (this.solverSystem == null) {
return null;
}
return this.solverSystem.getHostMachineHostname();
}

public Integer getNoEntriesResponseCode() {
if (this.server == null) {
return null;
}
return this.server.getDnsServerNoEntriesResponseCode();
}

public Integer getDnsServerPort() {
if (this.server == null) {
return null;
}
return this.server.getDnsServerPort();
}

public Integer getWebServerPort() {
if (this.server == null) {
return null;
}
return this.server.getWebServerPort();
}

public SimpleServer.Protocol getServerProtocol() {
if (this.server == null) {
return null;
}
return this.server.getServerProtocol();
}

@Value
@Builder(toBuilder = true)
public static class DefaultDns {

private Boolean active;
private ResolvConf resolvConf;

@Value
@Builder(toBuilder = true)
public static class ResolvConf {
private String paths;
private Boolean overrideNameServers;
}
}

@JsonIgnore
public Boolean isSolverRemoteActive() {
Expand All @@ -87,6 +217,9 @@ public Boolean isSolverRemoteActive() {
}

public void resetConfigFile() {
if (this.getConfigPath() == null) {
throw new IllegalStateException("config file is null");
}
try {
Files.deleteIfExists(this.getConfigPath());
} catch (IOException e) {
Expand Down Expand Up @@ -247,4 +380,9 @@ public boolean isAddressSolving() {
}
}
}

public static class ConfigBuilder {


}
}
19 changes: 19 additions & 0 deletions src/main/java/com/mageddo/dnsproxyserver/config/Server.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mageddo.dnsproxyserver.config;

import com.mageddo.dnsserver.SimpleServer;
import lombok.Builder;
import lombok.Value;

@Value
@Builder
public class Server {

private Integer webServerPort;

private Integer dnsServerPort;
private Integer dnsServerNoEntriesResponseCode;

private SimpleServer.Protocol serverProtocol;


}
53 changes: 53 additions & 0 deletions src/main/java/com/mageddo/dnsproxyserver/config/SolverDocker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.mageddo.dnsproxyserver.config;


import lombok.Builder;
import lombok.Value;
import org.apache.commons.lang3.BooleanUtils;

import java.net.URI;

@Value
@Builder(toBuilder = true)
public class SolverDocker {

private URI dockerDaemonUri;
private Boolean registerContainerNames;
private String domain;
private DpsNetwork dpsNetwork;
private Boolean hostMachineFallback;

public boolean shouldUseHostMachineFallback() {
return BooleanUtils.toBoolean(hostMachineFallback);
}

public boolean shouldAutoCreateDpsNetwork() {
if (this.dpsNetwork == null) {
return false;
}
return this.dpsNetwork.shouldAutoCreate();
}

public boolean shouldAutoConnect() {
if (this.dpsNetwork == null) {
return false;
}
return this.dpsNetwork.shouldAutoConnect();
}

@Value
@Builder
public static class DpsNetwork {

private Boolean autoCreate;
private Boolean autoConnect;

public boolean shouldAutoConnect() {
return BooleanUtils.isTrue(this.autoConnect);
}

public boolean shouldAutoCreate() {
return BooleanUtils.isTrue(this.autoCreate);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package com.mageddo.dnsproxyserver.config;

import com.mageddo.net.IpAddr;
import lombok.Builder;
import lombok.Value;

import java.util.ArrayList;
import java.util.List;

@Value
@Builder
public class SolverRemote {

private Boolean active;

private CircuitBreakerStrategyConfig circuitBreaker;

@Builder.Default
private List<IpAddr> dnsServers = new ArrayList<>();

}
10 changes: 10 additions & 0 deletions src/main/java/com/mageddo/dnsproxyserver/config/SolverSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mageddo.dnsproxyserver.config;

import lombok.Builder;
import lombok.Value;

@Value
@Builder
public class SolverSystem {
private String hostMachineHostname;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.mageddo.dnsproxyserver.config.Config;
import com.mageddo.dnsproxyserver.config.application.Configs;
import com.mageddo.dnsproxyserver.config.dataprovider.mapper.ConfigJsonV2Mapper;
import com.mageddo.dnsproxyserver.config.dataprovider.vo.ConfigJson;
import com.mageddo.dnsproxyserver.config.dataprovider.vo.ConfigJsonV1;
import com.mageddo.dnsproxyserver.config.dataprovider.vo.ConfigJsonV2;
Expand All @@ -25,6 +26,26 @@ public class JsonConfigs {
public static final int VERSION_2 = 2;
public static List<Integer> supportedVersions = List.of(VERSION_1, VERSION_2);

public static Config loadConfigAsConfig() {
return ConfigJsonV2Mapper.toConfig(
loadConfigJson(),
Configs
.getInstance()
.getConfigPath()
);
}

public static ConfigJsonV2 loadConfigJson() {
final var configPath = Configs
.getInstance()
.getConfigPath();
return (ConfigJsonV2) loadConfig(configPath);
}

public static Config loadConfigAsConfig(Path configPath) {
return ConfigJsonV2Mapper.toConfig(loadConfig(configPath), configPath);
}

/**
* Parser v1 or v2 config json then return the interface.
*
Expand Down Expand Up @@ -111,11 +132,4 @@ public static Integer findVersion(Path configPath) {
public static Integer findVersion(JsonNode tree) {
return tree.at("/version").asInt(VERSION_1);
}

public static ConfigJsonV2 loadConfigJson() {
final var configPath = Configs
.getInstance()
.getConfigPath();
return (ConfigJsonV2) loadConfig(configPath);
}
}
Loading
Loading