Skip to content

Commit 5d0a3e8

Browse files
committed
默认校验路径
1 parent 2280109 commit 5d0a3e8

11 files changed

Lines changed: 78 additions & 129 deletions

File tree

.github/workflows/windows.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ jobs:
4646
if: steps.cache-xmake.outputs.cache-hit != 'true'
4747
run: |
4848
# xmake g --pkg_installdir=./cache/.xmake/packages
49-
xmake f -vD -y -F action_xmake.lua
49+
xmake f -vD -y -F --openssl_no_sys=y
5050
5151
- name: Build
5252
run: |
53-
xmake -vrDw -y -F action_xmake.lua
53+
xmake -vrDw -y -F
5454
5555
- name: Package
5656
run: |
57-
xmake pack -y -o ./ -F action_xmake.lua
57+
xmake pack -y -o ./ -F
5858
5959
- name: Upload Artifact
6060
uses: actions/upload-artifact@v4

action_xmake.lua

Lines changed: 0 additions & 104 deletions
This file was deleted.

src/config.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ namespace hcpp
144144
if (path_prefix != nullptr)
145145
{
146146
i = path_prefix + i.substr(1);
147+
}else{
148+
log::warn("没有HOME系统变量,不处理 ~");
147149
}
148150
}
149151
};

src/https/ssl_socket_wrap.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "ssl_socket_wrap.h"
22
#include "http/thack.h"
3+
#include "os/common.h"
34

45
#include <asio/streambuf.hpp>
56
#include <asio/read_until.hpp>
@@ -187,8 +188,6 @@ namespace hcpp
187188
// context.use_certificate_file("output.crt", ssl::context::file_format::pem);
188189
ctx_->use_certificate_chain(asio::buffer(si.cert_pem_));
189190
ctx_->use_private_key(asio::buffer(si.pkey_pem_), asio::ssl::context::pem);
190-
// ctx_->use_certificate_chain_file("server.crt.pem");
191-
// ctx_->use_private_key_file("server.key.pem", asio::ssl::context::pem);
192191
ssl_sock_ = std::make_unique<ssl_socket>(std::move(sock), *ctx_);
193192
}
194193

@@ -228,9 +227,14 @@ namespace hcpp
228227
}
229228
}
230229

231-
void ssl_sock_mem::set_verify_callback(verify_callback cb)
230+
void ssl_sock_mem::set_verify_callback(verify_callback cb, std::optional<string> verify_path)
232231
{
233-
ctx_->add_verify_path(X509_get_default_cert_dir());
232+
if (!verify_path)
233+
{
234+
set_platform_default_verify_store(*ctx_);
235+
}else{
236+
ctx_->add_verify_path(*verify_path);
237+
}
234238
ssl_sock_->set_verify_mode(ssl::verify_peer);
235239
ssl_sock_->set_verify_callback(cb);
236240
}

src/https/ssl_socket_wrap.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "asio_coroutine_net.h"
55

66
#include <set>
7+
#include <optional>
78

89
#include <asio/ssl.hpp>
910

@@ -58,7 +59,7 @@ namespace hcpp
5859
void set_sni(std::string sni);
5960
void close_sni();
6061
using verify_callback = std::function<bool(bool, ssl::verify_context &)>;
61-
void set_verify_callback(verify_callback cb);
62+
void set_verify_callback(verify_callback cb,std::optional<string> verify_path=std::nullopt);
6263

6364
public:
6465
~ssl_sock_mem();

src/httpserver.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ namespace hcpp
181181
ta_ = w;
182182
}
183183

184-
static thread_local std::unordered_map<std::string, subject_identify> ca_subject_map;
184+
static thread_local std::unordered_map<std::string, subject_identify> server_subject_map;
185185

186186
awaitable<void> mimt_https_server::wait_c(std::size_t cn, std::vector<proxy_service> ps)
187187
{
@@ -210,7 +210,6 @@ namespace hcpp
210210
{
211211
try
212212
{
213-
// 放到单独的协程运行
214213
std::shared_ptr<channel_client> cc = co_await c->async_receive();
215214
auto hsc = std::make_unique<https_client>();
216215
hsc->host_ = cc->host_;
@@ -221,6 +220,7 @@ namespace hcpp
221220
log::error("无法获取socket");
222221
continue;
223222
}
223+
//TODO 抽象工厂方法
224224
auto sk = std::make_shared<mitm_svc>();
225225
part_cert_info pci{};
226226

@@ -232,15 +232,15 @@ namespace hcpp
232232
sk->close_sni();
233233
}
234234
co_await sk->make_memory(hsc->host_, hsc->service_, pci);
235-
if (auto i = ca_subject_map.find(hsc->host_); i != ca_subject_map.end())
235+
if (auto i = server_subject_map.find(hsc->host_); i != server_subject_map.end())
236236
{
237237
si = i->second;
238238
}
239239
else
240240
{
241-
log::info("mimt_https_server::wait_c:创建ca_subject_map缓存 => {}", hsc->host_);
241+
log::info("mimt_https_server::wait_c:创建server_subject_map缓存 => {}", hsc->host_);
242242
si = sk->make_fake_server_id(pci.dns_name_, ca_subject);
243-
ca_subject_map.insert({hsc->host_, si});
243+
server_subject_map.insert({hsc->host_, si});
244244
}
245245

246246
hsc->set_mem(co_await std::move(*cc).make(std::move(si)));
@@ -300,4 +300,9 @@ namespace hcpp
300300
ca_subject_ = std::make_shared<subject_identify>(std::move(ca_subject));
301301
}
302302

303+
void mimt_https_server::set_root_verify_store_path(std::string_view root_verify_store_path)
304+
{
305+
root_verify_store_path_=root_verify_store_path;
306+
}
307+
303308
} // namespace hcpp

src/httpserver.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
namespace hcpp
1818
{
19-
2019
using asio::awaitable;
2120
using namespace asio::experimental;
2221

@@ -85,9 +84,11 @@ namespace hcpp
8584
std::set<std::pair<std::string, std::string>> tunnel_set_;
8685

8786
void set_ca(subject_identify ca_subject);
87+
void set_root_verify_store_path(std::string_view root_verify_store_path);
8888
private:
8989
std::shared_ptr<socket_channel> channel_;
9090
std::shared_ptr<subject_identify> ca_subject_;
91+
std::string root_verify_store_path_;
9192
};
9293

9394
using notify_channel = asio::use_awaitable_t<>::as_default_on_t<concurrent_channel<void(asio::error_code, std::string)>>;

src/os/common.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef SRC_OS_COMMON
2+
#define SRC_OS_COMMON
3+
4+
#include <asio/ssl.hpp>
5+
6+
namespace hcpp
7+
{
8+
using namespace asio;
9+
10+
void set_platform_default_verify_store(ssl::context& ctx);
11+
} // namespace hcpp
12+
13+
#endif /* SRC_OS_COMMON */

src/os/linux.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "common.h"
2+
3+
namespace hcpp
4+
{
5+
void set_platform_default_verify_store(ssl::context& ctx)
6+
{
7+
// ctx_->add_verify_path(X509_get_default_cert_dir());
8+
ctx.set_default_verify_paths();
9+
}
10+
} // namespace hcpp

src/os/windows.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "common.h"
2+
3+
namespace hcpp
4+
{
5+
void set_platform_default_verify_store(ssl::context& ctx)
6+
{
7+
8+
}
9+
} // namespace hcpp

0 commit comments

Comments
 (0)