|
@@ -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 {
|