Пример #1
0
// runClientProxy runs the client-side (get mode) proxy.
func runClientProxy(cfg *config.Config) {
	var err error

	// Set Lantern as system proxy by creating and using a PAC file.
	setProxyAddr(cfg.Addr)

	if err = setUpPacTool(); err != nil {
		exit(err)
	}

	// Create the client-side proxy.
	client := &client.Client{
		Addr:         cfg.Addr,
		ReadTimeout:  0, // don't timeout
		WriteTimeout: 0,
	}

	// Start user interface.
	if cfg.UIAddr != "" {
		if err = ui.Start(cfg.UIAddr); err != nil {
			exit(fmt.Errorf("Unable to start UI: %v", err))
			return
		}
	}

	applyClientConfig(client, cfg)
	// Continually poll for config updates and update client accordingly
	go func() {
		for {
			cfg := <-configUpdates
			applyClientConfig(client, cfg)
		}
	}()

	// watchDirectAddrs will spawn a goroutine that will add any site that is
	// directly accesible to the PAC file.
	watchDirectAddrs()

	go func() {
		addExitFunc(pacOff)
		err := client.ListenAndServe(func() {
			pacOn()
			if showui {
				// Launch a browser window with Lantern but only after the pac
				// URL and the proxy server are all up and running to avoid
				// race conditions where we change the proxy setup while the
				// UI server and proxy server are still coming up.
				ui.Show()
			}
		})
		if err != nil {
			log.Errorf("Error calling listen and serve: %v", err)
		}
	}()
}
Пример #2
0
// runClientProxy runs the client-side (get mode) proxy.
func runClientProxy(cfg *config.Config) {
	// Set Lantern as system proxy by creating and using a PAC file.
	setProxyAddr(cfg.Addr)

	if err := setUpPacTool(); err != nil {
		exit(err)
	}

	if *clearProxySettings {
		// This is a workaround that attempts to fix a Windows-only problem where
		// Lantern was unable to clean the system's proxy settings before logging
		// off.
		//
		// See: https://github.com/getlantern/lantern/issues/2776
		doPACOff(fmt.Sprintf("http://%s/proxy_on.pac", cfg.UIAddr))
		exit(nil)
	}

	// Create the client-side proxy.
	client := &client.Client{
		Addr:         cfg.Addr,
		ReadTimeout:  0, // don't timeout
		WriteTimeout: 0,
	}

	// Start user interface.
	tcpAddr, err := net.ResolveTCPAddr("tcp4", cfg.UIAddr)
	if err != nil {
		exit(fmt.Errorf("Unable to resolve UI address: %v", err))
	}

	if err = ui.Start(tcpAddr, !showui); err != nil {
		// This very likely means Lantern is already running on our port. Tell
		// it to open a browser. This is useful, for example, when the user
		// clicks the Lantern desktop shortcut when Lantern is already running.
		showExistingUi(cfg.UIAddr)
		exit(fmt.Errorf("Unable to start UI: %s", err))
		return
	}

	applyClientConfig(client, cfg)
	// Continually poll for config updates and update client accordingly
	go func() {
		for {
			cfg := <-configUpdates
			applyClientConfig(client, cfg)
		}
	}()

	/*
		      Temporarily disabling localdiscover. See:
		      https://github.com/getlantern/lantern/issues/2813
		      // Continually search for local Lantern instances and update the UI
		      go func() {
			addExitFunc(localdiscovery.Stop)
			localdiscovery.Start(!showui, strconv.Itoa(tcpAddr.Port))
		      }()
	*/

	// watchDirectAddrs will spawn a goroutine that will add any site that is
	// directly accesible to the PAC file.
	watchDirectAddrs()

	err = client.ListenAndServe(func() {
		pacOn()
		addExitFunc(pacOff)
		if showui && !*startup {
			// Launch a browser window with Lantern but only after the pac
			// URL and the proxy server are all up and running to avoid
			// race conditions where we change the proxy setup while the
			// UI server and proxy server are still coming up.
			ui.Show()
		} else {
			log.Debugf("Not opening browser. Startup is: %v", *startup)
		}
	})
	if err != nil {
		exit(fmt.Errorf("Error calling listen and serve: %v", err))
	}
}