From 538639b62390271f275713259837cfe7d77e5cbc Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Tue, 9 May 2023 17:02:59 -0400 Subject: [PATCH] fix `(*service).Ports()` missing ports from IPv6 (#1069) --- go.mod | 4 ++-- go.sum | 4 ++-- service/health.go | 54 ++--------------------------------------------- 3 files changed, 6 insertions(+), 56 deletions(-) diff --git a/go.mod b/go.mod index 62ca7b22b..6b1a91a67 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.20 require ( github.com/Curtis-Milo/nat-type-identifier-go v0.0.0-20220215191915-18d42168c63d - github.com/IceWhaleTech/CasaOS-Common v0.4.4-alpha4 + github.com/IceWhaleTech/CasaOS-Common v0.4.4-alpha8 github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf github.com/deckarep/golang-set/v2 v2.3.0 github.com/deepmap/oapi-codegen v1.12.4 @@ -33,7 +33,6 @@ require ( github.com/patrickmn/go-cache v2.1.0+incompatible github.com/pkg/errors v0.9.1 github.com/robfig/cron/v3 v3.0.1 - github.com/samber/lo v1.38.1 github.com/satori/go.uuid v1.2.0 github.com/shirou/gopsutil/v3 v3.23.2 github.com/sirupsen/logrus v1.9.0 @@ -107,6 +106,7 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect + github.com/samber/lo v1.38.1 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect github.com/tklauser/go-sysconf v0.3.11 // indirect diff --git a/go.sum b/go.sum index 99eccc58c..2055a069a 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ github.com/Curtis-Milo/nat-type-identifier-go v0.0.0-20220215191915-18d42168c63d h1:62lEBImTxZ83pgzywgDNIrPPuQ+j4ep9QjqrWBn1hrU= github.com/Curtis-Milo/nat-type-identifier-go v0.0.0-20220215191915-18d42168c63d/go.mod h1:lW9x+yEjqKdPbE3+cf2fGPJXCw/hChX3Omi9QHTLFsQ= -github.com/IceWhaleTech/CasaOS-Common v0.4.4-alpha4 h1:KIMQL8fumAczZEsd7uC7n2NUzBYUC4DntRc8usSxGq8= -github.com/IceWhaleTech/CasaOS-Common v0.4.4-alpha4/go.mod h1:2IuYyy5qW1BE6jqC6M+tOU+WtUec1K565rLATBJ9p/0= +github.com/IceWhaleTech/CasaOS-Common v0.4.4-alpha8 h1:UhCg3d9Cxhx7KVmqh8oUrUl1qFmFdcHee3Zkk4+P2JA= +github.com/IceWhaleTech/CasaOS-Common v0.4.4-alpha8/go.mod h1:2IuYyy5qW1BE6jqC6M+tOU+WtUec1K565rLATBJ9p/0= github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk= github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= diff --git a/service/health.go b/service/health.go index e9cab09b6..0afcaf60d 100644 --- a/service/health.go +++ b/service/health.go @@ -1,15 +1,7 @@ package service import ( - "bufio" - "errors" - "fmt" - "os" - "strconv" - "strings" - - "github.com/samber/lo" - + "github.com/IceWhaleTech/CasaOS-Common/utils/port" "github.com/IceWhaleTech/CasaOS-Common/utils/systemctl" ) @@ -45,49 +37,7 @@ func (s *service) Services() (map[bool]*[]string, error) { } func (s *service) Ports() ([]int, []int, error) { - usedPorts := map[string]map[int]struct{}{ - "tcp": {}, - "udp": {}, - } - - for _, protocol := range []string{"tcp", "udp"} { - filename := fmt.Sprintf("/proc/net/%s", protocol) - - file, err := os.Open(filename) - if err != nil { - return nil, nil, errors.New("Failed to open " + filename) - } - defer file.Close() - - scanner := bufio.NewScanner(file) - for scanner.Scan() { - line := scanner.Text() - fields := strings.Fields(line) - if len(fields) < 2 { - continue - } - - localAddress := fields[1] - addressParts := strings.Split(localAddress, ":") - if len(addressParts) < 2 { - continue - } - - portHex := addressParts[1] - port, err := strconv.ParseInt(portHex, 16, 0) - if err != nil { - continue - } - - usedPorts[protocol][int(port)] = struct{}{} - } - - if err := scanner.Err(); err != nil { - return nil, nil, errors.New("Error reading from " + filename) - } - } - - return lo.Keys(usedPorts["tcp"]), lo.Keys(usedPorts["udp"]), nil + return port.ListPortsInUse() } func NewHealthService() HealthService {