|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "flag" |
5 | 4 | "fmt"
|
6 |
| - "net" |
7 |
| - "os" |
8 |
| - |
9 |
| - "github.com/cakturk/go-netstat/netstat" |
10 |
| -) |
11 |
| - |
12 |
| -var ( |
13 |
| - udp = flag.Bool("udp", false, "display UDP sockets") |
14 |
| - tcp = flag.Bool("tcp", false, "display TCP sockets") |
15 |
| - listening = flag.Bool("lis", false, "display only listening sockets") |
16 |
| - all = flag.Bool("all", false, "display both listening and non-listening sockets") |
17 |
| - resolve = flag.Bool("res", false, "lookup symbolic names for host addresses") |
18 |
| - ipv4 = flag.Bool("4", false, "display only IPv4 sockets") |
19 |
| - ipv6 = flag.Bool("6", false, "display only IPv6 sockets") |
20 |
| - help = flag.Bool("help", false, "display this help screen") |
21 |
| -) |
22 |
| - |
23 |
| -const ( |
24 |
| - protoIPv4 = 0x01 |
25 |
| - protoIPv6 = 0x02 |
| 5 | + "github.com/amit7itz/go-procnet/procnet" |
26 | 6 | )
|
27 | 7 |
|
28 | 8 | func main() {
|
29 |
| - flag.Parse() |
30 |
| - |
31 |
| - if *help { |
32 |
| - flag.Usage() |
33 |
| - os.Exit(0) |
34 |
| - } |
35 |
| - |
36 |
| - var proto uint |
37 |
| - if *ipv4 { |
38 |
| - proto |= protoIPv4 |
39 |
| - } |
40 |
| - if *ipv6 { |
41 |
| - proto |= protoIPv6 |
42 |
| - } |
43 |
| - if proto == 0x00 { |
44 |
| - proto = protoIPv4 | protoIPv6 |
| 9 | + // parsing the tcp sockets from /proc/net/tcp |
| 10 | + socks, err := procnet.TCPSocks() |
| 11 | + if err != nil { |
| 12 | + panic(err) |
45 | 13 | }
|
46 |
| - |
47 |
| - if os.Geteuid() != 0 { |
48 |
| - fmt.Println("Not all processes could be identified, you would have to be root to see it all.") |
49 |
| - } |
50 |
| - fmt.Printf("Proto %-23s %-23s %-12s %-16s\n", "Local Addr", "Foreign Addr", "State", "PID/Program name") |
51 |
| - |
52 |
| - if *udp { |
53 |
| - if proto&protoIPv4 == protoIPv4 { |
54 |
| - tabs, err := netstat.UDPSocks(netstat.NoopFilter) |
55 |
| - if err == nil { |
56 |
| - displaySockInfo("udp", tabs) |
57 |
| - } |
58 |
| - } |
59 |
| - if proto&protoIPv6 == protoIPv6 { |
60 |
| - tabs, err := netstat.UDP6Socks(netstat.NoopFilter) |
61 |
| - if err == nil { |
62 |
| - displaySockInfo("udp6", tabs) |
63 |
| - } |
64 |
| - } |
65 |
| - } else { |
66 |
| - *tcp = true |
67 |
| - } |
68 |
| - |
69 |
| - if *tcp { |
70 |
| - var fn netstat.AcceptFn |
71 |
| - |
72 |
| - switch { |
73 |
| - case *all: |
74 |
| - fn = func(*netstat.SockTabEntry) bool { return true } |
75 |
| - case *listening: |
76 |
| - fn = func(s *netstat.SockTabEntry) bool { |
77 |
| - return s.State == netstat.Listen |
78 |
| - } |
79 |
| - default: |
80 |
| - fn = func(s *netstat.SockTabEntry) bool { |
81 |
| - return s.State != netstat.Listen |
82 |
| - } |
83 |
| - } |
84 |
| - |
85 |
| - if proto&protoIPv4 == protoIPv4 { |
86 |
| - tabs, err := netstat.TCPSocks(fn) |
87 |
| - if err == nil { |
88 |
| - displaySockInfo("tcp", tabs) |
89 |
| - } |
90 |
| - } |
91 |
| - if proto&protoIPv6 == protoIPv6 { |
92 |
| - tabs, err := netstat.TCP6Socks(fn) |
93 |
| - if err == nil { |
94 |
| - displaySockInfo("tcp6", tabs) |
95 |
| - } |
96 |
| - } |
| 14 | + for _, sock := range socks { |
| 15 | + fmt.Printf("local ip: %s local port: %d remote IP: %s remote port: %d state: %s", |
| 16 | + sock.LocalAddr.IP, sock.LocalAddr.Port, sock.RemoteAddr.IP, sock.RemoteAddr.Port, sock.State) |
97 | 17 | }
|
98 |
| -} |
99 | 18 |
|
100 |
| -func displaySockInfo(proto string, s []netstat.SockTabEntry) { |
101 |
| - lookup := func(skaddr *netstat.SockAddr) string { |
102 |
| - const IPv4Strlen = 17 |
103 |
| - addr := skaddr.IP.String() |
104 |
| - if *resolve { |
105 |
| - names, err := net.LookupAddr(addr) |
106 |
| - if err == nil && len(names) > 0 { |
107 |
| - addr = names[0] |
108 |
| - } |
109 |
| - } |
110 |
| - if len(addr) > IPv4Strlen { |
111 |
| - addr = addr[:IPv4Strlen] |
112 |
| - } |
113 |
| - return fmt.Sprintf("%s:%d", addr, skaddr.Port) |
114 |
| - } |
| 19 | + // for parsing the sockets from a custom path |
| 20 | + socks, err = procnet.SocksFromPath("/proc/1234/net/udp") |
| 21 | + // ... |
115 | 22 |
|
116 |
| - for _, e := range s { |
117 |
| - p := "" |
118 |
| - if e.Process != nil { |
119 |
| - p = e.Process.String() |
120 |
| - } |
121 |
| - saddr := lookup(e.LocalAddr) |
122 |
| - daddr := lookup(e.RemoteAddr) |
123 |
| - fmt.Printf("%-5s %-23.23s %-23.23s %-12s %-16s\n", proto, saddr, daddr, e.State, p) |
124 |
| - } |
| 23 | + // for parsing the sockets from the textual content of a socket file |
| 24 | + socks, err = procnet.SocksFromText(` sl local_address remote_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode |
| 25 | + 0: 00000000000000000000000000000000:1B58 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 854815644 1 0000000000000000 100 0 0 10 0 |
| 26 | + 1: 0000000000000000FFFF0000860EA8C0:1B58 0000000000000000FFFF0000E70FA8C0:87F6 01 00000000:00000000 00:00000000 00000000 0 0 854824433 1 0000000000000000 20 4 1 10 -1`) |
| 27 | + // ... |
125 | 28 | }
|
0 commit comments