mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-06 09:16:58 +08:00
Update lsof.go
This commit is contained in:
@@ -11,15 +11,44 @@ var (
|
|||||||
ErrNotImplemented = errors.New("not implemented")
|
ErrNotImplemented = errors.New("not implemented")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func isLocalIP(host string) bool {
|
||||||
|
interfaces, err := net.Interfaces()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// all ip addresses
|
||||||
|
for _, i := range interfaces {
|
||||||
|
addrs, err := i.Addrs()
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
for _, addr := range addrs {
|
||||||
|
var ip net.IP
|
||||||
|
switch v := addr.(type) {
|
||||||
|
case *net.IPNet:
|
||||||
|
ip = v.IP
|
||||||
|
case *net.IPAddr:
|
||||||
|
ip = v.IP
|
||||||
|
}
|
||||||
|
if ip != nil && host == ip.String() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func GetProcessName(addr net.Addr) string {
|
func GetProcessName(addr net.Addr) string {
|
||||||
// set default value
|
// set default value
|
||||||
var process = "N/A"
|
var process = "N/A"
|
||||||
if addr != nil {
|
if addr != nil {
|
||||||
localHost, localPortStr, _ := net.SplitHostPort(addr.String())
|
localHost, localPortStr, _ := net.SplitHostPort(addr.String())
|
||||||
|
if isLocalIP(localHost) {
|
||||||
localPortInt, _ := strconv.Atoi(localPortStr)
|
localPortInt, _ := strconv.Atoi(localPortStr)
|
||||||
if cmd, _ := GetCommandNameBySocket(addr.Network(), localHost, uint16(localPortInt)); cmd != "" {
|
if cmd, _ := GetCommandNameBySocket(addr.Network(), localHost, uint16(localPortInt)); cmd != "" {
|
||||||
process = cmd
|
process = cmd
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return process
|
return process
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user