-
Notifications
You must be signed in to change notification settings - Fork 58
/
netstat_test.go
144 lines (117 loc) · 2.98 KB
/
netstat_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package netflow
import (
"encoding/json"
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
// func TestGetInfos(t *testing.T) {
// conns, _ := netstat("tcp")
// bs, err := json.MarshalIndent(conns, "", " ")
// assert.Equal(t, nil, err)
// t.Log(string(bs))
// for _, conn := range conns {
// ConnInodeHash.Add(conn.Addr, conn.Inode)
// }
// fmt.Println("conns", ConnInodeHash.String())
// ps, err := GetProcesses()
// assert.Equal(t, nil, err)
// for _, p := range ps {
// for _, inode := range p.Inodes {
// InodePidsHash.Add(inode, p.Pid)
// }
// }
// fmt.Println("processes", InodePidsHash.String())
// }
func TestParseAddr(t *testing.T) {
ip, port := parseAddr("2E0010AC:E898")
assert.Equal(t, ip, "172.16.0.46")
assert.Equal(t, port, "59544")
}
func TestHandleFile(t *testing.T) {
for _, fdn := range []string{"0", "1", "2", "3", "4"} {
name := "/proc/994998/fd/" + fdn
kk, err := os.Readlink(name)
fmt.Println(kk, err)
// 只是针对 unix socket
// fileinfo, err := os.Lstat(name)
// assert.Equal(t, nil, err)
// name = "/tmp/mongodb-27017.sock"
// fmt.Println("socket: ", fileinfo.Mode()&os.ModeSocket == os.ModeSocket)
// fmt.Println("symlink: ", fileinfo.Mode()&os.ModeSymlink == os.ModeSymlink)
// if fileinfo.Mode()&os.ModeSocket == os.ModeSocket {
// fmt.Println("this is socket")
// }
// if fileinfo.Mode() == os.ModeSocket {
// fmt.Println("this is socket")
// }
// fmt.Println(fileinfo.Mode().String())
// stat, _ := fileinfo.Sys().(*syscall.Stat_t)
// fmt.Println("socket222: ", stat.Mode, fileinfo.Name())
// fmt.Println("socket333: ", fileinfo.Mode())
// fmt.Println("socket444: ", KindFromFileInfo(fileinfo) == SOCKET)
// fmt.Println("socket555: ", fileinfo.Mode()&os.ModeSocket)
}
}
// https://www.cyub.vip/2020/11/22/Go%E8%AF%AD%E8%A8%80%E5%AE%9E%E7%8E%B0%E7%AE%80%E6%98%93%E7%89%88netstat%E5%91%BD%E4%BB%A4/
type Kind int
const (
DIR Kind = iota
LINK
PIPE
SOCKET
DEV
FILE
)
func KindFromFileInfo(fileInfo os.FileInfo) Kind {
if fileInfo.IsDir() {
return DIR
}
if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
return LINK
}
if fileInfo.Mode()&os.ModeNamedPipe == os.ModeNamedPipe {
return PIPE
}
if fileInfo.Mode()&os.ModeSocket == os.ModeSocket {
return SOCKET
}
if fileInfo.Mode()&os.ModeDevice == os.ModeDevice {
return DEV
}
return FILE
}
func (self Kind) MarshalJSON() ([]byte, error) {
return json.Marshal(self.String())
}
func (self *Kind) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
switch s {
case "DIR":
*self = DIR
case "LINK":
*self = LINK
case "PIPE":
*self = PIPE
case "SOCKET":
*self = SOCKET
case "DEV":
*self = DEV
case "FILE":
*self = FILE
default:
return fmt.Errorf("invalid Kind: '%s'", s)
}
return nil
}
func (self Kind) String() string {
names := []string{
"DIR", "LINK", "PIPE", "SOCKET", "DEV", "FILE",
}
// FIXME: bound check?
return names[self]
}