Skip to content

Commit baa4724

Browse files
author
minecraft server
committed
New plugin ban_ips. Fixed bug in schedule that occurs when callback registers with same key.
1 parent f4e78e2 commit baa4724

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

plugins.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import acls, dotstrip
88
import player_cmd as cmd
99
import menu_test, testing
10-
import zone_create_menu, zone_cmds, acl_cmds, zone_confirm, long_operations
10+
import zone_create_menu, zone_cmds, acl_cmds, zone_confirm, long_operations, ban_ips
1111

1212
# Set ordering here
1313

@@ -28,6 +28,7 @@
2828
plugins.append(menus) # Should probably be before anything that depends on it
2929

3030
# features
31+
plugins.append(ban_ips)
3132
plugins.append(welcome) # Has /help, so high priority. But should be after usernames (for the welcome)
3233
plugins.append(acls)
3334
plugins.append(dotstrip) # Must be before usercolors
@@ -37,7 +38,7 @@
3738
plugins.append(zone_confirm)
3839
plugins.append(acl_cmds)
3940

40-
plugins.append(testing)
41+
#plugins.append(testing)
4142

4243
# tail
4344
plugins.append(bad_cmd) # Always last out of chat commands

plugins/ban_ips.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
AUTHOR = 'ekimekim'
3+
CONTACT = '[email protected]'
4+
DESCRIPTION = """A plugin to replicate the ban-by-ip functionality that exists
5+
in the basic server, which doesn't work with the proxy (as all connections come from localhost).
6+
Reads ban list from the server files. Refreshes every second."""
7+
8+
9+
import os, logging
10+
import schedule
11+
from helpers import send_packet
12+
from packet_decoder import Packet
13+
from config import SERVER_DIR
14+
15+
16+
bans = None
17+
ban_file = os.path.join(SERVER_DIR, "banned-ips.txt")
18+
19+
INTERVAL = 1 # Seconds between refresh
20+
21+
22+
def on_start():
23+
load_bans()
24+
25+
26+
def load_bans():
27+
global bans
28+
try:
29+
new_bans = open(ban_file, 'rU').read().split('\n')
30+
except IOError, ex:
31+
if bans is None:
32+
raise
33+
logging.warning("Failed to refresh ban file:", exc_info=True)
34+
else:
35+
bans = new_bans
36+
37+
schedule.register(INTERVAL, load_bans) # Repeat self after INTERVAL
38+
39+
40+
def on_packet(packet, user, to_server):
41+
if to_server and packet.name() == 'Handshake':
42+
ip, port = user.addr
43+
if ip in bans:
44+
packet = Packet("Disconnect", reason="No U")
45+
send_packet(packet, user, False)
46+
return []
47+
return packet

plugins/schedule.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ def on_tick(users):
5555
now = time.time()
5656
while events and now >= events[0][0]:
5757
try:
58-
events[0][1]()
58+
events.pop(0)[1]()
5959
except InvalidUserError, ex:
6060
logging.info("User %s disconnected before packet could be sent in event: %s" % (ex.args[0], events[0]))
6161
except Exception:
6262
logging.exception("Error while running event: %s" % (events[0]))
63-
events.pop(0)

0 commit comments

Comments
 (0)