<commit_before>package freedom

//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg freedom -path Proxy,Freedom

import (
	"context"
	"time"

	"v2ray.com/core"
	"v2ray.com/core/common"
	"v2ray.com/core/common/buf"
	"v2ray.com/core/common/dice"
	"v2ray.com/core/common/net"
	"v2ray.com/core/common/retry"
	"v2ray.com/core/common/session"
	"v2ray.com/core/common/signal"
	"v2ray.com/core/common/task"
	"v2ray.com/core/proxy"
	"v2ray.com/core/transport/internet"
)

// Handler handles Freedom connections.
type Handler struct {
	policyManager core.PolicyManager
	dns           core.DNSClient
	config        Config
}

// New creates a new Freedom handler.
func New(ctx context.Context, config *Config) (*Handler, error) {
	v := core.MustFromContext(ctx)
	f := &Handler{
		config:        *config,
		policyManager: v.PolicyManager(),
		dns:           v.DNSClient(),
	}

	return f, nil
}

func (h *Handler) policy() core.Policy {
	p := h.policyManager.ForLevel(h.config.UserLevel)
	if h.config.Timeout > 0 && h.config.UserLevel == 0 {
		p.Timeouts.ConnectionIdle = time.Duration(h.config.Timeout) * time.Second
	}
	return p
}

func (h *Handler) resolveIP(ctx context.Context, domain string) net.Address {
	if resolver, ok := proxy.ResolvedIPsFromContext(ctx); ok {
		ips := resolver.Resolve()
		if len(ips) == 0 {
			return nil
		}
		return ips[dice.Roll(len(ips))]
	}

	ips, err := h.dns.LookupIP(domain)
	if err != nil {
		newError("failed to get IP address for domain ", domain).Base(err).WriteToLog(session.ExportIDToError(ctx))
	}
	if len(ips) == 0 {
		return nil
	}
	return net.IPAddress(ips[dice.Roll(len(ips))])
}

// Process implements proxy.Outbound.
func (h *Handler) Process(ctx context.Context, link *core.Link, dialer proxy.Dialer) error {
	destination, _ := proxy.TargetFromContext(ctx)
	if h.config.DestinationOverride != nil {
		server := h.config.DestinationOverride.Server
		destination = net.Destination{
			Network: destination.Network,
			Address: server.Address.AsAddress(),
			Port:    net.Port(server.Port),
		}
	}
	newError("opening connection to ", destination).WriteToLog(session.ExportIDToError(ctx))

	input := link.Reader
	output := link.Writer

	var conn internet.Connection
	err := retry.ExponentialBackoff(5, 100).On(func() error {
		dialDest := destination
		if h.config.DomainStrategy == Config_USE_IP && dialDest.Address.Family().IsDomain() {
			ip := h.resolveIP(ctx, dialDest.Address.Domain())
			if ip != nil {
				dialDest = net.Destination{
					Network: dialDest.Network,<commit_msg>allow partial override in freedom<commit_after>package freedom

//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg freedom -path Proxy,Freedom

import (
	"context"
	"time"

	"v2ray.com/core"
	"v2ray.com/core/common"
	"v2ray.com/core/common/buf"
	"v2ray.com/core/common/dice"
	"v2ray.com/core/common/net"
	"v2ray.com/core/common/retry"
	"v2ray.com/core/common/session"
	"v2ray.com/core/common/signal"
	"v2ray.com/core/common/task"
	"v2ray.com/core/proxy"
	"v2ray.com/core/transport/internet"
)

// Handler handles Freedom connections.
type Handler struct {
	policyManager core.PolicyManager
	dns           core.DNSClient
	config        Config
}

// New creates a new Freedom handler.
func New(ctx context.Context, config *Config) (*Handler, error) {
	v := core.MustFromContext(ctx)
	f := &Handler{
		config:        *config,
		policyManager: v.PolicyManager(),
		dns:           v.DNSClient(),
	}

	return f, nil
}

func (h *Handler) policy() core.Policy {
	p := h.policyManager.ForLevel(h.config.UserLevel)
	if h.config.Timeout > 0 && h.config.UserLevel == 0 {
		p.Timeouts.ConnectionIdle = time.Duration(h.config.Timeout) * time.Second
	}
	return p
}

func (h *Handler) resolveIP(ctx context.Context, domain string) net.Address {
	if resolver, ok := proxy.ResolvedIPsFromContext(ctx); ok {
		ips := resolver.Resolve()
		if len(ips) == 0 {
			return nil
		}
		return ips[dice.Roll(len(ips))]
	}

	ips, err := h.dns.LookupIP(domain)
	if err != nil {
		newError("failed to get IP address for domain ", domain).Base(err).WriteToLog(session.ExportIDToError(ctx))
	}
	if len(ips) == 0 {
		return nil
	}
	return net.IPAddress(ips[dice.Roll(len(ips))])
}

// Process implements proxy.Outbound.
func (h *Handler) Process(ctx context.Context, link *core.Link, dialer proxy.Dialer) error {
	destination, _ := proxy.TargetFromContext(ctx)
	if h.config.DestinationOverride != nil {
		server := h.config.DestinationOverride.Server
		if server.Address != nil {
			destination.Address = server.Address.AsAddress()
		}
		if server.Port != 0 {
			destination.Port = net.Port(server.Port)
		}
	}
	newError("opening connection to ", destination).WriteToLog(session.ExportIDToError(ctx))

	input := link.Reader
	output := link.Writer

	var conn internet.Connection
	err := retry.ExponentialBackoff(5, 100).On(func() error {
		dialDest := destination
		if h.config.DomainStrategy == Config_USE_IP && dialDest.Address.Family().IsDomain() {
			ip := h.resolveIP(ctx, dialDest.Address.Domain())
			if ip != nil {
				dialDest = net.Destination{
					Network: dialDest.Network,
{'fraction_non_alphanumeric': 0.141994546162836, 'fraction_numerical': 0.00798597584729256, 'mean_word_length': 3.9138755980861246, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4117216', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1809, 'n_words': 369}