Browse Source

server modifications

pull/123/head
notgiven688 3 years ago
parent
commit
935d40fed7
3 changed files with 109 additions and 49 deletions
  1. +23
    -3
      server/Server/AlgorithmHelper.cs
  2. +9
    -3
      server/Server/PoolConnection.cs
  3. +77
    -43
      server/Server/Program.cs

+ 23
- 3
server/Server/AlgorithmHelper.cs View File

@ -35,19 +35,39 @@ namespace Server {
{
{ "cryptonight/0", new Tuple<string, int>("cn", 0) },
{ "cryptonight/1", new Tuple<string, int>("cn", 1) },
{ "cryptonight/2", new Tuple<string, int>("cn", 2) },
{ "cryptonight-lite/0", new Tuple<string, int>("cn-lite", 0) },
{ "cryptonight-lite/1", new Tuple<string, int>("cn-lite", 1) },
{ "cryptonight-lite/2", new Tuple<string, int>("cn-lite", 2) },
{ "cn/0", new Tuple<string, int>("cn", 0) },
{ "cn/1", new Tuple<string, int>("cn", 1) },
{ "cn/2", new Tuple<string, int>("cn", 2) },
{ "cn-lite/0", new Tuple<string, int>("cn-lite", 0) },
{ "cn-lite/1", new Tuple<string, int>("cn-lite", 1) }
{ "cn-lite/1", new Tuple<string, int>("cn-lite", 1) },
{ "cn-lite/2", new Tuple<string, int>("cn-lite", 2) }
};
public static bool NormalizeAlgorithmAndVariant (JsonData job) {
string algo = job["algo"].GetString().ToLower();
if (lookup.ContainsKey(algo))
if (algo == "cn" || algo == "cryptonight")
job["algo"] = "cn";
else if (algo == "cn-lite" || algo == "cryptonight-lite")
job["algo"] = "cn-lite";
else if (lookup.ContainsKey(algo))
{
var tuple = lookup[algo];
job["algo"] = tuple.Item1;
job["variant"] = tuple.Item2;
}
else
{
return false;
}
/*if (lookup.ContainsKey(algo))
{
var tuple = lookup[algo];
job["algo"] = tuple.Item1;
@ -60,7 +80,7 @@ namespace Server {
else if (algo == "cn-lite" || algo == "cryptonight-lite")
job["algo"] = "cn-lite";
else return false;
}
}*/
return true;
}

+ 9
- 3
server/Server/PoolConnection.cs View File

@ -114,7 +114,7 @@ namespace Server {
PoolConnection mypc = result.AsyncState as PoolConnection;
TcpClient client = mypc.TcpClient;
if (!client.Connected) return;
if (mypc.Closed || !client.Connected) return;
NetworkStream networkStream;
@ -264,10 +264,11 @@ namespace Server {
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\"},\"algo\": [\"cn\", \"cn-lite\"], \"id\":1}";
string msg2 = "\",\"agent\":\"webminerpool.com\"},\"algo\": [\"cn/0\",\"cn/1\",\"cn/2\",\"cn-lite/0\",\"cn-lite/1\",\"cn-lite/2\"], \"id\":1}";
string msg = msg0 + mypc.Login + msg1 + mypc.Password + msg2 + "\n";
mypc.Send (mypc.LastSender, msg);
@ -287,7 +288,9 @@ namespace Server {
}
}
public static void Close (PoolConnection connection, Client client) {
public static void Close (Client client) {
PoolConnection connection = client.PoolConnection;
connection.WebClients.TryRemove (client);
if (connection.WebClients.Count == 0) {
@ -317,6 +320,9 @@ namespace Server {
}
public static void CheckPoolConnection (PoolConnection connection) {
if (connection.Closed) return;
if ((DateTime.Now - connection.LastInteraction).TotalMinutes < 10)
return;

+ 77
- 43
server/Server/Program.cs View File

@ -229,6 +229,33 @@ namespace Server {
}
}
private static bool IsCompatible(string blob, int variant, int clientVersion)
{
// clientVersion < 5 is not allowed to connect anymore.
// clientVersion 5 does support variant 0 and 1
// clientVersion 6 does support 0,1 and >1
// blobVersion7 = cn1, blobVersionX = cn2 if X > 7
if (clientVersion > 5) return true;
else
{
if (variant == -1)
{
bool iscn2 = false;
try { iscn2 = (HexToUInt32(blob.Substring(0, 2) + "000000") > 7); } catch { }
if (iscn2) return false;
}
else if (variant > 1)
{
return false;
}
return true;
}
}
private static void PoolReceiveCallback (Client client, JsonData msg, CcHashset<string> hashset) {
string jobId = Guid.NewGuid ().ToString ("N");
@ -260,18 +287,11 @@ namespace Server {
devJob.Target = ji.Target;
devJob.Variant = ji.Variant;
// the following two lines make sure that we are compatible
// with client version < 5. Can be removed in the future.
bool isv7 = false;
try { isv7 = (HexToUInt32 (ji.Blob.Substring (0, 2) + "000000") > 6); } catch { }
List<Client> slavelist = new List<Client> (slaves.Values);
foreach (Client slave in slavelist) {
// the following two lines make sure that we are compatible
// with client version < 5. Can be removed in the future.
bool compatible = (slave.Version > 4) || (isv7);
bool compatible = IsCompatible(devJob.Blob, devJob.Variant, slave.Version);
if (!compatible) continue;
string newtarget;
@ -311,11 +331,7 @@ namespace Server {
if (((DateTime.Now - devJob.Age).TotalSeconds < TimeDevJobsAreOld)) {
// the following three lines make sure that we are compatible
// with client version < 5. Can be removed in the future.
bool isv7 = false;
try { isv7 = (HexToUInt32 (devJob.Blob.Substring (0, 2) + "000000") > 6); } catch { }
bool compatible = (client.Version > 4) || (isv7);
bool compatible = IsCompatible(devJob.Blob, devJob.Variant, client.Version);
if (compatible) {
// okay, do not send devjob.Target, but
@ -348,6 +364,10 @@ namespace Server {
if (!tookdev) {
bool compatible = IsCompatible(ji.Blob, ji.Variant, client.Version);
if (!compatible) return;
forward = "{\"identifier\":\"" + "job" +
"\",\"job_id\":\"" + jobId +
"\",\"algo\":\"" + msg["algo"].GetString ().ToLower () +
@ -386,7 +406,7 @@ namespace Server {
try { client.WebSocket.Close (); } catch { }
if (client.PoolConnection != null)
PoolConnectionFactory.Close (client.PoolConnection, client);
PoolConnectionFactory.Close (client);
}
public static void DisconnectClient (Client client, string reason) {
@ -432,16 +452,14 @@ namespace Server {
ourself.PoolConnection.DefaultVariant = -1;
}
private static bool CheckLibHash (out Exception ex) {
// just check if we can successfully calculate a cn-hash.
string testStr = new string ('1', 151) + '3';
string hashedResult = string.Empty;
private static bool CheckLibHash (string input, string expected,
int lite, int variant, out Exception ex) {
IntPtr pStr;
string hashedResult = string.Empty;
try {
pStr = hash_cn (testStr, 0, 1);
IntPtr pStr = hash_cn (input, lite, variant);
hashedResult = Marshal.PtrToStringAnsi (pStr);
hash_free (pStr);
} catch (Exception e) {
@ -449,23 +467,7 @@ namespace Server {
return false;
}
// test -> cryptonight v1
if (hashedResult.Substring (0, 11) != "843ae6fc006") {
ex = new Exception ("Hash function returned wrong hash");
return false;
}
try {
pStr = hash_cn (testStr, 1, 0);
hashedResult = Marshal.PtrToStringAnsi (pStr);
hash_free (pStr);
} catch (Exception e) {
ex = e;
return false;
}
// test -> cryptonight lite v0
if (hashedResult.Substring (0, 11) != "f41e2a4e00e") {
if (hashedResult != expected) {
ex = new Exception ("Hash function returned wrong hash");
return false;
}
@ -475,10 +477,10 @@ namespace Server {
}
private static void ExcessiveHashTest () {
Parallel.For (0, 10000, (i) => {
Parallel.For (0, 100, (i) => {
string testStr = new string ('1', 151) + '3';
IntPtr ptr = hash_cn (testStr, 0, 1);
IntPtr ptr = hash_cn (testStr, 1, 0);
string str = Marshal.PtrToStringAnsi (ptr);
hash_free (ptr);
@ -516,10 +518,38 @@ namespace Server {
CConsole.ColorInfo (() => Console.WriteLine ("Loaded {0} pools from pools.json.", PoolList.Count));
Exception exception = null;
libHashAvailable = CheckLibHash (out exception);
libHashAvailable = true;
// cn
libHashAvailable &= CheckLibHash ("6465206f6d6e69627573206475626974616e64756d",
"2f8e3df40bd11f9ac90c743ca8e32bb391da4fb98612aa3b6cdc639ee00b31f5",
0, 0, out exception);
// cn_v1
libHashAvailable &= CheckLibHash("38274c97c45a172cfc97679870422e3a1ab0784960c60514d816271415c306ee3a3ed1a77e31f6a885c3cb",
"ed082e49dbd5bbe34a3726a0d1dad981146062b39d36d62c71eb1ed8ab49459b",
0, 1, out exception);
// cn_v2
libHashAvailable &= CheckLibHash("5468697320697320612074657374205468697320697320612074657374205468697320697320612074657374",
"353fdc068fd47b03c04b9431e005e00b68c2168a3cc7335c8b9b308156591a4f",
0, 2, out exception);
// cn_lite
libHashAvailable &= CheckLibHash("6465206f6d6e69627573206475626974616e64756d",
"1b73647a792df8724ce28fddc1e4b6f348dc39e6aa47c434fe400cec98ec2b91",
1, 0, out exception);
// cn_lite_v1
libHashAvailable &= CheckLibHash("38274c97c45a172cfc97679870422e3a1ab0784960c60514d816271415c306ee3a3ed1a77e31f6a885c3cb",
"4e785376ed2733262d83cc25321a9d0003f5395315de919acf1b97f0a84fbd2d",
1, 1, out exception);
// cn_lite_v2 (speculative)
libHashAvailable &= CheckLibHash("5468697320697320612074657374205468697320697320612074657374205468697320697320612074657374",
"49c95241af3bd74e78f473936ac36214bbc386a36869f9406b5da16aa0ee4b06",
1, 2, out exception);
if (!libHashAvailable) CConsole.ColorWarning (() =>
Console.WriteLine ("libhash.so is not available. Checking user submitted hashes disabled.")
@ -527,7 +557,6 @@ namespace Server {
PoolConnectionFactory.RegisterCallbacks (PoolReceiveCallback, PoolErrorCallback, PoolDisconnectCallback);
//FillPoolPool ();
if (File.Exists ("statistics.dat")) {
@ -706,6 +735,11 @@ namespace Server {
}
if (client.Version < 5) {
DisconnectClient (client, "Client version too old.");
return;
}
if (client.Version < 6) {
CConsole.ColorWarning (() => Console.WriteLine ("Warning: Outdated client connected. Make sure to update the clients"));
}

Loading…
Cancel
Save