Esempio n. 1
0
        public static void CheckPoolConnection(PoolConnection connection)
        {
            if ((DateTime.Now - connection.LastInteraction).TotalMinutes < 10)
            {
                return;
            }

            CConsole.ColorWarning(() => Console.WriteLine("Initiating reconnect! {0}:{1}", connection.Url, connection.Login));

            try {
                var networkStream = connection.TcpClient.GetStream();
                networkStream.EndRead(null);
            } catch { }

            try { connection.TcpClient.Close(); } catch { }
            try { connection.TcpClient.Client.Close(); } catch { }
            connection.ReceiveBuffer = null;

            connection.LastInteraction = DateTime.Now;

            connection.PoolId  = "";
            connection.LastJob = null;

            connection.TcpClient = new TcpClient();

            Fleck.SocketExtensions.SetKeepAlive(connection.TcpClient.Client, 60000, 1000);
            connection.TcpClient.Client.ReceiveBufferSize = 4096 * 2;

            try { connection.TcpClient.BeginConnect(connection.Url, connection.Port, new AsyncCallback(ConnectCallback), connection); } catch { }
        }
Esempio n. 2
0
        public static void Close(Client client)
        {
            PoolConnection connection = client.PoolConnection;

            connection.WebClients.TryRemove(client);

            if (connection.WebClients.Count == 0)
            {
                connection.Closed = true;

                try
                {
                    var networkStream = connection.TcpClient.GetStream();
                    networkStream.EndRead(null);
                }
                catch { }

                try { connection.TcpClient.Close(); } catch { }
                try { connection.TcpClient.Client.Close(); } catch { }
                try { connection.ReceiveBuffer = null; } catch { }

                Connections.TryRemove(connection.Credentials);

                Console.WriteLine("{0}: closed a pool connection.", client.WebSocket.ConnectionInfo.Id);
            }
        }
Esempio n. 3
0
        public static PoolConnection CreatePoolConnection(Client client, string url, int port, string login, string password)
        {
            string credential = url + port.ToString() + login + password;

            PoolConnection mygang;

            if (!Connections.TryGetValue(credential, out mygang))
            {
                Console.WriteLine("{0}: established new pool connection. {1} {2} {3}", client.WebSocket.ConnectionInfo.Id, url, login, password);

                mygang             = new PoolConnection();
                mygang.Credentials = credential;
                mygang.LastSender  = client;

                mygang.Client = new TcpClient();

                mygang.Client.Client.SetKeepAlive(60000, 1000);
                mygang.Client.Client.ReceiveBufferSize = 4096 * 2;

                mygang.Login    = login;
                mygang.Password = password;
                mygang.Port     = port;
                mygang.Url      = url;

                mygang.WebClients.TryAdd(client, byte.MaxValue);

                Connections.TryAdd(credential, mygang);

                try{ mygang.Client.Client.BeginConnect(url, port, new AsyncCallback(ConnectCallback), mygang); }
                catch {}
            }
            else
            {
                Console.WriteLine("{0}: reusing pool connection.", client.WebSocket.ConnectionInfo.Id);

                mygang.WebClients.TryAdd(client, byte.MaxValue);

                if (mygang.LastJob != null)
                {
                    ReceiveJob(client, mygang.LastJob, mygang.LastSolved);
                }
                else
                {
                    Console.WriteLine("{0} no job yet.", client.WebSocket.ConnectionInfo.Id);
                }
            }

            client.TcpClient = mygang;

            return(mygang);
        }
