def drop_trusted_ip(ip): global TRUSTED trusted = CONFIG.get("trusted", []) if ip in trusted: trusted.remove(ip) CONFIG["trusted"] = trusted TRUSTED.remove(ip) dumpJson(CONFIG, "config.json")
def get_trusted(): # https://raw.githubusercontent.com/ARKEcosystem/peers/master/mainnet.json global TRUSTED try: TRUSTED |= set([ peer["ip"] for peer in req.GET.ARKEcosystem.peers.master( "mainnet.json", peer="https://raw.githubusercontent.com") ]) except Exception: pass finally: TRUSTED |= set(["127.0.0.1"] + CONFIG.get("trusted", []))
def register(name, obj): code = binascii.hexlify( marshal.dumps( getattr(obj, "__code__" if PY3 else "func_code") ) ) CONFIG["rules"] = dict( CONFIG.get("rules", {}), **{name: code.decode("utf-8") if isinstance(code, bytes) else code} ) dumpJson(CONFIG, "config.json")
def get_peers(seeds=CONFIG.get("seeds", ["https://explorer.ark.io:8443"])): seed = random.choice(["http://%s:4003" % ip for ip in TRUSTED] + seeds) LOGGER.debug("getting peers from %s...", seed) if not req.connect(seed): raise Exception("connection with %s failed!" % seed) else: r = req.GET.api.peers() peers = r.get("data", []) for i in range(2, r.get("meta", {}).get('pageCount', 1) + 1, 1): r = req.GET.api.peers(page=i) peers.extend(r.get("data", [])) for peer in peers: yield (peer["ip"])
def register_trusted_ip(ip): global TRUSTED CONFIG["trusted"] = list(set(CONFIG.get("trusted", []) + [ip])) TRUSTED.add(ip) dumpJson(CONFIG, "config.json")
def get_suspicious_ip(): return (set(get_foreign_ip(CONFIG.get("p2p port", 4001))) - set(get_peers()) - TRUSTED)
# coding:utf-8 import subprocess import logging import random import sys from pyrex import CONFIG, dumpJson # $ pip install git+http://github.com/Moustikitos/micro-io#egg=uio from uio import req TRUSTED = set(["127.0.0.1"]) LOGGER = logging.getLogger(__name__) LOGGER.setLevel(CONFIG.get("log level", 1)) def get_trusted(): # https://raw.githubusercontent.com/ARKEcosystem/peers/master/mainnet.json global TRUSTED try: TRUSTED |= set([ peer["ip"] for peer in req.GET.ARKEcosystem.peers.master( "mainnet.json", peer="https://raw.githubusercontent.com") ]) except Exception: pass finally: TRUSTED |= set(["127.0.0.1"] + CONFIG.get("trusted", [])) def get_foreign_ip(*ports):
def load(): CONFIG.update(loadJson("config.json")) return dict([(n, _func(n, m)) for n, m in CONFIG.get("rules", {}).items()])
def drop(name): rule = CONFIG.get("rules", {}).pop(name, False) dumpJson(CONFIG, "config.json") return rule