lux/host/lux_host.go
2024-12-31 19:24:42 +02:00

88 lines
1.5 KiB
Go

package host
import (
"fmt"
"log"
"lux/crypto"
"lux/proto"
"gopkg.in/ini.v1"
)
type luxConfigRoute struct {
Id string `ini:"id"`
Channel string `ini:"channel"`
Address string `ini:"address"`
}
type luxConfig struct {
Host struct {
Name string `ini:"name"`
} `ini:"host"`
KeyStore struct {
Path string `ini:"path"`
} `ini:"keystore"`
Routes []luxConfigRoute
}
var config luxConfig
func LuxHostEntry(configPath string) error {
ini, err := ini.LoadSources(ini.LoadOptions{
AllowNonUniqueSections: true,
}, configPath)
if err != nil {
return err
}
if err = ini.MapTo(&config); err != nil {
return err
}
// parse routes
{
sections, err := ini.SectionsByName("route")
if err != nil {
log.Fatal(err)
}
config.Routes = make([]luxConfigRoute, 0)
for _, section := range sections {
route := luxConfigRoute{}
if err = section.MapTo(&route); err != nil {
log.Fatal(err)
}
config.Routes = append(config.Routes, route)
}
}
fmt.Println(config)
ks := crypto.NewLuxKeyStore(config.KeyStore.Path)
if err := ks.Load(); err != nil {
log.Fatal(err)
}
var hostA, hostB crypto.LuxKey
if ks.Count() < 2 {
hostA, _ = crypto.NewLuxKey(proto.LuxTypeHost)
hostB, _ = crypto.NewLuxKey(proto.LuxTypeHost)
if err := ks.Put(hostA); err != nil {
log.Fatal(err)
}
if err := ks.Put(hostB); err != nil {
log.Fatal(err)
}
} else {
keys := ks.Keys()
hostA, hostB = keys[0], keys[1]
}
fmt.Println(hostA.String())
fmt.Println(hostB.String())
return nil
}