<commit_before>package conf

import (
	"encoding/json"
	"errors"
	"strings"

	"io"
	"v2ray.com/core"
	"v2ray.com/core/common/loader"
	v2net "v2ray.com/core/common/net"
)

var (
	inboundConfigLoader = NewJSONConfigLoader(ConfigCreatorCache{
		"dokodemo-door": func() interface{} { return new(DokodemoConfig) },
		"http":          func() interface{} { return new(HttpServerConfig) },
		"shadowsocks":   func() interface{} { return new(ShadowsocksServerConfig) },
		"socks":         func() interface{} { return new(SocksServerConfig) },
		"vmess":         func() interface{} { return new(VMessInboundConfig) },
	}, "protocol", "settings")

	outboundConfigLoader = NewJSONConfigLoader(ConfigCreatorCache{
		"blackhole":   func() interface{} { return new(BlackholeConfig) },
		"freedom":     func() interface{} { return new(FreedomConfig) },
		"shadowsocks": func() interface{} { return new(ShadowsocksClientConfig) },
		"vmess":       func() interface{} { return new(VMessOutboundConfig) },
	}, "protocol", "settings")
)

type InboundConnectionConfig struct {
	Port          uint16          `json:"port"`
	Listen        *Address        `json:"listen"`
	Protocol      string          `json:"protocol"`
	StreamSetting *StreamConfig   `json:"streamSettings"`
	Settings      json.RawMessage `json:"settings"`
	AllowPassive  bool            `json:"allowPassive"`
}

func (this *InboundConnectionConfig) Build() (*core.InboundConnectionConfig, error) {
	config := new(core.InboundConnectionConfig)
	config.PortRange = &v2net.PortRange{
		From: uint32(this.Port),
		To:   uint32(this.Port),
	}
	if this.Listen != nil {
		if this.Listen.Family().IsDomain() {
			return nil, errors.New("Point: Unable to listen on domain address: " + this.Listen.Domain())
		}
		config.ListenOn = this.Listen.Build()
	}
	if this.StreamSetting != nil {
		ts, err := this.StreamSetting.Build()
		if err != nil {
			return nil, err
		}
		config.StreamSettings = ts
	}
	config.AllowPassiveConnection = this.AllowPassive

	jsonConfig, err := inboundConfigLoader.LoadWithID(this.Settings, this.Protocol)
	if err != nil {
		return nil, errors.New("Failed to load inbound config: " + err.Error())
	}
	ts, err := jsonConfig.(Buildable).Build()
	if err != nil {
		return nil, err
	}
	config.Settings = ts
	return config, nil
}

type OutboundConnectionConfig struct {
	Protocol      string          `json:"protocol"`
	SendThrough   *Address        `json:"sendThrough"`
	StreamSetting *StreamConfig   `json:"streamSettings"`
	ProxySettings *ProxyConfig    `json:"proxySettings"`
	Settings      json.RawMessage `json:"settings"`
}

func (this *OutboundConnectionConfig) Build() (*core.OutboundConnectionConfig, error) {
	config := new(core.OutboundConnectionConfig)
	rawConfig, err := outboundConfigLoader.LoadWithID(this.Settings, this.Protocol)
	if err != nil {
		return nil, errors.New("Failed to parse outbound config: " + err.Error())
	}
	ts, err := rawConfig.(Buildable).Build()
	if err != nil {
		return nil, err
	}
	config.Settings = ts

	if this.SendThrough != nil {
		address := this.SendThrough
		if address.Family().IsDomain() {
			return nil, errors.New("Point: Unable to send through: " + address.String())
		}
		config.SendThrough = address.Build()
	}
	if this.StreamSetting != nil {
		ss, err := this.StreamSetting.Build()
		if err != nil {
			return nil, err
		}
		config.StreamSettings = ss
	}
	if this.ProxySettings != nil {
		ps, err := this.ProxySettings.Build()
		if err != nil {
			return nil, errors.New("Outbound: invalid proxy settings: " + err.Error())
		}
		config.ProxySettings = ps
	}
	return config, nil
}

