Complete sources for a monero webminer.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
7.8 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. // https://github.com/statianzo/Fleck
  2. // The MIT License
  3. // Copyright (c) 2010-2016 Jason Staten
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. // this software and associated documentation files (the "Software"), to deal in
  6. // the Software without restriction, including without limitation the rights to
  7. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. // the Software, and to permit persons to whom the Software is furnished to do so,
  9. // subject to the following conditions:
  10. // The above copyright notice and this permission notice shall be included in all
  11. // copies or substantial portions of the Software.
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.IO;
  20. using System.Net;
  21. using System.Net.Security;
  22. using System.Net.Sockets;
  23. using System.Security.Authentication;
  24. using System.Security.Cryptography.X509Certificates;
  25. using System.Threading.Tasks;
  26. using System.Threading;
  27. namespace Fleck
  28. {
  29. public static class SocketExtensions
  30. {
  31. private const int BytesPerLong = 4;
  32. private const int BitsPerByte = 8;
  33. public static void SetKeepAlive(this Socket socket, UInt32 keepAliveInterval, UInt32 retryInterval)
  34. {
  35. int size = sizeof(UInt32);
  36. UInt32 on = 1;
  37. byte[] inArray = new byte[size * 3];
  38. Array.Copy(BitConverter.GetBytes(on), 0, inArray, 0, size);
  39. Array.Copy(BitConverter.GetBytes(keepAliveInterval), 0, inArray, size, size);
  40. Array.Copy(BitConverter.GetBytes(retryInterval), 0, inArray, size * 2, size);
  41. socket.IOControl(IOControlCode.KeepAliveValues, inArray, null);
  42. }
  43. }
  44. public class SocketWrapper : ISocket
  45. {
  46. private readonly Socket _socket;
  47. private Stream _stream;
  48. private CancellationTokenSource _tokenSource;
  49. private TaskFactory _taskFactory;
  50. public string RemoteIpAddress
  51. {
  52. get
  53. {
  54. var endpoint = _socket.RemoteEndPoint as IPEndPoint;
  55. return endpoint != null ? endpoint.Address.ToString() : null;
  56. }
  57. }
  58. public int RemotePort
  59. {
  60. get
  61. {
  62. var endpoint = _socket.RemoteEndPoint as IPEndPoint;
  63. return endpoint != null ? endpoint.Port : -1;
  64. }
  65. }
  66. public SocketWrapper(Socket socket)
  67. {
  68. _tokenSource = new CancellationTokenSource();
  69. _taskFactory = new TaskFactory(_tokenSource.Token);
  70. _socket = socket;
  71. if (_socket.Connected)
  72. _stream = new NetworkStream(_socket);
  73. socket.SetKeepAlive(60000,10000); // fix wmp
  74. }
  75. private void CloseSomething(SslStream sslstream, QueuedStream qs, Exception ex)
  76. {
  77. try{sslstream.Close ();}catch{}
  78. try{sslstream.EndAuthenticateAsServer (null);}catch{}
  79. try{qs.Close();}catch{}
  80. try{qs.Dispose();}catch{}
  81. try{qs.EndRead(null);}catch{}
  82. }
  83. public Task Authenticate(X509Certificate2 certificate, SslProtocols enabledSslProtocols, Action callback, Action<Exception> error)
  84. {
  85. var ssl = new SslStream(_stream, false);
  86. _stream = new QueuedStream(ssl);
  87. Func<AsyncCallback, object, IAsyncResult> begin =
  88. (cb, s) => ssl.BeginAuthenticateAsServer(certificate, false, enabledSslProtocols, false, cb, s);
  89. Task task = Task.Factory.FromAsync(begin, ssl.EndAuthenticateAsServer, null);
  90. task.ContinueWith(t => CloseSomething(ssl, _stream as QueuedStream, t.Exception), TaskContinuationOptions.OnlyOnFaulted);
  91. task.ContinueWith (t => callback (), TaskContinuationOptions.NotOnFaulted)
  92. .ContinueWith (t => error (t.Exception), TaskContinuationOptions.OnlyOnFaulted);
  93. //.ContinueWith(t => CloseSomething(ssl, _stream as QueuedStream, t.Exception), TaskContinuationOptions.OnlyOnFaulted);
  94. task.ContinueWith(t => error(t.Exception), TaskContinuationOptions.OnlyOnFaulted);
  95. return task;
  96. }
  97. public void Listen(int backlog)
  98. {
  99. _socket.Listen(backlog);
  100. }
  101. public void Bind(EndPoint endPoint)
  102. {
  103. _socket.Bind(endPoint);
  104. }
  105. public bool Connected
  106. {
  107. get { return _socket.Connected; }
  108. }
  109. public Stream Stream
  110. {
  111. get { return _stream; }
  112. }
  113. public bool NoDelay
  114. {
  115. get { return _socket.NoDelay; }
  116. set { _socket.NoDelay = value; }
  117. }
  118. public EndPoint LocalEndPoint
  119. {
  120. get { return _socket.LocalEndPoint; }
  121. }
  122. public Task<int> Receive(byte[] buffer, Action<int> callback, Action<Exception> error, int offset)
  123. {
  124. try
  125. {
  126. // Func<AsyncCallback, object, IAsyncResult> begin =
  127. // (cb, s) => _stream.BeginRead(buffer, offset, buffer.Length, cb, s);
  128. //Task<int> task = Task.Factory.FromAsync<int>(begin, _stream.EndRead, null);
  129. var task = _stream.ReadAsync(buffer,offset,buffer.Length);
  130. task.ContinueWith(t => callback(t.Result), TaskContinuationOptions.NotOnFaulted)
  131. .ContinueWith(t => error(t.Exception), TaskContinuationOptions.OnlyOnFaulted);
  132. task.ContinueWith(t => error(t.Exception), TaskContinuationOptions.OnlyOnFaulted);
  133. return task;
  134. }
  135. catch (Exception e)
  136. {
  137. error(e);
  138. return null;
  139. }
  140. }
  141. public Task Accept(Action<ISocket> callback, Action<Exception> error)
  142. {
  143. Func<IAsyncResult, ISocket> end = r => _tokenSource.Token.IsCancellationRequested ? null : new SocketWrapper(_socket.EndAccept(r));
  144. var task = _taskFactory.FromAsync(_socket.BeginAccept, end, null);
  145. task.ContinueWith(t => error(t.Exception), TaskContinuationOptions.OnlyOnFaulted);
  146. return task.ContinueWith(t => callback(t.Result), TaskContinuationOptions.OnlyOnRanToCompletion);
  147. }
  148. public void Dispose()
  149. {
  150. _tokenSource.Cancel();
  151. if (_stream != null) _stream.Dispose();
  152. if (_socket != null) _socket.Dispose();
  153. }
  154. public void Close()
  155. {
  156. _tokenSource.Cancel();
  157. if (_stream != null) _stream.Close();
  158. if (_socket != null) _socket.Close();
  159. }
  160. public int EndSend(IAsyncResult asyncResult)
  161. {
  162. _stream.EndWrite(asyncResult);
  163. return 0;
  164. }
  165. public Task Send(byte[] buffer, Action callback, Action<Exception> error)
  166. {
  167. if (_tokenSource.IsCancellationRequested)
  168. return null;
  169. try
  170. {
  171. // Func<AsyncCallback, object, IAsyncResult> begin =
  172. // (cb, s) => _stream.BeginWrite(buffer, 0, buffer.Length, cb, s);
  173. // Task task = Task.Factory.FromAsync(begin, _stream.EndWrite, null);
  174. Task task = _stream.WriteAsync(buffer,0,buffer.Length);
  175. task.ContinueWith(t => callback(), TaskContinuationOptions.NotOnFaulted)
  176. .ContinueWith(t => error(t.Exception), TaskContinuationOptions.OnlyOnFaulted);
  177. task.ContinueWith(t => error(t.Exception), TaskContinuationOptions.OnlyOnFaulted);
  178. return task;
  179. }
  180. catch (Exception e)
  181. {
  182. error(e);
  183. return null;
  184. }
  185. }
  186. }
  187. }