Skip to content

Commit edaa646

Browse files
committed
增加*.abc.com类型的主机名
1 parent 604d411 commit edaa646

7 files changed

Lines changed: 71 additions & 23 deletions

File tree

doc/1.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ vscode
7676
Shift+Alt+Right 扩大选择块
7777
Shift+Alt+Left 减小选择块
7878
Ctrl+, 打开设置面板
79+
Ctrl+Shift+- 向前
80+
Ctrl+Alt+- 向后
7981

8082
常用操作
8183
定位当前打开的文件在左侧的位置 右键打开的文件编辑器的tab页,点击 reveal in explorer view

main.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,15 @@ int main(int argc, char **argv)
6868
}
6969
mhs.set_ch(std::make_shared<hcpp::socket_channel>(io_context, 10));
7070

71-
hs.attach_tunnel([&mhs](auto &&c, auto h, auto s)
72-
{
73-
if(auto r=mhs.find_tunnel(h,s);r){
74-
return *r;
75-
}
76-
return c; });
71+
hs.attach_tunnel(
72+
[&mhs](auto &&c, auto h, auto s)
73+
{
74+
if (auto r = mhs.find_tunnel(h, s); r)
75+
{
76+
return *r;
77+
}
78+
return c;
79+
});
7780

7881
auto exit_handler = [&io_context](auto &&eptr)
7982
{
@@ -89,7 +92,7 @@ int main(int argc, char **argv)
8992
}
9093
};
9194

92-
co_spawn(io_context, hs.wait_http(c->get_port(),io_context), exit_handler);
95+
co_spawn(io_context, hs.wait_http(c->get_port(), io_context), exit_handler);
9396
co_spawn(io_context, mhs.wait_c(c->get_proxy_service()), exit_handler);
9497

9598
auto create_thread = [&](auto self, int i) -> void
@@ -113,7 +116,7 @@ int main(int argc, char **argv)
113116
spdlog::debug("线程{}退出成功", i);
114117
}
115118
};
116-
auto core_size=std::thread::hardware_concurrency()%32;
119+
auto core_size = std::thread::hardware_concurrency() % 32;
117120
// 为了防止对象在多线程情况下销毁出问题
118121
std::jthread t(create_thread, create_thread, core_size);
119122

src/config.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <fstream>
77
#include <filesystem>
88
#include <tuple>
9+
#include <regex>
910

1011
#include <spdlog/spdlog.h>
1112

@@ -124,14 +125,39 @@ namespace hcpp
124125

