Browse Source

various

pull/123/head
notgiven688 3 years ago
parent
commit
bfcead30d3
4 changed files with 87 additions and 60 deletions
  1. +16
    -3
      server/Server/CConsole.cs
  2. +1
    -1
      server/Server/Firewall.cs
  3. +8
    -7
      server/Server/PoolConnection.cs
  4. +62
    -49
      server/Server/Program.cs

+ 16
- 3
server/Server/CConsole.cs View File

@ -43,6 +43,19 @@ namespace Server {
}
}
private static void Write (Action consoleAction, ConsoleColor foreground) {
if (enabled) {
lock (locker) {
Console.ForegroundColor = foreground;
consoleAction ();
Console.ResetColor ();
}
} else {
consoleAction ();
}
}
private static void Write (Action consoleAction, ConsoleColor foreground, ConsoleColor background) {
if (enabled) {
@ -58,15 +71,15 @@ namespace Server {
}
public static void WriteInfo (Action consoleAction) {
Write (consoleAction, ConsoleColor.Black, ConsoleColor.Cyan);
Write (consoleAction, ConsoleColor.Cyan);
}
public static void WriteWarning (Action consoleAction) {
Write (consoleAction, ConsoleColor.Black, ConsoleColor.Yellow);
Write (consoleAction, ConsoleColor.Yellow);
}
public static void WriteAlert (Action consoleAction) {
Write (consoleAction, ConsoleColor.White, ConsoleColor.Red);
Write (consoleAction, ConsoleColor.Red);
}
}

+ 1
- 1
server/Server/Firewall.cs View File

@ -59,7 +59,7 @@ namespace Server {
private static void AddToIpTables (Entry entry, int rule) {
Helper.WriteTextAsyncWrapper ("ip_list", entry.Address + Environment.NewLine);
Console.WriteLine ("Added {0} to iptables (rule #{1})", entry.Address, rule.ToString ());
CConsole.WriteWarning(() => Console.WriteLine ("Added {0} to iptables (rule #{1})", entry.Address, rule.ToString ()));
entries.TryRemove (entry.Address);
}

+ 8
- 7
server/Server/PoolConnection.cs View File

@ -185,7 +185,8 @@ namespace Server {
var lastjob = msg["job"] as JsonData;
if (!VerifyJob (lastjob)) {
Console.WriteLine ("Failed to verify job.");
CConsole.WriteWarning(() =>
Console.WriteLine ("Failed to verify job: {0}", msg["job"].GetString()));
return;
}
@ -206,7 +207,8 @@ namespace Server {
var lastjob = msg["params"] as JsonData;
if (!VerifyJob (lastjob)) {
Console.WriteLine ("Failed to verify job.");
CConsole.WriteWarning(() =>
Console.WriteLine ("Failed to verify job: {0}", msg["params"].GetString()));
return;
}
@ -216,7 +218,7 @@ namespace Server {
List<Client> cllist2 = new List<Client> (mypc.WebClients.Values);
Console.WriteLine ("Sending to {0} clients!", cllist2.Count);
Console.WriteLine ("Sending to {0} client(s)!", cllist2.Count);
foreach (Client ev in cllist2) {
ReceiveJob (ev, mypc.LastJob, mypc.LastSolved);
@ -304,8 +306,7 @@ namespace Server {
if ((DateTime.Now - connection.LastInteraction).TotalMinutes < 10)
return;
Console.WriteLine ("Initiating reconnect!");
Console.WriteLine (connection.Credentials);
CConsole.WriteWarning(() => Console.WriteLine ("Initiating reconnect! {0}:{1}", connection.Url, connection.Login));
try {
var networkStream = connection.TcpClient.GetStream ();
@ -348,7 +349,7 @@ namespace Server {
if (mypc == null) {
Console.WriteLine ("{0}: established new pool connection. {1} {2} {3} {4}", client.WebSocket.ConnectionInfo.Id, url, login, password,batchCounter);
Console.WriteLine ("{0}: established new pool connection.", client.WebSocket.ConnectionInfo.Id);
mypc = new PoolConnection ();
mypc.Credentials = credential;
@ -374,7 +375,7 @@ namespace Server {
Console.WriteLine ("{0}: reusing pool connection {1}.", client.WebSocket.ConnectionInfo.Id, batchCounter);
Console.WriteLine ("{0}: reusing pool connection", client.WebSocket.ConnectionInfo.Id);
mypc.WebClients.TryAdd (client);

+ 62
- 49
server/Server/Program.cs View File

@ -38,7 +38,7 @@ using JsonData = System.Collections.Generic.Dictionary;
namespace Server {
public class Client {
public const double DevDefaultFee = 0.03;
public PoolConnection PoolConnection;
public IWebSocketConnection WebSocket;
public string Pool = string.Empty;
@ -51,7 +51,7 @@ namespace Server {
public string LastTarget = string.Empty;
public string UserId;
public int NumChecked = 0;
public double Fee = DevDefaultFee;
public double Fee = 0.03;
public int Version = 1;
}
@ -116,20 +116,20 @@ namespace Server {
private static Dictionary<string, PoolInfo> PoolPool = new Dictionary<string, PoolInfo> ();
private const int GraceConnectionTime = 16; // time to connect to a pool in seconds
private const int HeartbeatRate = 10; // server logic every x seconds
private const int TimeDevJobsAreOld = 600; // after that job-age we do not forward dev jobs
private const int PoolTimeout = 60 * 12; // in seconds, pool is not sending new jobs
private const int SpeedAverageOverXHeartbeats = 10; // for the statistics shown every heartbeat
private const int MaxHashChecksPerHeartbeat = 20; // try not to kill ourselfs
private const int ForceGCEveryXHeartbeat = 40; // so we can keep an eye on the memory
private const int SaveStatisticsEveryXHeartbeat = 40; // save statistics
public const int BatchSize = 200; // mining with the same credentials (pool, login, password)
// results in connections beeing "bundled" to a single connection
// seen by the pool. that can result in large difficulties and
// hashrate fluctuations. this parameter sets the number of clients
// in one batch, e.g. for BatchSize = 100 and 1000 clients
// there will be 10 pool connections.
private const int GraceConnectionTime = 16; // time to connect to a pool in seconds
private const int HeartbeatRate = 10; // server logic every x seconds
private const int TimeDevJobsAreOld = 600; // after that job-age we do not forward dev jobs
private const int PoolTimeout = 60 * 12; // in seconds, pool is not sending new jobs
private const int SpeedAverageOverXHeartbeats = 10; // for the statistics shown every heartbeat
private const int MaxHashChecksPerHeartbeat = 20; // try not to kill ourselfs
private const int ForceGCEveryXHeartbeat = 40; // so we can keep an eye on the memory
private const int SaveStatisticsEveryXHeartbeat = 40; // save statistics
public const int BatchSize = 200; // mining with the same credentials (pool, login, password)
// results in connections beeing "bundled" to a single connection
// seen by the pool. that can result in large difficulties and
// hashrate fluctuations. this parameter sets the number of clients
// in one batch, e.g. for BatchSize = 100 and 1000 clients
// there will be 10 pool connections.
private static int Hearbeats = 0;
private static int HashesCheckedThisHeartbeat = 0;
@ -495,30 +495,31 @@ namespace Server {
return true;
}
private static X509Certificate2 CheckSSLCertificate (out Exception ex) {
X509Certificate2 cert = null;
try { cert = new X509Certificate2 ("certificate.pfx", "miner"); } catch (Exception e) { ex = e; return null; }
ex = null;
return cert;
}
public static void Main (string[] args) {
CConsole.WriteInfo (() => {
Console.WriteLine ("[{0}] webminerpool server.", DateTime.Now);
#if (DEBUG)
Console.WriteLine ("[{0}] webminerpool server started - DEBUG MODE", DateTime.Now);
#else
Console.WriteLine ("[{0}] webminerpool server started", DateTime.Now);
#endif
double devfee = (new Client ()).Fee;
if (devfee > double.Epsilon)
Console.WriteLine ("Developer fee of {0}% enabled.", (devfee * 100.0d).ToString ("F1"));
Console.WriteLine ();
});
Exception exception = null;
hashLibAvailable = CheckLibHash (out exception);
if (!hashLibAvailable) CConsole.WriteWarning (() =>
Console.WriteLine ("hashlib.so support is not available. Checking user submitted hashes is therefore disabled.")
Console.WriteLine ("hashlib.so is not available. Checking user submitted hashes disabled.")
);
PoolConnectionFactory.RegisterCallbacks (PoolReceiveCallback, PoolErrorCallback, PoolDisconnectCallback);
@ -574,7 +575,10 @@ namespace Server {
}
X509Certificate2 cert = CheckSSLCertificate (out exception);
X509Certificate2 cert = null;
try { cert = new X509Certificate2 ("certificate.pfx", "miner"); } catch (Exception e) { exception = e; cert = null; }
bool certAvailable = (cert != null);
if (!certAvailable)
@ -601,7 +605,8 @@ namespace Server {
break;
case LogLevel.Error:
if (ex != null && !string.IsNullOrEmpty (ex.Message)) {
Console.WriteLine ("FLECK: " + message + " " + ex.Message);
CConsole.WriteAlert (() => Console.WriteLine ("FLECK: " + message + " " + ex.Message));
exceptionCounter++;
if ((exceptionCounter % 200) == 0) {
@ -739,7 +744,8 @@ namespace Server {
client.UserId = uid;
}
Console.WriteLine ("{0}: handshake - {1}", guid, client.Pool);
Console.WriteLine ("{0}: handshake - {1}, {2}", guid, client.Pool,
(client.Login.Length > 8 ? client.Login.Substring(0,8) + "..." : client.Login ) );
if (!string.IsNullOrEmpty (ipadr)) Firewall.Update (ipadr, Firewall.UpdateEntry.Handshake);
@ -839,7 +845,7 @@ namespace Server {
if (!validHash) {
CConsole.WriteWarning (() =>
Console.WriteLine ("{0} got disconnected for WRONG HASH.", client.WebSocket.ConnectionInfo.Id.ToString ()));
Console.WriteLine ("{0} got disconnected for WRONG hash.", client.WebSocket.ConnectionInfo.Id.ToString ()));
if (!string.IsNullOrEmpty (ipadr)) Firewall.Update (ipadr, Firewall.UpdateEntry.WrongHash);
RemoveClient (client.WebSocket.ConnectionInfo.Id);
@ -989,7 +995,7 @@ namespace Server {
try {
if (Hearbeats % SaveStatisticsEveryXHeartbeat == 0) {
Console.WriteLine ("Saving statistics...");
CConsole.WriteInfo (() => Console.WriteLine ("Saving statistics."));
StringBuilder sb = new StringBuilder ();
@ -998,18 +1004,17 @@ namespace Server {
}
File.WriteAllText ("statistics.dat", sb.ToString ().TrimEnd ('\r', '\n'));
Console.WriteLine ("done.");
}
} catch (Exception ex) {
Console.WriteLine ("Error saving statistics.dat: {0}", ex);
CConsole.WriteAlert (() => Console.WriteLine ("Error saving statistics.dat: {0}", ex));
}
try {
if (saveLoginIdsNextHeartbeat) {
saveLoginIdsNextHeartbeat = false;
Console.WriteLine ("Saving logins...");
CConsole.WriteInfo (() => Console.WriteLine ("Saving logins."));
StringBuilder sb = new StringBuilder ();
@ -1018,11 +1023,9 @@ namespace Server {
}
File.WriteAllText ("logins.dat", sb.ToString ().TrimEnd ('\r', '\n'));
Console.WriteLine ("done.");
}
} catch (Exception ex) {
Console.WriteLine ("Error saving logins.dat: {0}", ex);
CConsole.WriteAlert (() => Console.WriteLine ("Error saving logins.dat: {0}", ex));
}
try {
@ -1038,8 +1041,13 @@ namespace Server {
}
CConsole.WriteInfo (() =>
Console.WriteLine ("[{0}] heartbeat, connections: client {1}, pool {2}, jobqueue: {3}, total/dev: {4}/{5} h/s", DateTime.Now.ToString (),
clients.Count, PoolConnectionFactory.Connections.Count, jobQueue.Count, totalSpeed, totalDevSpeed));
Console.WriteLine ("[{0}] heartbeat; connections client/pool: {1}/{2}; jobqueue: {3} k; speed total/dev: {4}/{5} kH/s",
DateTime.Now.ToString (),
clients.Count,
PoolConnectionFactory.Connections.Count,
((double) jobQueue.Count / 1000.0d).ToString ("F1"),
((double) totalSpeed / 1000.0d).ToString ("F2"),
((double) totalDevSpeed / 1000.0d).ToString ("F2")));
while (jobQueue.Count > JobCacheSize) {
string deq;
@ -1080,7 +1088,8 @@ namespace Server {
// we removed ourself because we got disconnected from the pool
// make us alive again!
if (clients.Count > 0) {
Console.WriteLine ("disconnected from dev pool. trying to reconnect.");
CConsole.WriteWarning (() =>
Console.WriteLine ("disconnected from dev pool. trying to reconnect."));
devJob = new Job ();
CreateOurself ();
}
@ -1089,19 +1098,23 @@ namespace Server {
HashesCheckedThisHeartbeat = 0;
if (Hearbeats % ForceGCEveryXHeartbeat == 0) {
Console.WriteLine ("Garbage collection. Currently using {0} MB.", Math.Round (((double) (GC.GetTotalMemory (false)) / 1024 / 1024)));
CConsole.WriteInfo (() => {
Console.WriteLine ("Garbage collection. Currently using {0} MB.", Math.Round (((double) (GC.GetTotalMemory (false)) / 1024 / 1024)));
DateTime tbc = DateTime.Now;
DateTime tbc = DateTime.Now;
// trust me, I am a professional
GC.Collect (GC.MaxGeneration, GCCollectionMode.Forced); // DON'T DO THIS!!!
Console.WriteLine ("Garbage collected in {0} ms. Currently using {1} MB ({2} clients).", (DateTime.Now - tbc).Milliseconds,
Math.Round (((double) (GC.GetTotalMemory (false)) / 1024 / 1024)), clients.Count);
// trust me, I am a professional
GC.Collect (GC.MaxGeneration, GCCollectionMode.Forced); // DON'T DO THIS!!!
Console.WriteLine ("Garbage collected in {0} ms. Currently using {1} MB ({2} clients).", (DateTime.Now - tbc).Milliseconds,
Math.Round (((double) (GC.GetTotalMemory (false)) / 1024 / 1024)), clients.Count);
});
}
} catch (Exception ex) {
CConsole.WriteAlert (() =>
Console.WriteLine ("{0} Exception caught in the main loop !", ex));
Console.WriteLine ("Exception caught in the main loop ! {0}", ex));
}
}

Loading…
Cancel
Save