mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-10-08 06:26:38 +08:00
fix: 解决系统不存在 netstat 命令的问题 (#588)
This commit is contained in:
parent
f092927ab8
commit
aeb9135cde
2 changed files with 8 additions and 24 deletions
|
@ -114,8 +114,10 @@ func (u *FirewallService) SearchWithPage(req dto.RuleSearch) (int64, interface{}
|
||||||
if req.Type == "port" {
|
if req.Type == "port" {
|
||||||
apps := u.loadPortByApp()
|
apps := u.loadPortByApp()
|
||||||
for i := 0; i < len(backDatas); i++ {
|
for i := 0; i < len(backDatas); i++ {
|
||||||
backDatas[i].IsUsed = common.ScanPortWithProtocol(backDatas[i].Port, backDatas[i].Protocol)
|
port, _ := strconv.Atoi(backDatas[i].Port)
|
||||||
|
backDatas[i].IsUsed = common.ScanPort(port)
|
||||||
if backDatas[i].Protocol == "udp" {
|
if backDatas[i].Protocol == "udp" {
|
||||||
|
backDatas[i].IsUsed = common.ScanUDPPort(port)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, app := range apps {
|
for _, app := range apps {
|
||||||
|
@ -368,10 +370,7 @@ func (u *FirewallService) loadPortByApp() []portOfApp {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *FirewallService) PingStatus() (string, error) {
|
func (u *FirewallService) PingStatus() (string, error) {
|
||||||
stdout, err := cmd.Exec("sudo cat /etc/sysctl.conf | grep net/ipv4/icmp_echo_ignore_all= ")
|
stdout, _ := cmd.Exec("sudo cat /etc/sysctl.conf | grep net/ipv4/icmp_echo_ignore_all= ")
|
||||||
if err != nil {
|
|
||||||
return constant.StatusDisable, fmt.Errorf("load firewall ping status failed, err: %s", stdout)
|
|
||||||
}
|
|
||||||
if stdout == "net/ipv4/icmp_echo_ignore_all=1\n" {
|
if stdout == "net/ipv4/icmp_echo_ignore_all=1\n" {
|
||||||
return constant.StatusEnable, nil
|
return constant.StatusEnable, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,8 +11,6 @@ import (
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func CompareVersion(version1 string, version2 string) bool {
|
func CompareVersion(version1 string, version2 string) bool {
|
||||||
|
@ -85,7 +83,6 @@ func RandStr(n int) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ScanPort(port int) bool {
|
func ScanPort(port int) bool {
|
||||||
|
|
||||||
ln, err := net.Listen("tcp", ":"+strconv.Itoa(port))
|
ln, err := net.Listen("tcp", ":"+strconv.Itoa(port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return true
|
return true
|
||||||
|
@ -94,24 +91,12 @@ func ScanPort(port int) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func ScanPortWithProtocol(port, Protocol string) bool {
|
func ScanUDPPort(port int) bool {
|
||||||
command := "netstat -ntpl"
|
ln, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: port})
|
||||||
if Protocol == "udp" {
|
|
||||||
command = "netstat -nupl"
|
|
||||||
}
|
|
||||||
stdout, err := cmd.Execf("%s | awk '{print $4}' ", command)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return true
|
||||||
}
|
|
||||||
lines := strings.Split(stdout, "\n")
|
|
||||||
if len(lines) == 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
for _, line := range lines {
|
|
||||||
if strings.HasSuffix(line, ":"+port) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
defer ln.Close()
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue