public ListenPortToRemoteEndpointForwardService(SOCKServer parent) { Parent = parent; RemoteClient = new SocketClient(); Listener = new SocketListener(); Listener.OnNewConnection += new SocketListener.DelegateNewConnectedSocket(Listener_OnNewConnection); RemoteClient.ReceiveHandlerBytes += new SocketClient.SocketReceiveHandler(RemoteClient_ReceiveHandlerBytes); RemoteClient.DisconnectHandler += new SocketClient.SocketEventHandler(RemoteClient_DisconnectHandler); }
public SOCKSTransform(SocketClient client) { Client = client; }
void IncomingClient_DisconnectHandler(object sender, EventArgs e) { Parent.RemoveService(this); if (RemoteClient != null) { RemoteClient.Disconnect(); RemoteClient = null; } if (Listener != null) { Listener.Close(); Listener = null; } }
void ConnectClient_ReceiveHandlerBytes(SocketClient client, byte[] bData, int nLength) { if (this.Connected == true) { Send(bData, nLength); } }
void client_OnReceiveMessage(SocketClient client, byte[] bData, int nLength) { FileTransfer.BytesRemaining -= nLength; DownloadFileBuffer.AppendData(bData); if (DownloadFileBuffer.Size >= FileTransfer.BytesTotal) { FileTransfer.Bytes = DownloadFileBuffer.GetAllSamples(); EventGotAllData.Set(); FileTransfer.FileTransferState = FileTransferState.Done; IsCompleted = true; XMPPClient.FileTransferManager.FinishActiveFileTransfer(FileTransfer); } }
public SOCKSServerSession(Socket s, SOCKS5Listener parent) { this.Client = s; Parent = parent; ConnectClient = new SocketClient(); ConnectClient.DisconnectHandler += new SocketEventHandler(ConnectClient_DisconnectHandler); ConnectClient.ReceiveHandlerBytes += new SocketReceiveHandler(ConnectClient_ReceiveHandlerBytes); }
void RemoteClient_DisconnectHandler(object sender, EventArgs e) { if (IncomingClient != null) { IncomingClient.Disconnect(); IncomingClient = null; } }
public void SendFileThread(object obj) { ByteStreamQueryIQ bsiq = obj as ByteStreamQueryIQ; /// Attempt to open our hosts /// StreamHost host = bsiq.ByteStreamQuery.StreamHostUsed; SocketServer.SocketClient client = new SocketClient(); client.SetSOCKSProxy(5, XMPPClient.Server, 7777, "xmppclient"); client.OnAsyncConnectFinished += new DelegateConnectFinish(client_OnAsyncConnectFinished); EventConnected.Reset(); ConnectSuccesful = false; string strHost = string.Format("{0}{1}{2}", this.FileTransfer.sid, XMPPClient.JID, bsiq.From); System.Security.Cryptography.SHA1Managed sha = new System.Security.Cryptography.SHA1Managed(); byte [] bBytes = sha.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(strHost)); strHost = SocketServer.TLS.ByteHelper.HexStringFromByte(bBytes, false, int.MaxValue).ToLower(); /// Connect parametrs are the sha1 hash and 0, the socks proxy will connect us to the correct place client.ConnectAsync(strHost, 0); EventConnected.WaitOne(); if (ConnectSuccesful == true) { /// Now we must activate the proxy so we can send /// EventActivate.Reset(); IQActivate = new ByteStreamQueryIQ(); IQActivate.ByteStreamQuery = new ByteStreamQuery(); IQActivate.ByteStreamQuery.SID = this.FileTransfer.sid; IQActivate.From = XMPPClient.JID; IQActivate.To = host.Jid; IQActivate.Type = IQType.set.ToString(); IQActivate.ByteStreamQuery.Activate = FileTransfer.RemoteJID; XMPPClient.SendObject(IQActivate); EventActivate.WaitOne(); if (IsCompleted == true) { /// Error, exit this thread FileTransfer.FileTransferState = FileTransferState.Error; XMPPClient.FileTransferManager.FinishActiveFileTransfer(FileTransfer); return; } FileTransfer.BytesRemaining = FileTransfer.Bytes.Length; FileTransfer.FileTransferState = FileTransferState.Transferring; /// Now send all our data /// ByteBuffer buffer = new ByteBuffer(); buffer.AppendData(FileTransfer.Bytes); while (buffer.Size > 0) { int nSize = (buffer.Size > 16384) ? 16384 : buffer.Size; Sockets.SocketAsyncEventArgs asyncsend = new Sockets.SocketAsyncEventArgs(); asyncsend.SetBuffer(buffer.GetNSamples(nSize), 0, nSize); asyncsend.Completed += new EventHandler<Sockets.SocketAsyncEventArgs>(asyncsend_Completed); SendCompletedEvent.Reset(); bSendSuccess = false; bool bSent = false; try { client.socket.SendAsync(asyncsend); } catch (Exception ex) { IsCompleted = true; FileTransfer.Error = ex.Message; FileTransfer.FileTransferState = FileTransferState.Error; return; } SendCompletedEvent.WaitOne(); if (IsCompleted == true) break; if ((bSendSuccess == false) && (bSent == false) ) /// was sent async because bSent is false, so we can examine bSendSuccess to make sure we sent the right number of bytes { break; } FileTransfer.BytesRemaining -= nSize; } client.Disconnect(); FileTransfer.FileTransferState = FileTransferState.Done; IsCompleted = true; XMPPClient.FileTransferManager.FinishActiveFileTransfer(FileTransfer); return; } FileTransfer.Error = "Failed to send data"; FileTransfer.FileTransferState = FileTransferState.Error; IsCompleted = true; }
protected void FireConnectFinished(SocketClient client, string strError) { if (OnConnectFinished != null) OnConnectFinished(client, strError); }
void Listener_OnNewConnection(Socket s) { IncomingClient = new SocketClient(s, null); IncomingClient.ReceiveHandlerBytes += new SocketClient.SocketReceiveHandler(IncomingClient_ReceiveHandlerBytes); IncomingClient.DisconnectHandler += new SocketClient.SocketEventHandler(IncomingClient_DisconnectHandler); IncomingClient.DoAsyncRead(); RemoteClient.DoAsyncRead(); }
public void FireReceiveHandler(SocketClient client, System.EventArgs args) { if (ReceiveHandler != null) { ReceiveHandler(client, args); } }
public void FireDisconnectHandler(SocketClient client) { if (DisconnectHandler != null) { DisconnectHandler(client, new System.EventArgs()); } }
public void FireAcceptHandler(SocketClient client) { if (AcceptHandler != null) { AcceptHandler(client, new System.EventArgs()); } }
void client_OnAsyncConnectFinished(SocketClient client, bool bSuccess, string strErrors) { }
void RemoteClient_ReceiveHandlerBytes(SocketClient client, byte[] bData, int nLength) { if ((IncomingClient != null) && (IncomingClient.Connected == true)) IncomingClient.Send(bData, nLength); }
public void DownloadThread(object obj) { ByteStreamQueryIQ bsiq = obj as ByteStreamQueryIQ; /// Attempt to open our hosts /// foreach (StreamHost host in bsiq.ByteStreamQuery.Hosts) { if (IsCompleted == true) break; SocketServer.SocketClient client = new SocketClient(); client.SetSOCKSProxy(5, host.Host, Convert.ToInt32(host.Port), "xmppclient"); client.OnAsyncConnectFinished += new DelegateConnectFinish(client_OnAsyncConnectFinished); EventConnected.Reset(); ConnectSuccesful = false; string strHost = string.Format("{0}{1}{2}", this.FileTransfer.sid, bsiq.From, bsiq.To); System.Security.Cryptography.SHA1Managed sha = new System.Security.Cryptography.SHA1Managed(); byte [] bBytes = sha.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(strHost)); strHost = SocketServer.TLS.ByteHelper.HexStringFromByte(bBytes, false, int.MaxValue).ToLower(); /// Connect parametrs are the sha1 hash and 0, the socks proxy will connect us to the correct place client.StartReadOnConnect = true; client.ConnectAsync(strHost, 0); EventConnected.WaitOne(); if (ConnectSuccesful == true) { FileTransfer.FileTransferState = FileTransferState.Transferring; client.OnReceiveMessage += new SocketClient.SocketReceiveHandler(client_OnReceiveMessage); DownloadFileBuffer.GetAllSamples(); /// connected and negotiated socks5, tell the far end to start sending data /// ByteStreamQueryIQ iqresponse = new ByteStreamQueryIQ(); iqresponse.SID = this.FileTransfer.sid; iqresponse.ByteStreamQuery = new ByteStreamQuery(); iqresponse.ByteStreamQuery.StreamHostUsed = new StreamHost(); iqresponse.ByteStreamQuery.StreamHostUsed.Jid = host.Jid; iqresponse.From = XMPPClient.JID; iqresponse.ID = bsiq.ID; iqresponse.To = bsiq.From; iqresponse.Type = IQType.result.ToString(); XMPPClient.SendObject(iqresponse); /// Now read data until we get our desired amount /// EventGotAllData.WaitOne(); client.Disconnect(); return; } } /// Couldn't transfer file, send error /// ByteStreamQueryIQ response = new ByteStreamQueryIQ(); response.ByteStreamQuery = bsiq.ByteStreamQuery; response.From = XMPPClient.JID; response.To = bsiq.From; response.ID = bsiq.ID; response.Type = IQType.error.ToString(); response.Error = new Error(ErrorType.remoteservertimeout); XMPPClient.SendXMPP(response); FileTransfer.Error = "Could not connect to proxy"; FileTransfer.FileTransferState = FileTransferState.Error; XMPPClient.FileTransferManager.FinishActiveFileTransfer(FileTransfer); }
public SOCKSServerSession(Socket s, SOCKServer parent) { Init(s, null); Parent = parent; ConnectClient = new SocketClient(); ConnectClient.DisconnectHandler += new SocketEventHandler(ConnectClient_DisconnectHandler); ConnectClient.ReceiveHandlerBytes += new SocketReceiveHandler(ConnectClient_ReceiveHandlerBytes); }
void client_OnAsyncConnectFinished(SocketClient client, bool bSuccess, string strErrors) { ConnectSuccesful = bSuccess; EventConnected.Set(); }
public override void OnDisconnect(string strReason) { base.OnDisconnect(strReason); try { if ((this.ConnectClient != null) && (this.ConnectClient.Connected == true)) { this.ConnectClient.Disconnect(); this.ConnectClient = null; } } catch (Exception) { } finally { this.ConnectClient = null; } }
/// <summary> /// Process the receive of a connection. /// </summary> /// <param name="acceptEventArg">The parameter containing the connection information.</param> private void ProcessAccept(SocketAsyncEventArgs acceptEventArg) { SocketClient client = new SocketClient(acceptEventArg.AcceptSocket); ConnectionReceived.Invoke(client); }