125126
bool config::config_to(mimt_https_server &mhs)
126127
{
128+
static thread_local std::regex r(R"(^\*|((\*\.)+.+)$)");
129+
130+
std::map<unsigned int, std::pair<std::regex, std::string>> star_map;
131+
127132
for (auto &&i : cs_.proxy_service_)
128133
{
129134
if (i.mitm_)
130135
{
136+
std::smatch mr;
137+
if (std::regex_match(i.host_, mr, r))
138+
{
139+
auto nstar = mr.size();
140+
if (nstar > 2)
141+
{
142+
nstar -= 2;
143+
}
144+
assert(nstar > 0);
145+
if (i.host_ == "*")
146+
{
147+
i.host_ = "*.";
148+
}
149+
auto s = std::regex_replace(i.host_, std::regex(R"(\*\.)"), R"(.+\.)");
150+
log::info("构造的正则: {} -> {}", i.host_, s);
151+
star_map.insert({nstar, std::make_pair(std::regex(std::move(s)), i.svc_)});
152+
}
153+
131154
mhs.tunnel_set_.insert({i.host_, i.svc_});
132155
}
133156
}
134-
157+
for (auto &&i : star_map)
158+
{
159+
mhs.tunnel_regx_list_.push_back(std::move(i.second));
160+
}
135161
subject_identify si;
136162

137163
auto si_tu = std::forward_as_tuple(cs_.ca_pkey_path_, cs_.ca_cert_path_);
@@ -145,7 +171,9 @@ namespace hcpp
145171
if (path_prefix != nullptr)
146172
{
147173
i = path_prefix + i.substr(1);
148-
}else{
174+
}
175+
else
176+
{
149177
log::warn("没有HOME系统变量,不处理 ~");
150178
}
151179
}
@@ -246,7 +274,7 @@ namespace hcpp
246274
{
247275
ofs << si.pkey_pem_;
248276
ofs.close();
249-
log::warn("config::config_to: 保存{}成功",cs_.ca_pkey_path_);
277+
log::warn("config::config_to: 保存{}成功", cs_.ca_pkey_path_);
250278
}
251279
else
252280
{
@@ -261,7 +289,7 @@ namespace hcpp
261289
{
262290
ofs2 << si.cert_pem_;
263291
ofs2.close();
264-
log::warn("config::config_to: 保存{}成功",cs_.ca_cert_path_);
292+
log::warn("config::config_to: 保存{}成功", cs_.ca_cert_path_);
265293
}
266294
else
267295
{

src/hcpp-cfg.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"dns_provider_": [
77
{
88
"provider_": "1.1.1.1", //取一个名字
9-
"host_": "1.1.1.1"
9+
// "host_": "1.1.1.1"
10+
"host_": "9.9.9.9"
1011
}
1112
],
1213
"mitm_server_":{
@@ -53,7 +54,7 @@
5354
}
5455
,
5556
{
56-
"host_":"www.baidu.com",
57+
"host_":"*.github.io",
5758
"svc_":"443",
5859
"url_":"",
5960
"mitm_":true,

src/http/http_svc_keeper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ namespace hcpp
114114
{
115115
std::unique_lock<std::shared_mutex> lock(shm_c_);
116116
// 插入成功失败都无所谓
117-
cache_.insert({svc, v});
117+
cache_.insert(std::make_pair(svc, v));
118118
}
119119
m = v;
120120
}

src/httpserver.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <limits>
1010
#include <chrono>
1111
#include <map>
12+
#include <ranges>
13+
#include <algorithm>
1214

1315
#include <spdlog/spdlog.h>
1416

@@ -136,7 +138,7 @@ namespace hcpp
136138
}
137139
else // TODO 用于控制自身行为
138140
{
139-
if (auto p=base_handlers_.find({req.url_.data(),req.url_param_start_});p!=base_handlers_.end())
141+
if (auto p = base_handlers_.find({req.url_.data(), req.url_param_start_}); p != base_handlers_.end())
140142
{
141143
co_await ss->async_write_all((p->second)(req.url_));
142144
}
@@ -158,8 +160,8 @@ namespace hcpp
158160
}
159161
}
160162
}
161-
162-
void http_handler::add_handler(std::string_view path, std::function<std::string (std::string_view)> handler)
163+
164+
void http_handler::add_handler(std::string_view path, std::function<std::string(std::string_view)> handler)
163165
{
164166
base_handlers_.insert({path, handler});
165167
}
@@ -176,8 +178,7 @@ namespace hcpp
176178
hh.add_handler("/stop", [&ic](auto &&path)
177179
{
178180
ic.stop();
179-
return "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: 12\r\n\r\nserver stop!";
180-
});
181+
return "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: 12\r\n\r\nserver stop!"; });
181182
for (;;)
182183
{
183184
try
@@ -299,7 +300,7 @@ namespace hcpp
299300
};
300301
std::jthread t(https_service);
301302

302-
std::unique_ptr<int, std::function<void(int *)>> ptr(new int(0), [ &executor](auto &&p)
303+
std::unique_ptr<int, std::function<void(int *)>> ptr(new int(0), [&executor](auto &&p)
303304
{
304305
log::info("work_guard分离");
305306
executor.stop();
@@ -313,10 +314,21 @@ namespace hcpp
313314

314315
std::optional<std::shared_ptr<tunnel>> mimt_https_server::find_tunnel(std::string_view svc_host, std::string_view svc_service)
315316
{
317+
auto can_proxy = false;
318+
316319
if (tunnel_set_.contains({svc_host.data(), svc_service.data()}))
317320
{
318-
return std::make_optional(std::make_shared<channel_tunnel>(channel_));
321+
can_proxy = true;
319322
}
323+
if (std::ranges::any_of(tunnel_regx_list_, [&svc_host, &svc_service](auto &&i) {
324+
return std::regex_match(svc_host.data(),i.first)&&svc_service==i.second;
325+
}))
326+
{
327+
can_proxy = true;
328+
}
329+
330+
if (can_proxy)
331+
return std::make_optional(std::make_shared<channel_tunnel>(channel_));
320332
return std::nullopt;
321333
}
322334

@@ -332,7 +344,7 @@ namespace hcpp
332344

333345
void mimt_https_server::set_ch(std::shared_ptr<socket_channel> ch)
334346
{
335-
channel_=ch;
347+
channel_ = ch;
336348
}
337349

338350
} // namespace hcpp

src/httpserver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <functional>
1414
#include <memory>
1515
#include <set>
16+
#include <regex>
1617

1718
namespace hcpp
1819
{
@@ -91,6 +92,7 @@ namespace hcpp
9192
std::optional<std::shared_ptr<tunnel>> find_tunnel(std::string_view svc_host, std::string_view svc_service);
9293

9394
std::set<std::pair<std::string, std::string>> tunnel_set_;
95+
std::vector<std::pair<std::regex, std::string>> tunnel_regx_list_;
9496

9597
void set_ch(std::shared_ptr<socket_channel> ch);
9698

0 commit comments

Comments
 (0)