Skip to content

Commit

Permalink
Custom config folder support for VpnPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
bdbai committed Nov 23, 2022
1 parent 48127f6 commit d681ca2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
44 changes: 38 additions & 6 deletions Maple.Task/VpnPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "VpnPlugin.h"
#include "winrt/Windows.Storage.h"
#include "winrt/Windows.Storage.Streams.h"
#include "winrt/Windows.Storage.AccessCache.h"

extern "C" void* lwip_strerr(uint8_t) {
return (void*)(const char*)"";
Expand All @@ -15,6 +16,7 @@ namespace winrt::Maple_Task::implementation
using Windows::Storage::Streams::IOutputStream;
using namespace Windows::Networking::Vpn;
using namespace Windows::Storage;
using namespace Windows::Storage::AccessCache;

extern "C" {
typedef void(__cdecl* netstack_cb)(uint8_t*, size_t, void*);
Expand Down Expand Up @@ -110,12 +112,42 @@ namespace winrt::Maple_Task::implementation
return;
}

const auto& confPathW = localProperties.TryLookup(CONFIG_PATH_SETTING_KEY).try_as<hstring>().value_or(L"");
const auto& confPath = winrt::to_string(confPathW);
const auto confPathW = localProperties.TryLookup(CONFIG_PATH_SETTING_KEY).try_as<hstring>().value_or(L"");
const auto confPath = winrt::to_string(confPathW);
StorageFolder folder{ nullptr };
try {
folder = StorageApplicationPermissions::FutureAccessList().GetFolderAsync(ConfigFolderAccessListKey).get();
}
catch (hresult_invalid_argument const&) {}
Leaf* m_leaf{};
thread_local std::vector<HostName> dnsHosts{};
m_leaf = run_leaf(confPath.data(), [](const char* dns) {
dnsHosts.push_back(HostName{ to_hstring(dns) });
});
if (folder)
{
if (auto const pos = confPath.rfind('\\'); pos != std::string::npos)
{
auto const file = folder.GetFileAsync(to_hstring(confPath.substr(pos + 1))).get();
auto const text = FileIO::ReadBufferAsync(file).get();
auto const len = text.Length();
uint8_t* textPtr{};
check_hresult(text.as<IBufferByteAccess>()->Buffer(&textPtr));
m_leaf = uwp_run_leaf_with_config_content(reinterpret_cast<const char*>(textPtr), len,
[](const char* dns) {
dnsHosts.push_back(HostName{ to_hstring(dns) });
});
}
else
{
channel.TerminateConnection(L"Config folder has been changed. Please reset the default configuration file.");
return;
}
}
else
{
m_leaf = uwp_run_leaf(confPath.data(), [](const char* dns) {
dnsHosts.push_back(HostName{ to_hstring(dns) });
});
}

if (m_leaf == nullptr) {
channel.TerminateConnection(L"Error initializing Leaf runtime.\r\nPlease check your configuration file and default interface.\r\nPlease make sure all associated files (.dat, .mmdb, .cer) exist.");
StopLeaf();
Expand Down Expand Up @@ -145,7 +177,7 @@ namespace winrt::Maple_Task::implementation

auto leafHandle = m_leaf;
if (leafHandle != nullptr) {
stop_leaf(leafHandle);
uwp_stop_leaf(leafHandle);
m_leaf = nullptr;
}

Expand Down
3 changes: 2 additions & 1 deletion Maple.Task/VpnPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace winrt::Maple_Task::implementation
{
static const hstring CONFIG_PATH_SETTING_KEY = L"CONFIG_PATH";
static const hstring NETIF_SETTING_KEY = L"NETIF";
static constexpr std::wstring_view ConfigFolderAccessListKey = L"configFolder";
struct VpnPlugin : implements<VpnPlugin, Windows::Networking::Vpn::IVpnPlugIn>
{
VpnPlugin() = default;
Expand All @@ -30,6 +31,6 @@ namespace winrt::Maple_Task::implementation
std::queue<std::vector<uint8_t>> m_decapQueue{};
};
static const uint8_t dummyArr[] = { 0 };
static const auto dummyBuffer = winrt::make<CustomBuffer>(const_cast<uint8_t *>(static_cast<const uint8_t *>(dummyArr)), static_cast<uint32_t>(sizeof(dummyArr)));
static const auto dummyBuffer = winrt::make<CustomBuffer>(const_cast<uint8_t*>(static_cast<const uint8_t*>(dummyArr)), static_cast<uint32_t>(sizeof(dummyArr)));
static auto VpnPluginInstance = winrt::make_self<VpnPlugin>();
}
5 changes: 3 additions & 2 deletions Maple.Task/leaf.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

extern "C" {
typedef void Leaf;
Leaf* run_leaf(const char* path, void on_dns(const char*));
void stop_leaf(Leaf* leaf);
Leaf* uwp_run_leaf(const char* path, void on_dns(const char*));
Leaf* uwp_run_leaf_with_config_content(const char* config, size_t len, void on_dns(const char*));
void uwp_stop_leaf(Leaf* leaf);

typedef void NetStackHandle;
typedef int32_t NetStackSendResult;
Expand Down
2 changes: 1 addition & 1 deletion leaf
Submodule leaf updated 1 files
+45 −7 leaf-ffi/src/lib.rs

0 comments on commit d681ca2

Please sign in to comment.