// machineProxy is a convenience wrapper for calling Machine methods from an
// *Address.
350883
func (addr *Address) machineProxy() *Machine {
	return &Machine{st: addr.st, doc: machineDoc{Id: addr.doc.MachineID}}
}
439687
func StartMappingProxy(bind string, h http.Handler) error {
	s := http.Server{
		Addr:    bind,
		Handler: h,
	}
	return s.ListenAndServe()
}
2543
func newProxy(name, tenantEndpointID string, tcpMuxPort uint16, useTLS bool, listener net.Listener, allowDirectConn bool) (p *proxy, err error) {
	if len(name) == 0 {
		return nil, fmt.Errorf("prxy: name can not be empty")
	}
	p = &proxy{
		name:             name,
		tenantEndpointID: tenantEndpointID,
		addresses:        make([]addressTuple, 0),
		tcpMuxPort:       tcpMuxPort,
		useTLS:           useTLS,
		listener:         listener,
		allowDirectConn:  allowDirectConn,
	}
	p.newAddresses = make(chan []addressTuple, 2)
	go p.listenAndproxy()
	return p, nil
}
1010931
func NewProxyRegistry(factory ProxyFactory) ProxyRegistry {
	return &proxyRegistry{
		registry:     make(map[string]Proxy),
		proxyFactory: factory,
	}
}
899573
func NewProxy(frontendAddr, backendAddr net.Addr) (Proxy, error) {
	switch frontendAddr.(type) {
	case *net.UDPAddr:
		return NewUDPProxy(frontendAddr.(*net.UDPAddr), backendAddr.(*net.UDPAddr))
	case *net.TCPAddr:
		return NewTCPProxy(frontendAddr.(*net.TCPAddr), backendAddr.(*net.TCPAddr))
	default:
		panic(fmt.Errorf("Unsupported protocol"))
	}
}
584845
func getProxyPort(l7Type policy.L7ParserType, ingress bool) *ProxyPort {
	portType := l7Type
	switch l7Type {
	case policy.ParserTypeDNS, policy.ParserTypeKafka, policy.ParserTypeHTTP:
	default:
		// "Unknown" parsers are assumed to be Proxylib (TCP) parsers, which
		// is registered with an empty string.
		portType = ""
	}
	// proxyPorts is small enough to not bother indexing it.
	for i := range proxyPorts {
		if proxyPorts[i].parserType == portType && proxyPorts[i].ingress == ingress {
			return &proxyPorts[i]
		}
	}
	return nil
}
163151
func (G *global_data) SetProxy(url string) *global_data {
	G.Req.Proxy = url
	return G
}
1319010
func (cfg *centOSCloudConfig) PackageProxy() string {
	proxy, _ := cfg.attrs["package_proxy"].(string)
	return proxy
}
796049
func GetProxyPort(l7Type policy.L7ParserType, ingress bool) (uint16, string, error) {
	// Accessing pp.proxyPort requires the lock
	proxyPortsMutex.Lock()
	defer proxyPortsMutex.Unlock()
	pp := getProxyPort(l7Type, ingress)
	if pp != nil {
		return pp.proxyPort, pp.name, nil
	}
	return 0, "", proxyNotFoundError(l7Type, ingress)
}
87217
func useProxy(addr string) bool {
	if len(addr) == 0 {
		return true
	}
	host, _, err := net.SplitHostPort(addr)
	if err != nil {
		return false
	}
	if host == "localhost" {
		return false
	}
	if ip := net.ParseIP(host); ip != nil {
		if ip.IsLoopback() {
			return false
		}
	}

	no_proxy := getenvEitherCase("NO_PROXY")
	if no_proxy == "*" {
		return false
	}

	addr = strings.ToLower(strings.TrimSpace(addr))
	if hasPort(addr) {
		addr = addr[:strings.LastIndex(addr, ":")]
	}

	for _, p := range strings.Split(no_proxy, ",") {
		p = strings.ToLower(strings.TrimSpace(p))
		if len(p) == 0 {
			continue
		}
		if hasPort(p) {
			p = p[:strings.LastIndex(p, ":")]
		}
		if addr == p || (p[0] == '.' && (strings.HasSuffix(addr, p) || addr == p[1:])) {
			return false
		}
	}
	return true
}
1332202
func (p *proxy) prxy(local net.Conn, address addressTuple) {

	var (
		remote net.Conn
		err    error
	)
	glog.V(2).Infof("Setting up proxy for %#v", address)
	isLocalContainer := false
	localAddr := address.containerAddr
	if p.allowDirectConn {
		//check if the host for the container is running on the same host
		isLocalContainer = isLocalAddress(address.host)
		glog.V(4).Infof("Checking is local for %s %s in %#v", address.host, isLocalContainer, hostIPs)
		// don't proxy localhost addresses, we'll end up in a loop
		if isLocalContainer {
			switch {
			case strings.HasPrefix(address.host, "127"):
				isLocalContainer = false
			case address.host == "localhost":
				isLocalContainer = false
			case strings.HasPrefix(address.containerAddr, "127") || strings.HasPrefix(address.containerAddr, "localhost:"):
				//if the host is local and the container has a local style addr
				//then container is exposing port directly on host; go to host and use container port
				if containerPort, err := getPort(address.containerAddr); err != nil {
					glog.Warningf("could not get port %v", err)
					isLocalContainer = false
				} else {
					localAddr = fmt.Sprintf("%s:%d", address.host, containerPort)
				}
			}
		}
	}
	if p.tcpMuxPort == 0 {
		// TODO: Do this properly
		glog.Errorf("Mux port is unspecified. Using default of 22250.")
		p.tcpMuxPort = 22250
	}

	muxAddr := fmt.Sprintf("%s:%d", address.host, p.tcpMuxPort)

	// Build the authentication header before dialing the connection, so the
	// connection isn't sitting open waiting for an authentication token to be
	// loaded.
	var (
		muxAddrPacked []byte
		token         string
		tokenTimeout  = 30 * time.Second
	)

	if !isLocalContainer {
		muxAddrPacked, err = utils.PackTCPAddressString(address.containerAddr)
		if err != nil {
			glog.Errorf("Container address is invalid. Can't create proxy: %s", address.containerAddr)
			return
		}
		select {
		case token = <-auth.AuthToken(nil):
		case <-time.After(tokenTimeout):
			glog.Error("Unable to retrieve authentication token with 30 seconds")
			return
		}
	}

	// Dial the target connection, which will be either a local container
	// address or a mux port on a remote host.
	switch {
	case isLocalContainer:
		glog.V(2).Infof("dialing local addr=> %s", localAddr)
		remote, err = net.Dial("tcp4", localAddr)
		if err != nil {
			glog.Errorf("Error Local (net.Dial): %s", err)
			return
		}
	case p.useTLS:
		glog.V(2).Infof("dialing remote tls => %s", muxAddr)
		config := tls.Config{InsecureSkipVerify: true}
		tlsConn, err := tls.Dial("tcp4", muxAddr, &config)
		if err != nil {
			glog.Errorf("Error TLS (net.Dial): %s", err)
			return
		}
		remote = tlsConn // cast it to the net.Conn interface
		cipher := tlsConn.ConnectionState().CipherSuite
		glog.V(2).Infof("Proxy connected to mux with TLS cipher=%s (%d)", utils.GetCipherName(cipher), cipher)
	default:
		glog.V(2).Infof("dialing remote => %s", muxAddr)
		remote, err = net.Dial("tcp4", muxAddr)
		if err != nil {
			glog.Errorf("Error Remote (net.Dial): %s", err)
			return
		}
	}

	// If this is not a local container, write the mux header
	if token != "" && len(muxAddrPacked) > 0 {
		auth.AddSignedMuxHeader(remote, muxAddrPacked, token)
	}

	glog.V(2).Infof("Using hostAgent:%v to prxy %v<->%v<->%v<->%v",
		remote.RemoteAddr(), local.LocalAddr(), local.RemoteAddr(), remote.LocalAddr(), address)
	go func(address string) {
		defer local.Close()
		defer remote.Close()
		io.Copy(local, remote)
		glog.V(2).Infof("Closing hostAgent:%v to prxy %v<->%v<->%v<->%v",
			remote.RemoteAddr(), local.LocalAddr(), local.RemoteAddr(), remote.LocalAddr(), address)
	}(address.containerAddr)
	go func(address string) {
		defer local.Close()
		defer remote.Close()
		io.Copy(remote, local)
		glog.V(2).Infof("closing hostAgent:%v to prxy %v<->%v<->%v<->%v",
			remote.RemoteAddr(), local.LocalAddr(), local.RemoteAddr(), remote.LocalAddr(), address)
	}(address.containerAddr)
}
177061
func NewProxy(id int, st stapler.Stapler, o proxy.Options) (proxy.Proxy, error) {
	return mux.New(id, st, o)
}
45355
func (a *Agent) RemoveProxy(proxyID string, persist bool) error {
	a.stateLock.Lock()
	defer a.stateLock.Unlock()
	return a.removeProxyLocked(proxyID, persist)
}
373830
func NewProxy(addr, trans string) *Proxy {
	p := &Proxy{
		addr:      addr,
		fails:     0,
		probe:     up.New(),
		transport: newTransport(addr),
	}
	p.health = NewHealthChecker(trans)
	runtime.SetFinalizer(p, (*Proxy).finalizer)
	return p
}
112316
func (s *SessionClient) MachineLogin(ctx context.Context, tokenID, tokenSecret string) error {

	ID, err := identity.DecodeFromString(tokenID)
	if err != nil {
		return err
	}

	secret, err := base64.NewFromString(tokenSecret)
	if err != nil {
		return err
	}

	login := apitypes.MachineLogin{
		TokenID: &ID,
		Secret:  secret,
	}

	rawLogin, err := json.Marshal(login)
	if err != nil {
		return err
	}
	return performLogin(ctx, s, "machine", rawLogin)
}
652640
func (m *Machine) MachineAddresses() (addresses []network.Address) {
	for _, address := range m.doc.MachineAddresses {
		addresses = append(addresses, address.networkAddress())
	}
	return
}
1370905
func (s ServerSpec) WithProxy(proxy ProxyFunc) ServerSpec {
	s.connectionArgs.Proxy = proxy
	return s
}