Kaynağa Gözat

Moved network interface selection into controller.Run(). Removed listenIP as a config setting. Changed error printouts. Updated config documentation.

mfallone 10 yıl önce
ebeveyn
işleme
25f3b43a21

+ 1 - 17
ConsoleClient/psiphonClient.go

@@ -48,7 +48,7 @@ func main() {
 	flag.StringVar(&profileFilename, "profile", "", "CPU profile output file")
 
 	var interfaceName string
-	flag.StringVar(&interfaceName, "interface", "", "Interface Name")
+	flag.StringVar(&interfaceName, "listenInterface", "", "Interface Name")
 
 	flag.Parse()
 
@@ -158,22 +158,6 @@ func main() {
 
 	if interfaceName != "" {
 		config.ListenInterface = interfaceName
-		config.ListenIP, err = psiphon.GetInterfaceIPAddress(interfaceName)
-		if err != nil {
-			psiphon.NoticeError("error getting listener IP: %s", err)
-			os.Exit(1)
-		}
-		psiphon.NoticeAlert("Listening on interface: %s : %s", config.ListenInterface, config.ListenIP)
-	} else {
-		if config.ListenInterface != "" {
-			config.ListenIP, err = psiphon.GetInterfaceIPAddress(config.ListenInterface)
-			if err != nil {
-				psiphon.NoticeError("error getting listener IP: %s", err)
-				os.Exit(1)
-			}
-		} else {
-			config.ListenIP = "127.0.0.1"
-		}
 	}
 
 	// Run Psiphon

+ 3 - 5
psiphon/config.go

@@ -146,13 +146,11 @@ type Config struct {
 
 	// ListenInterface specifies which interface to listen on.  If no interface
 	// is provided then listen on 127.0.0.1.
-	// If 'any' is provided then use 0.0.0.0
+	// If an invalid interface is provided then listen on localhost (127.0.0.1).
+	// If 'any' is provided then use 0.0.0.0.
+	// If there are multiple IP addresses on an interface use the first IPv4 address.
 	ListenInterface string
 
-	// ListenIP specifies which IP address to listen on.  The IP address is taken
-	// from ListenInterface.
-	ListenIP string
-
 	// LocalSocksProxyPort specifies a port number for the local SOCKS proxy
 	// running at 127.0.0.1. For the default value, 0, the system selects a free
 	// port (a notice reporting the selected port is emitted).

+ 8 - 2
psiphon/controller.go

@@ -136,7 +136,13 @@ func (controller *Controller) Run(shutdownBroadcast <-chan struct{}) {
 
 	// Start components
 
-	socksProxy, err := NewSocksProxy(controller.config, controller)
+	listenIP, err := GetInterfaceIPAddress(controller.config.ListenInterface)
+	if err != nil {
+		NoticeError("error getting listener IP: %s", err)
+		return
+	}
+
+	socksProxy, err := NewSocksProxy(controller.config, controller, listenIP)
 	if err != nil {
 		NoticeAlert("error initializing local SOCKS proxy: %s", err)
 		return
@@ -144,7 +150,7 @@ func (controller *Controller) Run(shutdownBroadcast <-chan struct{}) {
 	defer socksProxy.Close()
 
 	httpProxy, err := NewHttpProxy(
-		controller.config, controller.untunneledDialConfig, controller)
+		controller.config, controller.untunneledDialConfig, controller, listenIP)
 	if err != nil {
 		NoticeAlert("error initializing local HTTP proxy: %s", err)
 		return

+ 3 - 2
psiphon/httpProxy.go

@@ -71,10 +71,11 @@ var _HTTP_PROXY_TYPE = "HTTP"
 func NewHttpProxy(
 	config *Config,
 	untunneledDialConfig *DialConfig,
-	tunneler Tunneler) (proxy *HttpProxy, err error) {
+	tunneler Tunneler,
+	listenIP string) (proxy *HttpProxy, err error) {
 
 	listener, err := net.Listen(
-		"tcp", fmt.Sprintf("%s:%d", config.ListenIP, config.LocalHttpProxyPort))
+		"tcp", fmt.Sprintf("%s:%d", listenIP, config.LocalHttpProxyPort))
 	if err != nil {
 		if IsAddressInUseError(err) {
 			NoticeHttpProxyPortInUse(config.LocalHttpProxyPort)

+ 38 - 32
psiphon/networkInterface.go

@@ -23,51 +23,57 @@ import (
 	"net"
 )
 
-func GetInterfaceIPAddress(interfaceName string) (string, error) {
-	var selectedInterface net.Interface
+// Take in an interface name ("lo", "eth0", "any") passed from either
+// a config setting or by -interface command line flag and return the IP
+// address associated with it.
+// If no interface is provided use the default loopback interface (127.0.0.1).
+// If "any" is passed then listen on 0.0.0.0
+func GetInterfaceIPAddress(listenInterface string) (string, error) {
 	var ip net.IP
 
-	//Get a list of interfaces
-	availableInterfaces, err := net.Interfaces()
-	if err != nil {
-		NoticeAlert("%s", ContextError(err))
-		return "", err
-	}
-
-	if interfaceName == "any" {
+	if listenInterface == "" {
+		ip = net.ParseIP("127.0.0.1")
+	} else if listenInterface == "any" {
 		ip = net.ParseIP("0.0.0.0")
 	} else {
+		//Get a list of interfaces
+		availableInterfaces, err := net.Interfaces()
+		if err != nil {
+			return "", ContextError(err)
+		}
+
+		var selectedInterface net.Interface
+		found := false
 		for _, networkInterface := range availableInterfaces {
-			if interfaceName == networkInterface.Name {
-				NoticeAlert("Using interface: %s", networkInterface.Name)
+			if listenInterface == networkInterface.Name {
+				NoticeInfo("Using interface: %s", networkInterface.Name)
 				selectedInterface = networkInterface
+				found = true
 				break
 			}
 		}
-	}
+		if !found {
+			NoticeAlert("Interface not found: %s", listenInterface)
+			ip = net.ParseIP("127.0.0.1")
+		} else {
+			netAddrs, err := selectedInterface.Addrs()
+			if err != nil {
+				return "", ContextError(err)
+			}
 
-	if ip.To4() == nil {
-		if selectedInterface.Name == "" {
-			selectedInterface = availableInterfaces[0]
-			NoticeAlert("No interface found, using %s", selectedInterface.Name)
+			for _, ipAddr := range netAddrs {
+				ip, _, err = net.ParseCIDR(ipAddr.String())
+				if err != nil {
+					return "", ContextError(err)
+				}
+				if ip.To4() != nil {
+					break
+				}
+			}
 		}
 	}
 
-	netAddrs, err := selectedInterface.Addrs()
-	if err != nil {
-		NoticeAlert("Error : %s", err.Error())
-		return "", err
-	}
-
-	for _, ipAddr := range netAddrs {
-		ip, _, err = net.ParseCIDR(ipAddr.String())
-		if err != nil {
-			NoticeAlert("Error parsing address %s", err.Error())
-		}
-		if ip.To4() != nil {
-			break
-		}
-	}
+	NoticeInfo("Listening on IP address: %s", ip.String())
 
 	return ip.String(), nil
 }

+ 6 - 2
psiphon/socksProxy.go

@@ -44,9 +44,13 @@ var _SOCKS_PROXY_TYPE = "SOCKS"
 // NewSocksProxy initializes a new SOCKS server. It begins listening for
 // connections, starts a goroutine that runs an accept loop, and returns
 // leaving the accept loop running.
-func NewSocksProxy(config *Config, tunneler Tunneler) (proxy *SocksProxy, err error) {
+func NewSocksProxy(
+	config *Config,
+	tunneler Tunneler,
+	listenIP string) (proxy *SocksProxy, err error) {
+
 	listener, err := socks.ListenSocks(
-		"tcp", fmt.Sprintf("%s:%d", config.ListenIP, config.LocalSocksProxyPort))
+		"tcp", fmt.Sprintf("%s:%d", listenIP, config.LocalSocksProxyPort))
 	if err != nil {
 		if IsAddressInUseError(err) {
 			NoticeSocksProxyPortInUse(config.LocalSocksProxyPort)