Kaynağa Gözat

Refactored psiphon.GetInterfaceIPAddress so it can be used by server and client.
Removed similar function: getNetworkInterfaceIP from server config.go.
Removed unnecessary const DEFAULT_NETWORK_INTERFACE. Updated documentation.

mfallone 9 yıl önce
ebeveyn
işleme
54e1124e1c
3 değiştirilmiş dosya ile 19 ekleme ve 62 silme
  1. 0 1
      psiphon/config.go
  2. 18 33
      psiphon/networkInterface.go
  3. 1 28
      psiphon/server/config.go

+ 0 - 1
psiphon/config.go

@@ -168,7 +168,6 @@ type Config struct {
 
 	// ListenInterface specifies which interface to listen on.  If no interface
 	// is provided then listen on 127.0.0.1.
-	// 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

+ 18 - 33
psiphon/networkInterface.go

@@ -20,60 +20,45 @@
 package psiphon
 
 import (
+	"errors"
 	"net"
 )
 
 // Take in an interface name ("lo", "eth0", "any") passed from either
-// a config setting or by -interface command line flag and return the IP
+// a config setting, by using the -listenInterface flag on client or
+// -interface flag on server from the command line 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
+// If "any" is passed then listen on 0.0.0.0 for client (invalid with server)
 func GetInterfaceIPAddress(listenInterface string) (string, error) {
 	var ip net.IP
-
 	if listenInterface == "" {
 		ip = net.ParseIP("127.0.0.1")
+		return ip.String(), nil
 	} else if listenInterface == "any" {
 		ip = net.ParseIP("0.0.0.0")
+		return ip.String(), nil
 	} else {
-		//Get a list of interfaces
-		availableInterfaces, err := net.Interfaces()
+		availableInterfaces, err := net.InterfaceByName(listenInterface)
 		if err != nil {
 			return "", ContextError(err)
 		}
 
-		var selectedInterface net.Interface
-		found := false
-		for _, networkInterface := range availableInterfaces {
-			if listenInterface == networkInterface.Name {
-				NoticeInfo("Using interface: %s", networkInterface.Name)
-				selectedInterface = networkInterface
-				found = true
-				break
-			}
+		addrs, err := availableInterfaces.Addrs()
+		if err != nil {
+			return "", ContextError(err)
 		}
-		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)
-			}
-
-			for _, ipAddr := range netAddrs {
-				ip, _, err = net.ParseCIDR(ipAddr.String())
-				if err != nil {
-					return "", ContextError(err)
-				}
-				if ip.To4() != nil {
-					break
-				}
+		for _, addr := range addrs {
+			iptype := addr.(*net.IPNet)
+			if iptype == nil {
+				continue
 			}
+			// TODO: IPv6 support
+			ip = iptype.IP.To4()
+			return ip.String(), nil
 		}
 	}
 
-	NoticeInfo("Listening on IP address: %s", ip.String())
+	return "", ContextError(errors.New("Could not find IP address of specified interface"))
 
-	return ip.String(), nil
 }

+ 1 - 28
psiphon/server/config.go

@@ -62,7 +62,6 @@ const (
 	REDIS_POOL_MAX_IDLE                    = 50
 	REDIS_POOL_MAX_ACTIVE                  = 1000
 	REDIS_POOL_IDLE_TIMEOUT                = 5 * time.Minute
-	DEFAULT_NETWORK_INTERFACE              = "lo"
 )
 
 // TODO: break config into sections (sub-structs)
@@ -460,7 +459,7 @@ func GenerateConfig(params *GenerateConfigParams) ([]byte, []byte, error) {
 
 	// Find IP address of the network interface (if not loopback)
 	serverNetworkInterface := params.ServerNetworkInterface
-	serverNetworkInterfaceIP, err := getNetworkInterfaceIP(serverNetworkInterface)
+	serverNetworkInterfaceIP, err := psiphon.GetInterfaceIPAddress(serverNetworkInterface)
 	if err != nil {
 		serverNetworkInterfaceIP = serverIPaddress
 		fmt.Printf("Could not find IP address of nic.  Falling back to %s\n", serverIPaddress)
@@ -590,29 +589,3 @@ func generateWebServerCertificate() (string, string, error) {
 
 	return string(webServerCertificate), string(webServerPrivateKey), nil
 }
-
-func getNetworkInterfaceIP(networkInterface string) (string, error) {
-	iface, err := net.InterfaceByName(networkInterface)
-	if err != nil {
-		return "", psiphon.ContextError(err)
-	}
-
-	addrs, err := iface.Addrs()
-	if err != nil {
-		return "", psiphon.ContextError(err)
-	}
-	for _, addr := range addrs {
-		var ip net.IP
-		switch t := addr.(type) {
-		case *net.IPNet:
-			ip = t.IP
-		}
-		ip = ip.To4()
-		if ip == nil {
-			continue
-		}
-		return ip.String(), nil
-	}
-
-	return "", psiphon.ContextError(errors.New("Could not find IP address of specified interface"))
-}