浏览代码

specify public port

Victor Vieux 12 年之前
父节点
当前提交
95d66ebc6b
共有 1 个文件被更改,包括 32 次插入14 次删除
  1. 32 14
      network.go

+ 32 - 14
network.go

@@ -485,20 +485,38 @@ type Nat struct {
 
 func parseNat(spec string) (*Nat, error) {
 	var nat Nat
-	// If spec starts with ':', external and internal ports must be the same.
-	// This might fail if the requested external port is not available.
-	var sameFrontend bool
-	if spec[0] == ':' {
-		sameFrontend = true
-		spec = spec[1:]
-	}
-	port, err := strconv.ParseUint(spec, 10, 16)
-	if err != nil {
-		return nil, err
-	}
-	nat.Backend = int(port)
-	if sameFrontend {
-		nat.Frontend = nat.Backend
+
+	if strings.Contains(spec, ":") {
+		specParts := strings.Split(spec, ":")
+		if len(specParts) != 2 {
+			return nil, fmt.Errorf("Invalid port format.")
+		}
+		// If spec starts with ':', external and internal ports must be the same.
+		// This might fail if the requested external port is not available.
+		var sameFrontend bool
+		if len(specParts[0]) == 0 {
+			sameFrontend = true
+		} else {
+			front, err := strconv.ParseUint(specParts[0], 10, 16)
+			if err != nil {
+				return nil, err
+			}
+			nat.Frontend = int(front)
+		}
+		back, err := strconv.ParseUint(specParts[1], 10, 16)
+		if err != nil {
+			return nil, err
+		}
+		nat.Backend = int(back)
+		if sameFrontend {
+			nat.Frontend = nat.Backend
+		}
+	} else {
+		port, err := strconv.ParseUint(spec, 10, 16)
+		if err != nil {
+			return nil, err
+		}
+		nat.Backend = int(port)
 	}
 	nat.Proto = "tcp"
 	return &nat, nil