update lsof code

This commit is contained in:
Jason
2019-08-13 12:05:00 +08:00
parent af574d4b65
commit d463bd9263
4 changed files with 17 additions and 12 deletions

10
common/lsof/lsof.go Normal file
View File

@@ -0,0 +1,10 @@
package lsof
import (
"errors"
)
var (
ErrNotFound = errors.New("not found")
ErrNotImplemented = errors.New("not implemented")
)

View File

@@ -2,10 +2,6 @@
package lsof package lsof
import (
"errors"
)
func GetCommandNameBySocket(network string, addr string, port uint16) (string, error) { func GetCommandNameBySocket(network string, addr string, port uint16) (string, error) {
return "", errors.New("not implemented") return "", ErrNotImplemented
} }

View File

@@ -20,7 +20,7 @@ func GetCommandNameBySocket(network string, addr string, port uint16) (string, e
// listen on the same udp port. Moreover, if // listen on the same udp port. Moreover, if
// the application closes the socket immediately // the application closes the socket immediately
// after sending out the packet (e.g. it just // after sending out the packet (e.g. it just
// uploading data but not receving any data), // uploading data but not receiving any data),
// we may not be able to find it. // we may not be able to find it.
pattern = fmt.Sprintf("-i%s:%d", network, port) pattern = fmt.Sprintf("-i%s:%d", network, port)
default: default:
@@ -38,8 +38,8 @@ func GetCommandNameBySocket(network string, addr string, port uint16) (string, e
// sockets in the list, just take // sockets in the list, just take
// the first one for simplicity. // the first one for simplicity.
if strings.HasPrefix(line, "c") { if strings.HasPrefix(line, "c") {
return line[1:len(line)], nil return line[1:], nil
} }
} }
return "", errors.New("not found") return "", ErrNotFound
} }

View File

@@ -3,7 +3,6 @@
package lsof package lsof
import ( import (
"errors"
"fmt" "fmt"
"syscall" "syscall"
"unsafe" "unsafe"
@@ -24,7 +23,7 @@ func GetCommandNameBySocket(network string, addr string, port uint16) (string, e
return getNameByPid(uint32(row.OwningPid)) return getNameByPid(uint32(row.OwningPid))
} }
} }
return "", errors.New("not found") return "", ErrNotFound
case "udp": case "udp":
var udpTable win.MIB_UDPTABLE_OWNER_PID var udpTable win.MIB_UDPTABLE_OWNER_PID
err := getUdpTable( err := getUdpTable(
@@ -56,9 +55,9 @@ func GetCommandNameBySocket(network string, addr string, port uint16) (string, e
// } // }
// } // }
return "", errors.New("not found") return "", ErrNotFound
default: default:
return "", errors.New("not found") return "", ErrNotFound
} }
} }