瀏覽代碼

Custom config folder support for VpnPlugin

bdbai 2 年之前
父節點
當前提交
d681ca2158
共有 4 個文件被更改,包括 44 次插入10 次删除
  1. 38 6
      Maple.Task/VpnPlugin.cpp
  2. 2 1
      Maple.Task/VpnPlugin.h
  3. 3 2
      Maple.Task/leaf.h
  4. 1 1
      leaf

+ 38 - 6
Maple.Task/VpnPlugin.cpp

@@ -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*)"";
@@ -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*);
@@ -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();
@@ -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;
         }
 

+ 2 - 1
Maple.Task/VpnPlugin.h

@@ -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;
@@ -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>();
 }

+ 3 - 2
Maple.Task/leaf.h

@@ -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;

+ 1 - 1
leaf

@@ -1 +1 @@
-Subproject commit 94732a0659a5c58564ced27b4caebecb87a1aa50
+Subproject commit ba51cfb6640d1751e7beecc67e7b2dd69c0b9fb3