static void Main(string[] args)
        {
            Logger.MinerProxyHeader();

            // Load and process settings in the Settings class
            Settings.ProcessArgs(args, out settings);

            AppDomain.CurrentDomain.UnhandledException += (s, e) => File.WriteAllText("Exceptions.txt", e.ExceptionObject.ToString());

            Logger.logFileName = DateTime.Now.ToString("s").Replace(":", ".") + "_log";
            CancellationTokenSource logTokenSource = new CancellationTokenSource();
            CancellationToken       logToken       = logTokenSource.Token;
            var logQueue = new Task(() => ProcessLogQueue(logToken), TaskCreationOptions.LongRunning);

            if (settings.log) //if logging enabled, let's start the logging queue
            {
                logQueue.Start();
            }

            var consoleQueue = new Task(() => ProcessConsoleQueue(), TaskCreationOptions.LongRunning);

            consoleQueue.Start();

            if (settings.debug)
            {
                Logger.LogToConsole("Debug enabled", "MinerProxy");
            }

            if (settings.identifyDevFee)
            {
                Logger.LogToConsole("Identifying DevFee connections as 'DevFee'", "MinerProxy");
            }

            Logger.LogToConsole("Coin protocol: " + settings.minedCoin, "MinerProxy");

            if (settings.donateDevFee)
            {
                Logger.LogToConsole(string.Format("You are donating {0}% of DevFees to LostSoulfly and Samut3. Thanks!", Program.settings.percentToDonate), "MinerProxy");
                SetUpDonateLists();
            }
            else
            {
                Logger.LogToConsole("You are not donating a DevFee percentage to MinerProxy maintainers.", "MinerProxy");
            }

            if (Program.settings.replaceWallet)
            {
                Logger.LogToConsole("Replacing Wallets with: " + settings.walletAddress, "MinerProxy", ConsoleColor.Yellow);
                if (!string.IsNullOrWhiteSpace(settings.devFeeWalletAddress))
                {
                    Logger.LogToConsole("Replacing DevFee wallets with " + settings.devFeeWalletAddress, "MinerProxy", ConsoleColor.Yellow);
                }
            }
            else
            {
                Logger.LogToConsole("Not replacing Wallets", "MinerProxy");
            }

            //initialize webSock
            if (Program.settings.useWebSockServer)
            {
                try
                {
                    webSock          = new HttpServer(settings.webSocketPort);
                    webSock.RootPath = Directory.GetCurrentDirectory() + @"\web\";
                    Directory.CreateDirectory(webSock.RootPath);
                    if (settings.debug)
                    {
                        Logger.LogToConsole(string.Format("Web root: {0}", webSock.RootPath), "MinerProxy");
                    }

                    webSock.OnGet += new EventHandler <HttpRequestEventArgs>(Web.WebIndex.OnGet);
                    webSock.AddWebSocketService <WebIndex>("/");
                    webSock.AddWebSocketService <WebConsole>("/console");
                    webSock.Start();
                    Logger.LogToConsole(string.Format("WebSockServer listening on port {0}", settings.webSocketPort), "MinerProxy");
                } catch (Exception ex)
                {
                    Logger.LogToConsole(string.Format("Unable to start WebSocketServer on port {0}. Error: {1}", settings.webSocketPort, ex.Message));
                }
            }

            try
            {
                listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                listener.Bind(new IPEndPoint(IPAddress.Any, settings.proxyListenPort));
                listener.Listen(100);
            } catch (Exception ex)
            {
                Logger.LogToConsole(string.Format("Error: {0}", ex.Message), color: ConsoleColor.Red);
                return;
            }

            allDone = new ManualResetEvent(false);

            UpdateConsoleTitle();
            Logger.LogToConsole(string.Format("Listening for miners on port {0}, on IP {1}", settings.proxyListenPort, listener.LocalEndPoint), "MinerProxy");
            Logger.LogToConsole("Accepting connections from: " + string.Join(", ", settings.allowedAddresses), "MinerProxy");

            Logger.LogToConsole("Press 'H' for available commands", "MinerProxy");

            var listenerTask = new Task(() => listenerStart(), TaskCreationOptions.LongRunning);

            listenerTask.Start();

            string key;

            while (true)
            {
                if (Console.KeyAvailable)
                {
                    key = Console.ReadKey(true).Key.ToString();

                    switch (key)
                    {
                    case "S":
                        settings.showRigStats = !settings.showRigStats;
                        Logger.LogToConsole((settings.showRigStats) ? "RigStats enabled" : "RigStats disabled", "MinerProxy");
                        break;

                    case "U":
                        //update settings file with new options
                        Settings.writeSettings(settings.settingsFile, settings);
                        Logger.LogToConsole(string.Format("Updated {0} file to newest version", settings.settingsFile), "MinerProxy");
                        break;

                    case "C":
                        settings.colorizeConsole = (!settings.colorizeConsole);
                        Logger.LogToConsole((settings.colorizeConsole) ? "Colors enabled" : "Colors disabled", "MinerProxy");
                        break;

                    case "E":
                        settings.showEndpointInConsole = (!settings.showEndpointInConsole);
                        Logger.LogToConsole((settings.showEndpointInConsole) ? "Endpoint prefix enabled" : "Endpoint prefix disabled", "MinerProxy");
                        break;

                    case "P":
                        Console.WriteLine("Pool count: " + settings.poolList.Count);
                        Console.WriteLine("Curent: " + settings.GetCurrentPool().poolAddress + ":" + settings.GetCurrentPool().poolPort);
                        Console.WriteLine("New Pool:" + settings.GetNextPool().poolAddress + ":" + settings.GetCurrentPool().poolPort);

                        break;

                    case "L":
                        settings.log = !settings.log;
                        Logger.LogToConsole((settings.log) ? "Logging enabled" : "Logging disabled", "MinerProxy");

                        if (settings.log)
                        {
                            logTokenSource = new CancellationTokenSource();
                            logToken       = logTokenSource.Token;
                            logQueue       = new Task(() => ProcessLogQueue(logToken), TaskCreationOptions.LongRunning);
                            logQueue.Start();
                        }
                        else
                        {
                            logTokenSource.Cancel();
                        }
                        break;

                    case "D":
                        settings.debug = !settings.debug;
                        Logger.LogToConsole((settings.debug) ? "Debug enabled" : "Debug disabled", "MinerProxy");
                        break;

                    case "Q":
                        Logger.LogToConsole("Shutting down MinerProxy..", "MinerProxy");
                        System.Environment.Exit(0);
                        return;

                    case "M":
                        for (int i = 0; i < _minerStats.Count; i++)
                        {
                            Console.WriteLine("Miner displayName: {0}", _minerStats[i].displayName);
                            Console.WriteLine("Miner workerName: {0}", _minerStats[i].workerName);
                            Console.WriteLine("Miner rigName: {0}", _minerStats[i].rigName);
                            Console.WriteLine("Miner numberOfConnects: {0}", _minerStats[i].numberOfConnects);
                            Console.WriteLine("Miner connectionAlive: {0}", _minerStats[i].connectionAlive);
                            Console.WriteLine("Miner endPoint: {0}", _minerStats[i].endPoint);
                            Console.WriteLine("Miner connectionName: {0}", _minerStats[i].connectionName);
                            Console.WriteLine("Miner firstConnectTime: {0}", _minerStats[i].firstConnectTime.ToString());
                            Console.WriteLine("Miner connectionStartTime: {0}", _minerStats[i].connectionStartTime.ToString());
                            Console.WriteLine("Miner totalTimeConnected: {0}", _minerStats[i].totalTimeConnected.ToString());
                            Console.WriteLine("Miner submittedShares: {0}", _minerStats[i].submittedShares);
                            Console.WriteLine("Miner acceptedShares: {0}", _minerStats[i].acceptedShares);
                            Console.WriteLine("Miner rejectedShares: {0}", _minerStats[i].rejectedShares);
                            Console.WriteLine("Miner hashrate: {0}", _minerStats[i].hashrate);
                            Console.WriteLine("Miner GetAverageHashrate: {0}", _minerStats[i].GetAverageHashrate());
                            if (!String.IsNullOrEmpty(_minerStats[i].displayName))
                            {
                                Console.WriteLine("Miner Wallets:");
                                Console.WriteLine(string.Join("\n", MinerManager.GetMinerWallets(_minerStats[i].displayName).ToArray()));
                            }
                        }
                        break;

                    case "X":
                        File.WriteAllText("MinerStats.json", Newtonsoft.Json.JsonConvert.SerializeObject(_minerStats, Newtonsoft.Json.Formatting.Indented));
                        Logger.LogToConsole("Exported MinerStats to MinerStats.json", "MinerProxy");
                        break;

                    case "O":
                        DonateList d       = new DonateList();
                        double     success = 0;
                        for (int i = 0; i < 1000; i++)      //be careful doing this with debug and more than 10k. Could take a while.
                        {
                            if (CheckForDonation(out d, "ETH"))
                            {
                                if (settings.debug)
                                {
                                    Logger.LogToConsole(d.donatePoolAddress + " " + d.donatePoolPort + " " + d.donateWallet);
                                }
                                success++;
                            }
                        }
                        Logger.LogToConsole("Success percentage: " + ((success / 1000) * 100) + "% out of 1,000", "MinerProxy");
                        Logger.LogToConsole("Win: " + success + " - Lose: " + (1000 - success) + ". Donate percent: " + settings.percentToDonate, "MinerProxy");
                        break;

                    case "H":
                        lock (Logger.ConsoleBlockLock)
                        {
                            Logger.LogToConsole("MinerProxy Available Commands", "HELP", ConsoleColor.Yellow);
                            Logger.LogToConsole("S key: Enable/Disable showing Rig Stats", "HELP", ConsoleColor.Yellow);
                            Logger.LogToConsole("C key: Enable/Disable console colors", "HELP", ConsoleColor.Yellow);
                            Logger.LogToConsole("E key: Enable/Disable Endpoint prefix on log messages", "HELP", ConsoleColor.Yellow);
                            Logger.LogToConsole("L key: Enable/Disable logging to file", "HELP", ConsoleColor.Yellow);
                            Logger.LogToConsole("D key: Enable/Disable debug messages", "HELP", ConsoleColor.Yellow);
                            Logger.LogToConsole("M key: Print all miner stats to console", "HELP", ConsoleColor.Yellow);
                            Logger.LogToConsole("X key: Export all miner stats to MinerStats.json", "HELP", ConsoleColor.Yellow);
                            Logger.LogToConsole("O key: Run a test of DevFee donation percentages. Turn debug off for faster tests.");
                            Logger.LogToConsole("U key: Update the loaded JSON file with current settings", "HELP", ConsoleColor.Yellow);
                            Logger.LogToConsole("Q key: Quit MinerProxy", "HELP", ConsoleColor.Yellow);
                        }
                        break;
                    }
                }
                Thread.Sleep(1);
            }
        }
