func createProxyConfig(options Options) proxy.Config {

	listenIP := net.ParseIP(options.ListenAddr)
	if listenIP == nil {
		log.Fatalf("cannot parse %s", options.ListenAddr)
	}

	// Init listen addresses and upstreams
	listenUDPAddr := &net.UDPAddr{Port: options.ListenPort, IP: listenIP}
	listenTCPAddr := &net.TCPAddr{Port: options.ListenPort, IP: listenIP}
	upstreams := make([]upstream.Upstream, 0)

	for i, u := range options.Upstreams {
		dnsUpstream, err := upstream.AddressToUpstream(u, upstream.Options{Bootstrap: options.BootstrapDNS, Timeout: defaultTimeout})
		if err != nil {
			log.Fatalf("cannot prepare the upstream %s (%s): %s", u, options.BootstrapDNS, err)
		}
		log.Printf("Upstream %d: %s", i, dnsUpstream.Address())
		upstreams = append(upstreams, dnsUpstream)
	}

	// Create the config
	config := proxy.Config{
		UDPListenAddr: listenUDPAddr,
		TCPListenAddr: listenTCPAddr,
		Upstreams:     upstreams,
		Ratelimit:     options.Ratelimit,
		CacheEnabled:  options.Cache,
		RefuseAny:     options.RefuseAny,
		AllServers:    options.AllServers,
	}

	if options.Fallbacks != nil {
		fallbacks := []upstream.Upstream{}
		for i, f := range options.Fallbacks {
			fallback, err := upstream.AddressToUpstream(f, upstream.Options{Timeout: defaultTimeout})
			if err != nil {
				log.Fatalf("cannot parse the fallback %s (%s): %s", f, options.BootstrapDNS, err)
			}
			log.Printf("Fallback %d is %s", i, fallback.Address())
			fallbacks = append(fallbacks, fallback)
		}
		config.Fallbacks = fallbacks
	}

	// Prepare the TLS config
	if options.TLSCertPath != "" && options.TLSKeyPath != "" {
		tlsConfig, err := newTLSConfig(options.TLSCertPath, options.TLSKeyPath)
		if err != nil {
			log.Fatalf("failed to load TLS config: %s", err)
		}
		config.TLSConfig = tlsConfig
	}

	if options.TLSListenPort > 0 && config.TLSConfig != nil {
		config.TLSListenAddr = &net.TCPAddr{Port: options.TLSListenPort, IP: listenIP}
	}

	if options.HTTPSListenPort > 0 && config.TLSConfig != nil {
		config.HTTPSListenAddr = &net.TCPAddr{Port: options.HTTPSListenPort, IP: listenIP}
	}

	return config
}
// createProxyConfig creates proxy.Config from the command line arguments
createProxyConfig creates proxy.Config from the command line arguments
// createProxyConfig creates proxy . Config from the command line arguments
func createProxyConfig(options Options) proxy.Config {
	listenIP := net.ParseIP(options.ListenAddr)
	if listenIP == nil {
		log.Fatalf("cannot parse %s", options.ListenAddr)
	}
	listenUDPAddr := &net.UDPAddr{Port: options.ListenPort, IP: listenIP}
	listenTCPAddr := &net.TCPAddr{Port: options.ListenPort, IP: listenIP}
	upstreams := make([]upstream.Upstream, 0)
	for i, u := range options.Upstreams {
		dnsUpstream, err := upstream.AddressToUpstream(u, upstream.Options{Bootstrap: options.BootstrapDNS, Timeout: defaultTimeout})
		if err != nil {
			log.Fatalf("cannot prepare the upstream %s (%s): %s", u, options.BootstrapDNS, err)
		}
		log.Printf("Upstream %d: %s", i, dnsUpstream.Address())
		upstreams = append(upstreams, dnsUpstream)
	}
	config := proxy.Config{
		UDPListenAddr: listenUDPAddr,
		TCPListenAddr: listenTCPAddr,
		Upstreams:     upstreams,
		Ratelimit:     options.Ratelimit,
		CacheEnabled:  options.Cache,
		RefuseAny:     options.RefuseAny,
		AllServers:    options.AllServers,
	}
	if options.Fallbacks != nil {
		fallbacks := []upstream.Upstream{}
		for i, f := range options.Fallbacks {
			fallback, err := upstream.AddressToUpstream(f, upstream.Options{Timeout: defaultTimeout})
			if err != nil {
				log.Fatalf("cannot parse the fallback %s (%s): %s", f, options.BootstrapDNS, err)
			}
			log.Printf("Fallback %d is %s", i, fallback.Address())
			fallbacks = append(fallbacks, fallback)
		}
		config.Fallbacks = fallbacks
	}
	if options.TLSCertPath != "" && options.TLSKeyPath != "" {
		tlsConfig, err := newTLSConfig(options.TLSCertPath, options.TLSKeyPath)
		if err != nil {
			log.Fatalf("failed to load TLS config: %s", err)
		}
		config.TLSConfig = tlsConfig
	}
	if options.TLSListenPort > 0 && config.TLSConfig != nil {
		config.TLSListenAddr = &net.TCPAddr{Port: options.TLSListenPort, IP: listenIP}
	}
	if options.HTTPSListenPort > 0 && config.TLSConfig != nil {
		config.HTTPSListenAddr = &net.TCPAddr{Port: options.HTTPSListenPort, IP: listenIP}
	}
	return config
}
func createProxyConfig ( options Options ) proxy . Config { listenIP := net . ParseIP ( options . ListenAddr ) if listenIP == nil { log . Fatalf ( " " , options . ListenAddr ) } listenUDPAddr := & net . UDPAddr { Port : options . ListenPort , IP : listenIP } listenTCPAddr := & net . TCPAddr { Port : options . ListenPort , IP : listenIP } upstreams := make ( [ ] upstream . Upstream , 0 ) for i , u := range options . Upstreams { dnsUpstream , err := upstream . AddressToUpstream ( u , upstream . Options { Bootstrap : options . BootstrapDNS , Timeout : defaultTimeout } ) if err != nil { log . Fatalf ( " " , u , options . BootstrapDNS , err ) } log . Printf ( " " , i , dnsUpstream . Address ( ) ) upstreams = append ( upstreams , dnsUpstream ) } config := proxy . Config { UDPListenAddr : listenUDPAddr , TCPListenAddr : listenTCPAddr , Upstreams : upstreams , Ratelimit : options . Ratelimit , CacheEnabled : options . Cache , RefuseAny : options . RefuseAny , AllServers : options . AllServers , } if options . Fallbacks != nil { fallbacks := [ ] upstream . Upstream { } for i , f := range options . Fallbacks { fallback , err := upstream . AddressToUpstream ( f , upstream . Options { Timeout : defaultTimeout } ) if err != nil { log . Fatalf ( " " , f , options . BootstrapDNS , err ) } log . Printf ( " " , i , fallback . Address ( ) ) fallbacks = append ( fallbacks , fallback ) } config . Fallbacks = fallbacks } if options . TLSCertPath != " " && options . TLSKeyPath != " " { tlsConfig , err := newTLSConfig ( options . TLSCertPath , options . TLSKeyPath ) if err != nil { log . Fatalf ( " " , err ) } config . TLSConfig = tlsConfig } if options . TLSListenPort > 0 && config . TLSConfig != nil { config . TLSListenAddr = & net . TCPAddr { Port : options . TLSListenPort , IP : listenIP } } if options . HTTPSListenPort > 0 && config . TLSConfig != nil { config . HTTPSListenAddr = & net . TCPAddr { Port : options . HTTPSListenPort , IP : listenIP } } return config }
createProxyConfig creates proxy.Config from the command line arguments
createProxyConfig creates proxy . Config from the command line arguments
// Init listen addresses and upstreams // Create the config // Prepare the TLS config