diff --git a/go.mod b/go.mod index d75c3e3..6b1a91a 100644 --- a/go.mod +++ b/go.mod @@ -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/service/health.go b/service/health.go index e9cab09..0afcaf6 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 {