Make TCPStart ip filter more IPv6 friendly (#19199)

This commit is contained in:
s-hadinger 2023-07-26 22:52:48 +02:00 committed by GitHub
parent 1a91dc441b
commit 11f056040e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 3 deletions

View File

@ -35,6 +35,7 @@ WiFiServer *server_tcp = nullptr;
WiFiClient client_tcp[TCP_BRIDGE_CONNECTIONS]; WiFiClient client_tcp[TCP_BRIDGE_CONNECTIONS];
uint8_t client_next = 0; uint8_t client_next = 0;
uint8_t *tcp_buf = nullptr; // data transfer buffer uint8_t *tcp_buf = nullptr; // data transfer buffer
bool ip_filter_enabled = false;
IPAddress ip_filter; IPAddress ip_filter;
#include <TasmotaSerial.h> #include <TasmotaSerial.h>
@ -65,7 +66,7 @@ void TCPLoop(void)
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_TCP "Got connection from %s"), new_client.remoteIP().toString().c_str()); AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_TCP "Got connection from %s"), new_client.remoteIP().toString().c_str());
// Check for IP filtering if it's enabled. // Check for IP filtering if it's enabled.
if (ip_filter) { if (ip_filter_enabled) {
if (ip_filter != new_client.remoteIP()) { if (ip_filter != new_client.remoteIP()) {
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_TCP "Rejected due to filtering")); AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_TCP "Rejected due to filtering"));
new_client.stop(); new_client.stop();
@ -173,9 +174,10 @@ void CmndTCPStart(void) {
if (ArgC() == 2) { if (ArgC() == 2) {
char sub_string[XdrvMailbox.data_len]; char sub_string[XdrvMailbox.data_len];
ip_filter.fromString(ArgV(sub_string, 2)); ip_filter.fromString(ArgV(sub_string, 2));
ip_filter_enabled = true;
} else { } else {
// Disable whitelist if previously set // Disable whitelist if previously set
ip_filter = (uint32_t)0; ip_filter_enabled = false;
} }
if (server_tcp) { if (server_tcp) {
@ -191,7 +193,7 @@ void CmndTCPStart(void) {
} }
if (tcp_port > 0) { if (tcp_port > 0) {
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_TCP "Starting TCP server on port %d"), tcp_port); AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_TCP "Starting TCP server on port %d"), tcp_port);
if (ip_filter) { if (ip_filter_enabled) {
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_TCP "Filtering %s"), ip_filter.toString().c_str()); AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_TCP "Filtering %s"), ip_filter.toString().c_str());
} }
server_tcp = new WiFiServer(tcp_port); server_tcp = new WiFiServer(tcp_port);