| 1 | package graph |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "io/ioutil" |
| 7 | "os" |
| 8 | "strings" |
| 9 | "time" |
| 10 | "unicode" |
| 11 | ) |
| 12 | |
| 13 | type NodeType string |
| 14 | |
| 15 | const ( |
| 16 | SSID NodeType = "ssid" |
| 17 | BLEServer NodeType = "ble_server" |
| 18 | Station NodeType = "station" |
| 19 | AccessPoint NodeType = "access_point" |
| 20 | Endpoint NodeType = "endpoint" |
| 21 | Gateway NodeType = "gateway" |
| 22 | ) |
| 23 | |
| 24 | var NodeTypes = []NodeType{ |
| 25 | SSID, |
| 26 | Station, |
| 27 | AccessPoint, |
| 28 | Endpoint, |
| 29 | Gateway, |
| 30 | BLEServer, |
| 31 | } |
| 32 | |
| 33 | var nodeTypeDescs = map[NodeType]string{ |
| 34 | SSID: "WiFI SSID probe", |
| 35 | BLEServer: "BLE Device", |
| 36 | Station: "WiFi Client", |
| 37 | AccessPoint: "WiFi AP", |
| 38 | Endpoint: "IP Client", |
| 39 | Gateway: "IP Gateway", |
| 40 | } |
| 41 | |
| 42 | var nodeDotStyles = map[NodeType]string{ |
| 43 | SSID: "shape=circle style=filled color=lightgray fillcolor=lightgray fixedsize=true penwidth=0.5", |
| 44 | BLEServer: "shape=box style=filled color=dodgerblue3", |
| 45 | Endpoint: "shape=box style=filled color=azure2", |
| 46 | Gateway: "shape=diamond style=filled color=azure4", |
| 47 | Station: "shape=box style=filled color=gold", |
| 48 | AccessPoint: "shape=diamond style=filled color=goldenrod3", |
| 49 | } |
| 50 | |
| 51 | type Node struct { |
| 52 | Type NodeType `json:"type"` |
| 53 | CreatedAt time.Time `json:"created_at"` |
| 54 | UpdatedAt time.Time `json:"updated_at"` |
| 55 | ID string `json:"id"` |
| 56 | Annotations string `json:"annotations"` |
| 57 | Entity interface{} `json:"entity"` |
| 58 | Dummy bool `json:"-"` |
| 59 | } |
| 60 | |
| 61 | func ReadNode(fileName string) (*Node, error) { |
| 62 | var node Node |
| 63 | if raw, err := ioutil.ReadFile(fileName); err != nil { |
| 64 | return nil, fmt.Errorf("error while reading %s: %v", fileName, err) |
| 65 | } else if err = json.Unmarshal(raw, &node); err != nil { |
| 66 | return nil, fmt.Errorf("error while decoding %s: %v", fileName, err) |
| 67 | } |
| 68 | return &node, nil |
| 69 | } |
| 70 | |
| 71 | func WriteNode(fileName string, node *Node, update bool) error { |
| 72 | if update { |
| 73 | node.UpdatedAt = time.Now() |
| 74 | } else { |
| 75 | node.CreatedAt = time.Now() |
| 76 | } |
| 77 | |
| 78 | if raw, err := json.Marshal(node); err != nil { |
| 79 | return fmt.Errorf("error creating data for %s: %v", fileName, err) |
| 80 | } else if err = ioutil.WriteFile(fileName, raw, os.ModePerm); err != nil { |
| 81 | return fmt.Errorf("error creating %s: %v", fileName, err) |
| 82 | } |
| 83 | return nil |
| 84 | } |
| 85 | |
| 86 | func CreateNode(fileName string, node *Node) error { |
| 87 | return WriteNode(fileName, node, false) |
| 88 | } |
| 89 | |
| 90 | func UpdateNode(fileName string, node *Node) error { |
| 91 | return WriteNode(fileName, node, true) |
| 92 | } |
| 93 | |
| 94 | func (n Node) String() string { |
| 95 | if n.Dummy == false { |
| 96 | return fmt.Sprintf("%s_%s", n.Type, n.ID) |
| 97 | } |
| 98 | return string(n.Type) |
| 99 | } |
| 100 | |
| 101 | func (n Node) Label() string { |
| 102 | if n.Dummy { |
| 103 | return n.Annotations |
| 104 | } |
| 105 | |
| 106 | switch n.Type { |
| 107 | case SSID: |
| 108 | s := n.Entity.(string) |
| 109 | allPrint := true |
| 110 | |
| 111 | for _, rn := range s { |
| 112 | if !unicode.IsPrint(rune(rn)) { |
| 113 | allPrint = false |
| 114 | break |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if !allPrint { |
| 119 | s = fmt.Sprintf("0x%x", s) |
| 120 | } |
| 121 | return s |
| 122 | case BLEServer: |
| 123 | return fmt.Sprintf("%s\\n(%s)", |
| 124 | n.Entity.(map[string]interface{})["mac"].(string), |
| 125 | n.Entity.(map[string]interface{})["vendor"].(string)) |
| 126 | case Station: |
| 127 | return fmt.Sprintf("%s\\n(%s)", |
| 128 | n.Entity.(map[string]interface{})["mac"].(string), |
| 129 | n.Entity.(map[string]interface{})["vendor"].(string)) |
| 130 | case AccessPoint: |
| 131 | return fmt.Sprintf("%s\\n%s\\n(%s)", |
| 132 | n.Entity.(map[string]interface{})["hostname"].(string), |
| 133 | n.Entity.(map[string]interface{})["mac"].(string), |
| 134 | n.Entity.(map[string]interface{})["vendor"].(string)) |
| 135 | case Endpoint: |
| 136 | return fmt.Sprintf("%s\\n(%s %s)", |
| 137 | n.Entity.(map[string]interface{})["ipv4"].(string), |
| 138 | n.Entity.(map[string]interface{})["mac"].(string), |
| 139 | n.Entity.(map[string]interface{})["vendor"].(string)) |
| 140 | case Gateway: |
| 141 | return fmt.Sprintf("%s\\n(%s %s)", |
| 142 | n.Entity.(map[string]interface{})["ipv4"].(string), |
| 143 | n.Entity.(map[string]interface{})["mac"].(string), |
| 144 | n.Entity.(map[string]interface{})["vendor"].(string)) |
| 145 | } |
| 146 | return "?" |
| 147 | } |
| 148 | |
| 149 | func (n Node) Dot(isTarget bool) string { |
| 150 | style := nodeDotStyles[n.Type] |
| 151 | if isTarget { |
| 152 | style += ", color=red" |
| 153 | } |
| 154 | return fmt.Sprintf("\"%s\" [%s, label=\"%s\"];", |
| 155 | n.String(), |
| 156 | style, |
| 157 | strings.ReplaceAll(n.Label(), "\"", "\\\"")) |
| 158 | } |
| 159 | |
| 160 | func (n Node) ToMap() (map[string]interface{}, error) { |
| 161 | var m map[string]interface{} |
| 162 | |
| 163 | if raw, err := json.Marshal(n); err != nil { |
| 164 | return nil, err |
| 165 | } else if err = json.Unmarshal(raw, &m); err != nil { |
| 166 | return nil, err |
| 167 | } |
| 168 | |
| 169 | return m, nil |
| 170 | } |