Update HaProxy document, move server/admin to correct place

This commit is contained in:
Griatch 2021-05-25 15:13:57 +02:00
parent d4f61f1a14
commit 59dd0b007a
9 changed files with 266 additions and 83 deletions

View file

@ -1,25 +0,0 @@
#
# This sets up how models are displayed
# in the web admin interface.
#
from django.contrib import admin
from evennia.server.models import ServerConfig
class ServerConfigAdmin(admin.ModelAdmin):
"""
Custom admin for server configs
"""
list_display = ("db_key", "db_value")
list_display_links = ("db_key",)
ordering = ["db_key", "db_value"]
search_fields = ["db_key"]
save_as = True
save_on_top = True
list_select_related = True
admin.site.register(ServerConfig, ServerConfigAdmin)

View file

@ -8,9 +8,10 @@ Config values should usually be set through the
manager's conf() method.
"""
import pickle
from django.db import models
from django.urls import reverse
from django.contrib.contenttypes.models import ContentType
from evennia.utils.idmapper.models import WeakSharedMemoryModel
from evennia.utils import logger, utils
from evennia.utils.dbserialize import to_pickle, from_pickle
@ -110,7 +111,7 @@ class ServerConfig(WeakSharedMemoryModel):
value = property(__value_get, __value_set, __value_del)
class Meta(object):
class Meta:
"Define Django meta options"
verbose_name = "Server Config value"
verbose_name_plural = "Server Config values"
@ -118,9 +119,8 @@ class ServerConfig(WeakSharedMemoryModel):
#
# ServerConfig other methods
#
def __repr__(self):
return "<{} {}>".format(self.__class__.__name__, self.key, self.value)
return "<{} {}>".format(self.__class__.__name__, self.key)
def store(self, key, value):
"""

View file

@ -13,7 +13,7 @@ class Throttle(object):
This version of the throttle is usable by both the terminal server as well
as the web server, imposes limits on memory consumption by using deques
with length limits instead of open-ended lists, and uses native Django
with length limits instead of open-ended lists, and uses native Django
caches for automatic key eviction and persistence configurability.
"""
@ -37,28 +37,28 @@ class Throttle(object):
except Exception as e:
logger.log_trace("Throttle: Errors encountered; using default cache.")
self.storage = caches['default']
self.name = kwargs.get('name', 'undefined-throttle')
self.limit = kwargs.get("limit", 5)
self.cache_size = kwargs.get('cache_size', self.limit)
self.timeout = kwargs.get("timeout", 5 * 60)
def get_cache_key(self, *args, **kwargs):
"""
Creates a 'prefixed' key containing arbitrary terms to prevent key
collisions in the same namespace.
"""
return '-'.join((self.name, *args))
def touch(self, key, *args, **kwargs):
"""
Refreshes the timeout on a given key and ensures it is recorded in the
key register.
Args:
key(str): Key of entry to renew.
"""
cache_key = self.get_cache_key(key)
if self.storage.touch(cache_key, self.timeout):
@ -86,11 +86,11 @@ class Throttle(object):
keys_key = self.get_cache_key('keys')
keys = self.storage.get_or_set(keys_key, set(), self.timeout)
data = self.storage.get_many((self.get_cache_key(x) for x in keys))
found_keys = set(data.keys())
if len(keys) != len(found_keys):
self.storage.set(keys_key, found_keys, self.timeout)
return data
def update(self, ip, failmsg="Exceeded threshold."):
@ -107,14 +107,14 @@ class Throttle(object):
"""
cache_key = self.get_cache_key(ip)
# Get current status
previously_throttled = self.check(ip)
# Get previous failures, if any
entries = self.storage.get(cache_key, [])
entries.append(time.time())
# Store updated record
self.storage.set(cache_key, deque(entries, maxlen=self.cache_size), self.timeout)
@ -124,54 +124,54 @@ class Throttle(object):
# If this makes it engage, log a single activation event
if not previously_throttled and currently_throttled:
logger.log_sec(f"Throttle Activated: {failmsg} (IP: {ip}, {self.limit} hits in {self.timeout} seconds.)")
self.record_ip(ip)
def remove(self, ip, *args, **kwargs):
"""
Clears data stored for an IP from the throttle.
Args:
ip(str): IP to clear.
"""
exists = self.get(ip)
if not exists: return False
cache_key = self.get_cache_key(ip)
self.storage.delete(cache_key)
self.unrecord_ip(ip)
# Return True if NOT exists
return ~bool(self.get(ip))
def record_ip(self, ip, *args, **kwargs):
"""
Tracks keys as they are added to the cache (since there is no way to
Tracks keys as they are added to the cache (since there is no way to
get a list of keys after-the-fact).
Args:
ip(str): IP being added to cache. This should be the original
IP, not the cache-prefixed key.
"""
keys_key = self.get_cache_key('keys')
keys = self.storage.get(keys_key, set())
keys.add(ip)
self.storage.set(keys_key, keys, self.timeout)
return True
def unrecord_ip(self, ip, *args, **kwargs):
"""
Forces removal of a key from the key registry.
Args:
ip(str): IP to remove from list of keys.
"""
keys_key = self.get_cache_key('keys')
keys = self.storage.get(keys_key, set())
try:
try:
keys.remove(ip)
self.storage.set(keys_key, keys, self.timeout)
return True
@ -194,7 +194,7 @@ class Throttle(object):
"""
now = time.time()
ip = str(ip)
cache_key = self.get_cache_key(ip)
# checking mode
@ -210,4 +210,4 @@ class Throttle(object):
self.remove(ip)
return False
else:
return False
return False