Skip to content
This repository has been archived by the owner on Nov 28, 2024. It is now read-only.

Commit

Permalink
auto flush and set DNS server to 127.0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
microcai committed Sep 19, 2019
1 parent 2618c26 commit 476b1f6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
51 changes: 51 additions & 0 deletions avpn/tun2socks/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <boost/asio/steady_timer.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/scope_exit.hpp>

#include "vpncore/logging.hpp"
#include "vpncore/tuntap.hpp"
Expand All @@ -22,6 +23,52 @@ using namespace avpncore;

#include "route.hpp"

#ifdef _WIN32
static void setdns()
{
HKEY dns_key = NULL;
typedef std::unique_ptr<std::remove_pointer<HKEY>::type,
decltype(&RegCloseKey)> register_closer;
DWORD dwDisposition = REG_OPENED_EXISTING_KEY;

auto status = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
DNS_KEY, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, &dns_key, &dwDisposition);
if (status != ERROR_SUCCESS)
exit(0);
register_closer adapter_key_close(dns_key, &RegCloseKey);

const wchar_t* dns_ip = L"127.0.0.1\0";

status = RegSetValueExW(dns_key, L"DhcpNameServer", 0, REG_SZ, (const BYTE*)dns_ip, lstrlenW(dns_ip) * 2);

typedef BOOL(WINAPI * DnsFlushResolverCacheFuncPtr)();

HMODULE dnsapi = LoadLibrary("dnsapi.dll");
if (dnsapi == NULL) {
printf("Failed loading module: %d\n", GetLastError());
return ;
}

BOOST_SCOPE_EXIT(&dnsapi)
{
FreeLibrary(dnsapi);
}BOOST_SCOPE_EXIT_END;

DnsFlushResolverCacheFuncPtr DnsFlushResolverCache = (DnsFlushResolverCacheFuncPtr)GetProcAddress(dnsapi, "DnsFlushResolverCache");
if (DnsFlushResolverCache == NULL) {
printf("Failed loading function: %d\n", GetLastError());
return;
}
BOOL result = DnsFlushResolverCache();
if (result) {
printf("DnsFlushResolverCache succeeded\n");
}
else {
printf("DnsFlushResolverCache succeeded: %d\n", GetLastError());
}
}
#endif

int platform_init()
{
#if defined(WIN32) || defined(_WIN32)
Expand Down Expand Up @@ -75,6 +122,7 @@ int main(int argc, char** argv)
platform_init();
init_logging(false);


boost::asio::io_context io;

dev_config cfg = { "10.0.0.1", "255.255.255.0", "10.0.0.0" };
Expand Down Expand Up @@ -123,6 +171,9 @@ int main(int argc, char** argv)
auto index = tap.get_if_index();
printf("tun device index %d\n", index);
nl_add_route(tap.get_if_index(), inet_addr("10.0.0.2"));
#ifdef _WIN32
setdns();
#endif
});

// running...
Expand Down
2 changes: 1 addition & 1 deletion avpn/tun2socks/src/route.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ int nl_add_route(int ifindex, uint32_t NewGateway)
// Copy the row
memcpy(&pRow, &(pIpForwardTable->table[i]), sizeof(MIB_IPFORWARDROW));
pRow.dwForwardMetric1 = 331;
dwStatus = SetIpForwardEntry(&pRow);/
dwStatus = SetIpForwardEntry(&pRow);
break;
}
}
Expand Down
1 change: 1 addition & 0 deletions vpncore/include/vpncore/tuntap_windows_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#define USERMODEDEVICEDIR TEXT("\\\\.\\Global\\")
#define TAPSUFFIX TEXT(".tap")
#define ADAPTER_KEY TEXT("SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}")
#define DNS_KEY TEXT("SYSTEM\\ControlSet001\\Services\\Tcpip\\Parameters")
#define NETWORK_CONNECTIONS_KEY TEXT("SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}")

#define TAP_CONTROL_CODE(request, method) \
Expand Down

0 comments on commit 476b1f6

Please sign in to comment.