type InboundDetourAllocationConfig struct {
	Strategy    string  `json:"strategy"`<commit_msg>tag for default inbound and outbound<commit_after>package conf

import (
	"encoding/json"
	"errors"
	"strings"

	"io"
	"v2ray.com/core"
	"v2ray.com/core/common/loader"
	v2net "v2ray.com/core/common/net"
)

var (
	inboundConfigLoader = NewJSONConfigLoader(ConfigCreatorCache{
		"dokodemo-door": func() interface{} { return new(DokodemoConfig) },
		"http":          func() interface{} { return new(HttpServerConfig) },
		"shadowsocks":   func() interface{} { return new(ShadowsocksServerConfig) },
		"socks":         func() interface{} { return new(SocksServerConfig) },
		"vmess":         func() interface{} { return new(VMessInboundConfig) },
	}, "protocol", "settings")

	outboundConfigLoader = NewJSONConfigLoader(ConfigCreatorCache{
		"blackhole":   func() interface{} { return new(BlackholeConfig) },
		"freedom":     func() interface{} { return new(FreedomConfig) },
		"shadowsocks": func() interface{} { return new(ShadowsocksClientConfig) },
		"vmess":       func() interface{} { return new(VMessOutboundConfig) },
	}, "protocol", "settings")
)

type InboundConnectionConfig struct {
	Port          uint16          `json:"port"`
	Listen        *Address        `json:"listen"`
	Protocol      string          `json:"protocol"`
	StreamSetting *StreamConfig   `json:"streamSettings"`
	Settings      json.RawMessage `json:"settings"`
	AllowPassive  bool            `json:"allowPassive"`
	Tag           string          `json:"tag"`
}

func (this *InboundConnectionConfig) Build() (*core.InboundConnectionConfig, error) {
	config := new(core.InboundConnectionConfig)
	config.PortRange = &v2net.PortRange{
		From: uint32(this.Port),
		To:   uint32(this.Port),
	}
	if this.Listen != nil {
		if this.Listen.Family().IsDomain() {
			return nil, errors.New("Point: Unable to listen on domain address: " + this.Listen.Domain())
		}
		config.ListenOn = this.Listen.Build()
	}
	if this.StreamSetting != nil {
		ts, err := this.StreamSetting.Build()
		if err != nil {
			return nil, err
		}
		config.StreamSettings = ts
	}
	config.AllowPassiveConnection = this.AllowPassive

	jsonConfig, err := inboundConfigLoader.LoadWithID(this.Settings, this.Protocol)
	if err != nil {
		return nil, errors.New("Failed to load inbound config: " + err.Error())
	}
	ts, err := jsonConfig.(Buildable).Build()
	if err != nil {
		return nil, err
	}
	config.Settings = ts
	if len(this.Tag) > 0 {
		config.Tag = this.Tag
	}
	return config, nil
}

type OutboundConnectionConfig struct {
	Protocol      string          `json:"protocol"`
	SendThrough   *Address        `json:"sendThrough"`
	StreamSetting *StreamConfig   `json:"streamSettings"`
	ProxySettings *ProxyConfig    `json:"proxySettings"`
	Settings      json.RawMessage `json:"settings"`
	Tag           string          `json:"tag"`
}

func (this *OutboundConnectionConfig) Build() (*core.OutboundConnectionConfig, error) {
	config := new(core.OutboundConnectionConfig)
	rawConfig, err := outboundConfigLoader.LoadWithID(this.Settings, this.Protocol)
	if err != nil {
		return nil, errors.New("Failed to parse outbound config: " + err.Error())
	}
	ts, err := rawConfig.(Buildable).Build()
	if err != nil {
		return nil, err
	}
	config.Settings = ts

	if this.SendThrough != nil {
		address := this.SendThrough
		if address.Family().IsDomain() {
			return nil, errors.New("Point: Unable to send through: " + address.String())
		}
		config.SendThrough = address.Build()
	}
	if this.StreamSetting != nil {
		ss, err := this.StreamSetting.Build()
		if err != nil {
			return nil, err
		}
		config.StreamSettings = ss
	}
	if this.ProxySettings != nil {
		ps, err := this.ProxySettings.Build()
		if err != nil {
			return nil, errors.New("Outbound: invalid proxy settings: " + err.Error())
		}
		config.ProxySettings = ps
	}
	if len(this.Tag) > 0 {
		config.Tag = this.Tag
	}
	return config, nil
}

type InboundDetourAllocationConfig struct {
	Strategy    string  `json:"strategy"`
{'fraction_non_alphanumeric': 0.13647740745619813, 'fraction_numerical': 0.0031616387827690688, 'mean_word_length': 3.860435339308579, 'pattern_counts': {'":': 18, '<': 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': '7016430', 'n_tokens_mistral': 2512, 'n_tokens_neox': 2387, 'n_words': 564}