android: Use configured custom DNS servers

This commit is contained in:
Tobias Brunner
2019-03-05 17:36:09 +01:00
parent dda8b891dc
commit dd5de792cf
@@ -838,8 +838,7 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
{ {
try try
{ {
mBuilder.addDnsServer(address); mCache.addDnsServer(address);
mCache.recordAddressFamily(address);
} }
catch (IllegalArgumentException ex) catch (IllegalArgumentException ex)
{ {
@@ -1073,8 +1072,9 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
private final int mSplitTunneling; private final int mSplitTunneling;
private final SelectedAppsHandling mAppHandling; private final SelectedAppsHandling mAppHandling;
private final SortedSet<String> mSelectedApps; private final SortedSet<String> mSelectedApps;
private final List<InetAddress> mDnsServers = new ArrayList<>();
private int mMtu; private int mMtu;
private boolean mIPv4Seen, mIPv6Seen; private boolean mIPv4Seen, mIPv6Seen, mDnsServersConfigured;
public BuilderCache(VpnProfile profile) public BuilderCache(VpnProfile profile)
{ {
@@ -1111,6 +1111,23 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
} }
mAppHandling = appHandling; mAppHandling = appHandling;
if (profile.getDnsServers() != null)
{
for (String server : profile.getDnsServers().split("\\s+"))
{
try
{
mDnsServers.add(InetAddress.getByName(server));
recordAddressFamily(server);
mDnsServersConfigured = true;
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
}
}
/* set a default MTU, will be set by the daemon for regular interfaces */ /* set a default MTU, will be set by the daemon for regular interfaces */
Integer mtu = profile.getMTU(); Integer mtu = profile.getMTU();
mMtu = mtu == null ? Constants.MTU_MAX : mtu; mMtu = mtu == null ? Constants.MTU_MAX : mtu;
@@ -1129,6 +1146,25 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
} }
} }
public void addDnsServer(String address)
{
/* ignore received DNS servers if any were configured */
if (mDnsServersConfigured)
{
return;
}
try
{
mDnsServers.add(InetAddress.getByName(address));
recordAddressFamily(address);
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
}
public void addRoute(String address, int prefixLength) public void addRoute(String address, int prefixLength)
{ {
try try
@@ -1179,6 +1215,10 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
{ {
builder.addAddress(address.getFrom(), address.getPrefix()); builder.addAddress(address.getFrom(), address.getPrefix());
} }
for (InetAddress server : mDnsServers)
{
builder.addDnsServer(server);
}
/* add routes depending on whether split tunneling is allowed or not, /* add routes depending on whether split tunneling is allowed or not,
* that is, whether we have to handle and block non-VPN traffic */ * that is, whether we have to handle and block non-VPN traffic */
if ((mSplitTunneling & VpnProfile.SPLIT_TUNNELING_BLOCK_IPV4) == 0) if ((mSplitTunneling & VpnProfile.SPLIT_TUNNELING_BLOCK_IPV4) == 0)