|
|
|
@ -87,17 +87,29 @@ func NetSpeedsByName(duration time.Duration, name string) (NetSpeed, error) {
|
|
|
|
|
|
|
|
|
|
// NetConnections return all TCP/UDP/UNIX DOMAIN SOCKET Connections
|
|
|
|
|
// if your uid != 0 ,and analysePid==true ,you should have CAP_SYS_PRTACE and CAP_DAC_OVERRIDE/CAP_DAC_READ_SEARCH Caps
|
|
|
|
|
func NetConnections(analysePid bool) ([]NetConn, error) {
|
|
|
|
|
func NetConnections(analysePid bool,types string) ([]NetConn, error) {
|
|
|
|
|
var result []NetConn
|
|
|
|
|
var inodeMap map[string]int64
|
|
|
|
|
var err error
|
|
|
|
|
fileList := []string{
|
|
|
|
|
var fileList []string
|
|
|
|
|
if types=="" || strings.Contains(strings.ToLower(types),"all") {
|
|
|
|
|
fileList = []string{
|
|
|
|
|
"/proc/net/tcp",
|
|
|
|
|
"/proc/net/tcp6",
|
|
|
|
|
"/proc/net/udp",
|
|
|
|
|
"/proc/net/udp6",
|
|
|
|
|
"/proc/net/unix",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if strings.Contains(strings.ToLower(types),"tcp") {
|
|
|
|
|
fileList =append(fileList,"/proc/net/tcp","/proc/net/tcp6")
|
|
|
|
|
}
|
|
|
|
|
if strings.Contains(strings.ToLower(types),"udp") {
|
|
|
|
|
fileList =append(fileList,"/proc/net/udp","/proc/net/udp6")
|
|
|
|
|
}
|
|
|
|
|
if strings.Contains(strings.ToLower(types),"unix") {
|
|
|
|
|
fileList =append(fileList,"/proc/net/unix")
|
|
|
|
|
}
|
|
|
|
|
if analysePid {
|
|
|
|
|
inodeMap, err = GetInodeMap()
|
|
|
|
|
if err != nil {
|
|
|
|
@ -135,6 +147,9 @@ func GetInodeMap() (map[string]int64, error) {
|
|
|
|
|
if err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(socket, "socket") {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
start := strings.Index(socket, "[")
|
|
|
|
|
if start < 0 {
|
|
|
|
|
continue
|
|
|
|
@ -147,7 +162,7 @@ func GetInodeMap() (map[string]int64, error) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil, err
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func analyseNetFiles(data []byte, inodeMap map[string]int64, typed string) ([]NetConn, error) {
|
|
|
|
@ -177,6 +192,60 @@ func analyseNetFiles(data []byte, inodeMap map[string]int64, typed string) ([]Ne
|
|
|
|
|
}
|
|
|
|
|
res.RemoteAddr = ip
|
|
|
|
|
res.RemotePort = port
|
|
|
|
|
//connection state
|
|
|
|
|
if strings.Contains(typed, "tcp") {
|
|
|
|
|
state, err := strconv.ParseInt(strings.TrimSpace(v[3]), 16, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return result, err
|
|
|
|
|
}
|
|
|
|
|
res.Status = TCP_STATE[state]
|
|
|
|
|
}
|
|
|
|
|
txrx_queue := strings.Split(strings.TrimSpace(v[4]), ":")
|
|
|
|
|
if len(txrx_queue) != 2 {
|
|
|
|
|
return result, errors.New("not a valid net file")
|
|
|
|
|
}
|
|
|
|
|
tx_queue, err := strconv.ParseInt(txrx_queue[0], 16, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return result, err
|
|
|
|
|
}
|
|
|
|
|
res.TX_Queue = tx_queue
|
|
|
|
|
rx_queue, err := strconv.ParseInt(txrx_queue[1], 16, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return result, err
|
|
|
|
|
}
|
|
|
|
|
res.RX_Queue = rx_queue
|
|
|
|
|
timer := strings.Split(strings.TrimSpace(v[5]), ":")
|
|
|
|
|
if len(timer) != 2 {
|
|
|
|
|
return result, errors.New("not a valid net file")
|
|
|
|
|
}
|
|
|
|
|
switch timer[0] {
|
|
|
|
|
case "00":
|
|
|
|
|
res.TimerActive = "NO_TIMER"
|
|
|
|
|
case "01":
|
|
|
|
|
//重传定时器
|
|
|
|
|
res.TimerActive = "RETRANSMIT"
|
|
|
|
|
case "02":
|
|
|
|
|
//连接定时器、FIN_WAIT_2定时器或TCP保活定时器
|
|
|
|
|
res.TimerActive = "KEEPALIVE"
|
|
|
|
|
case "03":
|
|
|
|
|
//TIME_WAIT定时器
|
|
|
|
|
res.TimerActive = "TIME_WAIT"
|
|
|
|
|
case "04":
|
|
|
|
|
//持续定时器
|
|
|
|
|
res.TimerActive = "ZERO_WINDOW_PROBE"
|
|
|
|
|
default:
|
|
|
|
|
res.TimerActive = "UNKNOWN"
|
|
|
|
|
}
|
|
|
|
|
timerJif, err := strconv.ParseInt(timer[1], 16, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return result, err
|
|
|
|
|
}
|
|
|
|
|
res.TimerJiffies = timerJif
|
|
|
|
|
timerCnt, err := strconv.ParseInt(strings.TrimSpace(v[6]), 16, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return result, err
|
|
|
|
|
}
|
|
|
|
|
res.RtoTimer = timerCnt
|
|
|
|
|
res.Uid, err = strconv.ParseInt(v[7], 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return result, err
|
|
|
|
@ -229,7 +298,7 @@ func analyseUnixFiles(data []byte, inodeMap map[string]int64, typed string) ([]N
|
|
|
|
|
res.Pid = -1
|
|
|
|
|
} else {
|
|
|
|
|
_, ok := pidMap[res.Pid]
|
|
|
|
|
if !ok {
|
|
|
|
|
if !ok || pidMap[res.Pid] == nil {
|
|
|
|
|
tmp, err := FindProcessByPid(res.Pid)
|
|
|
|
|
if err != nil {
|
|
|
|
|
pidMap[res.Pid] = nil
|
|
|
|
@ -237,10 +306,12 @@ func analyseUnixFiles(data []byte, inodeMap map[string]int64, typed string) ([]N
|
|
|
|
|
pidMap[res.Pid] = &tmp
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if pidMap[res.Pid] != nil {
|
|
|
|
|
res.Uid = int64(pidMap[res.Pid].RUID)
|
|
|
|
|
res.Process = pidMap[res.Pid]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
res.Typed = typed
|
|
|
|
|
result = append(result, res)
|
|
|
|
|
}
|
|
|
|
|