summarylogtreecommitdiffstats
path: root/core.py
blob: ba191b2fa4593f06f4dff6c11587be873a3928d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import logging
import time
from threading import Thread

import os

import sys

from generators import Generator
from scanners import Scanner
from settings import Settings

VERSION = '1.0'

# OpenLeecher class
# Application's core.
class OpenLeecher:
    # Init
    # Args : None
    def __init__(self):
        self.VERSION = VERSION
        self.running = False
        self.paused = False
        self.autoopen = False
        self.s = Settings()

        if getattr(sys, 'frozen', False):
            self.application_path = os.path.dirname(sys.executable)
        elif __file__:
            self.application_path = os.path.dirname(__file__)

        if os.name == 'nt':
            self.logfile = os.path.join(self.application_path, "openleecher.log")
        else:
            self.logfile = "/etc/openleecher/openleecher.log"
        logging.basicConfig(format='%(asctime)s | %(message)s', filename=self.logfile, level=logging.DEBUG)

        self.behavior = int(self.s.get('Core', 'behavior'))

        self.generator = Generator(self.s)
        self.scanner = Scanner(self.s)

        self.maxthreads = int(self.s.get('Core', 'threads'))
        self.threads = []

    # Can run
    # Return True if the core can start a session.
    # Args : None
    def can_run(self):
        if len(self.generator.get_actives()) > 0 and len(self.scanner.get_actives()) > 0:
            return True
        return False

    # Run
    # Power on the core.
    # Args : None
    def run(self):
        logging.info("Starting OpenLeecher v" + self.VERSION + "...")
        logging.info("Generators loaded : " + self.generator.tostring())
        logging.info("Scanners loaded : " + self.scanner.tostring())
        logging.info("== Session start ==")
        self.running = True
        while self.running:
            self.update_threads()
        logging.info("== Session end ==")
        self.flush_threads()
        self.deactivate_all()
        self.scanner.bl = []

    # Pause
    # Toggles the core.
    # Args : None
    def pause(self):
        self.running = not self.running

    # Stop
    # Stop the core.
    # Args : None
    def stop(self):
        self.paused = False
        self.running = False

    # Reboot
    # Reload core with new settings.
    # Args : None
    def reboot(self):
        if self.running:
            self.stop()
        while len(self.threads) > 0:
            time.sleep(1)
            print("waiting for threads to finish (" + str(len(self.threads)) + " left)")
        self.__init__()

    # Add thread
    # Manually add thread.
    # Args : v=address to scan
    def add_thread(self, v):
        self.threads.append(Thread(target=self.scanner.scan, args=(v,)))
        self.threads[len(self.threads) - 1].start()

    # Update threads
    # Clean then populate threads.
    # Args : None
    def update_threads(self):
        self.clean_threads()
        self.populate_threads()

    # Populate threads
    # Check then proceed to launch a new thread with a pair depending on the behavior.
    # Args : None
    def populate_threads(self):
        if len(self.threads) < self.maxthreads and len(self.generator.get_actives()) > 0 and len(self.scanner.get_actives()) > 0 and not self.paused:
            self.threads.append(Thread(target=self.scanner.scan, args=(self.generator.generate(), self.behavior,)))
            self.threads[len(self.threads) - 1].start()
        if self.paused:
            time.sleep(0.1)

    # Clean threads
    # Check for dead threads to remove them
    # Args : None
    def clean_threads(self):
        if len(self.threads) > 0:
            r = []
            for i in range(0, len(self.threads)):
                if self.threads[i].is_alive() is False:
                    r.append(self.threads[i])
            for x in r:
                self.threads.remove(x)
            del r

    # Flush treads
    # Remove all working threads
    # Args : None
    def flush_threads(self):
        self.threads = []

    # Deactivate All
    # Deactivate every scanner and generator
    # Args : None
    def deactivate_all(self):
        for g in self.generator.g:
            g.active = False
        for s in self.scanner.s:
            s.active = False