Esempio n. 4
0
        private static void ConnectCallback(IAsyncResult result)
        {
            PoolConnection mypc   = result.AsyncState as PoolConnection;
            TcpClient      client = mypc.TcpClient;

            if (!mypc.Closed && client.Connected)
            {
                try
                {
                    NetworkStream networkStream = client.GetStream();
                    mypc.ReceiveBuffer = new byte[client.ReceiveBufferSize];

                    networkStream.BeginRead(mypc.ReceiveBuffer, 0, mypc.ReceiveBuffer.Length, new AsyncCallback(ReceiveCallback), mypc);

                    // keep things stupid and simple
                    // https://github.com/xmrig/xmrig-proxy/blob/dev/doc/STRATUM_EXT.md#mining-algorithm-negotiation

                    string msg0 = "{\"method\":\"login\",\"params\":{\"login\":\"";
                    string msg1 = "\",\"pass\":\"";
                    string msg2 = "\",\"agent\":\"webminerpool.com\"";
                    string msg3 = ",\"algo\": [\"cn/0\",\"cn/1\",\"cn/2\",\"cn/3\",\"cn/r\",\"cn-lite/0\",\"cn-lite/1\",\"cn-lite/2\",\"cn-pico/trtl\",\"cn/rwz\",\"cn/half\"]";
                    string msg4 = ",\"algo-perf\": {\"cn/0\":100,\"cn/1\":96,\"cn/2\":84,\"cn/3\":84,\"cn/r\":37,\"cn-lite/0\":200,\"cn-lite/1\":200,\"cn-lite/2\":166,\"cn/rwz\":100,\"cn-pico/trtl\":630,\"cn/half\":120}}";
                    string msg5 = ",\"id\":1}";
                    string msg  = msg0 + mypc.Login + msg1 + mypc.Password + msg2 + msg3 + msg4 + msg5 + "\n";

                    mypc.Send(mypc.LastSender, msg);
                }
                catch { return; }
            }
            else
            {
                // slow that down a bit
                Task.Run(async delegate
                {
                    await Task.Delay(TimeSpan.FromSeconds(4));

                    List <Client> cllist = new List <Client>(mypc.WebClients.Values);
                    foreach (Client ev in cllist)
                    {
                        Disconnect(ev, "can not connect to pool.");
                    }
                });
            }
        }
Esempio n. 5
0
        private static void ConnectCallback(IAsyncResult result)
        {
            PoolConnection mygang = result.AsyncState as PoolConnection;
            TcpClient      client = mygang.Client;

            if (!mygang.Closed && client.Connected)
            {
                try {
                    NetworkStream networkStream = client.GetStream();
                    mygang.ReceiveBuffer = new byte[client.ReceiveBufferSize];

                    networkStream.BeginRead(mygang.ReceiveBuffer, 0, mygang.ReceiveBuffer.Length, new AsyncCallback(ReceiveCallback), mygang);

                    /* keep things stupid and simple */

                    string msg0 = "{\"method\":\"login\",\"params\":{\"login\":\"";
                    string msg1 = "\",\"pass\":\"";
                    string msg2 = "\",\"agent\":\"webminerpool.com\"},\"id\":1}";

                    string msg = msg0 + mygang.Login + msg1 + mygang.Password + msg2 + "\n";

                    mygang.Send(mygang.LastSender, msg);
                }
                catch { return; }
            }
            else
            {
                // slow that down a bit

                var t = Task.Run(async delegate {
                    await Task.Delay(TimeSpan.FromSeconds(4));

                    List <Client> cllist = new List <Client> (mygang.WebClients.Keys);
                    foreach (Client ev in cllist)
                    {
                        Disconnect(ev, "can not connect to pool.");
                    }
                });
            }
        }
Esempio n. 6
0
        public static PoolConnection CreatePoolConnection(Client client, string url, int port, string login, string password)
        {
            string credential = url + port.ToString() + login + password;

            PoolConnection lpc, mypc = null;

            int batchCounter = 0;

            while (Connections.TryGetValue(credential + batchCounter.ToString(), out lpc))
            {
                if (lpc.WebClients.Count > MainClass.BatchSize)
                {
                    batchCounter++;
                }
                else
                {
                    mypc = lpc; break;
                }
            }

            credential += batchCounter.ToString();


            if (mypc == null)
            {
                CConsole.ColorInfo(() => {
                    Console.WriteLine("{0}: initiated new pool connection", client.WebSocket.ConnectionInfo.Id);
                    Console.WriteLine("{0} {1} {2}", login, password, url);
                });


                mypc             = new PoolConnection();
                mypc.Credentials = credential;
                mypc.LastSender  = client;

                mypc.TcpClient = new TcpClient();

                Fleck.SocketExtensions.SetKeepAlive(mypc.TcpClient.Client, 60000, 1000);
                mypc.TcpClient.Client.ReceiveBufferSize = 4096 * 2;

                mypc.Login    = login;
                mypc.Password = password;
                mypc.Port     = port;
                mypc.Url      = url;

                mypc.WebClients.TryAdd(client);

                Connections.TryAdd(credential, mypc);

                try { mypc.TcpClient.Client.BeginConnect(url, port, new AsyncCallback(ConnectCallback), mypc); } catch { }
            }
            else
            {
                Console.WriteLine("{0}: reusing pool connection", client.WebSocket.ConnectionInfo.Id);

                mypc.WebClients.TryAdd(client);

                if (mypc.LastJob != null)
                {
                    ReceiveJob(client, mypc.LastJob, mypc.LastSolved);
                }
                else
                {
                    Console.WriteLine("{0} no job yet.", client.WebSocket.ConnectionInfo.Id);
                }
            }

            client.PoolConnection = mypc;

            return(mypc);
        }
