forked from c00w/bitHopper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.py
153 lines (127 loc) · 5.07 KB
/
pool.py
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
145
146
147
148
149
150
151
152
153
#License#
#bitHopper by Colin Rice is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
#Based on a work at github.com.
import json, re, ConfigParser, sys, random, traceback, logging
import pool_class
import eventlet
from eventlet.green import threading, os, time, socket
# Global timeout for sockets in case something leaks
socket.setdefaulttimeout(900)
try:
from collections import OrderedDict
except:
OrderedDict = dict
class Pool_Parse():
def __init__(self, bitHopper):
self.bitHopper = bitHopper
self.servers = {}
self.initialized = False
self.lock = threading.RLock()
self.pool_configs = ['pools.cfg', 'pools-custom.cfg']
self.started = False
self.current_list = []
self.server_map = {}
self.i = 0
with self.lock:
self.loadConfig()
self.bitHopper.db.pool = self
def load_file(self, file_path, parser):
try:
# determine if application is a script file or frozen exe
if hasattr(sys, 'frozen'):
application_path = os.path.dirname(sys.executable)
elif __file__:
application_path = os.path.dirname(__file__)
read = parser.read(os.path.join(application_path, file_path))
except:
read = parser.read(file_path)
return read
def loadConfig(self):
parser = ConfigParser.SafeConfigParser()
read = self.load_file('user.cfg', parser)
if len(read) == 0:
logging.info("user.cfg not found. You may need to move it from user.cfg.default")
os._exit(1)
userpools = parser.sections()
read_items = 0
for file_name in self.pool_configs:
read = self.load_file(file_name, parser)
read_items += len(read)
if len(read) == 0:
logging.info(file_name + " not found.")
if self.initialized == False:
if read_items == 0:
os._exit(1)
for pool in userpools:
self.servers[pool] = pool_class.Pool(pool, dict(parser.items(pool)), self.bitHopper)
for pool in parser.sections():
try:
if 'role' in dict(parser.items(pool)) and pool not in self.servers:
self.servers[pool] = pool_class.Pool(pool, dict(parser.items(pool)), self.bitHopper)
except:
continue
# random UA strings
try:
if ( self.bitHopper.config.getboolean('main', 'use_random_ua') ):
ua_strings = self.bitHopper.config.get('main', 'random_ua_list').split('|')
for pool in self.servers:
if 'user_agent' not in self.servers[pool]:
idx = random.randint(0, len(ua_strings)-1)
self.servers[pool]['user_agent'] = ua_strings[idx]
except:
traceback.print_exc()
if self.servers == {}:
logging.info("No pools found in pools.cfg or user.cfg")
if len(self.current_list) == 0:
self.current_list = [pool]
self.bitHopper.db.check_database()
#self.setup(self.bitHopper)
def setup(self, bitHopper):
with self.lock:
self.bitHopper = bitHopper
self.servers = OrderedDict(sorted(self.servers.items(), key=lambda t: t[1]['role'] + t[0]))
self.bitHopper.select_best_server()
if not self.started:
self.bitHopper.api.update_api_servers()
self.started = True
def get_entry(self, server):
if server in self.servers:
return self.servers[server]
else:
return None
def get_servers(self, ):
return self.servers
def get_current(self, ):
return self.get_work_server()
def get_work_server(self):
"""A function which returns the server to query for work.
Take the server map and cycles through it"""
value = self.i
self.i = (self.i +1) % 100
if value in self.server_map:
result = self.server_map[value]
if self.servers[result]['lag'] or self.servers[result]['role'] == 'disable':
return self.current_list[0]
else:
return result
else:
return self.current_list[0]
def build_server_map(self):
possible_servers = {}
for server in self.servers:
if 'percent' in self.servers[server]:
possible_servers[server] = int(self.servers[server]['percent'])
i = 0
server_map = {}
for k,v in possible_servers.items():
for _ in xrange(v):
server_map[i] = k
i += 1
while i <= 99:
server_map[i] = self.current_list[i%len(self.current_list)]
i += 1
self.server_map = server_map
def set_current(self, server):
self.current_server = server
if __name__ == "__main__":
print 'Run python bitHopper.py instead.'