|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding:utf-8 -*- |
| 3 | + |
| 4 | +import time |
| 5 | +import os |
| 6 | +import socket |
| 7 | + |
| 8 | +from stratum import settings |
| 9 | +import stratum.logger |
| 10 | +log = stratum.logger.get_logger('proxy') |
| 11 | + |
| 12 | +if __name__ == '__main__': |
| 13 | + if len(settings.WALLET)!=42 and len(settings.WALLET)!=40: |
| 14 | + log.error("Wrong WALLET!") |
| 15 | + quit() |
| 16 | + settings.CUSTOM_EMAIL = settings.MONITORING_EMAIL if settings.MONITORING_EMAIL and settings.MONITORING else "" |
| 17 | + |
| 18 | +from twisted.internet import reactor, defer, protocol |
| 19 | +from twisted.internet import reactor as reactor2 |
| 20 | +from stratum.socket_transport import SocketTransportFactory, SocketTransportClientFactory |
| 21 | +from stratum.services import ServiceEventHandler |
| 22 | +from twisted.web.server import Site |
| 23 | +from stratum.custom_exceptions import TransportException |
| 24 | + |
| 25 | +from mining_libs import getwork_listener |
| 26 | +from mining_libs import client_service |
| 27 | +from mining_libs import jobs |
| 28 | +from mining_libs import version |
| 29 | +from mining_libs.jobs import Job |
| 30 | + |
| 31 | +def on_shutdown(f): |
| 32 | + '''Clean environment properly''' |
| 33 | + log.info("Shutting down proxy...") |
| 34 | + if os.path.isfile('eth-proxy.pid'): |
| 35 | + os.remove('eth-proxy.pid') |
| 36 | + f.is_reconnecting = False # Don't let stratum factory to reconnect again |
| 37 | + |
| 38 | +# Support main connection |
| 39 | +@defer.inlineCallbacks |
| 40 | +def ping(f): |
| 41 | + if not f.is_reconnecting: |
| 42 | + return |
| 43 | + try: |
| 44 | + yield (f.rpc('eth_getWork', [], '')) |
| 45 | + reactor.callLater(60, ping, f) |
| 46 | + except Exception: |
| 47 | + pass |
| 48 | + |
| 49 | +@defer.inlineCallbacks |
| 50 | +def on_connect(f): |
| 51 | + '''Callback when proxy get connected to the pool''' |
| 52 | + log.info("Connected to Stratum pool at %s:%d" % f.main_host) |
| 53 | + #reactor.callLater(30, f.client.transport.loseConnection) |
| 54 | + |
| 55 | + # Hook to on_connect again |
| 56 | + f.on_connect.addCallback(on_connect) |
| 57 | + |
| 58 | + # Get first job and user_id |
| 59 | + initial_job = (yield f.rpc('eth_submitLogin', [settings.WALLET, settings.CUSTOM_EMAIL], 'Proxy_'+version.VERSION)) |
| 60 | + |
| 61 | + reactor.callLater(0, ping, f) |
| 62 | + |
| 63 | + defer.returnValue(f) |
| 64 | + |
| 65 | +def on_disconnect(f): |
| 66 | + '''Callback when proxy get disconnected from the pool''' |
| 67 | + log.info("Disconnected from Stratum pool at %s:%d" % f.main_host) |
| 68 | + f.on_disconnect.addCallback(on_disconnect) |
| 69 | + |
| 70 | + # Prepare to failover, currently works very bad |
| 71 | + #if f.main_host==(settings.POOL_HOST, settings.POOL_PORT): |
| 72 | + # main() |
| 73 | + #else: |
| 74 | + # f.is_reconnecting = False |
| 75 | + #return f |
| 76 | + |
| 77 | +@defer.inlineCallbacks |
| 78 | +def main(): |
| 79 | + reactor.disconnectAll() |
| 80 | + failover = False |
| 81 | + if settings.POOL_FAILOVER_ENABLE: |
| 82 | + failover = settings.failover_pool |
| 83 | + settings.failover_pool = not settings.failover_pool |
| 84 | + |
| 85 | + pool_host = settings.POOL_HOST |
| 86 | + pool_port = settings.POOL_PORT |
| 87 | + if failover and settings.POOL_FAILOVER_ENABLE: |
| 88 | + pool_host = settings.POOL_HOST_FAILOVER |
| 89 | + pool_port = settings.POOL_PORT_FAILOVER |
| 90 | + |
| 91 | + log.warning("Ethereum Stratum proxy version: %s" % version.VERSION) |
| 92 | + log.warning("Trying to connect to Stratum pool at %s:%d" % (pool_host, pool_port)) |
| 93 | + |
| 94 | + # Connect to Stratum pool, main monitoring connection |
| 95 | + f = SocketTransportClientFactory(pool_host, pool_port, |
| 96 | + debug=settings.DEBUG, proxy=None, |
| 97 | + event_handler=client_service.ClientMiningService) |
| 98 | + |
| 99 | + job_registry = jobs.JobRegistry(f) |
| 100 | + client_service.ClientMiningService.job_registry = job_registry |
| 101 | + client_service.ClientMiningService.reset_timeout() |
| 102 | + |
| 103 | + f.on_connect.addCallback(on_connect) |
| 104 | + f.on_disconnect.addCallback(on_disconnect) |
| 105 | + # Cleanup properly on shutdown |
| 106 | + reactor.addSystemEventTrigger('before', 'shutdown', on_shutdown, f) |
| 107 | + |
| 108 | + # Block until proxy connect to the pool |
| 109 | + try: |
| 110 | + yield f.on_connect |
| 111 | + except TransportException: |
| 112 | + log.warning("First pool server must be online first time to start failover") |
| 113 | + return |
| 114 | + |
| 115 | + conn = reactor.listenTCP(settings.PORT, Site(getwork_listener.Root(job_registry, settings.ENABLE_WORKER_ID)), interface=settings.HOST) |
| 116 | + |
| 117 | + try: |
| 118 | + conn.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # Enable keepalive packets |
| 119 | + conn.socket.setsockopt(socket.SOL_TCP, socket.TCP_KEEPIDLE, 60) # Seconds before sending keepalive probes |
| 120 | + conn.socket.setsockopt(socket.SOL_TCP, socket.TCP_KEEPINTVL, 1) # Interval in seconds between keepalive probes |
| 121 | + conn.socket.setsockopt(socket.SOL_TCP, socket.TCP_KEEPCNT, 5) # Failed keepalive probles before declaring other end dead |
| 122 | + except: |
| 123 | + pass # Some socket features are not available on all platforms (you can guess which one) |
| 124 | + |
| 125 | + log.warning("-----------------------------------------------------------------------") |
| 126 | + if settings.HOST == '0.0.0.0': |
| 127 | + log.warning("PROXY IS LISTENING ON ALL IPs ON PORT %d" % settings.PORT) |
| 128 | + else: |
| 129 | + log.warning("LISTENING FOR MINERS ON http://%s:%d" % (settings.HOST, settings.PORT)) |
| 130 | + log.warning("-----------------------------------------------------------------------") |
| 131 | + log.warning("Wallet: %s" % settings.WALLET) |
| 132 | + log.warning("Worker ID enabled: %s" % settings.ENABLE_WORKER_ID) |
| 133 | + if settings.MONITORING: |
| 134 | + log.warning("Email monitoring on %s" % settings.MONITORING_EMAIL) |
| 135 | + else: |
| 136 | + log.warning("Email monitoring diasbled") |
| 137 | + #log.warning("Failover enabled: %" % settings.POOL_FAILOVER_ENABLE) |
| 138 | + log.warning("-----------------------------------------------------------------------") |
| 139 | + |
| 140 | +if __name__ == '__main__': |
| 141 | + fp = file("eth-proxy.pid", 'w') |
| 142 | + fp.write(str(os.getpid())) |
| 143 | + fp.close() |
| 144 | + settings.failover_pool = False |
| 145 | + main() |
| 146 | + reactor.run() |
0 commit comments