// Proxy returns whether SSH connections should be proxied through the
// controller hosts for the associated model.
347142
func (facade *Facade) Proxy() (bool, error) {
	var out params.SSHProxyResult
	err := facade.caller.FacadeCall("Proxy", nil, &out)
	if err != nil {
		return false, errors.Trace(err)
	}
	return out.UseProxy, nil
}
125501
func (h *Harness) proxyWebsocket(w http.ResponseWriter, r *http.Request, host string) {
	var (
		d   net.Conn
		err error
	)
	if h.paths.HTTPSsl {
		// since this proxy isn't used in production,
		// it's OK to set InsecureSkipVerify to true
		// no need to add another configuration option.
		d, err = tls.Dial("tcp", host, &tls.Config{InsecureSkipVerify: true})
	} else {
		d, err = net.Dial("tcp", host)
	}
	if err != nil {
		http.Error(w, "Error contacting backend server.", 500)
		utils.Logger.Error("Error dialing websocket backend ", "host", host, "error", err)
		return
	}
	hj, ok := w.(http.Hijacker)
	if !ok {
		http.Error(w, "Not a hijacker?", 500)
		return
	}
	nc, _, err := hj.Hijack()
	if err != nil {
		utils.Logger.Error("Hijack error", "error", err)
		return
	}
	defer func() {
		if err = nc.Close(); err != nil {
			utils.Logger.Error("Connection close error", "error", err)
		}
		if err = d.Close(); err != nil {
			utils.Logger.Error("Dial close error", "error", err)
		}
	}()

	err = r.Write(d)
	if err != nil {
		utils.Logger.Error("Error copying request to target", "error", err)
		return
	}

	errc := make(chan error, 2)
	cp := func(dst io.Writer, src io.Reader) {
		_, err := io.Copy(dst, src)
		errc <- err
	}
	go cp(d, nc)
	go cp(nc, d)
	<-errc
}
815871
func NewProxy(address string) (*Proxy, error) {
	tcpAddr, err := net.ResolveTCPAddr("tcp", address)
	if err != nil {
		return nil, err
	}
	return &Proxy{
		target: tcpAddr,
	}, nil
}
211944
func (d proxyDial) Dial(network string, addr string, config *ssh.ClientConfig) (*ssh.Client, error) {
	// Build a proxy connection first.
	pconn, err := dialProxy(context.Background(), d.proxyHost, addr)
	if err != nil {
		return nil, trace.Wrap(err)
	}
	if config.Timeout > 0 {
		pconn.SetReadDeadline(time.Now().Add(config.Timeout))
	}
	// Do the same as ssh.Dial but pass in proxy connection.
	c, chans, reqs, err := ssh.NewClientConn(pconn, addr, config)
	if err != nil {
		return nil, trace.Wrap(err)
	}
	if config.Timeout > 0 {
		pconn.SetReadDeadline(time.Time{})
	}
	return ssh.NewClient(c, chans, reqs), nil
}
945698
func Proxy(frontend, backend, capture *Socket) error {
	items := NewPoller()
	items.Add(frontend, POLLIN)
	items.Add(backend, POLLIN)
	for {
		sockets, err := items.Poll(-1)
		if err != nil {
			return err
		}
		for _, socket := range sockets {
			for more := true; more; {
				msg, err := socket.Socket.RecvBytes(0)
				if err != nil {
					return err
				}
				more, err = socket.Socket.GetRcvmore()
				if err != nil {
					return err
				}
				fl := SNDMORE
				if !more {
					fl = 0
				}

				if capture != nil {
					_, err = capture.SendBytes(msg, fl)
					if err != nil {
						return err
					}
				}

				switch socket.Socket {
				case frontend:
					_, err = backend.SendBytes(msg, fl)
				case backend:
					_, err = frontend.SendBytes(msg, fl)
				}
				if err != nil {
					return err
				}
			}
		}
	}
	return nil
}
198499
func (self *DefaultRequest) GetProxy() string {
	self.once.Do(self.prepare)
	return self.Proxy
}
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)
}
675764
func (r *ProxyREST) Connect(ctx context.Context, id string, opts runtime.Object, responder rest.Responder) (http.Handler, error) {
	proxyOpts, ok := opts.(*api.PodProxyOptions)
	if !ok {
		return nil, fmt.Errorf("Invalid options object: %#v", opts)
	}
	location, transport, err := pod.ResourceLocation(r.Store, r.ProxyTransport, ctx, id)
	if err != nil {
		return nil, err
	}
	location.Path = net.JoinPreservingTrailingSlash(location.Path, proxyOpts.Path)
	// Return a proxy handler that uses the desired transport, wrapped with additional proxy handling (to get URL rewriting, X-Forwarded-* headers, etc)
	return newThrottledUpgradeAwareProxyHandler(location, transport, true, false, false, responder), nil
}
349628
func (pc *ProxyConfig) 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
	}
	ip := net.ParseIP(host)
	if ip != nil && ip.IsLoopback() {
		return false
	}

	if pc.noProxy == "*" {
		return false
	}

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

	for _, p := range strings.Split(pc.noProxy, ",") {
		p = strings.ToLower(strings.TrimSpace(p))
		if len(p) == 0 {
			continue
		}
		if hasPort(p) {
			p = p[:strings.LastIndex(p, ":")]
		}
		if addr == p {
			return false
		}
		if p[0] == '.' && (strings.HasSuffix(addr, p) || addr == p[1:]) {
			// no_proxy ".foo.com" matches "bar.foo.com" or "foo.com"
			return false
		}
		if p[0] != '.' && strings.HasSuffix(addr, p) && addr[len(addr)-len(p)-1] == '.' {
			// no_proxy "foo.com" matches "bar.foo.com"
			return false
		}
		if _, net, err := net.ParseCIDR(p); ip != nil && err == nil && net.Contains(ip) {
			return false
		}
	}
	return true
}
913833
func (client *Client) Proxies() (map[string]*Proxy, error) {
	resp, err := http.Get(client.endpoint + "/proxies")
	if err != nil {
		return nil, err
	}

	err = checkError(resp, http.StatusOK, "Proxies")
	if err != nil {
		return nil, err
	}

	proxies := make(map[string]*Proxy)
	err = json.NewDecoder(resp.Body).Decode(&proxies)
	if err != nil {
		return nil, err
	}
	for _, proxy := range proxies {
		proxy.client = client
		proxy.created = true
	}

	return proxies, nil
}
1242374
func NewProxyWithOptions(router route.Router, o Options) (*Proxy, error) {
	o, err := validateOptions(o)
	if err != nil {
		return nil, err
	}

	p := &Proxy{
		options: o,
		router:  router,
	}
	return p, nil
}
51697
func (h *AuthHandlers) isProxy() bool {
	if h.Component == teleport.ComponentProxy {
		return true
	}
	return false
}
1116268
func (dev *LinkLayerDevice) machineProxy(machineID string) *Machine {
	return &Machine{st: dev.st, doc: machineDoc{Id: machineID}}
}
463396
func (f *Forward) SetProxy(p *Proxy) {
	f.proxies = append(f.proxies, p)
	p.start(f.hcInterval)
}
424284
func (p *Proxy) Serve() {
	done := make(chan struct{}, 2)

	go func() {
		p.HTTPServe()
		done <- struct{}{}
	}()

	if p.grpcListenAddress != "" {
		go func() {
			p.gRPCServe()
			done <- struct{}{}
		}()
	}

	// wait until at least one of the servers has shut down
	<-done
	graceful.Shutdown()
	p.gRPCStop()
}
703764
func (s *PresenceService) GetProxies() ([]services.Server, error) {
	return s.getServers(services.KindProxy, proxiesPrefix)
}
287494
func (c *Context) Proxy() []string {
	if ips := c.Header("X-Forwarded-For"); ips != "" {
		return strings.Split(ips, ",")
	}
	return []string{}
}