-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
177 lines (140 loc) · 3.91 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"bufio"
"bytes"
"encoding/json"
"log"
"net/http"
"os"
"regexp"
"strconv"
)
type Nodes struct {
Nodes []Node `json:"hosts"`
}
type Node struct {
Name string `json:"name"`
Ip string `json:"ip"`
}
type Services struct {
Services []Service `json:"services"`
}
type Service struct {
Name string `json:"name"`
Protocol string `json:"protocol"`
Link string `json:"link"`
}
func check(e error) {
if e != nil {
panic(e)
}
}
func getUrl(url string) []byte {
resp, err := http.Get(url)
check(err)
defer resp.Body.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
respByte := buf.Bytes()
return respByte
}
func extractHost(s string) string {
var r = regexp.MustCompile("http://([^:]+)")
res := r.FindStringSubmatch(s)
return res[1]
}
// getServices list all the visible services in the network
func getService() string {
var services Services
respByte := getUrl("http://localnode:8080/cgi-bin/sysinfo.json?services=1")
err := json.Unmarshal(respByte, &services)
check(err)
result := "<h1>AREDN VISIBLE SERVICES</h1>"
result = result + "<table><tr>"
for i := 0; i < len(services.Services); i++ {
result = result + "<th><a href=\""
result = result + (services.Services[i].Link)
result = result + "/\">"
result = result + (services.Services[i].Name)
result = result + "</a><br><small>by " + extractHost(services.Services[i].Link)
result = result + "</small></th>"
// New table line for each 5 records
if (i+1)%5 == 0 {
result = result + "</tr><tr>"
}
}
result = result + "</tr></table><br>"
result = result + ("Number of services in the network: ")
result = result + strconv.Itoa(len(services.Services))
return result
}
// getNodes list all the visible nodes in the network
func getNodes() string {
var nodes Nodes
respByte := getUrl("http://localnode:8080/cgi-bin/sysinfo.json?hosts=1")
err := json.Unmarshal(respByte, &nodes)
check(err)
//Creating table
result := "<h1>AREDN VISIBLE NODES</h1>"
result = result + "<table><tr>"
for i := 0; i < len(nodes.Nodes); i++ {
result = result + "<th><a href=\"http://"
result = result + (nodes.Nodes[i].Name)
result = result + "/\">"
result = result + (nodes.Nodes[i].Name)
result = result + "</a></th>"
// New table line for each 5 records
if (i+1)%5 == 0 {
result = result + "</tr><tr>"
}
}
result = result + "</tr></table><br>"
result = result + "Number of hosts in the network: "
result = result + strconv.Itoa(len(nodes.Nodes))
result = result + "<br><br>"
return result
}
func generateCSS() {
if _, err := os.Stat("static/style/"); os.IsNotExist(err) {
os.Mkdir("static/style", 0755)
}
style, err := os.Create("static/style/style.css")
check(err)
defer style.Close()
ws := bufio.NewWriter(style)
// Generating CSS Style file
ws.WriteString("body {\n")
ws.WriteString(" font-family:verdana;\n}\n\n")
ws.WriteString(".content {\n")
ws.WriteString(" max-width: 1024px;\n")
ws.WriteString(" margin: auto;\n}\n\n")
ws.WriteString("table { \n")
ws.WriteString(" border-collapse: collapse; \n}\n\n")
ws.WriteString("table, th, td { \n")
ws.WriteString(" border: 1px solid black; \n}\n\n")
ws.WriteString("th, td { \n")
ws.WriteString(" padding: 15px; \n")
ws.WriteString(" text-align: left; \n}\n\n")
ws.WriteString("small {\n")
ws.WriteString(" font-size: smaller;\n}\n")
ws.Flush()
}
func main() {
// Check or Create static directory
if _, err := os.Stat("static/"); os.IsNotExist(err) {
os.Mkdir("static/", 0755)
}
generateCSS()
html, err := os.Create("static/index.html")
check(err)
defer html.Close()
w := bufio.NewWriter(html)
w.WriteString("<link rel=\"stylesheet\" type=\"text/css\" href=\"style/style.css\">")
w.WriteString("<body><div class=\"content\">")
w.WriteString(getNodes())
w.WriteString(getService())
w.WriteString("</div></body>")
w.Flush()
http.Handle("/", http.FileServer(http.Dir("./static")))
log.Fatal(http.ListenAndServe(":80", nil))
}