Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
eterban
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
etersoft
eterban
Commits
9b775f5b
Commit
9b775f5b
authored
Jul 19, 2026
by
Vitaly Lipatov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
gateway: reconnect Redis Pub/Sub subscription
parent
6f8a02be
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
10 deletions
+45
-10
eterban_switcher.py
gateway/usr/share/eterban/eterban_switcher.py
+45
-10
No files found.
gateway/usr/share/eterban/eterban_switcher.py
View file @
9b775f5b
...
@@ -285,14 +285,35 @@ redis_server, ban_server, ban_server_ipv6, wan_ifaces, internal_interface, maxel
...
@@ -285,14 +285,35 @@ redis_server, ban_server, ban_server_ipv6, wan_ifaces, internal_interface, maxel
#print (time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()))
#print (time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()))
#subprocess.call ('ipset create blacklist hash:ip', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell = True)
#subprocess.call ('ipset create blacklist hash:ip', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell = True)
try
:
def
log_redis_error
(
message
):
r
=
redis
.
Redis
(
host
=
redis_server
)
info
=
time
.
strftime
(
"
%
Y-
%
m-
%
d
%
H:
%
M:
%
S"
,
time
.
localtime
())
p
=
r
.
pubsub
()
info
+=
" "
+
message
+
"
\n
"
print
(
info
,
end
=
''
)
log
.
write
(
info
)
log
.
flush
()
p
.
subscribe
(
'ban'
,
'unban'
,
'by'
)
except
:
def
connect_redis
():
print
(
"Unable to connect redis"
)
"""Connect to Redis and subscribe, retrying after a connection failure."""
sys
.
exit
(
1
)
while
True
:
try
:
redis_client
=
redis
.
Redis
(
host
=
redis_server
,
socket_connect_timeout
=
5
,
socket_keepalive
=
True
,
health_check_interval
=
30
,
)
redis_client
.
ping
()
pubsub
=
redis_client
.
pubsub
()
pubsub
.
subscribe
(
'ban'
,
'unban'
,
'by'
)
log_redis_error
(
"Connected to Redis and subscribed to ban, unban, by"
)
return
redis_client
,
pubsub
except
(
redis
.
exceptions
.
RedisError
,
OSError
)
as
error
:
log_redis_error
(
"Unable to connect to Redis: "
+
str
(
error
)
+
"; retrying in 5 seconds"
)
time
.
sleep
(
5
)
r
,
p
=
connect_redis
()
# Инициализация AutoBanManager
# Инициализация AutoBanManager
config
=
configparser
.
ConfigParser
()
config
=
configparser
.
ConfigParser
()
...
@@ -334,7 +355,7 @@ create_ip6tables_rules()
...
@@ -334,7 +355,7 @@ create_ip6tables_rules()
load_whitelist
()
load_whitelist
()
for
message
in
p
.
listen
(
):
def
process_message
(
message
):
if
message
is
not
None
and
message
[
'type'
]
==
'message'
and
message
[
'channel'
]
==
b
'ban'
:
if
message
is
not
None
and
message
[
'type'
]
==
'message'
and
message
[
'channel'
]
==
b
'ban'
:
ip
=
message
[
'data'
]
.
decode
(
'utf-8'
)
ip
=
message
[
'data'
]
.
decode
(
'utf-8'
)
ipo
=
ipaddress
.
ip_address
(
ip
)
ipo
=
ipaddress
.
ip_address
(
ip
)
...
@@ -344,7 +365,7 @@ for message in p.listen():
...
@@ -344,7 +365,7 @@ for message in p.listen():
print
(
info
)
print
(
info
)
log
.
write
(
info
)
log
.
write
(
info
)
log
.
flush
()
log
.
flush
()
continue
return
if
isinstance
(
ipo
,
ipaddress
.
IPv6Address
):
if
isinstance
(
ipo
,
ipaddress
.
IPv6Address
):
ban
=
'ipset -A '
+
ipset_eterban_1_ipv6
+
' '
+
ip
ban
=
'ipset -A '
+
ipset_eterban_1_ipv6
+
' '
+
ip
else
:
else
:
...
@@ -391,7 +412,7 @@ for message in p.listen():
...
@@ -391,7 +412,7 @@ for message in p.listen():
ip
=
match
.
group
(
1
)
ip
=
match
.
group
(
1
)
if
is_whitelisted
(
ip
):
if
is_whitelisted
(
ip
):
# Whitelisted IPs are not actually banned, so no offense tracking
# Whitelisted IPs are not actually banned, so no offense tracking
continue
return
source
=
match
.
group
(
2
)
.
strip
()
source
=
match
.
group
(
2
)
.
strip
()
reason
=
match
.
group
(
3
)
if
match
.
group
(
3
)
else
'auto'
reason
=
match
.
group
(
3
)
if
match
.
group
(
3
)
else
'auto'
meta
=
auto_mgr
.
on_ban
(
ip
,
source
=
source
,
reason
=
reason
)
meta
=
auto_mgr
.
on_ban
(
ip
,
source
=
source
,
reason
=
reason
)
...
@@ -414,3 +435,17 @@ for message in p.listen():
...
@@ -414,3 +435,17 @@ for message in p.listen():
log
.
flush
()
log
.
flush
()
else
:
else
:
pass
pass
while
True
:
try
:
for
message
in
p
.
listen
():
process_message
(
message
)
except
(
redis
.
exceptions
.
RedisError
,
OSError
)
as
error
:
log_redis_error
(
"Redis subscription lost: "
+
str
(
error
)
+
"; reconnecting"
)
try
:
p
.
close
()
except
(
redis
.
exceptions
.
RedisError
,
OSError
):
pass
r
,
p
=
connect_redis
()
auto_mgr
.
r
=
r
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment