Browse Source

cleaning

pull/123/head
notgiven688 3 years ago
parent
commit
37340cce43
10 changed files with 700 additions and 944 deletions
  1. +16
    -3
      server/Server.userprefs
  2. +18
    -30
      server/Server/DataStructures.cs
  3. +32
    -21
      server/Server/EmptyWebsocket.cs
  4. +0
    -17
      server/Server/Extensions.cs
  5. +35
    -67
      server/Server/Firewall.cs
  6. +21
    -20
      server/Server/Fleck/SocketWrapper.cs
  7. +8
    -8
      server/Server/JSONParser.cs
  8. +6
    -4
      server/Server/PoolConnection.cs
  9. +562
    -772
      server/Server/Program.cs
  10. +2
    -2
      server/Server/Server.csproj

+ 16
- 3
server/Server.userprefs View File

@ -1,17 +1,30 @@
<Properties StartupConfiguration="{DC564972-9DEF-4897-A8F5-C4C21CEBDE2F}|Default" StartupItem="Server/Server.csproj">
<MonoDevelop.Ide.Workbench ActiveDocument="Server/Program.cs">
<Files>
<File FileName="Server/Program.cs" Line="13" Column="16" />
<File FileName="Server/PoolConnection.cs" Line="1" Column="1" />
<File FileName="Server/Program.cs" Line="907" Column="36" />
<File FileName="Server/PoolConnection.cs" Line="145" Column="1" />
<File FileName="Server/JSONParser.cs" Line="1" Column="1" />
<File FileName="Server/Extensions.cs" Line="1" Column="1" />
<File FileName="Server/EmptyWebsocket.cs" Line="1" Column="1" />
<File FileName="Server/Execute.cs" Line="1" Column="1" />
<File FileName="Server/DataStructures.cs" Line="1" Column="1" />
<File FileName="Server/Firewall.cs" Line="1" Column="1" />
<File FileName="Server/Properties/AssemblyInfo.cs" Line="1" Column="1" />
<File FileName="Server/Fleck/IntExtensions.cs" Line="1" Column="1" />
<File FileName="Server/Fleck/WebSocketServer.cs" Line="1" Column="1" />
<File FileName="Server/Fleck/SocketWrapper.cs" Line="1" Column="1" />
<File FileName="Server/Fleck/BufferPool.cs" Line="1" Column="1" />
<File FileName="Server/Helper.cs" Line="1" Column="1" />
<File FileName="Server/Fleck/ConnectionNotAvailableException.cs" Line="1" Column="1" />
<File FileName="Server/Fleck/FleckLog.cs" Line="1" Column="1" />
<File FileName="Server/Fleck/FrameType.cs" Line="1" Column="1" />
<File FileName="Server/Fleck/HandlerFactory.cs" Line="1" Column="1" />
<File FileName="Server/Fleck/HandshakeException.cs" Line="1" Column="1" />
<File FileName="Server/Fleck/RequestParser.cs" Line="1" Column="1" />
<File FileName="Server/Fleck/WebSocketException.cs" Line="1" Column="1" />
<File FileName="Server/Fleck/WebSocketConnection.cs" Line="1" Column="1" />
<File FileName="Server/Fleck/WebSocketConnectionInfo.cs" Line="1" Column="1" />
<File FileName="Server/Fleck/SubProtocolNegotiator.cs" Line="1" Column="1" />
<File FileName="Server/Fleck/WebSocketHttpRequest.cs" Line="1" Column="1" />
</Files>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Release|x86" />

+ 18
- 30
server/Server/DataStructures.cs View File