Example #2
0
        internal static void OnGet(object sender, HttpRequestEventArgs e)
        {
            var    req    = e.Request;
            var    resp   = e.Response;
            var    path   = req.RawUrl;
            string body   = "<html><body style=\"background-color:black\">" + "\n";
            string footer = "\n" + "</body></html>";
            string output = null;

            if (path == "/")
            {
                path += "index.html";
            }

            if (path.ToLower().Contains("/console"))
            {
                resp.ContentType     = "text/html";
                resp.ContentEncoding = Encoding.UTF8;

                var queue = new List <ConsoleList>(Program._webConsoleQueue);

                foreach (ConsoleList cl in queue)
                {
                    output += string.Format("<font face = \"Lucida Console\" color={0}>" + cl.message + "</font>" + "<br>\n", cl.color.ToString());
                }
                resp.WriteContent(Encoding.UTF8.GetBytes(body + output + footer));
                return;
            }

            if (path.ToLower().Contains("/miners"))
            {
                output += ("<font face = \"Lucida Console\" color=White>");
                for (int i = 0; i < Program._minerStats.Count; i++)
                {
                    output += string.Format("Miner displayName: {0}<br>\n", Program._minerStats[i].displayName);
                    output += string.Format("Miner workerName: {0}<br>\n", Program._minerStats[i].workerName);
                    output += string.Format("Miner rigName: {0}<br>\n", Program._minerStats[i].rigName);
                    output += string.Format("Miner numberOfConnects: {0}<br>\n", Program._minerStats[i].numberOfConnects);
                    output += string.Format("Miner connectionAlive: {0}<br>\n", Program._minerStats[i].connectionAlive);
                    output += string.Format("Miner endPoint: {0}<br>\n", Program._minerStats[i].endPoint);
                    output += string.Format("Miner connectionName: {0}<br>\n", Program._minerStats[i].connectionName);
                    output += string.Format("Miner firstConnectTime: {0}<br>\n", Program._minerStats[i].firstConnectTime.ToString());
                    output += string.Format("Miner connectionStartTime: {0}<br>\n", Program._minerStats[i].connectionStartTime.ToString());
                    output += string.Format("Miner totalTimeConnected: {0}<br>\n", Program._minerStats[i].totalTimeConnected.ToString());
                    output += string.Format("Miner submittedShares: {0}<br>\n", Program._minerStats[i].submittedShares);
                    output += string.Format("Miner acceptedShares: {0}<br>\n", Program._minerStats[i].acceptedShares);
                    output += string.Format("Miner rejectedShares: {0}<br>\n", Program._minerStats[i].rejectedShares);
                    output += string.Format("Miner hashrate: {0}<br>\n", Program._minerStats[i].hashrate);
                    output += string.Format("Miner GetAverageHashrate: {0}<br>\n", Program._minerStats[i].GetAverageHashrate());
                    output += string.Format("Miner Wallets:<br>\n");
                    output += string.Format(string.Join("\n", MinerManager.GetMinerWallets(Program._minerStats[i].displayName).ToArray()) + "<br>\n");
                }
                output += "</font>";
                resp.WriteContent(Encoding.UTF8.GetBytes(body + output + footer));
                return;
            }

            if (path.ToLower().Contains("/status"))
            {
                resp.WriteContent(Encoding.UTF8.GetBytes("True"));
                return;
            }

            if ((path.IndexOfAny(new char[] { '*', '&', '#', '~', '^', '\\', '\0' }) >= 0) | path.Contains("..") == true)
            {  //prevent directory traversal attacks
                if (Program.settings.debug)
                {
                    Logging.Logger.LogToConsole(string.Format("Http InvalidChars 404: {0} by {1}", path, e.Request.RemoteEndPoint));
                }
                resp.StatusCode = (int)HttpStatusCode.NotFound;
                return;
            }

            var content = Program.webSock.GetFile(path);

            if (content == null)
            {
                if (Program.settings.debug)
                {
                    Logging.Logger.LogToConsole(string.Format("Http request 404: {0} by {1}", path, e.Request.RemoteEndPoint));
                }
                resp.StatusCode = (int)HttpStatusCode.NotFound;
                return;
            }



            if (path.EndsWith(".html"))
            {
                resp.ContentType     = "text/html";
                resp.ContentEncoding = Encoding.UTF8;
            }
            else if (path.EndsWith(".js"))
            {
                resp.ContentType     = "application/javascript";
                resp.ContentEncoding = Encoding.UTF8;
            }

            Logging.Logger.LogToConsole(string.Format("Http request: {0} by {1}", path, e.Request.RemoteEndPoint));

            resp.WriteContent(content);
        }