| 1 | package graphscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/ |
| 2 | |
| 3 | import ( |
| 4 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src go1.16 fmt/" |
| 6 | "github.com/bettercap/bettercap/sessionscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/" |
| 7 | "github.com/evilsocket/islazy/fsscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/fs`/" |
| 8 | "pathscip-go gomod github.com/golang/go/src go1.16 path/" |
| 9 | "syncscip-go gomod github.com/golang/go/src go1.16 sync/" |
| 10 | "timescip-go gomod github.com/golang/go/src go1.16 time/" |
| 11 | ) |
| 12 | |
| 13 | var Loadedscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Loaded. = (* Graphscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#)(nil) |
| 14 | |
| 15 | type NodeCallbackscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/NodeCallback# func(*Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#) |
| 16 | type EdgeCallbackscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/EdgeCallback# func(*Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#, []Edgescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edge#, *Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#) |
| 17 | |
| 18 | type Graphscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph# struct { |
| 19 | syncscip-go gomod github.com/golang/go/src go1.16 sync/.Mutexscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#Mutex. |
| 20 | |
| 21 | pathscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#path. string |
| 22 | edgesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#edges. *Edgesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edges# |
| 23 | } |
| 24 | |
| 25 | func NewGraphscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/NewGraph().(path string) (*Graphscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#, error) { |
| 26 | if edges, err := LoadEdgesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/LoadEdges().(path); err != nil { |
| 27 | return nil, err |
| 28 | } else { |
| 29 | Loadedscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Loaded. = &Graphscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#{ |
| 30 | pathscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#path.: path, |
| 31 | edgesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#edges.: edges, |
| 32 | } |
| 33 | return Loadedscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Loaded., nil |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func (g *Graphscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#) EachNodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#EachNode().(cb NodeCallbackscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/NodeCallback#) error { |
| 38 | g.Lockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Lock().() |
| 39 | defer g.Unlockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Unlock().() |
| 40 | |
| 41 | for _, nodeType := range NodeTypesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/NodeTypes. { |
| 42 | err := fsscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/fs`/.Globscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/fs`/Glob().(g.pathscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#path., fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("%s_*.json", nodeType), func(fileName string) error { |
| 43 | if node, err := ReadNodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/ReadNode().(fileName); err != nil { |
| 44 | return err |
| 45 | } else { |
| 46 | cb(node) |
| 47 | } |
| 48 | return nil |
| 49 | }) |
| 50 | if err != nil { |
| 51 | return err |
| 52 | } |
| 53 | } |
| 54 | return nil |
| 55 | } |
| 56 | |
| 57 | func (g *Graphscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#) EachEdgescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#EachEdge().(cb EdgeCallbackscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/EdgeCallback#) error { |
| 58 | g.Lockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Lock().() |
| 59 | defer g.Unlockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Unlock().() |
| 60 | |
| 61 | return g.edgesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#edges..ForEachEdgescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edges#ForEachEdge().(func(fromID string, edges []Edgescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edge#, toID string) error { |
| 62 | var left, right *Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node# |
| 63 | var err error |
| 64 | |
| 65 | leftFileName := pathscip-go gomod github.com/golang/go/src go1.16 path/.Joinscip-go gomod github.com/golang/go/src go1.16 path/Join().(g.pathscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#path., fromID+".json") |
| 66 | rightFileName := pathscip-go gomod github.com/golang/go/src go1.16 path/.Joinscip-go gomod github.com/golang/go/src go1.16 path/Join().(g.pathscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#path., toID+".json") |
| 67 | |
| 68 | if left, err = ReadNodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/ReadNode().(leftFileName); err != nil { |
| 69 | return err |
| 70 | } else if right, err = ReadNodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/ReadNode().(rightFileName); err != nil { |
| 71 | return err |
| 72 | } |
| 73 | |
| 74 | cb(left, edges, right) |
| 75 | |
| 76 | return nil |
| 77 | }) |
| 78 | } |
| 79 | |
| 80 | func (g *Graphscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#) Traversescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#Traverse().(root string, onNode NodeCallbackscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/NodeCallback#, onEdge EdgeCallbackscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/EdgeCallback#) error { |
| 81 | if root == "" { |
| 82 | // traverse the entire graph |
| 83 | if err := g.EachNodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#EachNode().(onNode); err != nil { |
| 84 | return err |
| 85 | } else if err = g.EachEdgescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#EachEdge().(onEdge); err != nil { |
| 86 | return err |
| 87 | } |
| 88 | } else { |
| 89 | // start by a specific node |
| 90 | roots, err := g.FindOtherTypesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#FindOtherTypes().("", root) |
| 91 | if err != nil { |
| 92 | return err |
| 93 | } |
| 94 | |
| 95 | stack := NewStackscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/NewStack().() |
| 96 | for _, root := range roots { |
| 97 | stack.Pushscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Stack#Push().(root) |
| 98 | } |
| 99 | |
| 100 | type edgeBucket struct { |
| 101 | left *Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node# |
| 102 | edges []Edgescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edge# |
| 103 | right *Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node# |
| 104 | } |
| 105 | |
| 106 | allEdges := make([]edgeBucket, 0) |
| 107 | visited := make(map[string]bool) |
| 108 | |
| 109 | for { |
| 110 | if last := stack.Popscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Stack#Pop().(); last == nil { |
| 111 | break |
| 112 | } else { |
| 113 | node := last.(*Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#) |
| 114 | nodeID := node.Stringscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#String().() |
| 115 | if _, found := visited[nodeID]; found { |
| 116 | continue |
| 117 | } else { |
| 118 | visited[nodeID] = true |
| 119 | } |
| 120 | |
| 121 | onNode(node) |
| 122 | |
| 123 | // collect all edges starting from this node |
| 124 | err = g.edgesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#edges..ForEachEdgeFromscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edges#ForEachEdgeFrom().(nodeID, func(_ string, edges []Edgescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edge#, toID string) error { |
| 125 | rightFileName := pathscip-go gomod github.com/golang/go/src go1.16 path/.Joinscip-go gomod github.com/golang/go/src go1.16 path/Join().(g.pathscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#path., toID+".json") |
| 126 | if right, err := ReadNodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/ReadNode().(rightFileName); err != nil { |
| 127 | return err |
| 128 | } else { |
| 129 | // collect new node |
| 130 | if _, found := visited[toID]; !found { |
| 131 | stack.Pushscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Stack#Push().(right) |
| 132 | } |
| 133 | // collect all edges, we'll emit this later |
| 134 | allEdges = append(allEdges, edgeBucket{ |
| 135 | left: node, |
| 136 | edges: edges, |
| 137 | right: right, |
| 138 | }) |
| 139 | } |
| 140 | return nil |
| 141 | }) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | for _, edge := range allEdges { |
| 146 | onEdge(edge.left, edge.edges, edge.right) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return nil |
| 151 | } |
| 152 | |
| 153 | func (g *Graphscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#) IsConnectedscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#IsConnected().(nodeType string, nodeID string) bool { |
| 154 | return g.edgesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#edges..IsConnectedscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edges#IsConnected().(fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("%s_%s", nodeType, nodeID)) |
| 155 | } |
| 156 | |
| 157 | func (g *Graphscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#) Dotscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#Dot().(filter, layout, name string, disconnected bool) (string, int, int, error) { |
| 158 | size := 0 |
| 159 | discarded := 0 |
| 160 | |
| 161 | data := fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("digraph %s {\n", name) |
| 162 | data += fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().(" layout=%s\n", layout) |
| 163 | |
| 164 | typeMap := make(map[NodeTypescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/NodeType#]bool) |
| 165 | |
| 166 | type typeCount struct { |
| 167 | edge Edgescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edge# |
| 168 | count int |
| 169 | } |
| 170 | |
| 171 | if err := g.Traversescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#Traverse().(filter, func(node *Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#) { |
| 172 | include := false |
| 173 | if disconnected { |
| 174 | include = true |
| 175 | } else { |
| 176 | include = g.edgesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#edges..IsConnectedscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edges#IsConnected().(node.Stringscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#String().()) |
| 177 | } |
| 178 | |
| 179 | if include { |
| 180 | size++ |
| 181 | typeMap[node.Typescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#Type.] = true |
| 182 | data += fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().(" %s\n", node.Dotscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#Dot().(filter == node.IDscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#ID.)) |
| 183 | } else { |
| 184 | discarded++ |
| 185 | } |
| 186 | }, func(left *Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#, edges []Edgescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edge#, right *Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#) { |
| 187 | // collect counters by edge type in order to calculate proportional widths |
| 188 | byType := make(map[string]typeCount) |
| 189 | tot := len(edges) |
| 190 | |
| 191 | for _, edge := range edges { |
| 192 | if c, found := byType[string(edge.Typescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edge#Type.)]; found { |
| 193 | c.count++ |
| 194 | } else { |
| 195 | byType[string(edge.Typescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edge#Type.)] = typeCount{ |
| 196 | edge: edge, |
| 197 | count: 1, |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | max := 2.0 |
| 203 | for _, c := range byType { |
| 204 | w := max * float64(c.count/tot) |
| 205 | if w < 0.5 { |
| 206 | w = 0.5 |
| 207 | } |
| 208 | data += fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().(" %s\n", c.edge.Dotscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edge#Dot().(left, right, w)) |
| 209 | } |
| 210 | }); err != nil { |
| 211 | return "", 0, 0, err |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | data += "\n" |
| 216 | data += "node [style=filled height=0.55 fontname=\"Verdana\" fontsize=10];\n" |
| 217 | data += "subgraph legend {\n" + |
| 218 | "graph[style=dotted];\n" + |
| 219 | "label = \"Legend\";\n" |
| 220 | |
| 221 | var types []NodeType |
| 222 | for nodeType, _ := range typeMap { |
| 223 | types = append(types, nodeType) |
| 224 | node := Node{ |
| 225 | Type: nodeType, |
| 226 | Annotations: nodeTypeDescs[nodeType], |
| 227 | Dummy: true, |
| 228 | } |
| 229 | data += fmt.Sprintf(" %s\n", node.Dot(false)) |
| 230 | } |
| 231 | |
| 232 | ntypes := len(types) |
| 233 | for i := 0; i < ntypes - 1; i++ { |
| 234 | data += fmt.Sprintf(" \"%s\" -> \"%s\" [style=invis];\n", types[i], types[i + 1]) |
| 235 | } |
| 236 | data += "}\n" |
| 237 | */ |
| 238 | |
| 239 | data += "\n" |
| 240 | data += " overlap=false\n" |
| 241 | data += "}" |
| 242 | |
| 243 | return data, size, discarded, nil |
| 244 | } |
| 245 | |
| 246 | func (g *Graphscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#) JSONscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#JSON().(filter string, disconnected bool) (string, int, int, error) { |
| 247 | size := 0 |
| 248 | discarded := 0 |
| 249 | |
| 250 | type link struct { |
| 251 | Source string `json:"source"` |
| 252 | Target string `json:"target"` |
| 253 | Edge interface{} `json:"edge"` |
| 254 | } |
| 255 | |
| 256 | type data struct { |
| 257 | Nodes []map[string]interface{} `json:"nodes"` |
| 258 | Links []link `json:"links"` |
| 259 | } |
| 260 | |
| 261 | jsData := data{ |
| 262 | Nodes: make([]map[string]interface{}, 0), |
| 263 | Links: make([]link, 0), |
| 264 | } |
| 265 | |
| 266 | if err := g.Traversescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#Traverse().(filter, func(node *Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#) { |
| 267 | include := false |
| 268 | if disconnected { |
| 269 | include = true |
| 270 | } else { |
| 271 | include = g.edgesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#edges..IsConnectedscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edges#IsConnected().(node.Stringscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#String().()) |
| 272 | } |
| 273 | |
| 274 | if include { |
| 275 | size++ |
| 276 | |
| 277 | if nm, err := node.ToMapscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#ToMap().(); err != nil { |
| 278 | panic(err) |
| 279 | } else { |
| 280 | // patch id |
| 281 | nm["id"] = node.Stringscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#String().() |
| 282 | jsData.Nodes = append(jsData.Nodes, nm) |
| 283 | } |
| 284 | } else { |
| 285 | discarded++ |
| 286 | } |
| 287 | }, func(left *Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#, edges []Edgescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edge#, right *Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#) { |
| 288 | for _, edge := range edges { |
| 289 | jsData.Links = append(jsData.Links, link{ |
| 290 | Source: left.Stringscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#String().(), |
| 291 | Target: right.Stringscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#String().(), |
| 292 | Edge: edge, |
| 293 | }) |
| 294 | } |
| 295 | }); err != nil { |
| 296 | return "", 0, 0, err |
| 297 | } |
| 298 | |
| 299 | if raw, err := jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/Marshal().(jsData); err != nil { |
| 300 | return "", 0, 0, err |
| 301 | } else { |
| 302 | return string(raw), size, discarded, nil |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | func (g *Graphscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#) FindNodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#FindNode().(t NodeTypescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/NodeType#, id string) (*Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#, error) { |
| 307 | g.Lockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Lock().() |
| 308 | defer g.Unlockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Unlock().() |
| 309 | |
| 310 | nodeFileName := pathscip-go gomod github.com/golang/go/src go1.16 path/.Joinscip-go gomod github.com/golang/go/src go1.16 path/Join().(g.pathscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#path., fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("%s_%s.json", t, id)) |
| 311 | if fsscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/fs`/.Existsscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/fs`/Exists().(nodeFileName) { |
| 312 | return ReadNodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/ReadNode().(nodeFileName) |
| 313 | } |
| 314 | |
| 315 | return nil, nil |
| 316 | } |
| 317 | |
| 318 | func (g *Graphscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#) FindOtherTypesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#FindOtherTypes().(t NodeTypescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/NodeType#, id string) ([]*Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#, error) { |
| 319 | g.Lockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Lock().() |
| 320 | defer g.Unlockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Unlock().() |
| 321 | |
| 322 | var otherNodes []*Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node# |
| 323 | |
| 324 | for _, otherType := range NodeTypesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/NodeTypes. { |
| 325 | if otherType != t { |
| 326 | if nodeFileName := pathscip-go gomod github.com/golang/go/src go1.16 path/.Joinscip-go gomod github.com/golang/go/src go1.16 path/Join().(g.pathscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#path., fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("%s_%s.json", otherType, id)); fsscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/fs`/.Existsscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/fs`/Exists().(nodeFileName) { |
| 327 | if node, err := ReadNodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/ReadNode().(nodeFileName); err != nil { |
| 328 | return nil, err |
| 329 | } else { |
| 330 | otherNodes = append(otherNodes, node) |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | return otherNodes, nil |
| 337 | } |
| 338 | |
| 339 | func (g *Graphscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#) CreateNodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#CreateNode().(t NodeTypescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/NodeType#, id string, entity interface{}, annotations string) (*Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#, error) { |
| 340 | g.Lockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Lock().() |
| 341 | defer g.Unlockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Unlock().() |
| 342 | |
| 343 | node := &Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#{ |
| 344 | Typescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#Type.: t, |
| 345 | IDscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#ID.: id, |
| 346 | Entityscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#Entity.: entity, |
| 347 | Annotationsscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#Annotations.: annotations, |
| 348 | } |
| 349 | |
| 350 | nodeFileName := pathscip-go gomod github.com/golang/go/src go1.16 path/.Joinscip-go gomod github.com/golang/go/src go1.16 path/Join().(g.pathscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#path., fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("%s.json", node.Stringscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#String().())) |
| 351 | if err := CreateNodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/CreateNode().(nodeFileName, node); err != nil { |
| 352 | return nil, err |
| 353 | } |
| 354 | |
| 355 | sessionscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/.Iscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/I..Eventsscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/Session#Events..Addscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/EventPool#Add().("graph.node.new", node) |
| 356 | |
| 357 | return node, nil |
| 358 | } |
| 359 | |
| 360 | func (g *Graphscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#) UpdateNodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#UpdateNode().(node *Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#) error { |
| 361 | g.Lockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Lock().() |
| 362 | defer g.Unlockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Unlock().() |
| 363 | |
| 364 | nodeFileName := pathscip-go gomod github.com/golang/go/src go1.16 path/.Joinscip-go gomod github.com/golang/go/src go1.16 path/Join().(g.pathscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#path., fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("%s.json", node.Stringscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#String().())) |
| 365 | if err := UpdateNodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/UpdateNode().(nodeFileName, node); err != nil { |
| 366 | return err |
| 367 | } |
| 368 | |
| 369 | return nil |
| 370 | } |
| 371 | |
| 372 | func (g *Graphscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#) FindLastEdgeOfTypescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#FindLastEdgeOfType().(from, to *Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#, edgeType EdgeTypescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/EdgeType#) (*Edgescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edge#, error) { |
| 373 | edges := g.edgesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#edges..FindEdgesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edges#FindEdges().(from.Stringscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#String().(), to.Stringscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#String().(), true) |
| 374 | num := len(edges) |
| 375 | for i := range edges { |
| 376 | // loop backwards |
| 377 | idx := num - 1 - i |
| 378 | edge := edges[idx] |
| 379 | if edge.Typescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edge#Type. == edgeType { |
| 380 | return &edge, nil |
| 381 | } |
| 382 | } |
| 383 | return nil, nil |
| 384 | } |
| 385 | |
| 386 | func (g *Graphscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#) FindLastRecentEdgeOfTypescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#FindLastRecentEdgeOfType().(from, to *Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#, edgeType EdgeTypescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/EdgeType#, staleTime timescip-go gomod github.com/golang/go/src go1.16 time/.Durationscip-go gomod github.com/golang/go/src go1.16 time/Duration#) (*Edgescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edge#, error) { |
| 387 | edges := g.edgesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#edges..FindEdgesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edges#FindEdges().(from.Stringscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#String().(), to.Stringscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#String().(), true) |
| 388 | num := len(edges) |
| 389 | for i := range edges { |
| 390 | // loop backwards |
| 391 | idx := num - 1 - i |
| 392 | edge := edges[idx] |
| 393 | if edge.Typescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edge#Type. == edgeType { |
| 394 | if timescip-go gomod github.com/golang/go/src go1.16 time/.Sincescip-go gomod github.com/golang/go/src go1.16 time/Since().(edge.CreatedAtscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edge#CreatedAt.) >= staleTime { |
| 395 | return nil, nil |
| 396 | } |
| 397 | return &edge, nil |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | return nil, nil |
| 402 | } |
| 403 | |
| 404 | func (g *Graphscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#) CreateEdgescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#CreateEdge().(from, to *Nodescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#, edgeType EdgeTypescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/EdgeType#) (*Edgescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edge#, error) { |
| 405 | edge := Edgescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edge#{ |
| 406 | Typescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edge#Type.: edgeType, |
| 407 | CreatedAtscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edge#CreatedAt.: timescip-go gomod github.com/golang/go/src go1.16 time/.Nowscip-go gomod github.com/golang/go/src go1.16 time/Now().(), |
| 408 | } |
| 409 | |
| 410 | if sessionscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/.Iscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/I..GPSscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/Session#GPS..Updatedscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/GPS#Updated..IsZeroscip-go gomod github.com/golang/go/src go1.16 time/Time#IsZero().() == false { |
| 411 | edge.Positionscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edge#Position. = &sessionscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/.Iscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/I..GPSscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/Session#GPS. |
| 412 | } |
| 413 | |
| 414 | if err := g.edgesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Graph#edges..Connectscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Edges#Connect().(from.Stringscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#String().(), to.Stringscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/Node#String().(), edge); err != nil { |
| 415 | return nil, err |
| 416 | } |
| 417 | |
| 418 | sessionscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/.Iscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/I..Eventsscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/Session#Events..Addscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/EventPool#Add().("graph.edge.new", EdgeEventscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/EdgeEvent#{ |
| 419 | Leftscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/EdgeEvent#Left.: from, |
| 420 | Edgescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/EdgeEvent#Edge.: &edge, |
| 421 | Rightscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/graph`/EdgeEvent#Right.: to, |
| 422 | }) |
| 423 | |
| 424 | return &edge, nil |
| 425 | } |