-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoutput.go
66 lines (55 loc) · 1.5 KB
/
output.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
package main
// Author: Glen Newton
// BSD 3-Clause License
import (
"encoding/json"
"fmt"
"log"
"time"
)
const CLOSED = "close"
const OPEN = "open"
func printFile(filename string, t *time.Time, isOpen bool, fi *FDInfo) {
if jsonOut {
printJson(filename, t, isOpen, fi)
} else {
printText(filename, t, isOpen, fi)
}
}
func printText(filename string, t *time.Time, isOpen bool, fi *FDInfo) {
si := fi.SocketInfo
status := CLOSED
if isOpen {
status = OPEN
}
if si == nil {
fmt.Printf("%s "+status+" %s\n", t.Format("2006-01-02T15:04:05.999999-07:00"), filename)
} else {
if fi.Type == TCPSocket {
fmt.Printf("%s "+status+" %s %s %s:%d %s\n", t.Format("2006-01-02T15:04:05.999999-07:00"), filename, fi.Type, si.Tcp.RemoteIP, si.Tcp.RemotePort, getRemoteHostname(si.Tcp.RemoteIP.String()))
} else {
if si.Unix != nil && si.Unix.Path == "" {
si.Unix.Path = EmptyValue
fmt.Printf("%s "+status+" %s %s %s\n", t.Format("2006-01-02T15:04:05.999999-07:00"), filename, fi.Type, si.Unix.Path)
} else {
fmt.Printf("%s "+status+" %s %s %s\n", t.Format("2006-01-02T15:04:05.999999-07:00"), filename, fi.Type)
}
}
}
}
func printJson(filename string, t *time.Time, isOpen bool, fi *FDInfo) {
si := fi.SocketInfo
if si != nil && si.Tcp != nil {
si.Tcp.RemoteHostName = getRemoteHostname(si.Tcp.RemoteIP.String())
}
if isOpen {
fi.Status = OPEN
} else {
fi.Status = CLOSED
}
jsonBytes, err := json.Marshal(fi)
if err != nil {
log.Println(err)
}
fmt.Println(string(jsonBytes))
}