public static Socks5Request FormBytes(byte[] buffer) { var request = new Socks5Request { Version = buffer[0], Command = buffer[1], RSV = buffer[2], }; request.Remote = SocksRemote.Parse(new Memory <byte>(buffer, 3, buffer.Length - 3), out var left); return(request); }
public static Socks5Response FromRequest(Socks5Request request, byte response, bool resolveRemote) { var resp = new Socks5Response { Version = request.Version, Response = response, RSV = request.RSV, Remote = resolveRemote? request.Remote.Resolve() : request.Remote }; return(resp); }
public static Socks5Response FromRequest(Socks5Request request, IPEndPoint bindedEndPoint) { var resp = new Socks5Response { Version = request.Version, Response = SocksResponse.SUCCESS, RSV = request.RSV, Remote = new SocksRemote() { AddressType = bindedEndPoint.AddressFamily == AddressFamily.InterNetworkV6 ? SocksAddressType.IPV6 : SocksAddressType.IPV4, Address = AddressUtils.GetVaildLocalIpAddress(bindedEndPoint), Port = (ushort)bindedEndPoint.Port, } }; return(resp); }
protected async Task <Socks5Request> ReadRequest() { var header = await ReadBytes(5); byte[] host; if (header[3] == SocksAddressType.IPV4) { host = await ReadBytes(5); } else if (header[3] == SocksAddressType.IPV6) { host = await ReadBytes(17); } else { host = await ReadBytes(header[4] + 2); } return(Socks5Request.FormBytes(header.Concat(host).ToArray())); }
private async Task <HostData> ProcessRequest() { _request = await ReadRequest(); if (_request.Command == SocksCommand.CONNECT) { _target = new HostData() { Address = _request.Remote.Address, Port = _request.Remote.Port }; Logger.LogInformation($"Connecting to {_target}, {Id}"); return(_target); } else if (_request.Command == SocksCommand.UDP) { Logger.LogInformation($"Configing udp relay, {Id}"); _target = new HostData() { Address = _request.Remote.Address, Port = _request.Remote.Port, Type = RemoteType.Udp }; BindUdp(_target); return(_target); } var resp = Socks5Response.FromRequest(_request, SocksResponse.CANNOT_CONNECT, _supportPrivoxy); var data = resp.ToBytes(); await WriteAsync(data); await FlushAsync(); _pipe.Reader.Complete(); throw new SocksException("Command not supported"); }