|
1 | 1 | package service
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "bufio" |
5 |
| - "errors" |
6 |
| - "fmt" |
7 |
| - "os" |
8 |
| - "strconv" |
9 |
| - "strings" |
10 |
| - |
11 |
| - "github.com/samber/lo" |
12 |
| - |
| 4 | + "github.com/IceWhaleTech/CasaOS-Common/utils/port" |
13 | 5 | "github.com/IceWhaleTech/CasaOS-Common/utils/systemctl"
|
14 | 6 | )
|
15 | 7 |
|
@@ -45,49 +37,7 @@ func (s *service) Services() (map[bool]*[]string, error) {
|
45 | 37 | }
|
46 | 38 |
|
47 | 39 | func (s *service) Ports() ([]int, []int, error) {
|
48 |
| - usedPorts := map[string]map[int]struct{}{ |
49 |
| - "tcp": {}, |
50 |
| - "udp": {}, |
51 |
| - } |
52 |
| - |
53 |
| - for _, protocol := range []string{"tcp", "udp"} { |
54 |
| - filename := fmt.Sprintf("/proc/net/%s", protocol) |
55 |
| - |
56 |
| - file, err := os.Open(filename) |
57 |
| - if err != nil { |
58 |
| - return nil, nil, errors.New("Failed to open " + filename) |
59 |
| - } |
60 |
| - defer file.Close() |
61 |
| - |
62 |
| - scanner := bufio.NewScanner(file) |
63 |
| - for scanner.Scan() { |
64 |
| - line := scanner.Text() |
65 |
| - fields := strings.Fields(line) |
66 |
| - if len(fields) < 2 { |
67 |
| - continue |
68 |
| - } |
69 |
| - |
70 |
| - localAddress := fields[1] |
71 |
| - addressParts := strings.Split(localAddress, ":") |
72 |
| - if len(addressParts) < 2 { |
73 |
| - continue |
74 |
| - } |
75 |
| - |
76 |
| - portHex := addressParts[1] |
77 |
| - port, err := strconv.ParseInt(portHex, 16, 0) |
78 |
| - if err != nil { |
79 |
| - continue |
80 |
| - } |
81 |
| - |
82 |
| - usedPorts[protocol][int(port)] = struct{}{} |
83 |
| - } |
84 |
| - |
85 |
| - if err := scanner.Err(); err != nil { |
86 |
| - return nil, nil, errors.New("Error reading from " + filename) |
87 |
| - } |
88 |
| - } |
89 |
| - |
90 |
| - return lo.Keys(usedPorts["tcp"]), lo.Keys(usedPorts["udp"]), nil |
| 40 | + return port.ListPortsInUse() |
91 | 41 | }
|
92 | 42 |
|
93 | 43 | func NewHealthService() HealthService {
|
|
0 commit comments