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

DPS 3 - Local DB Solver #273

Merged
merged 19 commits into from
Feb 6, 2023
47 changes: 33 additions & 14 deletions src/main/java/com/mageddo/dnsproxyserver/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ public class Config {
@Builder.Default
private List<IpAddr> remoteDnsServers = new ArrayList<>();

// fixme isso nao precisa estar aqui,
// soh precisa ficar no json para ser respondido quando o solver da base local perguntar
//
// @NonNull
// @Builder.Default
// private List<Env> envs = new ArrayList<>();

@NonNull
private String activeEnv;

@NonNull
private Integer webServerPort;

Expand Down Expand Up @@ -82,25 +72,54 @@ public static class Env {
public static final String DEFAULT_ENV = "";

private String name;
private List<Hostname> hostnames;
private List<Entry> entries;

public Env add(Entry entry){
this.entries.add(entry);
return this;
}

public static Env of(String name, List<Entry> entries) {
return new Env(name, entries);
}

public static Env theDefault() {
return new Env(DEFAULT_ENV, new ArrayList<>());
}

}

@Value
@Builder
public static class Hostname {
@Builder(builderClassName = "EntryBuilder", buildMethodName = "_build")
public static class Entry {
@NonNull
private Long id;

@NonNull
private String hostname;

private String ip; // hostname ip when type=A

private String target; // target hostname when type=CNAME

@NonNull
private Integer ttl;

@NonNull
private EntryType type;
private Config.Entry.Type type;

public static class EntryBuilder {
public Entry build() {
if (this.id == null) {
this.id = System.nanoTime();
}
return this._build();
}
}

public enum Type {
A,
CNAME
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@

public interface ConfigDAO {

Config.Env findActiveEnv();

Config.Env findEnv(String env);

Config.Entry findEntryForActiveEnv(String hostname);

void addEntry(String env, Config.Entry entry);
}
87 changes: 87 additions & 0 deletions src/main/java/com/mageddo/dnsproxyserver/config/ConfigDAOJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.mageddo.dnsproxyserver.config;

import com.mageddo.dnsproxyserver.config.entrypoint.ConfigJson;
import com.mageddo.dnsproxyserver.config.entrypoint.ConfigJsonV2;
import com.mageddo.dnsproxyserver.config.entrypoint.JsonConfigs;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Objects;

@Slf4j
@Singleton
@AllArgsConstructor(onConstructor = @__({@Inject}))
public class ConfigDAOJson implements ConfigDAO {

@Override
public Config.Env findActiveEnv(){
final var configPath = Configs
.getInstance()
.getConfigPath()
;
final var configJson = JsonConfigs.loadConfig(configPath);
return findEnv(configJson.getActiveEnv(), configJson);
}

@Override
public Config.Env findEnv(String envKey) {
final var configPath = Configs
.getInstance()
.getConfigPath()
;
return findEnv(envKey, JsonConfigs.loadConfig(configPath));
}

@Override
public Config.Entry findEntryForActiveEnv(String hostname) {
final var env = this.findActiveEnv();
return env.getEntries()
.stream()
.filter(it -> Objects.equals(it.getHostname(), hostname))
.findFirst()
.orElse(null);
}

@Override
public void addEntry(String env, Config.Entry entry) {

final var configPath = Configs
.getInstance()
.getConfigPath()
;

final var config = (ConfigJsonV2) JsonConfigs.loadConfig(configPath);
final var found = findOrBind(env, config);
found.add(ConfigJsonV2.Entry.from(entry));
JsonConfigs.write(configPath, config);

}

ConfigJsonV2.Env findOrBind(String envKey, ConfigJsonV2 configJson) {
for (final var env : configJson.get_envs()) {
if (Objects.equals(env.getName(), envKey)) {
log.debug("status=envFound, activeEnv={}", envKey);
return env;
}
}
log.debug("status=notFound, action=usingDefaultEnv, activeEnv={}", Config.Env.DEFAULT_ENV);
final var def = ConfigJsonV2.Env.from(Config.Env.theDefault());
configJson.get_envs().add(def);
return def;
}

static Config.Env findEnv(String envKey, final ConfigJson configJson) {
final var env = configJson
.getEnvs()
.stream()
.filter(it -> Objects.equals(it.getName(), envKey))
.findFirst()
.orElse(Config.Env.theDefault());
log.debug("activeEnv={}", env.getName());
return env;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public static Config build(ConfigFlag configFlag) {
public static Config build(ConfigFlag flag, ConfigEnv env, ConfigJson json, Path configPath) {
return Config.builder()
.version(ConfigProps.getVersion())
.activeEnv(json.getActiveEnv())
.webServerPort(Numbers.positiveOrDefault(json.getWebServerPort(), flag.getWebServerPort()))
.dnsServerPort(Numbers.positiveOrDefault(json.getDnsServerPort(), flag.getDnsServerPort()))
.defaultDns(firstNonNullRequiring(json.getDefaultDns(), flag.getDefaultDns()))
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mageddo.dnsproxyserver.config.entrypoint;

import com.mageddo.dnsproxyserver.config.Config;
import com.mageddo.dnsproxyserver.server.dns.IpAddr;

import java.util.List;
Expand Down Expand Up @@ -29,4 +30,6 @@ public interface ConfigJson {
Boolean getDpsNetworkAutoConnect();

List<IpAddr> getRemoteDnsServers();

List<Config.Env> getEnvs();
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.mageddo.dnsproxyserver.config.entrypoint;

import com.mageddo.dnsproxyserver.server.dns.IP;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.mageddo.dnsproxyserver.config.Config;
import com.mageddo.dnsproxyserver.server.dns.IpAddr;
import com.mageddo.utils.Bytes;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.experimental.Accessors;

import java.util.List;

Expand All @@ -22,8 +26,10 @@ public class ConfigJsonV1 implements ConfigJson {

private Boolean registerContainerNames;

private List<Byte[]> remoteDnsServers;
private List<Integer[]> remoteDnsServers;

@JsonProperty("envs")
private List<Env> _envs;

@Override
public Boolean getDefaultDns() {
Expand Down Expand Up @@ -54,11 +60,67 @@ public Boolean getDpsNetworkAutoConnect() {
public List<IpAddr> getRemoteDnsServers() {
return this.remoteDnsServers
.stream()
.map(it -> toIpAddr(Bytes.toNative(it)))
.map(IpAddr::of)
.toList();
}

private IpAddr toIpAddr(byte[] ip) {
return IpAddr.of(IP.of(ip));
@JsonIgnore
@Override
public List<Config.Env> getEnvs() {
return ConfigJsonV1EnvsConverter.toDomainEnvs(this._envs);
}

public ConfigJsonV2 toConfigV2() {
return new ConfigJsonV2()
.setDomain(this.getDomain())
.setActiveEnv(this.getActiveEnv())
.setDefaultDns(this.getDefaultDns())
.setDpsNetwork(this.getDpsNetwork())
.setDnsServerPort(this.getDnsServerPort())
.setWebServerPort(this.getWebServerPort())
.setDpsNetworkAutoConnect(this.getDpsNetworkAutoConnect())
.setHostMachineHostname(this.getHostMachineHostname())
.setRegisterContainerNames(this.getRegisterContainerNames())
.setLogFile(this.getLogFile())
.setLogLevel(this.getLogLevel())
.setRemoteDnsServers(this
.getRemoteDnsServers()
.stream()
.map(IpAddr::toString).toList()
)
.set_envs(this.getEnvs()
.stream()
.map(ConfigJsonV2.Env::from)
.toList()
)
;
}


@Data
@Accessors(chain = true)
public static class Env {

private String name;

@JsonProperty("hostnames")
private List<Entry> entries;
}

@Data
@Accessors(chain = true)
@NoArgsConstructor
public static class Entry {
private Long id;

@NonNull
private String hostname;

@NonNull
private Integer[] ip;

@NonNull
private Integer ttl;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.mageddo.dnsproxyserver.config.entrypoint;

import com.mageddo.dnsproxyserver.config.Config;
import com.mageddo.dnsproxyserver.server.dns.IpAddr;

import java.util.List;

public class ConfigJsonV1EnvsConverter {

static List<Config.Env> toDomainEnvs(List<ConfigJsonV1.Env> envs) {
return envs.stream()
.map(ConfigJsonV1EnvsConverter::toDomainEnv)
.toList();
}

static Config.Env toDomainEnv(ConfigJsonV1.Env env) {
return new Config.Env(env.getName(), ConfigJsonV1EnvsConverter.toDomainEntries(env.getEntries()));
}

static List<Config.Entry> toDomainEntries(List<ConfigJsonV1.Entry> hostnames) {
return hostnames
.stream()
.map(ConfigJsonV1EnvsConverter::toDomainEntry)
.toList();
}

static Config.Entry toDomainEntry(ConfigJsonV1.Entry entry) {
return Config.Entry
.builder()
.hostname(entry.getHostname())
.id(entry.getId())
.ttl(entry.getTtl())
.ip(IpAddr.of(entry.getIp()).getRawIP())
.type(Config.Entry.Type.A)
.build();
}


}
Loading