Skip to content

Commit 2280109

Browse files
committed
ca
1 parent 7340259 commit 2280109

11 files changed

Lines changed: 103 additions & 50 deletions

File tree

main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ int main(int argc, char **argv)
7777
return c; });
7878

7979
co_spawn(io_context, hs.wait_http(c->get_port()), detached);
80-
co_spawn(io_context, mhs.wait_c(10), detached);
80+
co_spawn(io_context, mhs.wait_c(10,c->get_proxy_service()), detached);
8181
// co_spawn(io_context, []()->asio::awaitable<void>{co_await hcpp::nc->async_send(asio::error_code{}, "ok");}, detached);
8282

8383
auto create_thread = [&](auto self, int i) -> void

src/certificate/cert.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,17 @@ namespace hcpp
275275
return pkey_array;
276276
}
277277

278+
std::string make_pem_str(X509_PUBKEY *pkey)
279+
{
280+
BIO *bio = BIO_new(BIO_s_mem());
281+
PEM_write_bio_X509_PUBKEY(bio, pkey);
282+
auto pkey_size = BIO_pending(bio);
283+
auto pubkey_array = new char[pkey_size + 1];
284+
BIO_read(bio, pubkey_array, pkey_size);
285+
BIO_free_all(bio);
286+
return pubkey_array;
287+
}
288+
278289
X509 *make_x509(const std::string_view cert)
279290
{
280291
auto bp = BIO_new_mem_buf(cert.data(), cert.size());

src/certificate/cert.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ namespace hcpp
4949

5050
std::string make_pem_str(X509 *cert);
5151
std::string make_pem_str(EVP_PKEY *pkey);
52+
std::string make_pem_str(X509_PUBKEY *pkey);
5253

5354
X509 *make_x509(const std::string_view cert);
5455
EVP_PKEY *make_pkey(const std::string_view pkey);

src/config.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,23 +236,29 @@ namespace hcpp
236236
si.pkey_pem_ = make_pem_str(pkey);
237237
si.cert_pem_ = make_pem_str(cert);
238238

239+
std::filesystem::path p1(cs_.ca_pkey_path_);
240+
std::filesystem::create_directories(p1.parent_path());
239241
std::ofstream ofs(cs_.ca_pkey_path_);
240242
if (ofs.is_open())
241243
{
242244
ofs << si.pkey_pem_;
243245
ofs.close();
246+
log::warn("config::config_to: 保存{}成功",cs_.ca_pkey_path_);
244247
}
245248
else
246249
{
247250
log::error("config::config_to: 打开 {} 失败", cs_.ca_pkey_path_);
248251
return false;
249252
}
250253

254+
std::filesystem::path p2(cs_.ca_cert_path_);
255+
std::filesystem::create_directories(p2.parent_path());
251256
std::ofstream ofs2(cs_.ca_cert_path_);
252257
if (ofs2.is_open())
253258
{
254259
ofs2 << si.cert_pem_;
255260
ofs2.close();
261+
log::warn("config::config_to: 保存{}成功",cs_.ca_cert_path_);
256262
}
257263
else
258264
{
@@ -274,6 +280,11 @@ namespace hcpp
274280
return cs_.port_;
275281
}
276282

283+
std::vector<proxy_service> config::get_proxy_service() const
284+
{
285+
return cs_.proxy_service_;
286+
}
287+
277288
void hcpp::config::save_callback(std::function<void(config &)> sc)
278289
{
279290
save_callback_.push_back(sc);

src/config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ namespace hcpp
8181
bool config_to(mimt_https_server &mhs);
8282

8383
uint16_t get_port() const;
84+
std::vector<proxy_service> get_proxy_service() const;
8485

8586
void save_callback(std::function<void(config &)> sc);
8687

src/hcpp-cfg.json

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
// "port_":55555,
2+
"port_":55555,
33
"ca_cert_path_": "~/.config/hcpp/ca.cert.pem",
44
"ca_pkey_path_": "~/.config/hcpp/ca.key.pem",
55
"host_mapping_path_": "", //hm.json
@@ -15,65 +15,59 @@
1515
"proxy_service_path_": "", //放到文件中
1616
"proxy_service_": [
1717
{
18-
1918
"host_":"github.com",
2019
"svc_":"443", //端口或服务名.例如 http https
2120
"url_":"", //暂未使用
2221
"mitm_":true, //使用mimt代理
2322
"doh_":true,
24-
"sni_host_":"", //假的主机名
25-
"close_sni_":false //默认就是false.优先级高于sni_host_
23+
"sni_host_":"www.baidu.com", //假的主机名
24+
"close_sni_":false //默认就是false.设置为true时,优先级高于sni_host_
2625
},
2726
{
28-
2927
"host_":"avatars.githubusercontent.com",
3028
"svc_":"443",
3129
"url_":"",
3230
"mitm_":true,
3331
"doh_":true,
34-
"sni_host_":"",
32+
"sni_host_":"www.baidu.com",
3533
"close_sni_":false
3634
},
3735
{
38-
3936
"host_":"github.githubassets.com",
4037
"svc_":"443",
4138
"url_":"",
4239
"mitm_":true,
4340
"doh_":true,
44-
"sni_host_":"",
41+
"sni_host_":"www.baidu.com",
4542
"close_sni_":false
4643
}
4744
,
4845
{
49-
5046
"host_":"gitee.com",
5147
"svc_":"443",
5248
"url_":"",
5349
"mitm_":true,
5450
"doh_":false,
55-
"sni_host_":"",
51+
"sni_host_":"www.baidu.com",
5652
"close_sni_":false
5753
}
5854
,
5955
{
60-
6156
"host_":"www.baidu.com",
6257
"svc_":"443",
6358
"url_":"",
6459
"mitm_":true,
6560
"doh_":false,
66-
"sni_host_":"",
61+
"sni_host_":"www.baidu.com",
6762
"close_sni_":false
6863
},
6964
{
70-
7165
"host_":"tieba.baidu.com",
7266
"svc_":"443",
7367
"url_":"",
7468
"mitm_":true,
7569
"doh_":false,
76-
"sni_host_":"",
70+
"sni_host_":"www.baidu.com",
7771
"close_sni_":false
7872
}
7973
]

src/https/mitm_svc.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace hcpp
4040
{
4141
enable_sni_ = false;
4242
}
43-
awaitable<void> mitm_svc::make_memory(std::shared_ptr<mitm_svc> self, std::string svc_host, std::string svc_service)
43+
awaitable<void> mitm_svc::make_memory(std::string svc_host, std::string svc_service, part_cert_info &pci)
4444
{
4545
try
4646
{
@@ -56,8 +56,7 @@ namespace hcpp
5656
{
5757
ssl_m->set_sni(sni_host_);
5858
}
59-
60-
auto verify_fun = [self, host = svc_host](bool preverified, auto &v_ctx)
59+
auto verify_fun = [&pci, host = svc_host](bool preverified, auto &v_ctx)
6160
{
6261
if (ssl::host_name_verification(host)(preverified, v_ctx))
6362
{
@@ -68,7 +67,7 @@ namespace hcpp
6867
// echo -n | openssl s_client -connect gitee.com:443 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' | openssl x509 -text -noout
6968
// 获取目标主机证书中的subjectAltName
7069
// https://www.openssl.org/docs/manmaster/man3/X509_get_ext_d2i.html
71-
if (GENERAL_NAMES *names = (GENERAL_NAMES *)X509_get_ext_d2i(cert, NID_subject_alt_name, nullptr, nullptr); names != nullptr)
70+
if (auto names = (GENERAL_NAMES *)X509_get_ext_d2i(cert, NID_subject_alt_name, nullptr, nullptr); names != nullptr)
7271
{
7372

7473
for (int j = 0; j < sk_GENERAL_NAME_num(names); j++)
@@ -79,12 +78,14 @@ namespace hcpp
7978
// 如果是DNS类型的Subject Alternative Name
8079
// 获取DNS名称
8180
char *dns_name = (char *)ASN1_STRING_get0_data(name->d.dNSName);
82-
log::error("DNS Name: {}", dns_name);
83-
self->dns_name_.push_back(dns_name);
81+
log::info("DNS Name: {}", dns_name);
82+
pci.dns_name_.push_back(dns_name);
8483
}
8584
// 可以根据需要处理其他类型的Subject Alternative Name
8685
}
8786
}
87+
88+
pci.pubkey_ = make_pem_str(X509_get_X509_PUBKEY(cert));
8889
return true;
8990
}
9091
else
@@ -107,14 +108,10 @@ namespace hcpp
107108
throw;
108109
}
109110
}
110-
subject_identify mitm_svc::make_fake_server_id(std::shared_ptr<subject_identify> si)
111+
subject_identify mitm_svc::make_fake_server_id(const std::vector<std::string> &dns_name, std::shared_ptr<subject_identify> ca_si)
111112
{
112-
if (!m_)
113-
{
114-
throw std::runtime_error("mitm_svc::make_fake_cert:m_ is null");
115-
}
116-
auto ca_pky = make_pkey(si->pkey_pem_);
117-
auto ca_cert = make_x509(si->cert_pem_);
113+
auto ca_pky = make_pkey(ca_si->pkey_pem_);
114+
auto ca_cert = make_x509(ca_si->cert_pem_);
118115

119116
auto cert = make_x509();
120117
auto pky = make_pkey();
@@ -125,7 +122,7 @@ namespace hcpp
125122

126123
set_subject(cert);
127124
// 设置目标服务器的dns
128-
add_DNS_SAN(cert, dns_name_);
125+
add_DNS_SAN(cert, dns_name);
129126

130127
add_AKI(cert);
131128
add_SKI(cert);

src/https/mitm_svc.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,21 @@ namespace hcpp
4848
std::string service_;
4949
};
5050

51+
struct part_cert_info
52+
{
53+
std::vector<std::string> dns_name_;
54+
std::string pubkey_;
55+
};
56+
5157
class ssl_mem_factory : public mem_factory
5258
{
5359
public:
5460
awaitable<std::shared_ptr<memory>> create(std::string host, std::string service) override;
5561
};
5662

57-
class mitm_svc : public http_svc_keeper, std::enable_shared_from_this<mitm_svc>
63+
// https://zh.cppreference.com/w/cpp/memory/enable_shared_from_this
64+
// XXX std::shared_ptr 的构造函数检测无歧义且可访问的 enable_shared_from_this 基类(即强制公开继承)
65+
class mitm_svc : public http_svc_keeper, public std::enable_shared_from_this<mitm_svc>
5866
{
5967
public:
6068
static std::unique_ptr<ssl_mem_factory> make_mem_factory()
@@ -68,17 +76,15 @@ namespace hcpp
6876
void set_sni_host(std::string_view host);
6977
void close_sni();
7078

71-
awaitable<void> make_memory(std::shared_ptr<mitm_svc> self, std::string svc_host, std::string svc_service);
79+
awaitable<void> make_memory(std::string svc_host, std::string svc_service, part_cert_info &pci);
7280

73-
subject_identify make_fake_server_id(std::shared_ptr<subject_identify> si);
81+
subject_identify make_fake_server_id(const std::vector<std::string> &dns_name, std::shared_ptr<subject_identify> ca_si);
7482

7583
private:
7684
std::unique_ptr<ssl_mem_factory> f_;
7785
std::shared_ptr<memory> m_;
7886
std::string sni_host_;
7987
bool enable_sni_ = true;
80-
81-
std::vector<std::string> dns_name_;
8288
};
8389

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

0 commit comments

Comments
 (0)