geoip_shop_manager.py aktualisiert
This commit is contained in:
@@ -10,7 +10,7 @@ Supports three modes:
|
||||
- php-only: GeoIP blocking without CrowdSec
|
||||
- bot-only: Block only bots, shop remains globally accessible
|
||||
|
||||
v3.2.0: Added bot-only blocking mode
|
||||
v3.3.0: Added option to activate only direct shops (not behind Link11)
|
||||
"""
|
||||
|
||||
import os
|
||||
@@ -795,6 +795,26 @@ def uses_crowdsec(mode):
|
||||
return mode in ['php+crowdsec', 'bot+crowdsec']
|
||||
|
||||
|
||||
def get_direct_shops(available_shops):
|
||||
"""Return list of shops that are NOT behind Link11 (direct exposure)"""
|
||||
direct_shops = []
|
||||
for shop in available_shops:
|
||||
link11_info = check_link11(shop)
|
||||
if not link11_info['is_link11']:
|
||||
direct_shops.append(shop)
|
||||
return direct_shops
|
||||
|
||||
|
||||
def get_link11_shops(available_shops):
|
||||
"""Return list of shops that ARE behind Link11"""
|
||||
link11_shops = []
|
||||
for shop in available_shops:
|
||||
link11_info = check_link11(shop)
|
||||
if link11_info['is_link11']:
|
||||
link11_shops.append(shop)
|
||||
return link11_shops
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# MAIN FUNCTIONS
|
||||
# =============================================================================
|
||||
@@ -1129,6 +1149,153 @@ def activate_all_shops():
|
||||
print(f"{'=' * 60}")
|
||||
|
||||
|
||||
def activate_direct_shops_only():
|
||||
"""Activate blocking only for shops that are NOT behind Link11 (direct exposure)"""
|
||||
available_shops = [s for s in get_available_shops() if s not in get_active_shops()]
|
||||
|
||||
if not available_shops:
|
||||
print("\n⚠️ Keine Shops zum Aktivieren verfügbar")
|
||||
return
|
||||
|
||||
# Filter: only shops NOT behind Link11
|
||||
direct_shops = get_direct_shops(available_shops)
|
||||
link11_shops = get_link11_shops(available_shops)
|
||||
|
||||
if not direct_shops:
|
||||
print("\n⚠️ Keine direkten Shops gefunden (alle sind hinter Link11)")
|
||||
if link11_shops:
|
||||
print(f"\n📋 {len(link11_shops)} Shop(s) hinter Link11 (werden übersprungen):")
|
||||
for shop in link11_shops:
|
||||
print(f" {COLOR_GREEN}• {shop} [Link11]{COLOR_RESET}")
|
||||
return
|
||||
|
||||
print(f"\n{'=' * 60}")
|
||||
print(f" Blocking für DIREKTE Shops aktivieren (ohne Link11)")
|
||||
print(f"{'=' * 60}")
|
||||
print(f" {COLOR_GREEN}Grün = hinter Link11 (übersprungen){COLOR_RESET}")
|
||||
print(f" {COLOR_RED}Rot = Direkt (wird aktiviert){COLOR_RESET}")
|
||||
|
||||
print(f"\n📋 {len(direct_shops)} direkte Shop(s) werden aktiviert:")
|
||||
for shop in direct_shops:
|
||||
print(f" {COLOR_RED}• {shop} [Direkt]{COLOR_RESET}")
|
||||
|
||||
if link11_shops:
|
||||
print(f"\n⏭️ {len(link11_shops)} Shop(s) hinter Link11 werden übersprungen:")
|
||||
for shop in link11_shops:
|
||||
print(f" {COLOR_GREEN}• {shop} [Link11]{COLOR_RESET}")
|
||||
|
||||
mode = select_mode()
|
||||
is_bot_mode = is_bot_only_mode(mode)
|
||||
|
||||
if is_bot_mode:
|
||||
geo_region = "none"
|
||||
region_info = get_geo_region_info("none")
|
||||
else:
|
||||
geo_region = select_geo_region()
|
||||
region_info = get_geo_region_info(geo_region)
|
||||
|
||||
print(f"\n⚠️ Modus: {get_mode_description(mode)} {get_mode_icon(mode)}")
|
||||
if not is_bot_mode:
|
||||
print(f" Region: {region_info['icon']} {region_info['name']}")
|
||||
print(f" Aktiviert: {len(direct_shops)} direkte Shop(s)")
|
||||
print(f" Übersprungen: {len(link11_shops)} Link11-Shop(s)")
|
||||
|
||||
if input(f"\nFortfahren? (ja/nein): ").strip().lower() not in ['ja', 'j', 'yes', 'y']:
|
||||
print("\n❌ Abgebrochen")
|
||||
return
|
||||
|
||||
print(f"\n{'=' * 60}")
|
||||
|
||||
if uses_crowdsec(mode) and check_crowdsec():
|
||||
if not [s for s in get_active_shops() if uses_crowdsec(get_shop_mode(s))]:
|
||||
print("\n📦 Installiere CrowdSec-Watcher-Service...")
|
||||
install_watcher_service()
|
||||
|
||||
success_count = 0
|
||||
for i, shop in enumerate(direct_shops, 1):
|
||||
print(f"\n[{i}/{len(direct_shops)}] {shop}")
|
||||
|
||||
httpdocs = os.path.join(VHOSTS_DIR, shop, 'httpdocs')
|
||||
index_php = os.path.join(httpdocs, 'index.php')
|
||||
backup_php = os.path.join(httpdocs, f'index.php{BACKUP_SUFFIX}')
|
||||
blocking_file = os.path.join(httpdocs, BLOCKING_FILE)
|
||||
|
||||
if os.path.isfile(backup_php) or not os.path.isfile(index_php):
|
||||
print(f" ⚠️ Übersprungen")
|
||||
continue
|
||||
|
||||
shutil.copy2(index_php, backup_php)
|
||||
|
||||
with open(index_php, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
lines = content.split('\n')
|
||||
insert_line = 0
|
||||
for idx, line in enumerate(lines):
|
||||
if 'declare(strict_types' in line:
|
||||
insert_line = idx + 1
|
||||
break
|
||||
elif '<?php' in line and insert_line == 0:
|
||||
insert_line = idx + 1
|
||||
|
||||
require_statement = f"require_once __DIR__ . '/{BLOCKING_FILE}';"
|
||||
if require_statement not in content:
|
||||
lines.insert(insert_line, require_statement)
|
||||
with open(index_php, 'w', encoding='utf-8') as f:
|
||||
f.write('\n'.join(lines))
|
||||
|
||||
expiry = datetime.now() + timedelta(hours=72)
|
||||
|
||||
if is_bot_mode:
|
||||
if uses_crowdsec(mode):
|
||||
template = BOT_ONLY_SCRIPT_TEMPLATE
|
||||
else:
|
||||
template = BOT_ONLY_SCRIPT_TEMPLATE_NO_CROWDSEC
|
||||
|
||||
geoip_content = template.format(
|
||||
expiry_date=expiry.strftime('%Y-%m-%d %H:%M:%S CET'),
|
||||
expiry_timestamp=expiry.strftime('%Y-%m-%d %H:%M:%S'),
|
||||
log_file=LOG_FILE,
|
||||
crowdsec_queue=CROWDSEC_QUEUE_FILE,
|
||||
shop_name=shop,
|
||||
bot_patterns=generate_php_bot_patterns()
|
||||
)
|
||||
else:
|
||||
template = GEOIP_SCRIPT_TEMPLATE if uses_crowdsec(mode) else GEOIP_SCRIPT_TEMPLATE_PHP_ONLY
|
||||
geoip_content = template.format(
|
||||
region_name=region_info['name'],
|
||||
region_description=region_info['description'],
|
||||
expiry_date=expiry.strftime('%Y-%m-%d %H:%M:%S CET'),
|
||||
expiry_timestamp=expiry.strftime('%Y-%m-%d %H:%M:%S'),
|
||||
cache_file=CACHE_FILE,
|
||||
log_file=LOG_FILE,
|
||||
crowdsec_queue=CROWDSEC_QUEUE_FILE,
|
||||
shop_name=shop,
|
||||
countries_array=generate_php_countries_array(geo_region),
|
||||
min_ranges=MIN_RANGES.get(geo_region, 1000)
|
||||
)
|
||||
|
||||
with open(blocking_file, 'w', encoding='utf-8') as f:
|
||||
f.write(geoip_content)
|
||||
|
||||
if is_bot_mode:
|
||||
add_shop_to_active(shop, mode, geo_region)
|
||||
print(f" ✅ Aktiviert ({len(BOT_PATTERNS)} Bot-Patterns)")
|
||||
else:
|
||||
print(f" ⏳ Cache generieren...")
|
||||
cache_ok, count, _ = generate_and_validate_cache(httpdocs, geo_region)
|
||||
add_shop_to_active(shop, mode, geo_region)
|
||||
print(f" ✅ Aktiviert ({count:,} Ranges)" if cache_ok else f" ⚠️ Aktiviert (Cache-Warnung)")
|
||||
|
||||
success_count += 1
|
||||
|
||||
print(f"\n{'=' * 60}")
|
||||
print(f" ✅ {success_count} direkte Shop(s) aktiviert")
|
||||
print(f" ⏭️ {len(link11_shops)} Link11-Shop(s) übersprungen")
|
||||
if not is_bot_mode:
|
||||
print(f" 🛡️ Fail-Open bei Cache-Fehlern aktiv")
|
||||
print(f"{'=' * 60}")
|
||||
|
||||
|
||||
def deactivate_all_shops():
|
||||
active_shops = get_active_shops()
|
||||
|
||||
@@ -1466,7 +1633,7 @@ def show_all_logs():
|
||||
|
||||
def main():
|
||||
print("\n" + "=" * 60)
|
||||
print(" GeoIP Shop Blocker Manager v3.2.0")
|
||||
print(" GeoIP Shop Blocker Manager v3.3.0")
|
||||
print(" 🇩🇪🇦🇹🇨🇭 DACH | 🇪🇺 Eurozone+GB | 🤖 Bot-Only")
|
||||
print(" 🛡️ Mit Cache-Validierung und Fail-Open")
|
||||
print("=" * 60)
|
||||
@@ -1484,6 +1651,7 @@ def main():
|
||||
print("-" * 40)
|
||||
print("[5] 🚀 ALLE aktivieren")
|
||||
print("[6] 🛑 ALLE deaktivieren")
|
||||
print(f"[7] {COLOR_RED}🎯 Nur DIREKTE aktivieren (ohne Link11){COLOR_RESET}")
|
||||
print("-" * 40)
|
||||
print("[0] Beenden")
|
||||
|
||||
@@ -1583,6 +1751,9 @@ def main():
|
||||
elif choice == "6":
|
||||
deactivate_all_shops()
|
||||
|
||||
elif choice == "7":
|
||||
activate_direct_shops_only()
|
||||
|
||||
elif choice == "0":
|
||||
print("\n👋 Auf Wiedersehen!")
|
||||
break
|
||||
|
||||
Reference in New Issue
Block a user