///<summary>Called when we receive a reply from the FTP server that should be ignored.</summary> ///<param name="ar">The result of the asynchronous operation.</param> private void OnIgnoreReply(IAsyncResult ar) { try { int Ret = DestinationSocket.EndReceive(ar); if (Ret <= 0) { Dispose(); return; } FtpReply += Encoding.ASCII.GetString(RemoteBuffer, 0, Ret); if (IsValidReply(FtpReply)) { DestinationSocket.BeginReceive(RemoteBuffer, 0, RemoteBuffer.Length, SocketFlags.None, new AsyncCallback(OnReplyReceived), DestinationSocket); DestinationSocket.BeginSend(Encoding.ASCII.GetBytes(User), 0, User.Length, SocketFlags.None, new AsyncCallback(OnCommandSent), DestinationSocket); } else { DestinationSocket.BeginReceive(RemoteBuffer, 0, RemoteBuffer.Length, SocketFlags.None, new AsyncCallback(OnIgnoreReply), DestinationSocket); } } catch { Dispose(); } }
///<summary>Called when we have received some bytes from the client.</summary> ///<param name="ar">The result of the asynchronous operation.</param> private void OnReceiveCommand(IAsyncResult ar) { try { int Ret = ClientSocket.EndReceive(ar); string Command; if (Ret <= 0) { Dispose(); return; } FtpCommand += Encoding.ASCII.GetString(Buffer, 0, Ret); if (FtpClient.IsValidCommand(FtpCommand)) { Command = FtpCommand; if (ProcessCommand(Command)) { DestinationSocket.BeginSend(Encoding.ASCII.GetBytes(Command), 0, Command.Length, SocketFlags.None, new AsyncCallback(this.OnCommandSent), DestinationSocket); } FtpCommand = ""; } else { ClientSocket.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnReceiveCommand), ClientSocket); } } catch { Dispose(); } }
private void OnConnected(IAsyncResult ar) { try { DestinationSocket.EndConnect(ar); string rq; if (HttpRequestType.ToUpper().Equals("CONNECT")) { //HTTPS rq = HttpVersion + " 200 Connection established\r\nProxy-Agent: DoctorProxy Proxy Server\r\n\r\n"; ClientSocket.BeginSend(Encoding.ASCII.GetBytes(rq), 0, rq.Length, SocketFlags.None, new AsyncCallback(this.OnOkSent), ClientSocket); } else { //Normal HTTP rq = RebuildQuery(); //اینجا لاگ کن DestinationSocket.BeginSend(Encoding.ASCII.GetBytes(rq), 0, rq.Length, SocketFlags.None, new AsyncCallback(this.OnQuerySent), DestinationSocket); } } catch (Exception ex) { Log.Write(MethodInfo.GetCurrentMethod(), ex); Dispose(); } }
private void OnConnected(IAsyncResult ar) { try { if (DestinationSocket == null) { return; } string str; DestinationSocket.EndConnect(ar); if (HttpRequestType.ToUpper().Equals("CONNECT")) { if (ClientSocket != null) { str = HttpVersion + " 200 Connection established\r\n\r\n"; ClientSocket.BeginSend(Encoding.ASCII.GetBytes(str), 0, str.Length, SocketFlags.None, OnOkSent, ClientSocket); } } else { str = RebuildQuery(); DestinationSocket.BeginSend(Encoding.ASCII.GetBytes(str), 0, str.Length, SocketFlags.None, OnQuerySent, DestinationSocket); } } catch { Dispose(); } }
///<summary>Called when we have received data from the local client.<br>Incoming data will immediately be forwarded to the remote host.</br></summary> ///<param name="ar">The result of the asynchronous operation.</param> protected void OnClientReceive(IAsyncResult ar) { try { int Ret = ClientSocket.EndReceive(ar); if (Ret <= 0) { Dispose(); return; } // RTRT HTTP/S S&R byte[] ReplacedBuffer = SearchandReplace(Buffer, "HTTP/1.1", "HTTP/1.0", Ret); ReplacedBuffer = SearchandReplace(ReplacedBuffer, "gzip,", "kaas,", Ret); foreach (string item in Mentalis.Proxy.WebProxy.SRList) { string[] parts = item.Split('\t'); ReplacedBuffer = SearchandReplace(ReplacedBuffer, parts[0], parts[1], Ret); } DestinationSocket.BeginSend(ReplacedBuffer, 0, Ret, SocketFlags.None, new AsyncCallback(this.OnRemoteSent), DestinationSocket); } catch (Exception ex) { //Console.WriteLine("Error: " + ex.Message); Dispose(); } }
///<summary>Called when we have received data from the local client.<br>Incoming data will immediately be forwarded to the remote host.</br></summary> ///<param name="ar">The result of the asynchronous operation.</param> protected void OnClientReceive(IAsyncResult ar) { try { int Ret = ClientSocket.EndReceive(ar); if (Ret <= 0) { Dispose(); return; } DestinationSocket.BeginSend(Buffer, 0, Ret, SocketFlags.None, new AsyncCallback(this.OnRemoteSent), DestinationSocket); } catch { Dispose(); } }
///<summary>Processes a PORT command, sent from the client.</summary> ///<param name="Input">The parameters of the PORT command.</param> private void ProcessPortCommand(string Input) { try { string [] Parts = Input.Split(','); if (Parts.Length == 6) { DataForward = new FtpDataConnection(); string Reply = DataForward.ProcessPort(new IPEndPoint(IPAddress.Parse(String.Join(".", Parts, 0, 4)), int.Parse(Parts[4]) * 256 + int.Parse(Parts[5]))); DestinationSocket.BeginSend(Encoding.ASCII.GetBytes(Reply), 0, Reply.Length, SocketFlags.None, new AsyncCallback(this.OnCommandSent), DestinationSocket); } } catch { Dispose(); } }
protected void OnClientReceive(IAsyncResult ar) { try { var ret = ClientSocket.EndReceive(ar); if (ret <= 0) { Dispose(); return; } DestinationSocket.BeginSend(Buffer, 0, ret, SocketFlags.None, OnRemoteSent, DestinationSocket); } catch { Dispose(); } }
private void OnConnected(IAsyncResult ar) { try { DestinationSocket.EndConnect(ar); string rq; if (HttpRequestType.ToUpper().Equals("CONNECT")) //HTTPS { rq = HttpVersion + " 200 Connection established\r\nProxy-Agent: xProxy Server\r\n\r\n"; ClientSocket.BeginSend(Encoding.ASCII.GetBytes(rq), 0, rq.Length, SocketFlags.None, new AsyncCallback(this.OnOkSent), ClientSocket); } else //Normal HTTP { rq = RebuildQuery(); DestinationSocket.BeginSend(Encoding.ASCII.GetBytes(rq), 0, rq.Length, SocketFlags.None, new AsyncCallback(this.OnQuerySent), DestinationSocket); } } catch { Dispose(); } }
public void OnClientReceive(IAsyncResult ar) { try { if (ClientSocket == null) { return; } int size = ClientSocket.EndReceive(ar); if (size > 0 && DestinationSocket != null) { DestinationSocket.BeginSend(Buffer, 0, size, SocketFlags.None, OnRemoteSent, DestinationSocket); } } catch { Dispose(); } }
///<summary>Called when we're connected to the requested remote host.</summary> ///<param name="ar">The result of the asynchronous operation.</param> private void OnConnected(IAsyncResult ar) { try { DestinationSocket.EndConnect(ar); if (OnConnectedEnd != null) { OnConnectedEnd.Invoke(DestinationSocket, this); } if (Cancel) { SendCancelledResponse(); return; } string rq; if (HttpRequestType.ToUpper().Equals("CONNECT")) { //HTTPS rq = HttpVersion + " 200 Connection established\r\nProxy-Agent: arachnode.net Proxy Server\r\n\r\n"; ClientSocket.BeginSend(Encoding.ASCII.GetBytes(rq), 0, rq.Length, SocketFlags.None, new AsyncCallback(OnOkSent), ClientSocket); } else { //Normal HTTP rq = RebuildQuery(); Console.WriteLine(rq); DestinationSocket.BeginSend(Encoding.ASCII.GetBytes(rq), 0, rq.Length, SocketFlags.None, new AsyncCallback(OnQuerySent), DestinationSocket); } } catch { Dispose(); } }
///<summary>Called when we have received data from the local client.<br>Incoming data will immediately be forwarded to the remote host.</br></summary> ///<param name="ar">The result of the asynchronous operation.</param> protected void OnClientReceive(IAsyncResult ar) { lock (_bufferLock) { try { if (ClientSocket == null) { return; } int Ret = ClientSocket.EndReceive(ar); if (Ret <= 0) { if (OnClientReceiveEnd != null) { if (_cachedClientBuffer != null) { //OnClientReceivedEnd.BeginInvoke(DestinationSocket, this, _cachedClientBufferHeaders, _cachedClientBuffer, true, null, null); OnClientReceiveEnd.Invoke(DestinationSocket, this, _cachedClientBufferHeaders, _cachedClientBuffer.Count == 0 ? null : _cachedClientBuffer, true); } else { OnClientReceiveEnd.Invoke(DestinationSocket, this, _cachedClientBufferHeaders, _cachedClientBuffer, true); } } if (OnRemoteSentEnd != null) { if (_cachedClientBufferHeaders != null) { //OnClientSentEnd.BeginInvoke(ClientSocket, this, _cachedClientBufferHeaders, _cachedClientBuffer, true, null, null); OnClientSentEnd.Invoke(ClientSocket, this, _cachedClientBufferHeaders, _cachedClientBuffer.Count == 0 ? null : _cachedClientBuffer, true); } else { OnClientSentEnd.Invoke(ClientSocket, this, _cachedClientBufferHeaders, _cachedClientBuffer, true); } } Dispose(); return; } _cachedClientBuffer.AddRange(Buffer); if (_cacheClientBufferHeaders) { _cachedClientBufferHeaders = Encoding.Default.GetString(_cachedClientBuffer.ToArray()).TrimEnd("\0".ToCharArray()); int index = _cachedClientBufferHeaders.IndexOf("\r\n\r\n"); if (index != -1) { _cachedClientBufferHeaders = _cachedClientBufferHeaders.Substring(0, index); _cacheClientBufferHeaders = false; if (Ret != index + 4) { _cachedClientBuffer = _cachedClientBuffer.GetRange(index + 4, _cachedClientBuffer.Count - index - 4); } else { _cachedClientBuffer.Clear(); _cachedRemoteBuffer.Clear(); } if (OnClientHeadersParsed != null) { OnClientHeadersParsed.Invoke(_cachedClientBufferHeaders); } } } if (OnClientReceiveEnd != null) { OnClientReceiveEnd.Invoke(ClientSocket, this, _cachedClientBufferHeaders, _cachedClientBuffer.Count == 0 ? null : _cachedClientBuffer, false); //OnClientReceiveEnd.BeginInvoke(ClientSocket, this, _cachedClientBufferHeaders, _cachedClientBuffer.Count == 0 ? null : _cachedClientBuffer, false, null, null); } if (!Cancel) { DestinationSocket.BeginSend(Buffer, 0, Ret, SocketFlags.None, new AsyncCallback(OnRemoteSent), DestinationSocket); } else { Dispose(); } } catch { Dispose(); } } }