private void ConnectVersion5(Stream stm, IpProxyToken token, Logger logger) { byte[] req = new byte[3] { 5, 1, 0 }; stm.Write(req, 0, req.Length); byte[] resp = GeneralUtils.ReadBytes(stm, 2); if ((resp[0] == 5) && (resp[1] == 0)) { List<byte> connect = new List<byte>(); connect.Add(5); connect.Add(1); connect.Add(0); if (_sendHostName && !String.IsNullOrWhiteSpace(token.Hostname)) { connect.Add(3); connect.Add((byte)token.Hostname.Length); connect.AddRange(new BinaryEncoding().GetBytes(token.Hostname)); } else if(token.Address != null) { if (token.Address.AddressFamily == AddressFamily.InterNetwork) { connect.Add(1); } else if (token.Address.AddressFamily == AddressFamily.InterNetworkV6) { connect.Add(4); } else { throw new ArgumentException(CANAPE.Net.Properties.Resources.SocksProxyClient_InvalidProxyToken); } connect.AddRange(token.Address.GetAddressBytes()); } else { throw new ArgumentException(CANAPE.Net.Properties.Resources.SocksProxyClient_InvalidProxyToken2); } connect.Add((byte)(token.Port >> 8)); connect.Add((byte)(token.Port & 0xFF)); stm.Write(connect.ToArray(), 0, connect.Count); if (stm.ReadByte() != 5) { logger.LogError(CANAPE.Net.Properties.Resources.SocksProxyClient_InvalidV5Response); } int status = stm.ReadByte(); // Read out the rest of the data stm.ReadByte(); int addrType = stm.ReadByte(); switch (addrType) { case 1: GeneralUtils.ReadBytes(stm, 4); break; case 3: int len = stm.ReadByte(); if (len < 0) { throw new EndOfStreamException(CANAPE.Net.Properties.Resources.SocksProxyClient_EosInDomain); } GeneralUtils.ReadBytes(stm, len); break; case 4: GeneralUtils.ReadBytes(stm, 16); break; default: throw new ArgumentException(CANAPE.Net.Properties.Resources.SocksProxyClient_InvalidAddrType); } // Port GeneralUtils.ReadBytes(stm, 2); if (status == 0) { token.Status = NetStatusCodes.Success; } else { token.Status = NetStatusCodes.ConnectFailure; } } else { logger.LogError(CANAPE.Net.Properties.Resources.SocksProxyClient_InvalidV5Response2, resp[0], resp[1]); token.Status = NetStatusCodes.ConnectFailure; } }
private static void ConnectWithIpProxyToken(Stream stm, IpProxyToken token, Logger logger) { string hostname = token.Hostname != null ? token.Hostname : token.Address.ToString(); string req = String.Format("CONNECT {0}:{1} HTTP/1.0\r\n\r\n", hostname, token.Port); byte[] reqBytes = Encoding.ASCII.GetBytes(req); stm.Write(reqBytes, 0, reqBytes.Length); List<string> headers = new List<string>(); // Read out response headers while (true) { string nextLine = GeneralUtils.ReadLine(stm); headers.Add(nextLine); if (nextLine.Trim().Length == 0) { break; } } if (headers.Count > 0) { string[] vals = headers[0].Split(' '); int res = 0; if (vals.Length >= 2) { if (int.TryParse(vals[1], out res) && (res == 200)) { token.Status = NetStatusCodes.Success; } else { logger.LogError(CANAPE.Net.Properties.Resources.HttpProxyClient_ErrorOnConnect, res, hostname, token.Port); } } else { logger.LogError(CANAPE.Net.Properties.Resources.HttpProxyClient_InvalidResponse); } } else { logger.LogError(CANAPE.Net.Properties.Resources.HttpProxyClient_NoResponse); } }