Permalink
Browse files

detect stratum proxies

  • Loading branch information...
1 parent 060441f commit e1fe107bd294358165e4606379086ccf6a2d4703 @m0mchil committed Oct 29, 2012
Showing with 51 additions and 11 deletions.
  1. +30 −1 StratumSource.py
  2. +18 −7 Switch.py
  3. +2 −2 log.py
  4. +1 −1 poclbm.py
View
@@ -18,6 +18,35 @@
BASE_DIFFICULTY = 0x00000000FFFF0000000000000000000000000000000000000000000000000000
+def detect_stratum_proxy(host):
+ s = None
+ try:
+ s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
+ s.sendto(dumps({"id": 0, "method": "mining.get_upstream", "params": []}), ('239.3.3.3', 3333))
+
+ say_line('Searching stratum proxy for %s', host)
+
+ s.settimeout(2)
+
+ try:
+ while True:
+ response, address = s.recvfrom(128)
+ try:
+ response = loads(response)
+ response_host = response['result'][0][0] + ':' + str(response['result'][0][1])
+ if response_host == host:
+ proxy_address = address[0] + ':' + str(response['result'][1])
+ say_line('Using stratum proxy at %s', proxy_address)
+ return proxy_address
+ except ValueError:
+ pass
+ except socket.timeout:
+ pass
+
+ finally:
+ if s != None:
+ s.close()
+
class StratumSource(Source):
def __init__(self, switch, server):
@@ -62,7 +91,7 @@ def loop(self):
if not self.options.proxy:
- self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ self.socket = socket.nodelay_socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((address, int(port)))
else:
proxy_proto, user, pwd, proxy_host = self.options.proxy[:4]
View
@@ -226,7 +226,7 @@ def set_server(self, server):
name = server[4]
#say_line('Setting server %s (%s @ %s)', (name, user, host))
say_line('Setting server (%s @ %s)', (user, name))
- log.server = name + ' '
+ log.server = name
def add_servers(self, hosts):
self.servers = list(self.user_servers)
@@ -261,24 +261,35 @@ def server_transport(self):
return None
if len(self.server) < 6:
if self.server[0] == 'stratum':
- self.server = self.server + (StratumSource.StratumSource(self, self.server), )
+ self.add_stratum_source()
else:
- http_server = GetworkSource.GetworkSource(self, self.server)
+ getwork_source = GetworkSource.GetworkSource(self, self.server)
say_line('checking for stratum...')
- stratum_host = http_server.detect_stratum()
+ stratum_host = getwork_source.detect_stratum()
if stratum_host:
- http_server.close_connection()
+ getwork_source.close_connection()
user = self.server[1]
pwd = self.server[2]
name = self.server[4]
self.server = self.servers[self.server_index] = ('stratum', user, pwd, stratum_host, name)
- self.server = self.server + (StratumSource.StratumSource(self, self.server), )
+ self.add_stratum_source()
else:
- self.server = self.servers[self.server_index] = (self.server + (http_server, ))
+ self.server = self.servers[self.server_index] = (self.server + (getwork_source, ))
return self.server[5]
+ def add_stratum_source(self):
+ proto, user, pwd, host, name = self.server[:5]
+ stratum_proxy = StratumSource.detect_stratum_proxy(host)
+ if stratum_proxy:
+ original_server = self.server
+ self.servers.insert(self.backup_server_index, original_server + (StratumSource.StratumSource(self, original_server), ))
+ name += '(p)'
+ self.server = (proto, user, pwd, stratum_proxy, name)
+ log.server = name
+ self.server = self.servers[self.server_index] = (self.server + (StratumSource.StratumSource(self, self.server), ))
+
def server_key(self, server):
return server[1:4]
View
4 log.py
@@ -15,9 +15,9 @@ def say(format_, args=(), say_quiet=False):
with lock:
p = format_ % args
if verbose:
- print '%s%s,' % (server, datetime.now().strftime(TIME_FORMAT)), p
+ print '%s %s,' % (server, datetime.now().strftime(TIME_FORMAT)), p
else:
- sys.stdout.write('\r%s\r%s%s' % (' '*80, server, p))
+ sys.stdout.write('\r%s\r%s %s' % (' '*80, server, p))
sys.stdout.flush()
def say_line(format_, args=()):
View
@@ -16,7 +16,7 @@ def socketwrap(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0):
sockobj.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
sockobj.settimeout(5)
return sockobj
-socket.socket = socketwrap
+socket.nodelay_socket = socketwrap
usage = "usage: %prog [OPTION]... SERVER[#tag]...\nSERVER is one or more [http[s]|stratum://]user:pass@host:port (required)\n[#tag] is a per SERVER user friendly name displayed in stats (optional)"
parser = OptionParser(version=VERSION, usage=usage)

0 comments on commit e1fe107

Please sign in to comment.