-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
181 lines (142 loc) · 4.17 KB
/
parser.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
178
179
180
181
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/go-xmlfmt/xmlfmt"
)
func HusarnetPresent() bool {
_, err := HusarnetAPIrequest("api/status")
if err != nil {
return false
}
return true
}
type hostTable struct {
Result struct {
HostTable map[string]string `json:"host_table"`
LocalHostname string `json:"local_hostname"`
LocalIPv6 string `json:"local_ip"`
} `json:"result"`
}
func HusarnetAPIrequest(endpoint string) ([]byte, error) {
// create a new HTTP client
client := &http.Client{}
// create a new HTTP request
req, err := http.NewRequest("GET", "http://localhost:16216/"+endpoint, nil)
if err != nil {
fmt.Println("Error creating request:", err)
return nil, err
}
// make the GET request
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error making request:", err)
return nil, err
}
defer resp.Body.Close()
// read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return nil, err
}
return body, nil
}
func GetHostIPv6(hostname string) string {
body, err := HusarnetAPIrequest("api/status")
if err != nil {
fmt.Println("Error reading api/status:", err)
os.Exit(1)
}
var data hostTable
if err := json.Unmarshal(body, &data); err != nil {
fmt.Printf("Error unmarshaling response: %v\r\n", err)
os.Exit(1)
}
ipv6Address, ok := data.Result.HostTable[hostname]
if !ok {
fmt.Printf("Host not found: %s", hostname)
os.Exit(1)
}
return ipv6Address
}
func GetOwnHusarnetIPv6() string {
body, err := HusarnetAPIrequest("api/status")
if err != nil {
fmt.Println("Error reading api/status:", err)
os.Exit(1)
}
var data hostTable
if err := json.Unmarshal(body, &data); err != nil {
fmt.Printf("Error unmarshaling response: %v\r\n", err)
os.Exit(1)
}
ipv6Address := data.Result.LocalIPv6
return ipv6Address
}
func ParseCycloneDDSSimple(templateXML string) string {
body, err := HusarnetAPIrequest("api/status")
if err != nil {
fmt.Println("Error reading api/status:", err)
os.Exit(1)
}
var data hostTable
if err := json.Unmarshal(body, &data); err != nil {
fmt.Printf("Error unmarshaling response: %v\r\n", err)
os.Exit(1)
}
// Initialize an empty buffer to hold the output
var output bytes.Buffer
// Split the input file into lines
inputLines := strings.Split(templateXML, "\n")
// Iterate over the lines of the input file
for _, line := range inputLines {
// Append the line to the output
output.WriteString(line + "\n")
// Check if the line contains the <Peers> tag
if strings.Contains(line, "<Peers>") {
for _, ipv6Address := range data.Result.HostTable {
output.WriteString(fmt.Sprintf("\t<Peer address='%s'/>\n", ipv6Address))
}
}
}
prettyXML := xmlfmt.FormatXML(string(output.Bytes()), "", " ", true)
return prettyXML
}
func ParseFastDDSSimple(templateXML string) string {
body, err := HusarnetAPIrequest("api/status")
if err != nil {
fmt.Println("Error reading api/status:", err)
os.Exit(1)
}
var data hostTable
if err := json.Unmarshal(body, &data); err != nil {
fmt.Printf("Error unmarshaling response: %v\r\n", err)
os.Exit(1)
}
// Initialize an empty buffer to hold the output
var output bytes.Buffer
// Split the input file into lines
inputLines := strings.Split(string(templateXML), "\n")
// Iterate over the lines of the input file
for _, line := range inputLines {
// Append the line to the output
output.WriteString(line + "\n")
// Check if the line contains the <Peers> tag
if strings.Contains(line, "<initialPeersList>") {
for _, ipv6Address := range data.Result.HostTable {
output.WriteString(fmt.Sprintf("\t<locator><udpv6><address>%s</address></udpv6></locator>\n", ipv6Address))
}
} else if strings.Contains(line, "<defaultUnicastLocatorList>") || strings.Contains(line, "<metatrafficUnicastLocatorList>") {
// Append the IP address to the output as a <Peer> tag
output.WriteString(fmt.Sprintf("\t<locator><udpv6><address>%s</address></udpv6></locator>\n", GetOwnHusarnetIPv6()))
}
}
prettyXML := xmlfmt.FormatXML(string(output.Bytes()), "", " ", true)
return prettyXML
}