Skip to content

Commit 538639b

Browse files
authored
fix (*service).Ports() missing ports from IPv6 (#1069)
1 parent 39535d6 commit 538639b

File tree

3 files changed

+6
-56
lines changed

3 files changed

+6
-56
lines changed

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.20
44

55
require (
66
github.com/Curtis-Milo/nat-type-identifier-go v0.0.0-20220215191915-18d42168c63d
7-
github.com/IceWhaleTech/CasaOS-Common v0.4.4-alpha4
7+
github.com/IceWhaleTech/CasaOS-Common v0.4.4-alpha8
88
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
99
github.com/deckarep/golang-set/v2 v2.3.0
1010
github.com/deepmap/oapi-codegen v1.12.4
@@ -33,7 +33,6 @@ require (
3333
github.com/patrickmn/go-cache v2.1.0+incompatible
3434
github.com/pkg/errors v0.9.1
3535
github.com/robfig/cron/v3 v3.0.1
36-
github.com/samber/lo v1.38.1
3736
github.com/satori/go.uuid v1.2.0
3837
github.com/shirou/gopsutil/v3 v3.23.2
3938
github.com/sirupsen/logrus v1.9.0
@@ -107,6 +106,7 @@ require (
107106
github.com/pmezard/go-difflib v1.0.0 // indirect
108107
github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect
109108
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
109+
github.com/samber/lo v1.38.1 // indirect
110110
github.com/tidwall/match v1.1.1 // indirect
111111
github.com/tidwall/pretty v1.2.1 // indirect
112112
github.com/tklauser/go-sysconf v0.3.11 // indirect

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
github.com/Curtis-Milo/nat-type-identifier-go v0.0.0-20220215191915-18d42168c63d h1:62lEBImTxZ83pgzywgDNIrPPuQ+j4ep9QjqrWBn1hrU=
22
github.com/Curtis-Milo/nat-type-identifier-go v0.0.0-20220215191915-18d42168c63d/go.mod h1:lW9x+yEjqKdPbE3+cf2fGPJXCw/hChX3Omi9QHTLFsQ=
3-
github.com/IceWhaleTech/CasaOS-Common v0.4.4-alpha4 h1:KIMQL8fumAczZEsd7uC7n2NUzBYUC4DntRc8usSxGq8=
4-
github.com/IceWhaleTech/CasaOS-Common v0.4.4-alpha4/go.mod h1:2IuYyy5qW1BE6jqC6M+tOU+WtUec1K565rLATBJ9p/0=
3+
github.com/IceWhaleTech/CasaOS-Common v0.4.4-alpha8 h1:UhCg3d9Cxhx7KVmqh8oUrUl1qFmFdcHee3Zkk4+P2JA=
4+
github.com/IceWhaleTech/CasaOS-Common v0.4.4-alpha8/go.mod h1:2IuYyy5qW1BE6jqC6M+tOU+WtUec1K565rLATBJ9p/0=
55
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
66
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
77
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=

service/health.go

+2-52
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
package service
22

33
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"
135
"github.com/IceWhaleTech/CasaOS-Common/utils/systemctl"
146
)
157

@@ -45,49 +37,7 @@ func (s *service) Services() (map[bool]*[]string, error) {
4537
}
4638

4739
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()
9141
}
9242

9343
func NewHealthService() HealthService {

0 commit comments

Comments
 (0)