Skip to content

Commit 5734951

Browse files
committed
v
1 parent 54b677f commit 5734951

5 files changed

Lines changed: 61 additions & 48 deletions

File tree

src/hcpp-cfg.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"host_":"avatars.githubusercontent.com",
2828
"svc_":"443",
2929
"url_":"",
30-
"mitm_":false,
30+
"mitm_":true,
3131
"doh_":true,
3232
"sni_host_":"www.baidu.com",
3333
"close_sni_":false
@@ -36,7 +36,7 @@
3636
"host_":"github.githubassets.com",
3737
"svc_":"443",
3838
"url_":"",
39-
"mitm_":false,
39+
"mitm_":true,
4040
"doh_":true,
4141
"sni_host_":"www.baidu.com",
4242
"close_sni_":false

src/http/httpclient.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "memory.h"
44
#include "socket_wrap.h"
55
#include "httpmsg.h"
6+
#include "asio_coroutine_net.h"
67

78
#include <optional>
89

@@ -50,7 +51,7 @@ namespace hcpp
5051

5152
virtual http_request make_request() const;
5253

53-
virtual void init(){};
54+
virtual awaitable<void> init(){co_return;}
5455

5556
protected:
5657
std::shared_ptr<memory> mem_;

src/https/mitm_svc.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,10 @@ namespace hcpp
210210
return hr;
211211
}
212212

213-
void https_client::init()
213+
awaitable<void> https_client::init()
214214
{
215-
//TODO 在这里实现创建假证书
215+
assert(init_);
216+
co_await init_(*this);
216217
}
217218

218219
awaitable<std::shared_ptr<memory>> channel_client::make(subject_identify si) &&

src/https/mitm_svc.h

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,6 @@ namespace hcpp
3535
// HACK 好像只能支持发送存在默认构造函数的对象.所以不能使用std::unique_ptr
3636
// using socket_channel = asio::use_awaitable_t<>::as_default_on_t<concurrent_channel<void(asio::error_code, std::shared_ptr<tls_client>)>>;
3737

38-
struct https_client : public http_client
39-
{
40-
virtual http_request make_request() const override;
41-
42-
virtual void init() override;
43-
44-
void set_mem(std::shared_ptr<memory> m)
45-
{
46-
mem_ = m;
47-
}
48-
49-
public:
50-
std::string host_;
51-
std::string service_;
52-
};
53-
5438
struct part_cert_info
5539
{
5640
std::vector<std::string> dns_name_;
@@ -63,6 +47,14 @@ namespace hcpp
6347
awaitable<std::shared_ptr<memory>> create(std::string host, std::string service) override;
6448
};
6549

50+
template <typename T>
51+
class cache
52+
{
53+
public:
54+
virtual std::optional<T> get() {}
55+
virtual void put(T t) {}
56+
};
57+
6658
// https://zh.cppreference.com/w/cpp/memory/enable_shared_from_this
6759
// XXX std::shared_ptr 的构造函数检测无歧义且可访问的 enable_shared_from_this 基类(即强制公开继承)
6860
class mitm_svc : public http_svc_keeper, public std::enable_shared_from_this<mitm_svc>
@@ -90,7 +82,24 @@ namespace hcpp
9082
std::string sni_host_;
9183
bool enable_sni_ = true;
9284

93-
std::function<bool (bool preverified, ssl::verify_context &v_ctx)> verify_fun_{};
85+
std::function<bool(bool preverified, ssl::verify_context &v_ctx)> verify_fun_{};
86+
};
87+
88+
struct https_client : public http_client
89+
{
90+
virtual http_request make_request() const override;
91+
92+
virtual awaitable<void> init() override;
93+
94+
void set_mem(std::shared_ptr<memory> m)
95+
{
96+
mem_ = m;
97+
}
98+
99+
public:
100+
std::string host_;
101+
std::string service_;
102+
std::function<awaitable<void> (https_client & )> init_{};
94103
};
95104

96105
class channel_tunnel : public tunnel, public std::enable_shared_from_this<channel_tunnel>, public mem_move

src/httpserver.cpp

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace hcpp
1919

2020
awaitable<void> http_do(std::unique_ptr<http_client> client, std::shared_ptr<service_keeper> sk)
2121
{
22-
client->init();
22+
co_await client->init();
2323
while (true)
2424
{
2525
auto ss = client->get_memory();
@@ -211,11 +211,10 @@ namespace hcpp
211211
try
212212
{
213213
std::shared_ptr<channel_client> cc = co_await c->async_receive();
214-
//BUG 这里以下部分需要放到协程执行,因为一些操作会阻塞住线程
214+
// BUG 这里以下部分需要放到协程执行,因为一些操作会阻塞住线程
215215
auto hsc = std::make_unique<https_client>();
216216
hsc->host_ = cc->host_;
217217
hsc->service_ = cc->service_;
218-
log::error("channel_client: {}", hsc->host_);
219218
if (!cc->sock_)
220219
{
221220
log::error("无法获取socket");
@@ -224,32 +223,35 @@ namespace hcpp
224223
// TODO 抽象工厂方法
225224
auto sk = std::make_shared<mitm_svc>();
226225

227-
subject_identify si{};
228-
if (auto i = cr_ps_map.find(hsc->host_); i != cr_ps_map.end())
226+
hsc->init_ = [&cr_ps_map,sk,cc,ca_subject](https_client &self)->awaitable<void>
229227
{
230-
sk->set_sni_host(i->second.sni_host_);
231-
if (i->second.close_sni_)
232-
sk->close_sni();
233-
}
228+
subject_identify si{};
229+
if (auto i = cr_ps_map.find(self.host_); i != cr_ps_map.end())
230+
{
231+
sk->set_sni_host(i->second.sni_host_);
232+
if (i->second.close_sni_)
233+
sk->close_sni();
234+
}
234235

235-
if (auto i = server_subject_map.find(hsc->host_); i != server_subject_map.end())
236-
{
237-
co_await sk->make_memory(hsc->host_, hsc->service_);
238-
si = i->second;
239-
}
240-
else
241-
{
242-
part_cert_info pci{};
243-
sk->add_SAN_collector(pci);
244-
co_await sk->make_memory(hsc->host_, hsc->service_);
245-
si = sk->make_fake_server_id(pci.dns_name_, ca_subject);
246-
// XXX 插入失败不用管
247-
server_subject_map.insert({hsc->host_, si});
248-
log::error("mimt_https_server::wait_c:创建server_subject_map缓存 => {}", hsc->host_);
249-
log::error("mimt_https_server::wait_c:当前缓存数量 {}", server_subject_map.size());
250-
}
236+
if (auto i = server_subject_map.find(self.host_); i != server_subject_map.end())
237+
{
238+
co_await sk->make_memory(self.host_, self.service_);
239+
si = i->second;
240+
}
241+
else
242+
{
243+
part_cert_info pci{};
244+
sk->add_SAN_collector(pci);
245+
co_await sk->make_memory(self.host_, self.service_);
246+
si = sk->make_fake_server_id(pci.dns_name_, ca_subject);
247+
// XXX 插入失败不用管
248+
server_subject_map.insert({self.host_, si});
249+
log::error("mimt_https_server::wait_c:创建server_subject_map缓存 => {}", self.host_);
250+
log::error("mimt_https_server::wait_c:当前缓存数量 {}", server_subject_map.size());
251+
}
251252

252-
hsc->set_mem(co_await std::move(*cc).make(std::move(si)));
253+
self.set_mem(co_await std::move(*cc).make(std::move(si)));
254+
};
253255

254256
co_spawn(executor, http_do(std::move(hsc), std::move(sk)), detached);
255257
}

0 commit comments

Comments
 (0)