diff --git a/proc/proc.go b/proc/proc.go index a3406dd..939ebc4 100644 --- a/proc/proc.go +++ b/proc/proc.go @@ -4,21 +4,21 @@ import ( "encoding/hex" "errors" "fmt" + "github.com/subgraph/fw-daemon/Godeps/_workspace/src/github.com/op/go-logging" "io/ioutil" "net" "strconv" "strings" - "github.com/subgraph/fw-daemon/Godeps/_workspace/src/github.com/op/go-logging" ) var log = logging.MustGetLogger("proc") + func SetLogger(logger *logging.Logger) { log = logger } var pcache = &pidCache{} - func LookupUDPSocketProcess(srcPort uint16) *ProcInfo { ss := findUDPSocket(srcPort) if ss == nil { @@ -36,8 +36,8 @@ func LookupTCPSocketProcess(srcPort uint16, dstAddr net.IP, dstPort uint16) *Pro } type ConnectionInfo struct { - pinfo *ProcInfo - local *socketAddr + pinfo *ProcInfo + local *socketAddr remote *socketAddr } @@ -63,7 +63,6 @@ func (sa *socketAddr) parse(s string) error { return nil } - func ParseIp(ip string) (net.IP, error) { var result net.IP dst, err := hex.DecodeString(ip) @@ -88,7 +87,7 @@ func ParsePort(port string) (uint16, error) { } func getConnections() ([]*ConnectionInfo, error) { - conns,err := readConntrack() + conns, err := readConntrack() if err != nil { return nil, err } @@ -98,7 +97,7 @@ func getConnections() ([]*ConnectionInfo, error) { func resolveProcinfo(conns []*ConnectionInfo) { var sockets []*socketStatus - for _,line := range getSocketLines("tcp") { + for _, line := range getSocketLines("tcp") { if len(strings.TrimSpace(line)) == 0 { continue } @@ -107,16 +106,16 @@ func resolveProcinfo(conns []*ConnectionInfo) { log.Warning("Unable to parse line [%s]: %v", line, err) } else { /* - pid := findPidForInode(ss.inode) - if pid > 0 { - ss.pid = pid - fmt.Println("Socket", ss) - sockets = append(sockets, ss) - } + pid := findPidForInode(ss.inode) + if pid > 0 { + ss.pid = pid + fmt.Println("Socket", ss) + sockets = append(sockets, ss) + } */ } } - for _,ci := range conns { + for _, ci := range conns { ss := findContrackSocket(ci, sockets) if ss == nil { continue @@ -129,7 +128,7 @@ func resolveProcinfo(conns []*ConnectionInfo) { } func findContrackSocket(ci *ConnectionInfo, sockets []*socketStatus) *socketStatus { - for _,ss := range sockets { + for _, ss := range sockets { if ss.local.port == ci.local.port && ss.remote.ip.Equal(ci.remote.ip) && ss.remote.port == ci.remote.port { return ss } @@ -145,8 +144,8 @@ func readConntrack() ([]*ConnectionInfo, error) { } var result []*ConnectionInfo lines := strings.Split(string(data), "\n") - for _,line := range(lines) { - ci,err := parseConntrackLine(line) + for _, line := range lines { + ci, err := parseConntrackLine(line) if err != nil { return nil, err } @@ -163,33 +162,33 @@ func parseConntrackLine(line string) (*ConnectionInfo, error) { return nil, nil } - local,err := conntrackAddr(parts[4], parts[6]) + local, err := conntrackAddr(parts[4], parts[6]) if err != nil { return nil, err } - remote,err := conntrackAddr(parts[5], parts[7]) + remote, err := conntrackAddr(parts[5], parts[7]) if err != nil { return nil, err } return &ConnectionInfo{ - local: local, + local: local, remote: remote, - },nil + }, nil } func conntrackAddr(ip_str, port_str string) (*socketAddr, error) { ip := net.ParseIP(stripLabel(ip_str)) if ip == nil { - return nil, errors.New("Could not parse IP: "+ip_str) + return nil, errors.New("Could not parse IP: " + ip_str) } i64, err := strconv.Atoi(stripLabel(port_str)) if err != nil { return nil, err } return &socketAddr{ - ip: ip, + ip: ip, port: uint16(i64), - },nil + }, nil } func stripLabel(s string) string { diff --git a/proc/proc_pid.go b/proc/proc_pid.go index 1536b8c..2000643 100644 --- a/proc/proc_pid.go +++ b/proc/proc_pid.go @@ -1,39 +1,38 @@ package proc import ( + "fmt" + "io/ioutil" "os" + "path" "strconv" - "fmt" "strings" - "path" - "io/ioutil" "sync" "syscall" ) - type ProcInfo struct { - Uid int + Uid int Pid int - loaded bool + loaded bool ExePath string CmdLine string } type pidCache struct { cacheMap map[uint64]*ProcInfo - lock sync.Mutex + lock sync.Mutex } func (pc *pidCache) lookup(inode uint64) *ProcInfo { pc.lock.Lock() defer pc.lock.Unlock() - pi,ok := pc.cacheMap[inode] + pi, ok := pc.cacheMap[inode] if ok && pi.loadProcessInfo() { return pi } pc.cacheMap = loadCache() - pi,ok = pc.cacheMap[inode] + pi, ok = pc.cacheMap[inode] if ok && pi.loadProcessInfo() { return pi } @@ -46,7 +45,7 @@ func loadCache() map[uint64]*ProcInfo { pid := toPid(n) if pid != 0 { pinfo := &ProcInfo{Pid: pid} - for _,inode := range inodesFromPid(pid) { + for _, inode := range inodesFromPid(pid) { cmap[inode] = pinfo } } @@ -60,7 +59,7 @@ func toPid(name string) int { return 0 } fdpath := fmt.Sprintf("/proc/%d/fd", pid) - fi,err := os.Stat(fdpath) + fi, err := os.Stat(fdpath) if err != nil { return 0 } @@ -91,8 +90,8 @@ func extractSocket(name string) uint64 { if !strings.HasPrefix(name, "socket:[") || !strings.HasSuffix(name, "]") { return 0 } - val := name[8:len(name)-1] - inode,err := strconv.ParseUint(val, 10, 64) + val := name[8 : len(name)-1] + inode, err := strconv.ParseUint(val, 10, 64) if err != nil { log.Warning("Error parsing inode value from %s: %v", name, err) return 0 @@ -101,7 +100,7 @@ func extractSocket(name string) uint64 { } func readdir(dir string) []string { - d,err := os.Open(dir) + d, err := os.Open(dir) if err != nil { log.Warning("Error opening directory %s: %v", dir, err) return nil diff --git a/proc/socket.go b/proc/socket.go index a8a0d4d..7792ab9 100644 --- a/proc/socket.go +++ b/proc/socket.go @@ -1,15 +1,16 @@ package proc + import ( - "net" + "errors" "fmt" "io/ioutil" - "strings" - "errors" + "net" "strconv" + "strings" ) type socketAddr struct { - ip net.IP + ip net.IP port uint16 } @@ -18,11 +19,11 @@ func (sa socketAddr) String() string { } type socketStatus struct { - local socketAddr + local socketAddr remote socketAddr - uid int - inode uint64 - line string + uid int + inode uint64 + line string } func (ss *socketStatus) String() string { @@ -43,7 +44,7 @@ func findTCPSocket(srcPort uint16, dstAddr net.IP, dstPort uint16) *socketStatus func findSocket(proto string, matcher func(socketStatus) bool) *socketStatus { var ss socketStatus - for _,line := range getSocketLines(proto) { + for _, line := range getSocketLines(proto) { if len(line) == 0 { continue } @@ -83,7 +84,6 @@ func (ss *socketStatus) parseLine(line string) error { return nil } - func getSocketLines(proto string) []string { path := fmt.Sprintf("/proc/net/%s", proto) data, err := ioutil.ReadFile(path)