Skip to content

Commit

Permalink
Version 20230415
Browse files Browse the repository at this point in the history
Change STUN outputs
  • Loading branch information
cnbatch committed Apr 15, 2023
1 parent 623178e commit 98ef92d
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ punchnat config1.conf config2.conf
```

### Log 文件
在首次获取打洞后的 IP 地址与端口后,以及打洞的 IP 地址与端口发生变化后,会向 Log 目录创建 ip_address.txt 文件(若存在就追加),将 IP 地址与端口写进去。
在首次获取打洞后的 IP 地址与端口后,以及打洞的 IP 地址与端口发生变化后,会向 Log 目录创建 ip_address.txt 文件(若存在就覆盖),将 IP 地址与端口写进去。

获取到的打洞地址会同时显示在控制台当中。

Expand Down
8 changes: 4 additions & 4 deletions sln/punchnat/punchnat.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>ASIO_STANDALONE;NOMINMAX;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>ASIO_STANDALONE;NOMINMAX;_CRT_SECURE_NO_WARNINGS;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
Expand All @@ -131,7 +131,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>ASIO_STANDALONE;NOMINMAX;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>ASIO_STANDALONE;NOMINMAX;_CRT_SECURE_NO_WARNINGS;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
Expand All @@ -149,7 +149,7 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>ASIO_STANDALONE;NOMINMAX;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>ASIO_STANDALONE;NOMINMAX;_CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
Expand All @@ -168,7 +168,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>ASIO_STANDALONE;NOMINMAX;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>ASIO_STANDALONE;NOMINMAX;_CRT_SECURE_NO_WARNINGS;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
Expand Down
6 changes: 4 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ int main(int argc, char *argv[])
{
if (argc <= 1)
{
printf("Usage: %s config1.conf\n", argv[0]);
printf(" %s config1.conf config2.conf...\n", argv[0]);
char app_name[] = "punchnat";
printf("%s version 20230415\n", app_name);
printf("Usage: %s config1.conf\n", app_name);
printf(" %s config1.conf config2.conf...\n", app_name);
return 0;
}

Expand Down
8 changes: 0 additions & 8 deletions src/networks/connections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,6 @@ void udp_server::handle_receive(std::unique_ptr<uint8_t[]> buffer_cache, const a
udp::endpoint copy_of_incoming_endpoint = incoming_endpoint;
start_receive();
callback(std::move(buffer_cache), bytes_transferred, copy_of_incoming_endpoint, port_number);
//asio::post(task_assigner, [this, data = std::move(buffer_cache), bytes_transferred, peer_ep = std::move(copy_of_incoming_endpoint)]() mutable
//{
// callback(std::move(data), bytes_transferred, std::move(peer_ep), port_number);
//});
}

asio::ip::port_type udp_server::get_port_number()
Expand Down Expand Up @@ -565,8 +561,4 @@ void udp_client::handle_receive(std::unique_ptr<uint8_t[]> buffer_cache, const a
udp::endpoint copy_of_incoming_endpoint = incoming_endpoint;
start_receive();
callback(std::move(buffer_cache), bytes_transferred, copy_of_incoming_endpoint, local_port);
//asio::post(task_assigner, [this, data_ptr = std::move(buffer_cache), bytes_transferred, copy_of_incoming_endpoint, local_port]() mutable
//{
// callback(std::move(data_ptr), bytes_transferred, copy_of_incoming_endpoint, local_port);
//});
}
16 changes: 11 additions & 5 deletions src/networks/tcp_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,18 @@ void tcp_mode::stun_disconnected(tcp_session *incoming_session)

void tcp_mode::save_external_ip_address(uint32_t ipv4_address, uint16_t ipv4_port, const std::array<uint8_t, 16>& ipv6_address, uint16_t ipv6_port)
{
std::string v4_info;
std::string v6_info;

if (ipv4_address != 0 && ipv4_port != 0 && (external_ipv4_address.load() != ipv4_address || external_ipv4_port.load() != ipv4_port))
{
external_ipv4_address.store(ipv4_address);
external_ipv4_port.store(ipv4_port);
std::stringstream ss;
ss << "TCP Mode - External IPv4 Address: " << asio::ip::make_address_v4(ipv4_address) << "\n";
ss << "TCP Mode - External IPv4 Port: " << ipv4_port << "\n";
std::string message = ss.str();
if (!current_settings.log_ip_address.empty())
print_ip_to_file(message, current_settings.log_ip_address);
std::cout << message;
v4_info = ss.str();
}

std::shared_lock locker(mutex_ipv6);
Expand All @@ -223,9 +224,14 @@ void tcp_mode::save_external_ip_address(uint32_t ipv4_address, uint16_t ipv4_por
std::stringstream ss;
ss << "TCP Mode - External IPv6 Address: " << asio::ip::make_address_v6(ipv6_address) << "\n";
ss << "TCP Mode - External IPv6 Port: " << ipv6_port << "\n";
std::string message = ss.str();
if (!current_settings.log_ip_address.empty())
print_ip_to_file(message, current_settings.log_ip_address);
v6_info = ss.str();
}

if (!current_settings.log_ip_address.empty())
{
std::string message = "Update Time: " + time_to_string() + "\n" + v4_info + v6_info;
print_ip_to_file(message, current_settings.log_ip_address);
std::cout << message;
}
}
Expand Down
16 changes: 11 additions & 5 deletions src/networks/udp_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,18 @@ void udp_mode::send_stun_request(const asio::error_code &e)

void udp_mode::save_external_ip_address(uint32_t ipv4_address, uint16_t ipv4_port, const std::array<uint8_t, 16>& ipv6_address, uint16_t ipv6_port)
{
std::string v4_info;
std::string v6_info;

if (ipv4_address != 0 && ipv4_port != 0 && (external_ipv4_address.load() != ipv4_address || external_ipv4_port.load() != ipv4_port))
{
external_ipv4_address.store(ipv4_address);
external_ipv4_port.store(ipv4_port);
std::stringstream ss;
ss << "UDP Mode - External IPv4 Address: " << asio::ip::make_address_v4(ipv4_address) << "\n";
ss << "UDP Mode - External IPv4 Port: " << ipv4_port << "\n";
std::string message = ss.str();
if (!current_settings.log_ip_address.empty())
print_ip_to_file(message, current_settings.log_ip_address);
std::cout << message;
v4_info = ss.str();
}

std::shared_lock locker(mutex_ipv6);
Expand All @@ -264,9 +265,14 @@ void udp_mode::save_external_ip_address(uint32_t ipv4_address, uint16_t ipv4_por
std::stringstream ss;
ss << "UDP Mode - External IPv6 Address: " << asio::ip::make_address_v6(ipv6_address) << "\n";
ss << "UDP Mode - External IPv6 Port: " << ipv6_port << "\n";
std::string message = ss.str();
if (!current_settings.log_ip_address.empty())
print_ip_to_file(message, current_settings.log_ip_address);
v6_info = ss.str();
}

if (!current_settings.log_ip_address.empty())
{
std::string message = "Update Time: " + time_to_string() + "\n" + v4_info + v6_info;
print_ip_to_file(message, current_settings.log_ip_address);
std::cout << message;
}
}
12 changes: 11 additions & 1 deletion src/shares/share_defines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdexcept>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <mutex>
#include "share_defines.hpp"
#include "string_utils.hpp"
Expand Down Expand Up @@ -129,12 +130,21 @@ int64_t calculate_difference(int64_t number1, int64_t number2)
return std::abs(number1 - number2);
}

std::string time_to_string()
{
std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);
std::ostringstream oss;
oss << std::put_time(&tm, "%F %T %z");
return oss.str();
}

void print_ip_to_file(const std::string& message, const std::filesystem::path& log_file)
{
static std::ofstream output_file{};
static std::mutex mtx;
std::unique_lock locker{ mtx };
output_file.open(log_file, std::ios::out | std::ios::app);
output_file.open(log_file, std::ios::out | std::ios::trunc);
output_file << message;
output_file.close();
}
Expand Down
1 change: 1 addition & 0 deletions src/shares/share_defines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct user_settings
user_settings parse_from_args(const std::vector<std::string> &args, std::vector<std::string> &error_msg);
void check_settings(user_settings &current_user_settings, std::vector<std::string> &error_msg);
int64_t calculate_difference(int64_t number1, int64_t number2);
std::string time_to_string();
void print_ip_to_file(const std::string& message, const std::filesystem::path& log_file);
void print_message_to_file(const std::string &message, const std::filesystem::path &log_file);

Expand Down

0 comments on commit 98ef92d

Please sign in to comment.