Skip to content

Commit

Permalink
Auto configure as default DNS: OSX (#335)
Browse files Browse the repository at this point in the history
* cobfiguring bat

* creating service to list networks

* Creating new services

* New services

* Some test

* Created mac default dns configurator

* making it mockable

* refactoring

* Testing

* Add osx configurator

* Add logs

* removing log var which doesn't makes sense anymore

* must treat exception

* Logs...

* setup docs image for dev

* Hugo docs is working

* mac running it

* testing activate docker for mac

* enabling docker features for mac

* Docs adjustments

* [Gradle Release Plugin] - new version commit:  '3.6.1'.

* doc upadte

* [Gradle Release Plugin] - new version commit:  '3.7.0'.

* fixing tests

* unnecessary class
  • Loading branch information
mageddo authored Mar 4, 2023
1 parent 060c7e4 commit e5aeab0
Show file tree
Hide file tree
Showing 20 changed files with 422 additions and 124 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ or via docker looking at [Dockerhub images][3].

Basic running it on Linux
```bash
$ curl -s -L https://github.com/mageddo/dns-proxy-server/releases/download/3.6.0/dns-proxy-server-linux-amd64-3.6.0.tgz | tar -vzx &&\
$ curl -s -L https://github.com/mageddo/dns-proxy-server/releases/download/3.7.0/dns-proxy-server-linux-amd64-3.7.0.tgz | tar -vzx &&\
sudo ./dns-proxy-server
```

Expand Down
3 changes: 3 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 3.7.0
* Support to configure DPS as default DNS Server on MacOS, [see the docs]()

### 3.6.0
* Configure DPS at system-resolved with a custom port when is necessary
* Auto DNS configurator will give up to configure after 3 failures
Expand Down
9 changes: 9 additions & 0 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@ services:
# - "127.0.0.1:53:53/udp"
# - "127.0.0.1:53:53/tcp"
network_mode: host

# https://github.com/gohugoio/hugo/releases/tag/v0.55.4
docs:
image: docker.io/defreitas/tools-generic-cd:0.2.4
volumes:
- ./docs:/src
ports:
- 1313:1313
command: "hugo server --bind 0.0.0.0 --source /src"
43 changes: 40 additions & 3 deletions docs/content/1-getting-started/running-it/_index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,49 @@ $ nslookup -port=8980 google.com 127.0.0.1

### Running on MAC

MAC isn't fully supported yet but as of DPS 3 we are one step closer to solve this issue, stay tight on
[the discussion][5] to keep up to date.
DPS actually works on MAC except by docker features. Despite on this,
there is no additional configuration to run DPS on docker, you can do just like [on Linux](#running-on-linux).

Download the [latest release][3], extract and run:
```bash
$ sudo ./dns-proxy-server
```

When running on standalone mode (not on docker container) DPS will be able to auto-configure itself as OSX default DNS,
after 5 seconds you see something like `usingDnsConfigurator=DnsConfiguratorOSx` at the logs.

If by some reason it doesn't work or you want to configure it manually because are running DPS on a docker conainer,
then check the instructions bellow:

To list available networks:
```bash
$ networksetup -listallnetworkservices
An asterisk (*) denotes that a network service is disabled.
USB 10/100/1000 LAN
Wi-Fi
Thunderbolt Bridge
```
In my case the right Network is `Wi-Fi`, before change anything let's check if it has some manual
configured DNS server:
```bash
$ networksetup -getdnsservers Wi-Fi
There aren't any DNS Servers set on Wi-Fi.
```
If it returns some server IP then is a good idea to save it to restore the configurations later.
Let's set DPS as the default DNS Server, you can get DPS IP by search for `Starting UDP server` at the starting logs,
it's `192.168.0.14` in my case, remember you need to run DPS in port 53 as MAC doesn't accept custom port especification.

```bash
$ networksetup -setdnsservers Wi-Fi 192.168.0.14
```

If you need to remove the configured DNS server then it will use your network provider DNS
```bash
$ networksetup -setdnsservers Wi-Fi Empty
```

[1]: https://imgur.com/a/LlDH8AM
[2]: {{%relref "2-features/dps-network-resolution/_index.md" %}}
[3]: https://github.com/mageddo/dns-proxy-server/releases
[4]: https://github.com/mageddo/dns-proxy-server/issues/314
[5]: https://github.com/mageddo/dns-proxy-server/issues/158
2 changes: 1 addition & 1 deletion docs/content/5-developing/_index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ $ ./gradlew release

Major version
```bash
$ ./gradlew release -Prelease.releaseVersion=3.6.0 -Prelease.newVersion=3.6.0
$ VERSION=3.7.0 && ./gradlew release -Prelease.releaseVersion=${VERSION} -Prelease.newVersion=${VERSION}
```
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=3.6.0
version=3.7.0
quarkusPluginId=io.quarkus
quarkusPluginVersion=2.16.0.Final
quarkusPlatformGroupId=io.quarkus.platform
Expand Down
184 changes: 92 additions & 92 deletions gradlew.bat
Original file line number Diff line number Diff line change
@@ -1,92 +1,92 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar


@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*

:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar


@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*

:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static Entry sample() {
return Entry.from(Config.Entry
.builder()
.type(Type.A)
.hostname("dps-sample.local")
.hostname("dps-sample.dev")
.ip("192.168.0.254")
.ttl(30)
.id(1L)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.mageddo.dnsproxyserver.dnsconfigurator;

import com.mageddo.dnsproxyserver.server.dns.IpAddr;
import com.mageddo.dnsproxyserver.utils.Dns;
import com.mageddo.os.osx.Networks;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Singleton
@RequiredArgsConstructor(onConstructor = @__({@Inject}))
public class DnsConfiguratorOSx implements DnsConfigurator {

private final Map<String, List<String>> serversBefore = new HashMap<>();

@Override
public void configure(IpAddr addr) {
Dns.validateIsDefaultPort(addr);
for (final String network : this.findNetworks()) {
final var serversBefore = this.findNetworkDnsServers(network);
if (serversBefore != null) {
this.serversBefore.put(network, serversBefore);
final var success = this.updateDnsServers(network, Collections.singletonList(addr.getRawIP()));
log.debug("status=configuring, network={}, serversBefore={}, success={}", network, serversBefore, success);
}
}
}

@Override
public void restore() {
log.info("status=restoringPreviousDnsServers...");
this.serversBefore.forEach((network, servers) -> {
final var success = this.updateDnsServers(network, servers);
log.info("status=restoring, network={}, servers={}, success={}", network, servers, success);
});
}

boolean updateDnsServers(String network, List<String> servers) {
return Networks.updateDnsServers(network, servers);
}

List<String> findNetworkDnsServers(String network) {
return Networks.findNetworkDnsServersOrNull(network);
}

List<String> findNetworks() {
return Networks.findNetworksNames();
}

Map<String, List<String>> getServersBefore() {
return this.serversBefore;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.mageddo.commons.concurrent.ThreadPool;
import com.mageddo.dnsproxyserver.config.Config;
import com.mageddo.dnsproxyserver.config.Configs;
import com.mageddo.dnsproxyserver.dnsconfigurator.linux.LinuxDnsConfigurator;
import com.mageddo.dnsproxyserver.dnsconfigurator.linux.DnsConfiguratorLinux;
import com.mageddo.dnsproxyserver.server.dns.IpAddr;
import io.quarkus.runtime.StartupEvent;
import lombok.RequiredArgsConstructor;
Expand All @@ -23,7 +23,8 @@
@RequiredArgsConstructor(onConstructor = @__({@Inject}))
public class DnsConfigurators {

private final LinuxDnsConfigurator linuxConfigurator;
private final DnsConfiguratorLinux linuxConfigurator;
private final DnsConfiguratorOSx osxConfigurator;
private final DpsIpDiscover ipDiscover;
private final AtomicInteger failures = new AtomicInteger();

Expand Down Expand Up @@ -51,10 +52,7 @@ private void configurationHook(Config config) {
} catch (Throwable e) {
this.failures.incrementAndGet();
if (e instanceof IOException) {
log.warn(
"status=failedToConfigureAsDefaultDns, path={}, msg={}:{}",
config.getResolvConfPaths(), ClassUtils.getName(e), e.getMessage()
);
log.warn("status=failedToConfigureAsDefaultDns, msg={}:{}", ClassUtils.getName(e), e.getMessage());
} else {
log.warn("status=failedToConfigureAsDefaultDns, path={}, msg={}", config.getResolvConfPaths(), e.getMessage(), e);
}
Expand Down Expand Up @@ -105,7 +103,15 @@ DnsConfigurator getInstance() {
}

private DnsConfigurator getInstance0() {
if (OS.isFamilyUnix() && !OS.isFamilyMac()) {
final var instance = this.getInstance1();
log.info("usingDnsConfigurator={}", ClassUtils.getSimpleName(instance));
return instance;
}

private DnsConfigurator getInstance1() {
if (OS.isFamilyMac()) {
return this.osxConfigurator;
} else if (OS.isFamilyUnix()) {
return this.linuxConfigurator;
}
log.info("status=unsupported-platform-to-set-as-default-dns-automatically, os={}", System.getProperty("os.name"));
Expand Down
Loading

0 comments on commit e5aeab0

Please sign in to comment.