github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/portutil/iptable/iptables.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package iptable 18 19 import ( 20 "regexp" 21 "strconv" 22 "strings" 23 ) 24 25 // ParseIPTableRules takes a slice of iptables rules as input and returns a slice of 26 // uint64 containing the parsed destination port numbers from the rules. 27 func ParseIPTableRules(rules []string) []uint64 { 28 ports := []uint64{} 29 30 // Regex to match the '--dports' option followed by the port number 31 dportRegex := regexp.MustCompile(`--dports ((,?\d+)+)`) 32 33 for _, rule := range rules { 34 matches := dportRegex.FindStringSubmatch(rule) 35 if len(matches) > 1 { 36 for _, _match := range strings.Split(matches[1], ",") { 37 port64, err := strconv.ParseUint(_match, 10, 16) 38 if err != nil { 39 continue 40 } 41 ports = append(ports, port64) 42 } 43 } 44 } 45 46 return ports 47 }