private ProxyToken HandleConnectRequest(DataAdapterToStream stm) { ProxyToken ret = null; int version = stm.ReadByte(); if (IsSupported(version)) { if (version == 4) { _logger.LogVerbose(CANAPE.Net.Properties.Resources.SocksProxyServer_NewV4ConnectionLog); ret = HandleSocksv4Request(stm); } else if (version == 5) { _logger.LogVerbose(CANAPE.Net.Properties.Resources.SocksProxyServer_NewV5ConnectionLog); ret = HandleSocksv5Request(stm); } } else { _logger.LogError(CANAPE.Net.Properties.Resources.SocksProxyServer_UnsupportedVersionLog, version); } return(ret); }
private bool HandleV5Auth(DataAdapterToStream stm) { int authCount = stm.ReadByte(); bool foundAuth = false; if (authCount > 0) { byte[] authModes = GeneralUtils.ReadBytes(stm, authCount); foreach (byte b in authModes) { if (b == 0) { foundAuth = true; break; } } } byte[] ret = new byte[2]; ret[0] = 5; if (foundAuth) { ret[1] = 0; } else { ret[1] = 0xFF; } stm.Write(ret, 0, ret.Length); return(foundAuth); }
private ProxyToken HandleSocksv4Request(DataAdapterToStream stm) { SocksProxyToken ret = null; int req = stm.ReadByte(); ushort port = ReadUShort(stm); byte[] addrBytes = GeneralUtils.ReadBytes(stm, 4); IPAddress addr = new IPAddress(addrBytes); string addrName = addr.ToString(); // Discard username ReadZString(stm); if ((addrBytes[0] == 0) && (addrBytes[1] == 0) && (addrBytes[2] == 0) && (addrBytes[3] != 0)) { StringBuilder builder = new StringBuilder(); _logger.LogVerbose(CANAPE.Net.Properties.Resources.SocksProxyServer_V4AUsed); addrName = ReadZString(stm); addr = null; } if (req == 1) { _logger.LogVerbose(CANAPE.Net.Properties.Resources.SocksProxyServer_V4ConnectionLog, addrName, port); ret = new SocksProxyToken(addr, addrName, port, IpProxyToken.IpClientType.Tcp, false, stm, 4); } return(ret); }
private ProxyToken HandleV5RequestData(DataAdapterToStream stm) { SocksProxyToken ret = null; IPAddress addr = null; string addrName = null; bool ipv6 = false; ushort port = 0; int ver = stm.ReadByte(); int code = stm.ReadByte(); stm.ReadByte(); // Reserved int type = stm.ReadByte(); if ((ver == 5) && (code == 1)) { byte[] data = null; switch (type) { case 1: // IPv4 { data = GeneralUtils.ReadBytes(stm, 4); addr = new IPAddress(data); addrName = addr.ToString(); } break; case 3: // Domain name { int nameLen = stm.ReadByte(); if (nameLen > 0) { data = GeneralUtils.ReadBytes(stm, nameLen); addrName = Encoding.ASCII.GetString(data); } } break; case 4: // IPv6 data = GeneralUtils.ReadBytes(stm, 16); addr = new IPAddress(data); addrName = addr.ToString(); ipv6 = true; break; default: break; } port = ReadUShort(stm); if ((addrName != null) && (port > 0)) { _logger.LogVerbose(CANAPE.Net.Properties.Resources.SocksProxyServer_V5ConnectionLog, addrName, port); ret = new SocksProxyToken(addr, addrName, port, IpProxyToken.IpClientType.Tcp, ipv6, stm, 5); } } return(ret); }