0

Im currently building a internet scanner which scans ip ranges for the port 25565. When it find a ip with this port open it should ping it to get more info about the port. For that i want to use dramatiq to do it asynchrous, but dramatiq just doesnt work as I expect it. The dramatiq function should only be called if there is a result, but instead the dramatiq worker runs the script(partly).

import json
import masscan
import socket
import dramatiq
import redis
import random
import pymongo
from colorama import Fore,init
from mcstatus import JavaServer
from dramatiq.brokers.redis import RedisBroker
#-------------------------
init()
red = Fore.RED
white = Fore.WHITE
r = redis.Redis()
#-------------------------
client = pymongo.MongoClient("192.168.188.32:27017")
if client.server_info():
    print("Connected to MongoDB successfully!")
else:
    print("Could not connect to MongoDB.")
db = client["treffer"]
collection = db["ips"]
#-------------------------
#if __name__ == "__main__":
broker = RedisBroker(host="192.168.188.32", port=6379)
dramatiq.set_broker(broker)
print("startet")
#-------------------------
@dramatiq.actor
def mc_and_check_task(ip):
    host = JavaServer.lookup(ip)
    try:
        status = host.status().raw
        mc_status = {"ip": str(ip), "status": status}
        mc_status_json = json.dumps(mc_status)
        x = collection.insert_one(json.loads(mc_status_json))
        print(x)
    except socket.timeout:
        print("Fehler socket.timeout")
    except BrokenPipeError:
        print("Fehler BrokenPipeError")
    except Exception as e:
        print(f"An error occurred: + {e}")
#-------------------------


A = list(range(1,255))
B = list(range(1,255))
random.shuffle(A)
random.shuffle(B)
#-------------------------
ip_ranges = []
for a in A:
    for b in B:
        ip_range = f"{a}.{b}.0.0/16"
        ip_ranges.append(ip_range)
#-------------------------
for ip_range in ip_ranges:
    print(ip_range)
    try:
        mas = masscan.PortScanner()
        mas.scan(ip_range, ports="25565", arguments="--max-rate 100000")
        x = json.loads(mas.scan_result)
        len_result = len(x["scan"])
        print(len_result)
        if len_result > 0:
            print(f"Results: {red}{len_result}{white} ")
            for ip in x["scan"]:
                adresse = ip + ":" + "25565"
                mc_and_check_task.send(adresse)
        else:
            print(f"Results: {white}{len_result}")

    except masscan.NetworkConnectionError:
        print(f"{ip_range}masscan connection error")
    print("done scanning")

When there is a result the worker should get the task to check for more info. But instead it returns this: 132.41.0.0/16 0 Results: 0 done scanning 132.169.0.0/16 0 Results: 0 done scanning 132.222.0.0/16 0 Results: 0 done scanning 213.172.0.0/16 0 Results: 0

0 Answers0