///<summary>Returns text information about this HttpClient object.</summary> ///<returns>A string representing this HttpClient object.</returns> ///<param name="WithUrl">Specifies whether or not to include information about the requested URL.</param> public string ToString(bool WithUrl) { string Ret; try { if (DestinationSocket == null || DestinationSocket.RemoteEndPoint == null) { Ret = "Incoming HTTP connection from " + ((IPEndPoint)ClientSocket.RemoteEndPoint).Address.ToString(); } else { Ret = "HTTP connection from " + ((IPEndPoint)ClientSocket.RemoteEndPoint).Address.ToString() + " to " + ((IPEndPoint)DestinationSocket.RemoteEndPoint).Address.ToString() + " on port " + ((IPEndPoint)DestinationSocket.RemoteEndPoint).Port.ToString(); } if (HeaderFields != null && HeaderFields.ContainsKey("Host") && RequestedPath != null) { Ret += "\r\n" + " requested URL: http://" + HeaderFields["Host"] + RequestedPath; } } catch { Ret = "HTTP Connection"; } return(Ret); }
public string ToString(bool withUrl) { string str; try { if ((DestinationSocket == null) || (DestinationSocket.RemoteEndPoint == null)) { str = "Incoming HTTP connection from " + ((IPEndPoint)ClientSocket.RemoteEndPoint).Address; } else { str = "HTTP connection from " + ((IPEndPoint)ClientSocket.RemoteEndPoint).Address + " to " + ((IPEndPoint)DestinationSocket.RemoteEndPoint).Address + " on port " + ((IPEndPoint)DestinationSocket.RemoteEndPoint).Port.ToString(CultureInfo.InvariantCulture); } if (((HeaderFields != null) && HeaderFields.ContainsKey("Host")) && (RequestedPath != null)) { str = str + "\r\n requested URL: http://" + HeaderFields["Host"] + RequestedPath; } } catch { str = "HTTP Connection"; } return(str); }
private void ProcessQuery(string query) { HeaderFields = ParseQuery(query); if (HeaderFields == null || !HeaderFields.ContainsKey("Host")) { SendBadRequest(); return; } int port; string host; int ret; if (HttpRequestType.ToUpper().Equals("CONNECT")) { //HTTPS ret = RequestedPath.IndexOf(":", StringComparison.Ordinal); if (ret >= 0) { host = RequestedPath.Substring(0, ret); port = RequestedPath.Length > ret + 1 ? int.Parse(RequestedPath.Substring(ret + 1)) : 443; } else { host = RequestedPath; port = 443; } } else { //HTTP ret = HeaderFields["Host"].IndexOf(":", StringComparison.Ordinal); if (ret > 0) { host = HeaderFields["Host"].Substring(0, ret); port = int.Parse(HeaderFields["Host"].Substring(ret + 1)); } else { host = HeaderFields["Host"]; port = 80; } if (HttpRequestType.ToUpper().Equals("POST")) { var index = query.IndexOf("\r\n\r\n", StringComparison.Ordinal); _httpPost = query.Substring(index + 4); } } try { var destinationEndPoint = new IPEndPoint(Dns.GetHostEntry(host).AddressList[0], port); DestinationSocket = new Socket(destinationEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); if (HeaderFields.ContainsKey("Proxy-Connection") && HeaderFields["Proxy-Connection"].ToLower().Equals("keep-alive")) { DestinationSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1); } DestinationSocket.BeginConnect(destinationEndPoint, OnConnected, DestinationSocket); } catch { SendBadRequest(); } }
private bool IsValidQuery(string query) { if (String.IsNullOrEmpty(query)) { return(false); } HeaderFields = ParseQuery(query); return(!HttpRequestType.ToUpper().Equals("POST") || !HeaderFields.ContainsKey("Content-Length")); }
private void ProcessQuery(string Query) { HeaderFields = ParseQuery(Query); if (HeaderFields == null || !HeaderFields.ContainsKey("Host")) { SendBadRequest(); return; } int Port; string Host; int Ret; if (HttpRequestType.ToUpper().Equals("CONNECT")) //HTTPS { Ret = RequestedPath.IndexOf(":"); if (Ret >= 0) { Host = RequestedPath.Substring(0, Ret); if (RequestedPath.Length > Ret + 1) { Port = int.Parse(RequestedPath.Substring(Ret + 1)); } else { Port = 443; } } else { Host = RequestedPath; Port = 443; } } else //Normal HTTP { Ret = ((string)HeaderFields["Host"]).IndexOf(":"); if (Ret > 0) { Host = ((string)HeaderFields["Host"]).Substring(0, Ret); Port = int.Parse(((string)HeaderFields["Host"]).Substring(Ret + 1)); } else { Host = (string)HeaderFields["Host"]; Port = 80; } if (HttpRequestType.ToUpper().Equals("POST")) { int index = Query.IndexOf("\r\n\r\n"); m_HttpPost = Query.Substring(index + 4); } } try { IPEndPoint DestinationEndPoint = new IPEndPoint(Dns.Resolve(Host).AddressList[0], Port); DestinationSocket = new Socket(DestinationEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); if (HeaderFields.ContainsKey("Proxy-Connection") && HeaderFields["Proxy-Connection"].ToLower().Equals("keep-alive")) { DestinationSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1); } DestinationSocket.BeginConnect(DestinationEndPoint, new AsyncCallback(this.OnConnected), DestinationSocket); } catch { SendBadRequest(); return; } }
private void ProcessQuery(string Query) { HeaderFields = ParseQuery(Query); if (HeaderFields == null || !HeaderFields.ContainsKey("Host")) { SendBadRequest(); return; } if (UserValidator != null) { var authValid = true; try { if (HeaderFields.ContainsKey("Proxy-Authorization") == false) { authValid = false; } else { var authHeader = HeaderFields["Proxy-Authorization"]; if (authHeader.IndexOf("Basic") > -1) { var base64String = authHeader.Replace("Basic", "").Trim(); var strUserPass = Encoding.UTF8.GetString(Convert.FromBase64String(base64String)); var authParam = strUserPass.Split(':'); var username = authParam[0]; var password = authParam[1]; authValid = UserValidator.IsValid(username, password); } else { authValid = false; } } } catch (Exception ex) { Log.Write(MethodInfo.GetCurrentMethod(), ex); authValid = false; } if (authValid == false) { SendAuthenticationFailed(); return; } } int Port; string Host; int Ret; if (HttpRequestType.ToUpper().Equals("CONNECT")) { //HTTPS Ret = RequestedPath.IndexOf(":"); if (Ret >= 0) { Host = RequestedPath.Substring(0, Ret); if (RequestedPath.Length > Ret + 1) { Port = int.Parse(RequestedPath.Substring(Ret + 1)); } else { Port = 443; } } else { Host = RequestedPath; Port = 443; } } else { //Normal HTTP Ret = ((string)HeaderFields["Host"]).IndexOf(":"); if (Ret > 0) { Host = ((string)HeaderFields["Host"]).Substring(0, Ret); Port = int.Parse(((string)HeaderFields["Host"]).Substring(Ret + 1)); } else { Host = (string)HeaderFields["Host"]; Port = 80; } if (HttpRequestType.ToUpper().Equals("POST")) { int index = Query.IndexOf("\r\n\r\n"); m_HttpPost = Query.Substring(index + 4); } } try { IPEndPoint DestinationEndPoint = new IPEndPoint(Dns.GetHostEntry(Host).AddressList[0], Port); DestinationSocket = new Socket(DestinationEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); if (HeaderFields.ContainsKey("Proxy-Connection") && HeaderFields["Proxy-Connection"].ToLower().Equals("keep-alive")) { DestinationSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1); } DestinationSocket.BeginConnect(DestinationEndPoint, new AsyncCallback(this.OnConnected), DestinationSocket); } catch { SendBadRequest(); return; } }
protected override void ProcessQuery(string Query) { HeaderFields = ParseQuery(Query); if (HeaderFields == null || !HeaderFields.ContainsKey("Host")) { SendBadRequest(); return; } int Port; string Host; int Ret; if (HttpRequestType.ToUpper().Equals("CONNECT")) { //HTTPS Ret = RequestedPath.IndexOf(":"); if (Ret >= 0) { Host = RequestedPath.Substring(0, Ret); if (RequestedPath.Length > Ret + 1) { Port = int.Parse(RequestedPath.Substring(Ret + 1)); } else { Port = 443; } } else { Host = RequestedPath; Port = 443; } } else { //Normal HTTP Ret = ((string)HeaderFields["Host"]).IndexOf(":"); if (Ret > 0) { Host = ((string)HeaderFields["Host"]).Substring(0, Ret); Port = int.Parse(((string)HeaderFields["Host"]).Substring(Ret + 1)); } else { Host = (string)HeaderFields["Host"]; Port = 80; } if (HttpRequestType.ToUpper().Equals("POST")) { int index = Query.IndexOf("\r\n\r\n"); m_HttpPost = Query.Substring(index + 4); } } try { DestinationSocket = new ProxySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ((ProxySocket)DestinationSocket).ProxyEndPoint = new IPEndPoint(Config.SocksAddress, Config.SocksPort); ((ProxySocket)DestinationSocket).ProxyUser = Config.Username; ((ProxySocket)DestinationSocket).ProxyPass = Config.Password; ((ProxySocket)DestinationSocket).ProxyType = Config.ProxyType; if (HeaderFields.ContainsKey("Proxy-Connection") && HeaderFields["Proxy-Connection"].ToLower().Equals("keep-alive")) { ((ProxySocket)DestinationSocket).SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1); } ((ProxySocket)DestinationSocket).BeginConnect(Host, Port, new AsyncCallback(this.OnProxyConnected), DestinationSocket); } catch { SendBadRequest(); return; } }
private void QueryHandle(string query) { HeaderFields = ParseQuery(query); if ((HeaderFields == null) || !HeaderFields.ContainsKey("Host")) { SendBadRequest(); } else { int num; string requestedPath; int index; if (HttpRequestType.ToUpper().Equals("CONNECT")) { index = RequestedPath.IndexOf(":"); if (index >= 0) { requestedPath = RequestedPath.Substring(0, index); num = RequestedPath.Length > (index + 1) ? int.Parse(RequestedPath.Substring(index + 1)) : 443; } else { requestedPath = RequestedPath; num = 80; } } else { index = HeaderFields["Host"].IndexOf(":"); if (index > 0) { requestedPath = HeaderFields["Host"].Substring(0, index); num = int.Parse(HeaderFields["Host"].Substring(index + 1)); } else { requestedPath = HeaderFields["Host"]; num = 80; } if (HttpRequestType.ToUpper().Equals("POST")) { int tempnum = query.IndexOf("\r\n\r\n"); _mHttpPost = query.Substring(tempnum + 4); } } var localFile = String.Empty; if (MonitorLog.RegexUrl(RequestedUrl)) { localFile = UrlOperate.MatchFile(RequestedUrl); } _uinfo.PsnUrl = string.IsNullOrEmpty(_uinfo.PsnUrl) ? RequestedUrl : _uinfo.PsnUrl; if (!HttpRequestType.ToUpper().Equals("CONNECT") && localFile != string.Empty && File.Exists(localFile)) { _uinfo.ReplacePath = localFile; _updataUrlLog(_uinfo); SendLocalFile(localFile, HeaderFields.ContainsKey("Range") ? HeaderFields["Range"] : null, HeaderFields.ContainsKey("Proxy-Connection") ? HeaderFields["Proxy-Connection"] : null); } else { try { bool iscdn; IPAddress hostIp = CdnOperate.GetCdnAddress(requestedPath, out iscdn); var remoteEp = new IPEndPoint(hostIp, num); DestinationSocket = new Socket(remoteEp.AddressFamily, SocketType.Stream, ProtocolType.Tcp); if (HeaderFields.ContainsKey("Proxy-Connection") && HeaderFields["Proxy-Connection"].ToLower().Equals("keep-alive")) { DestinationSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1); } DestinationSocket.BeginConnect(remoteEp, OnConnected, DestinationSocket); _uinfo.Host = hostIp.ToString(); _uinfo.IsCdn = iscdn; _uinfo.ReplacePath = string.Empty; _updataUrlLog(_uinfo); } catch { SendBadRequest(); } } } }
///<summary>Processes a specified query and connects to the requested HTTP web server.</summary> ///<param name="Query">A string containing the query to process.</param> ///<remarks>If there's an error while processing the HTTP request or when connecting to the remote server, the Proxy sends a "400 - Bad Request" error to the client.</remarks> private void ProcessQuery(string Query) { HeaderFields = ParseQuery(Query); if (HeaderFields == null || !HeaderFields.ContainsKey("Host")) { SendBadRequest(); return; } #region Parse destination address and port int Port; string Host; int Ret; if (HttpRequestType.ToUpper().Equals("CONNECT")) //HTTPS { isThisSSL = true; this.isPayloadSecure = true; Ret = RequestedPath.IndexOf(":"); if (Ret >= 0) { Host = RequestedPath.Substring(0, Ret); if (RequestedPath.Length > Ret + 1) { Port = int.Parse(RequestedPath.Substring(Ret + 1)); CurrentHTTPSport = Port; } else { Port = 443; CurrentHTTPSport = 443; } } else { Host = RequestedPath; Port = 443; CurrentHTTPSport = 443; } } else { isThisSSL = false; Ret = ((string)HeaderFields["Host"]).IndexOf(":"); if (Ret > 0) { Host = ((string)HeaderFields["Host"]).Substring(0, Ret); Port = int.Parse(((string)HeaderFields["Host"]).Substring(Ret + 1)); CurrentHTTPSport = Port; } else { Host = (string)HeaderFields["Host"]; Port = 80; CurrentHTTPSport = 80; } if (HttpRequestType.ToUpper().Equals("GET") == false) { int index = Query.IndexOf("\r\n\r\n"); m_HttpPost = Query.Substring(index + 4); } } #endregion #region Create destination socket try { IPEndPoint DestinationEndPoint = new IPEndPoint(Dns.Resolve(Host).AddressList[0], Port); if (this.isPayloadSecure) { SecurityOptions options = new SecurityOptions( SecureProtocol.Ssl3 | SecureProtocol.Tls1, // use SSL3 or TLS1 null, // not required for SSL client ConnectionEnd.Client, // this is the client side CredentialVerification.None, // do not check the certificate -- this should not be used in a real-life application :-) null, // not used with automatic certificate verification "www.bogus.com", // this is the common name of the Microsoft web server SecurityFlags.Default, // use the default security flags SslAlgorithms.SECURE_CIPHERS, // only use secure ciphers null); // do not process certificate requests. // This line for intercept proxy DestinationSocket = new SecureSocket(DestinationEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp, options); // This line for pass-through proxy //DestinationSocket = new SecureSocket(DestinationEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); } else { DestinationSocket = new SecureSocket(DestinationEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); } if (HeaderFields.ContainsKey("Proxy-Connection") && HeaderFields["Proxy-Connection"].ToLower().Equals("keep-alive")) { DestinationSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1); } DestinationSocket.BeginConnect(DestinationEndPoint, new AsyncCallback(this.OnConnected), DestinationSocket); } catch { SendBadRequest(); return; } #endregion }