-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
419 lines (346 loc) · 11.1 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package main
import (
"embed"
"fmt"
"io/ioutil"
"log"
"os"
"os/signal"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/kardianos/service"
"github.com/pterm/pcli"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
var template_path_cyclonedds string
var template_path_fastdds_simple string
var template_path_fastdds_ds_client string
var template_path_fastdds_ds_server string
var dds_discovery_server_port string
var version string = "unknown"
//go:embed dds-templates/cyclonedds-simple.xml
//go:embed dds-templates/fastdds-simple.xml
//go:embed dds-templates/fastdds-ds-server.xml
//go:embed dds-templates/fastdds-ds-client.xml
var f embed.FS
var husarnet_temp_dir string
func main_loop() {
default_config_cyclonedds_simple, _ := f.ReadFile("dds-templates/cyclonedds-simple.xml")
default_config_fastdds_simple, _ := f.ReadFile("dds-templates/fastdds-simple.xml")
default_config_fastdds_ds_server, _ := f.ReadFile("dds-templates/fastdds-ds-server.xml")
default_config_fastdds_ds_client, _ := f.ReadFile("dds-templates/fastdds-ds-client.xml")
if HusarnetPresent() {
var output_xml string
var input_xml string
var output_xml_path string
myos := runtime.GOOS
fmt.Printf("Host OS: %s\r\n", myos)
switch myos {
case "linux":
husarnet_temp_dir = "/var/tmp/husarnet-dds"
default:
husarnet_temp_dir = os.TempDir() + "/husarnet-dds"
}
fmt.Println("Temporary directory:", husarnet_temp_dir)
// Prepare a config for Discovery Server (server) in all cases
input_xml = string(default_config_fastdds_ds_server)
// check if non-default DDS config file exists
if _, err := os.Stat(template_path_fastdds_ds_server); err == nil {
input_xml_bytes, _ := ioutil.ReadFile(template_path_fastdds_ds_server)
input_xml = string(input_xml_bytes)
}
output_xml = strings.Replace(input_xml, "$HOST_IPV6", GetOwnHusarnetIPv6(), -1)
// Read the port for the Discovery Server "server" config
dds_discovery_server_port, ok := os.LookupEnv("DISCOVERY_SERVER_PORT")
if ok {
fmt.Println("DISCOVERY_SERVER_PORT:", dds_discovery_server_port)
} else {
dds_discovery_server_port = "11811"
}
output_xml = strings.Replace(output_xml, "$DISCOVERY_SERVER_PORT", dds_discovery_server_port, -1)
output_xml_path = husarnet_temp_dir + "/fastdds-ds-server.xml"
// Create necessary directories for Discovery Server (server) xml config
dir := filepath.Dir(output_xml_path)
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
fmt.Printf("Err: can not create \"%s\" path\r\n", dir)
os.Exit(1)
}
ioutil.WriteFile(output_xml_path, []byte(output_xml), 0644)
// =========================================
// CYCLONE DDS CONFIG
// =========================================
input_xml = string(default_config_cyclonedds_simple)
// check if non-default DDS config file exists
if _, err := os.Stat(template_path_cyclonedds); err == nil {
input_xml_bytes, _ := ioutil.ReadFile(template_path_cyclonedds)
input_xml = string(input_xml_bytes)
}
// prepare XML files with Husarnet hosts
output_xml = ParseCycloneDDSSimple(input_xml)
// defaul output path
output_xml_path = husarnet_temp_dir + "/husarnet-cyclonedds.xml"
// check whether env to set non-default path is set
cyclonedds_uri, ok := os.LookupEnv("CYCLONEDDS_URI")
if ok {
fmt.Println("CYCLONEDDS_URI:", cyclonedds_uri)
if strings.Contains(cyclonedds_uri, "husarnet") {
output_xml_path = strings.Split(cyclonedds_uri, "file://")[1]
}
}
// Create necessary directories
dir = filepath.Dir(output_xml_path)
err = os.MkdirAll(dir, os.ModePerm)
if err != nil {
fmt.Printf("Err: can not create \"%s\" path\r\n", dir)
os.Exit(1)
}
ioutil.WriteFile(output_xml_path, []byte(output_xml), 0644)
fmt.Printf("Cyclone DDS config saved here: \"%s\"\r\n", output_xml_path)
// =========================================
// FAST DDS CONFIG
// =========================================
// Load the appriopriate XML default config
ros_discovery_server, is_ds_client := os.LookupEnv("ROS_DISCOVERY_SERVER")
if is_ds_client {
fmt.Println("ROS_DISCOVERY_SERVER:", ros_discovery_server)
input_xml = string(default_config_fastdds_ds_client)
} else {
input_xml = string(default_config_fastdds_simple)
}
// check if non-default DDS config file exists
if _, err := os.Stat(template_path_fastdds_simple); err == nil {
input_xml_bytes, _ := ioutil.ReadFile(template_path_fastdds_simple)
input_xml = string(input_xml_bytes)
}
// prepare XML files with Husarnet hosts
if is_ds_client {
var ds_server_addr string
var ds_server_port string
// check whether IPv6 address is provided instead of hostname
ipv6 := strings.Split(ros_discovery_server, ":")
if ipv6[0] == "[fc94" {
// IPv6 hostname is provided
parts := strings.Split(ros_discovery_server, "]:")
if len(parts) == 1 {
fmt.Println("Error: Invalid string format")
os.Exit(1)
}
ds_server_addr = strings.Trim(parts[0], "[")
ds_server_port = parts[1]
output_xml = strings.Replace(input_xml, "$DISCOVERY_SERVER_IPV6", ds_server_addr, 1)
} else {
// normal hostname is provided
ds_server_addr = strings.Split(ros_discovery_server, ":")[0]
ds_server_port = strings.Split(ros_discovery_server, ":")[1]
output_xml = strings.Replace(input_xml, "$DISCOVERY_SERVER_IPV6", GetHostIPv6(ds_server_addr), 1)
}
output_xml = strings.Replace(output_xml, "$DISCOVERY_SERVER_PORT", ds_server_port, 1)
output_xml = strings.Replace(output_xml, "$HOST_IPV6", GetOwnHusarnetIPv6(), -1)
} else {
output_xml = ParseFastDDSSimple(input_xml)
}
// defaul output path
output_xml_path = husarnet_temp_dir + "/husarnet-fastdds.xml"
// check whether env to set non-default path is set
fastrtps_default_profiles_file, ok := os.LookupEnv("FASTRTPS_DEFAULT_PROFILES_FILE")
if ok {
fmt.Println("FASTRTPS_DEFAULT_PROFILES_FILE:", fastrtps_default_profiles_file)
if strings.Contains(fastrtps_default_profiles_file, "husarnet") {
output_xml_path = fastrtps_default_profiles_file
}
}
// Create necessary directories
dir = filepath.Dir(output_xml_path)
err = os.MkdirAll(dir, os.ModePerm)
if err != nil {
fmt.Printf("Err: can not create \"%s\" path\r\n", dir)
os.Exit(1)
}
ioutil.WriteFile(output_xml_path, []byte(output_xml), 0644)
fmt.Printf("Fast DDS config saved here: \"%s\"\r\n", output_xml_path)
} else {
fmt.Println("can't reach Husarnet client API")
os.Exit(1)
}
}
type program struct{}
func (p *program) Start(s service.Service) error {
// Start the service logic in a goroutine
go p.run()
return nil
}
func (p *program) run() {
// Your service logic here
fmt.Println("Service loop")
for {
fmt.Println("===============================")
main_loop()
time.Sleep(5 * time.Second)
}
}
func (p *program) Stop(s service.Service) error {
// Your service logic here
return nil
}
var rootCmd = &cobra.Command{
Use: "husarnet-dds",
Short: "Create DDS config for Husarnet automatically",
Long: `Create DDS config for Husarnet automatically`,
Example: `
husarnet-dds singleshot
husarnet-dds install $USER
husarnet-dds start
husarnet-dds stop
husarnet-dds uninstall
`,
Version: version,
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
// Fetch user interrupt
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
pterm.Warning.Println("user interrupt")
pcli.CheckForUpdates()
os.Exit(0)
}()
// Execute cobra
if err := rootCmd.Execute(); err != nil {
pcli.CheckForUpdates()
os.Exit(1)
}
pcli.CheckForUpdates()
}
func main() {
userName := "root"
prg := &program{}
svcConfig := &service.Config{
Name: "husarnet-dds",
DisplayName: "Husarnet DDS Configurator",
Description: "Creating a DDS config for Husarion VPN",
Arguments: []string{"daemon"},
UserName: userName,
}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Fatalf("Failed to create service: %v", err)
}
var envs []string
// Define your CLI commands and flags here
installCommand := &cobra.Command{
Use: "install",
Short: "Install the service",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 1 {
userName = args[0]
}
envvars := make(map[string]string)
for i, env := range envs {
fmt.Printf("env[%d]: %s\r\n", i, env)
key := strings.Split(env, "=")[0]
value := strings.Split(env, "=")[1]
envvars[key] = value
}
options := service.KeyValue{}
if runtime.GOOS == "windows" {
userName = "LocalSystem"
}
if runtime.GOOS == "darwin" {
options["LogDirectory"] = "/tmp/"
}
fmt.Println("username:", userName)
svcConfig = &service.Config{
Name: "husarnet-dds",
DisplayName: "Husarnet DDS Configurator",
Description: "Creating a DDS config for Husarion VPN",
Arguments: []string{"daemon"},
UserName: userName,
EnvVars: envvars,
Option: options,
}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Fatalf("Failed to create service: %v", err)
}
err = s.Install()
if err != nil {
log.Fatalf("Failed to install service: %v", err)
}
fmt.Println("Service installed.")
},
}
installCommand.Flags().StringArrayVarP(&envs, "env", "e",
[]string{
"CYCLONEDDS_URI=file://" + husarnet_temp_dir + "/cyclonedds.xml",
"FASTRTPS_DEFAULT_PROFILES_FILE=" + husarnet_temp_dir + "/fastdds.xml"},
"environment variables for the service")
uninstallCommand := &cobra.Command{
Use: "uninstall",
Short: "Uninstall the service",
Run: func(cmd *cobra.Command, args []string) {
err := s.Uninstall()
if err != nil {
log.Fatalf("Failed to uninstall service: %v", err)
}
fmt.Println("Service removed.")
},
}
startCommand := &cobra.Command{
Use: "start",
Short: "Start the service",
Run: func(cmd *cobra.Command, args []string) {
err := s.Start()
if err != nil {
log.Fatalf("Failed to start service: %v", err)
}
fmt.Println("Service started.")
},
}
stopCommand := &cobra.Command{
Use: "stop",
Short: "Stop the service",
Run: func(cmd *cobra.Command, args []string) {
err := s.Stop()
if err != nil {
log.Fatalf("Failed to stop service: %v", err)
}
fmt.Println("Service stopped.")
},
}
daemonCommand := &cobra.Command{
Use: "daemon",
Short: "Run the program in the inifinite loop",
Run: func(cmd *cobra.Command, args []string) {
err := s.Run()
if err != nil {
log.Fatalf("Failed to run service: %v", err)
}
},
}
singleShotCommand := &cobra.Command{
Use: "singleshot",
Short: "Run the program only once (not as a service)",
Run: func(cmd *cobra.Command, args []string) {
main_loop()
},
}
rootCmd.AddCommand(installCommand)
rootCmd.AddCommand(uninstallCommand)
rootCmd.AddCommand(startCommand)
rootCmd.AddCommand(stopCommand)
rootCmd.AddCommand(daemonCommand)
rootCmd.AddCommand(singleShotCommand)
rootCmd.CompletionOptions.DisableDefaultCmd = true
pcli.SetRepo("husarnet/husarnet-dds")
pcli.DisableUpdateChecking = true
pcli.SetRootCmd(rootCmd)
pcli.Setup()
Execute()
}