<commit_before>package socks5

import (
	"fmt"
	"io"
	"log"
	"net"
	"strings"
	"time"
)

const (
	connectCommand   = uint8(1)
	bindCommand      = uint8(2)
	associateCommand = uint8(3)
	ipv4Address      = uint8(1)
	fqdnAddress      = uint8(3)
	ipv6Address      = uint8(4)
)

const (
	successReply uint8 = iota
	serverFailure
	ruleFailure
	networkUnreachable
	hostUnreachable
	connectionRefused
	ttlExpired
	commandNotSupported
	addrTypeNotSupported
)

var (
	unrecognizedAddrType = fmt.Errorf("Unrecognized address type")
)

// AddressRewriter is used to rewrite a destination transparently
type AddressRewriter interface {
	Rewrite(addr *AddrSpec) *AddrSpec
}

// AddrSpec is used to return the target AddrSpec
// which may be specified as IPv4, IPv6, or a FQDN
type AddrSpec struct {
	FQDN string
	IP   net.IP
	Port int
}

type conn interface {
	Write([]byte) (int, error)
	RemoteAddr() net.Addr
}

func (a *AddrSpec) String() string {
	if a.FQDN != "" {
		return fmt.Sprintf("%s (%s):%d", a.FQDN, a.IP, a.Port)
	}
	return fmt.Sprintf("%s:%d", a.IP, a.Port)
}

// handleRequest is used for request processing after authentication
func (s *Server) handleRequest(conn conn, bufConn io.Reader) error {
	// Read the version byte
	header := []byte{0, 0, 0}
	if _, err := io.ReadAtLeast(bufConn, header, 3); err != nil {
		return fmt.Errorf("Failed to get command version: %v", err)
	}

	// Ensure we are compatible
	if header[0] != socks5Version {
		return fmt.Errorf("Unsupported command version: %v", header[0])
	}

	// Read in the destination address
	dest, err := readAddrSpec(bufConn)
	if err != nil {
		if err == unrecognizedAddrType {
			if err := sendReply(conn, addrTypeNotSupported, nil); err != nil {
				return fmt.Errorf("Failed to send reply: %v", err)
			}
		}
		return fmt.Errorf("Failed to read destination address: %v", err)
	}

		if s.AdvancedDial == false{

			// Resolve the address if we have a FQDN
			if dest.FQDN != "" {
				addr, err := s.config.Resolver.Resolve(dest.FQDN)
				if err != nil {
					if err := sendReply(conn, hostUnreachable, nil); err != nil {
						return fmt.Errorf("Failed to send reply: %v", err)
					}
					return fmt.Errorf("Failed to resolve destination '%v': %v", dest.FQDN, err)
				}
				dest.IP = addr
			}

			// Apply any address rewrites
			realDest := dest
			if s.config.Rewriter != nil {
				realDest = s.config.Rewriter.Rewrite(dest)
			}

			// Switch on the command
			switch header[1] {
			case connectCommand:
				return s.handleConnect(conn, bufConn, dest, realDest)
			case bindCommand:
				return s.handleBind(conn, bufConn, dest, realDest)
			case associateCommand:
				return s.handleAssociate(conn, bufConn, dest, realDest)
			default:
				if err := sendReply(conn, commandNotSupported, nil); err != nil {
					return fmt.Errorf("Failed to send reply: %v", err)
				}
				return fmt.Errorf("Unsupported command: %v", header[1])
			}
		}else{

			//AdvancedServe
			if(dest.FQDN==""){
				if dest.IP.To4()!=nil{
					//This is a ipv4 addr
					avdst:=dest.IP.String()+":"+strconv.Itoa(dest.Port)
				}else{
					//or a ipv6 addr
					avdst:="["+dest.IP.String()+"]:"+strconv.Itoa(dest.Port)
				}
			}

			s.AdvancedServe(,)

		}



}