Esempio n. 7
0
        private static void ReceiveCallback(IAsyncResult result)
        {
            PoolConnection mypc   = result.AsyncState as PoolConnection;
            TcpClient      client = mypc.TcpClient;

            if (!client.Connected)
            {
                return;
            }

            NetworkStream networkStream;

            try { networkStream = client.GetStream(); } catch { return; }

            int bytesread = 0;

            try { bytesread = networkStream.EndRead(result); } catch { return; }

            string json = string.Empty;

            try {
                if (bytesread == 0)                 // disconnected
                {
                    // slow that down a bit to avoid negative feedback loop

                    Task.Run(async delegate {
                        await Task.Delay(TimeSpan.FromSeconds(4));

                        List <Client> cllist = new List <Client> (mypc.WebClients.Values);
                        foreach (Client ev in cllist)
                        {
                            Disconnect(ev, "lost pool connection.");
                        }
                    });

                    return;
                }

                json = ASCIIEncoding.ASCII.GetString(mypc.ReceiveBuffer, 0, bytesread);

                networkStream.BeginRead(mypc.ReceiveBuffer, 0, mypc.ReceiveBuffer.Length, new AsyncCallback(ReceiveCallback), mypc);
            } catch { return; }

            if (bytesread == 0 || string.IsNullOrEmpty(json))
            {
                return;                                                            //?!
            }
            var msg = json.FromJson <JsonData> ();

            if (msg == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(mypc.PoolId))
            {
                // this "protocol" is strange
                if (!msg.ContainsKey("result"))
                {
                    string additionalInfo = "none";

                    // try to get the error
                    if (msg.ContainsKey("error"))
                    {
                        msg = msg["error"] as JsonData;

                        if (msg != null && msg.ContainsKey("message"))
                        {
                            additionalInfo = msg["message"].GetString();
                        }
                    }

                    List <Client> cllist = new List <Client> (mypc.WebClients.Values);
                    foreach (Client ev in cllist)
                    {
                        Disconnect(ev, "can not connect. additional information: " + additionalInfo);
                    }

                    return;
                }

                msg = msg["result"] as JsonData;

                if (msg == null)
                {
                    return;
                }
                if (!msg.ContainsKey("id"))
                {
                    return;
                }
                if (!msg.ContainsKey("job"))
                {
                    return;
                }

                mypc.PoolId = msg["id"].GetString();

                var lastjob = msg["job"] as JsonData;

                if (!VerifyJob(lastjob))
                {
                    CConsole.ColorWarning(() =>
                                          Console.WriteLine("Failed to verify job: {0}", json));
                    return;
                }

                mypc.LastJob         = lastjob;
                mypc.LastInteraction = DateTime.Now;

                mypc.LastSolved = new CcHashset <string> ();

                List <Client> cllist2 = new List <Client> (mypc.WebClients.Values);
                foreach (Client ev in cllist2)
                {
                    ReceiveJob(ev, mypc.LastJob, mypc.LastSolved);
                }
            }
            else if (msg.ContainsKey("method") && msg["method"].GetString() == "job")
            {
                if (!msg.ContainsKey("params"))
                {
                    return;
                }

                var lastjob = msg["params"] as JsonData;

                if (!VerifyJob(lastjob))
                {
                    CConsole.ColorWarning(() =>
                                          Console.WriteLine("Failed to verify job: {0}", json));
                    return;
                }

                mypc.LastJob         = lastjob;
                mypc.LastInteraction = DateTime.Now;
                mypc.LastSolved      = new CcHashset <string> ();

                List <Client> cllist2 = new List <Client> (mypc.WebClients.Values);

                Console.WriteLine("Sending job to {0} client(s)!", cllist2.Count);

                foreach (Client ev in cllist2)
                {
                    ReceiveJob(ev, mypc.LastJob, mypc.LastSolved);
                }
            }
            else
            {
                if (msg.ContainsKey("error"))
                {
                    // who knows?
                    ReceiveError(mypc.LastSender, msg);
                }
                else
                {
                    CConsole.ColorWarning(() =>
                                          Console.WriteLine("Pool is sending nonsense."));
                }
            }
        }