Example #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)
		}
	}()
}
Example #2
0
File: main.go Project: 2722/lantern
func beforeStart(cfg *config.Config) bool {
	log.Debug("Got first config")
	if *help {
		flag.Usage()
		exit(fmt.Errorf("Wrong arguments"))
		return false
	}

	if cfg.CpuProfile != "" || cfg.MemProfile != "" {
		log.Debugf("Start profiling with cpu file %s and mem file %s", cfg.CpuProfile, cfg.MemProfile)
		finishProfiling := profiling.Start(cfg.CpuProfile, cfg.MemProfile)
		addExitFunc(finishProfiling)
	}

	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
		log.Debug("Clearing proxy settings")
		doPACOff(fmt.Sprintf("http://%s/proxy_on.pac", *uiaddr))
		exit(nil)
	}

	bootstrap, err := config.ReadBootstrapSettings()
	var startupUrl string
	if err != nil {
		log.Errorf("Could not read settings? %v", err)
		startupUrl = ""
	} else {
		startupUrl = bootstrap.StartupUrl
	}

	log.Debugf("Starting client UI at %v", *uiaddr)
	actualUIAddr, err := ui.Start(*uiaddr, !showui, startupUrl)
	if 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.
		err2 := showExistingUi(*uiaddr)
		if err2 != nil {
			exit(fmt.Errorf("Unable to start UI: %s", err))
		} else {
			log.Debug("Lantern already running, showing existing UI")
			exit(nil)
		}
		return false
	}
	client.UIAddr = actualUIAddr

	// Only run analytics once on startup.
	if settings.IsAutoReport() {
		stopAnalytics := analytics.Start(cfg, flashlight.Version)
		addExitFunc(stopAnalytics)
	}
	watchDirectAddrs()

	return true
}
Example #3
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))
	}
}