// handleConnect is used to handle a connect command
func (s *Server) handleConnect(conn conn, bufConn io.Reader, dest, realDest *AddrSpec) error {
	// Check if this is allowed
	client := conn.RemoteAddr().(*net.TCPAddr)
	if !s.config.Rules.AllowConnect(realDest.IP, realDest.Port, client.IP, client.Port) {
		if err := sendReply(conn, ruleFailure, nil); err != nil {
			return fmt.Errorf("Failed to send reply: %v", err)
		}
		return fmt.Errorf("Connect to %v blocked by rules", dest)
	}

	// Attempt to connect
	addr := net.TCPAddr{IP: realDest.IP, Port: realDest.Port}
	target, err := net.DialTCP("tcp", nil, &addr)
	if err != nil {
		msg := err.Error()
		resp := hostUnreachable
		if strings.Contains(msg, "refused") {
			resp = connectionRefused<commit_msg>finialize sock5 docker part<commit_after>package socks5

import (
	"fmt"
	"io"
	"log"
	"net"
	"strings"
	"time"
)

const (
	connectCommand   = uint8(1)
	bindCommand      = uint8(2)
	associateCommand = uint8(3)
	ipv4Address      = uint8(1)
	fqdnAddress      = uint8(3)
	ipv6Address      = uint8(4)
)

const (
	successReply uint8 = iota
	serverFailure
	ruleFailure
	networkUnreachable
	hostUnreachable
	connectionRefused
	ttlExpired
	commandNotSupported
	addrTypeNotSupported
)

var (
	unrecognizedAddrType = fmt.Errorf("Unrecognized address type")
)

// AddressRewriter is used to rewrite a destination transparently
type AddressRewriter interface {
	Rewrite(addr *AddrSpec) *AddrSpec
}

// AddrSpec is used to return the target AddrSpec
// which may be specified as IPv4, IPv6, or a FQDN
type AddrSpec struct {
	FQDN string
	IP   net.IP
	Port int
}

type conn interface {
	Write([]byte) (int, error)
	RemoteAddr() net.Addr
}

func (a *AddrSpec) String() string {
	if a.FQDN != "" {
		return fmt.Sprintf("%s (%s):%d", a.FQDN, a.IP, a.Port)
	}
	return fmt.Sprintf("%s:%d", a.IP, a.Port)
}

// handleRequest is used for request processing after authentication
func (s *Server) handleRequest(conn conn, bufConn io.Reader) error {
	// Read the version byte
	header := []byte{0, 0, 0}
	if _, err := io.ReadAtLeast(bufConn, header, 3); err != nil {
		return fmt.Errorf("Failed to get command version: %v", err)
	}

	// Ensure we are compatible
	if header[0] != socks5Version {
		return fmt.Errorf("Unsupported command version: %v", header[0])
	}

	// Read in the destination address
	dest, err := readAddrSpec(bufConn)
	if err != nil {
		if err == unrecognizedAddrType {
			if err := sendReply(conn, addrTypeNotSupported, nil); err != nil {
				return fmt.Errorf("Failed to send reply: %v", err)
			}
		}
		return fmt.Errorf("Failed to read destination address: %v", err)
	}

		if s.AdvancedDial == false{

			// Resolve the address if we have a FQDN
			if dest.FQDN != "" {
				addr, err := s.config.Resolver.Resolve(dest.FQDN)
				if err != nil {
					if err := sendReply(conn, hostUnreachable, nil); err != nil {
						return fmt.Errorf("Failed to send reply: %v", err)
					}
					return fmt.Errorf("Failed to resolve destination '%v': %v", dest.FQDN, err)
				}
				dest.IP = addr
			}

			// Apply any address rewrites
			realDest := dest
			if s.config.Rewriter != nil {
				realDest = s.config.Rewriter.Rewrite(dest)
			}

			// Switch on the command
			switch header[1] {
			case connectCommand:
				return s.handleConnect(conn, bufConn, dest, realDest)
			case bindCommand:
				return s.handleBind(conn, bufConn, dest, realDest)
			case associateCommand:
				return s.handleAssociate(conn, bufConn, dest, realDest)
			default:
				if err := sendReply(conn, commandNotSupported, nil); err != nil {
					return fmt.Errorf("Failed to send reply: %v", err)
				}
				return fmt.Errorf("Unsupported command: %v", header[1])
			}
		}else{

			//AdvancedServe
			if(dest.FQDN==""){
				if dest.IP.To4()!=nil{
					//This is a ipv4 addr
					avdst:=dest.IP.String()+":"+strconv.Itoa(dest.Port)
				}else{
					//or a ipv6 addr
					avdst:="["+dest.IP.String()+"]:"+strconv.Itoa(dest.Port)
				}
			}

			connsti,ok:=s.AdvancedServe(avdst)

			if ok!=0 {
				sendReply(conn, "Bad Connection", nil)
				return nil
			}else{
				//send success
				sendReply(conn, successReply,nil)

				//is it OK to send nil as Local addr? if not I will create a fack addr

				go proxy("target", connsti, bufConn, errCh)
				go proxy("client", conn, connsti, errCh




			}


		}



}

// handleConnect is used to handle a connect command
func (s *Server) handleConnect(conn conn, bufConn io.Reader, dest, realDest *AddrSpec) error {
	// Check if this is allowed
	client := conn.RemoteAddr().(*net.TCPAddr)
	if !s.config.Rules.AllowConnect(realDest.IP, realDest.Port, client.IP, client.Port) {
		if err := sendReply(conn, ruleFailure, nil); err != nil {
			return fmt.Errorf("Failed to send reply: %v", err)
		}
		return fmt.Errorf("Connect to %v blocked by rules", dest)
	}

	// Attempt to connect
	addr := net.TCPAddr{IP: realDest.IP, Port: realDest.Port}
	target, err := net.DialTCP("tcp", nil, &addr)
	if err != nil {
		msg := err.Error()
		resp := hostUnreachable
		if strings.Contains(msg, "refused") {
			resp = connectionRefused
{'fraction_non_alphanumeric': 0.13214156299069935, 'fraction_numerical': 0.007488827153037806, 'mean_word_length': 3.3879173290937996, 'pattern_counts': {'":': 2, '<': 3, '<?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': '4770409', 'n_tokens_mistral': 3235, 'n_tokens_neox': 2855, 'n_words': 824}