Просмотр исходного кода

Added ability to listen on a specified network interface. Use -interface flag or set in config file.

mfallone 10 лет назад
Родитель
Сommit
0469db5090
5 измененных файлов с 97 добавлено и 2 удалено
  1. 15 0
      ConsoleClient/psiphonClient.go
  2. 9 0
      psiphon/config.go
  3. 1 1
      psiphon/httpProxy.go
  4. 71 0
      psiphon/networkInterface.go
  5. 1 1
      psiphon/socksProxy.go

+ 15 - 0
ConsoleClient/psiphonClient.go

@@ -47,6 +47,9 @@ func main() {
 	var profileFilename string
 	flag.StringVar(&profileFilename, "profile", "", "CPU profile output file")
 
+	var interfaceName string
+	flag.StringVar(&interfaceName, "interface", "", "Interface Name")
+
 	flag.Parse()
 
 	// Initialize default Notice output (stderr)
@@ -153,6 +156,18 @@ func main() {
 		}
 	}
 
+	if interfaceName != "" {
+		config.ListenInterface = interfaceName
+		config.ListenIP = psiphon.GetInterfaceIPAddress(interfaceName)
+		psiphon.NoticeAlert("Listening on interface: %s : %s", config.ListenInterface, config.ListenIP)
+	} else {
+		if config.ListenInterface != "" {
+			config.ListenIP = psiphon.GetInterfaceIPAddress(config.ListenInterface)
+		} else {
+			config.ListenIP = "127.0.0.1"
+		}
+	}
+
 	// Run Psiphon
 
 	controller, err := psiphon.NewController(config)

+ 9 - 0
psiphon/config.go

@@ -144,6 +144,15 @@ type Config struct {
 	// the controller will keep trying indefinitely.
 	EstablishTunnelTimeoutSeconds *int
 
+	// ListenInterface specifies whic 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
+	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).

+ 1 - 1
psiphon/httpProxy.go

@@ -74,7 +74,7 @@ func NewHttpProxy(
 	tunneler Tunneler) (proxy *HttpProxy, err error) {
 
 	listener, err := net.Listen(
-		"tcp", fmt.Sprintf("127.0.0.1:%d", config.LocalHttpProxyPort))
+		"tcp", fmt.Sprintf("%s:%d", config.ListenIP, config.LocalHttpProxyPort))
 	if err != nil {
 		if IsAddressInUseError(err) {
 			NoticeHttpProxyPortInUse(config.LocalHttpProxyPort)

+ 71 - 0
psiphon/networkInterface.go

@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2015, Psiphon Inc.
+ * All rights reserved.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+package psiphon
+
+import (
+	"net"
+)
+
+func GetInterfaceIPAddress(interfaceName string) string {
+	var selectedInterface net.Interface
+	var ip net.IP
+
+	//Get a list of interfaces
+	availableInterfaces, err := net.Interfaces()
+	if err != nil {
+		NoticeAlert("%s", ContextError(err))
+	}
+
+	if interfaceName == "any" {
+		ip = net.ParseIP("0.0.0.0")
+	} else {
+		for _, networkInterface := range availableInterfaces {
+			if interfaceName == networkInterface.Name {
+				NoticeAlert("Using interface: %s", networkInterface.Name)
+				selectedInterface = networkInterface
+				break
+			}
+		}
+	}
+
+	if ip.To4() == nil {
+		if selectedInterface.Name == "" {
+			selectedInterface = availableInterfaces[0]
+			NoticeAlert("No interface found, using %s", selectedInterface.Name)
+		}
+	}
+
+	netAddrs, err := selectedInterface.Addrs()
+	if err != nil {
+		NoticeAlert("Error : %s", err.Error())
+	}
+
+	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
+		}
+	}
+
+	return ip.String()
+}

+ 1 - 1
psiphon/socksProxy.go

@@ -46,7 +46,7 @@ var _SOCKS_PROXY_TYPE = "SOCKS"
 // leaving the accept loop running.
 func NewSocksProxy(config *Config, tunneler Tunneler) (proxy *SocksProxy, err error) {
 	listener, err := socks.ListenSocks(
-		"tcp", fmt.Sprintf("127.0.0.1:%d", config.LocalSocksProxyPort))
+		"tcp", fmt.Sprintf("%s:%d", config.ListenIP, config.LocalSocksProxyPort))
 	if err != nil {
 		if IsAddressInUseError(err) {
 			NoticeSocksProxyPortInUse(config.LocalSocksProxyPort)