@ -1,51 +1,39 @@
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace Server {
namespace Server
{
public class CcDictionary<T, V> : ConcurrentDictionary<T, V>
{
public bool TryRemove(T item)
{
public class CcDictionary<T, V> : ConcurrentDictionary<T, V> {
public bool TryRemove (T item) {
V dummy;
return this.TryRemove(item, out dummy);
return this.TryRemove (item, out dummy);
}
}
public class CcQueue<T> : ConcurrentQueue<T>
{
}
public class CcQueue<T> : ConcurrentQueue<T> { }
public class CcHashset<T>
{
ConcurrentDictionary<T, byte> dictionary = new ConcurrentDictionary<T, byte>();
public class CcHashset<T> {
ConcurrentDictionary<T, byte> dictionary = new ConcurrentDictionary<T, byte> ();
public bool TryAdd(T item)
{
return dictionary.TryAdd(item, byte.MaxValue);
public bool TryAdd (T item) {
return dictionary.TryAdd (item, byte.MaxValue);
}
public ICollection<T> Values
{
public ICollection<T> Values {
get { return dictionary.Keys; }
}
public int Count { get { return dictionary.Count; }}
public int Count { get { return dictionary.Count; } }
public bool Contains(T item)
{
return dictionary.ContainsKey(item);
public bool Contains (T item) {
return dictionary.ContainsKey (item);
}
public bool TryRemove(T item)
{
public bool TryRemove (T item) {
byte dummy;
return dictionary.TryRemove(item, out dummy);
return dictionary.TryRemove (item, out dummy);
}
}
}
}

+ 32
- 21
server/Server/EmptyWebsocket.cs View File

@ -1,13 +1,31 @@
// The MIT License (MIT)
// Copyright (c) 2018 - the webminerpool developer
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using Fleck;
namespace Server {
namespace Server
{
public class EmptyConnectionInfo : IWebSocketConnectionInfo
{
public class EmptyConnectionInfo : IWebSocketConnectionInfo {
#region IWebSocketConnectionInfo implementation
public string SubProtocol {
get {
@ -62,32 +80,26 @@ namespace Server
#endregion
}
public class EmptyWebsocket : IWebSocketConnection
{
public class EmptyWebsocket : IWebSocketConnection {
private static EmptyConnectionInfo eci =
new EmptyConnectionInfo();
new EmptyConnectionInfo ();
#region IWebSocketConnection implementation
public System.Threading.Tasks.Task Send (string message)
{
public System.Threading.Tasks.Task Send (string message) {
//throw new NotImplementedException ();
return null;
}
public System.Threading.Tasks.Task Send (byte[] message)
{
public System.Threading.Tasks.Task Send (byte[] message) {
throw new NotImplementedException ();
}
public System.Threading.Tasks.Task SendPing (byte[] message)
{
public System.Threading.Tasks.Task SendPing (byte[] message) {
throw new NotImplementedException ();
}
public System.Threading.Tasks.Task SendPong (byte[] message)
{
public System.Threading.Tasks.Task SendPong (byte[] message) {
throw new NotImplementedException ();
}
public void Close ()
{
public void Close () {
}
public Action OnOpen {
get {
@ -158,5 +170,4 @@ namespace Server
#endregion
}
}
}

+ 0
- 17
server/Server/Extensions.cs View File

@ -1,23 +1,6 @@
namespace System.Net.Sockets
{
public static class SocketExtensions
{
private const int BytesPerLong = 4;
private const int BitsPerByte = 8;
public static void SetKeepAlive(this Socket socket, UInt32 keepAliveInterval, UInt32 retryInterval)
{
int size = sizeof(UInt32);
UInt32 on = 1;
byte[] inArray = new byte[size * 3];
Array.Copy(BitConverter.GetBytes(on), 0, inArray, 0, size);
Array.Copy(BitConverter.GetBytes(keepAliveInterval), 0, inArray, size, size);
Array.Copy(BitConverter.GetBytes(retryInterval), 0, inArray, size * 2, size);
socket.IOControl(IOControlCode.KeepAliveValues, inArray, null);
}
}
public static class ObjectExtensionClass
{
public static string GetString(this object input)

+ 35
- 67
server/Server/Firewall.cs View File

@ -1,26 +1,25 @@
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using System.Text;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace Server
{
public static class Firewall
{
namespace Server {
public static class Firewall {
public enum UpdateEntry
{
SolvedJob, AuthSuccess, AuthFailure, WrongHash, Handshake
public enum UpdateEntry {
SolvedJob,
AuthSuccess,
AuthFailure,
WrongHash,
Handshake
}
private class Entry
{
private class Entry {
public string Address;
public Entry(string adr)
{
public Entry (string adr) {
Address = adr;
}
@ -33,34 +32,18 @@ namespace Server
public DateTime FirstSeen = DateTime.Now;
}
private static ConcurrentDictionary<string,Entry> entries = new ConcurrentDictionary<string, Entry>();
public const int CheckTimeInHeartbeats = 6*10; // every 10min
private static int HeartBeats = 0;
private static ConcurrentDictionary<string, Entry> entries = new ConcurrentDictionary<string, Entry> ();
public const int CheckTimeInHeartbeats = 6 * 10; // every 10min
private static void AddToIpTables(string ip)
{
WriteTextAsync ("ip_list", ip + Environment.NewLine);
}
private static async Task WriteTextAsync(string filePath, string text)
{
byte[] encodedText = Encoding.ASCII.GetBytes(text);
using (FileStream sourceStream = new FileStream(filePath,
FileMode.Append, FileAccess.Write, FileShare.None,
bufferSize: 4096, useAsync: true))
{
await sourceStream.WriteAsync(encodedText, 0, encodedText.Length);
};
private static void AddToIpTables (string ip) {
Helper.WriteTextAsyncWrapper ("ip_list", ip + Environment.NewLine);
}
public static void Update(string ip, UpdateEntry update)
{
public static void Update (string ip, UpdateEntry update) {
Entry entry = null;
if (entries.TryGetValue (ip, out entry)) {
if (update == UpdateEntry.SolvedJob)
entry.SolvedJobs++;
else if (update == UpdateEntry.AuthFailure)
@ -71,67 +54,52 @@ namespace Server
entry.WrongHash++;
else if (update == UpdateEntry.Handshake)
entry.Handshake++;
} else
{
entries.TryAdd(ip,new Entry(ip));
} else {
entries.TryAdd (ip, new Entry (ip));
}
}
public static void Heartbeat()
{
HeartBeats++;
public static void Heartbeat (int heartBeats) {
Entry dummy;
List<Entry> entrylst = new List<Entry>(entries.Values);
foreach(Entry entry in entrylst)
{
List<Entry> entrylst = new List<Entry> (entries.Values);
foreach (Entry entry in entrylst) {
// decide here...
if (entry.AuthSuccess == 0 && entry.SolvedJobs == 0
&& entry.AuthFailure > 20) {
if (entry.AuthSuccess == 0 && entry.SolvedJobs == 0 &&
entry.AuthFailure > 20) {
AddToIpTables (entry.Address);
entries.TryRemove (entry.Address, out dummy);
Console.WriteLine ("Added {0} to iptables (rule #1)", entry.Address);
} else if (entry.AuthFailure > 500 && entry.AuthSuccess < 500)
{
} else if (entry.AuthFailure > 500 && entry.AuthSuccess < 500) {
AddToIpTables (entry.Address);
entries.TryRemove (entry.Address, out dummy);
Console.WriteLine ("Added {0} to iptables (rule #2)", entry.Address);
}
else if (entry.AuthSuccess + entry.AuthFailure > 1000 && entry.SolvedJobs < 3)
{
} else if (entry.AuthSuccess + entry.AuthFailure > 1000 && entry.SolvedJobs < 3) {
AddToIpTables (entry.Address);
entries.TryRemove (entry.Address, out dummy);
Console.WriteLine ("Added {0} to iptables (rule #3)", entry.Address);
}
else if (entry.AuthSuccess + entry.AuthFailure > 4000)
{
} else if (entry.AuthSuccess + entry.AuthFailure > 4000) {
AddToIpTables (entry.Address);
entries.TryRemove (entry.Address, out dummy);
Console.WriteLine ("Added {0} to iptables (rule #4)", entry.Address);
}
else if (entry.WrongHash > 0 && entry.AuthSuccess < 5)
{
} else if (entry.WrongHash > 0 && entry.AuthSuccess < 5) {
AddToIpTables (entry.Address);
entries.TryRemove (entry.Address, out dummy);
Console.WriteLine ("Added {0} to iptables (rule #5)", entry.Address);
}
else if (entry.AuthSuccess + entry.AuthFailure > 2000 && entry.Handshake < 1)
{
} else if (entry.AuthSuccess + entry.AuthFailure > 2000 && entry.Handshake < 1) {
AddToIpTables (entry.Address);
entries.TryRemove (entry.Address, out dummy);
Console.WriteLine ("Added {0} to iptables (rule #6)", entry.Address);
}
}
if ((HeartBeats % CheckTimeInHeartbeats) == 0) {
if ((heartBeats % CheckTimeInHeartbeats) == 0) {
entries.Clear ();
}
}
}
}
}
}

+ 21
- 20
server/Server/Fleck/SocketWrapper.cs View File

@ -11,6 +11,25 @@ using System.Threading;
namespace Fleck
{
public static class SocketExtensions
{
private const int BytesPerLong = 4;
private const int BitsPerByte = 8;
public static void SetKeepAlive(this Socket socket, UInt32 keepAliveInterval, UInt32 retryInterval)
{
int size = sizeof(UInt32);
UInt32 on = 1;
byte[] inArray = new byte[size * 3];
Array.Copy(BitConverter.GetBytes(on), 0, inArray, 0, size);
Array.Copy(BitConverter.GetBytes(keepAliveInterval), 0, inArray, size, size);
Array.Copy(BitConverter.GetBytes(retryInterval), 0, inArray, size * 2, size);
socket.IOControl(IOControlCode.KeepAliveValues, inArray, null);
}
}
public class SocketWrapper : ISocket
{
@ -36,25 +55,7 @@ namespace Fleck
return endpoint != null ? endpoint.Port : -1;
}
}
/// <summary>
/// Sets the keep-alive interval for the socket.
/// </summary>
/// <param name="socket">The socket.</param>
/// <param name="time">Time between two keep alive "pings" in ms.</param>
/// <param name="interval">Time between two keep alive "pings" when first one fails in ms.</param>
public void SetKeepAlive(Socket socket, UInt32 keepAliveInterval, UInt32 retryInterval)
{
int size = sizeof(UInt32);
UInt32 on = 1;
byte[] inArray = new byte[size * 3];
Array.Copy(BitConverter.GetBytes(on), 0, inArray, 0, size);
Array.Copy(BitConverter.GetBytes(keepAliveInterval), 0, inArray, size, size);
Array.Copy(BitConverter.GetBytes(retryInterval), 0, inArray, size * 2, size);
socket.IOControl(IOControlCode.KeepAliveValues, inArray, null);
}
public SocketWrapper(Socket socket)
{
@ -65,7 +66,7 @@ namespace Fleck
_stream = new NetworkStream(_socket);
SetKeepAlive(socket,60000,10000); // fix TL
socket.SetKeepAlive(60000,10000); // fix TL
}
private void CloseSomething(SslStream sslstream, QueuedStream qs, Exception ex)

+ 8
- 8
server/Server/JSONParser.cs View File

@ -1,3 +1,11 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
/*
https://github.com/zanders3/json
@ -25,14 +33,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
namespace TinyJson
{

+ 6
- 4
server/Server/PoolConnection.cs View File

@ -10,6 +10,8 @@ using TinyJson;
using JsonData = System.Collections.Generic.Dictionary<string, object>;
using Fleck;
namespace Server
{
@ -70,8 +72,8 @@ namespace Server
private static DisconnectedDelegate Disconnect;
public static ConcurrentDictionary<string,PoolConnection> Connections
= new ConcurrentDictionary<string,PoolConnection> ();
public static CcDictionary<string,PoolConnection> Connections
= new CcDictionary<string,PoolConnection> ();
private static bool VerifyJob(JsonData data)
@ -351,7 +353,7 @@ namespace Server
connection.TcpClient = new TcpClient ();
connection.TcpClient.Client.SetKeepAlive (60000, 1000);
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); }
@ -377,7 +379,7 @@ namespace Server
mypc.TcpClient = new TcpClient ();
mypc.TcpClient.Client.SetKeepAlive (60000, 1000);
Fleck.SocketExtensions.SetKeepAlive(mypc.TcpClient.Client,60000, 1000);
mypc.TcpClient.Client.ReceiveBufferSize = 4096*2;
mypc.Login = login;

+ 562
- 772
server/Server/Program.cs
File diff suppressed because it is too large
View File


+ 2
- 2
server/Server/Server.csproj View File

@ -54,11 +54,9 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="JSONParser.cs" />
<Compile Include="EmptyWebsocket.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="PoolConnection.cs" />
<Compile Include="DataStructures.cs" />
<Compile Include="Execute.cs" />
<Compile Include="Fleck\BufferPool.cs" />
<Compile Include="Fleck\ConnectionNotAvailableException.cs" />
<Compile Include="Fleck\FleckLog.cs" />
@ -89,6 +87,8 @@
<Compile Include="Fleck\Interfaces\IWebSocketConnectionInfo.cs" />
<Compile Include="Fleck\Interfaces\IWebSocketServer.cs" />
<Compile Include="Firewall.cs" />
<Compile Include="Helper.cs" />
<Compile Include="EmptyWebsocket.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

Loading…
Cancel
Save