<commit_before>package com.subgraph.orchid.socks;

import java.net.Socket;

import com.subgraph.orchid.TorException;

public class Socks5Request extends SocksRequest {
	final static int SOCKS5_VERSION = 5;
	final static int SOCKS5_AUTH_NONE = 0;
	final static int SOCKS5_COMMAND_CONNECT = 1;
	final static int SOCKS5_ADDRESS_IPV4 = 1;
	final static int SOCKS5_ADDRESS_HOSTNAME = 3;
	final static int SOCKS5_ADDRESS_IPV6 = 4;
	final static int SOCKS5_STATUS_SUCCESS = 0;
	final static int SOCKS5_STATUS_FAILURE = 1;
	final static int SOCKS5_STATUS_CONNECTION_REFUSED = 5;
	
	private int command;
	private int addressType;
	private byte[] addressBytes = new byte[0];
	private byte[] portBytes = new byte[0];
	
	Socks5Request(Socket socket) {
		super(socket);
	}
	
	public boolean isConnectRequest() {
		return command == SOCKS5_COMMAND_CONNECT;
	}
	
	private String addressBytesToHostname() {
		if(addressType != SOCKS5_ADDRESS_HOSTNAME)
			throw new TorException("SOCKS 4 request is not a hostname request");
		final StringBuilder sb = new StringBuilder();
		for(int i = 1; i < addressBytes.length; i++) {
			char c = (char) (addressBytes[i] & 0xFF);
			sb.append(c);
		}
		return sb.toString();
	}
	
	public void readRequest() throws SocksRequestException {
		processAuthentication();
		if(readByte() != SOCKS5_VERSION)
			throw new SocksRequestException();

		command = readByte();
		readByte(); // Reserved
		addressType = readByte();
		addressBytes = readAddressBytes();
		portBytes = readPortData();
		if(addressType == SOCKS5_ADDRESS_IPV4)
			setIPv4AddressData(addressBytes);
		else if(addressType == SOCKS5_ADDRESS_HOSTNAME)
			setHostname(addressBytesToHostname());
		else 
			throw new SocksRequestException();
		setPortData(portBytes);		
	}
	
	public void sendConnectionRefused() throws SocksRequestException {<commit_msg>Fail SOCKS request if authentication method cannot be negotiated<commit_after>package com.subgraph.orchid.socks;

import java.net.Socket;

import com.subgraph.orchid.TorException;

public class Socks5Request extends SocksRequest {
	final static int SOCKS5_VERSION = 5;
	final static int SOCKS5_AUTH_NONE = 0;
	final static int SOCKS5_COMMAND_CONNECT = 1;
	final static int SOCKS5_ADDRESS_IPV4 = 1;
	final static int SOCKS5_ADDRESS_HOSTNAME = 3;
	final static int SOCKS5_ADDRESS_IPV6 = 4;
	final static int SOCKS5_STATUS_SUCCESS = 0;
	final static int SOCKS5_STATUS_FAILURE = 1;
	final static int SOCKS5_STATUS_CONNECTION_REFUSED = 5;
	
	private int command;
	private int addressType;
	private byte[] addressBytes = new byte[0];
	private byte[] portBytes = new byte[0];
	
	Socks5Request(Socket socket) {
		super(socket);
	}
	
	public boolean isConnectRequest() {
		return command == SOCKS5_COMMAND_CONNECT;
	}
	
	private String addressBytesToHostname() {
		if(addressType != SOCKS5_ADDRESS_HOSTNAME)
			throw new TorException("SOCKS 4 request is not a hostname request");
		final StringBuilder sb = new StringBuilder();
		for(int i = 1; i < addressBytes.length; i++) {
			char c = (char) (addressBytes[i] & 0xFF);
			sb.append(c);
		}
		return sb.toString();
	}
	
	public void readRequest() throws SocksRequestException {
		if(!processAuthentication()) {
			throw new SocksRequestException("Failed to negotiate authentication");
		}
		if(readByte() != SOCKS5_VERSION)
			throw new SocksRequestException();

		command = readByte();
		readByte(); // Reserved
		addressType = readByte();
		addressBytes = readAddressBytes();
		portBytes = readPortData();
		if(addressType == SOCKS5_ADDRESS_IPV4)
			setIPv4AddressData(addressBytes);
		else if(addressType == SOCKS5_ADDRESS_HOSTNAME)
			setHostname(addressBytesToHostname());
		else 
			throw new SocksRequestException();
		setPortData(portBytes);		
	}
	
	public void sendConnectionRefused() throws SocksRequestException {
{'fraction_non_alphanumeric': 0.0908133719399842, 'fraction_numerical': 0.017899447222953408, 'mean_word_length': 5.089743589743589, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5444082', 'n_tokens_mistral': 1334, 'n_tokens_neox': 1210, 'n_words': 321}