begin working on host data structures

This commit is contained in:
mykola2312 2025-01-11 02:00:33 +02:00
parent 6969d75024
commit 73c1690c00
4 changed files with 157 additions and 82 deletions

View file

@ -1,88 +1,9 @@
package host
import (
"fmt"
"log"
"lux/crypto"
"lux/proto"
"gopkg.in/ini.v1"
"lux/net"
)
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
type LuxHost struct {
router net.LuxRouter
}

88
host/lux_host_entry.go Normal file
View file

@ -0,0 +1,88 @@
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
}

64
host/lux_option.go Normal file
View file

@ -0,0 +1,64 @@
package host
import (
"lux/proto"
"net"
)
type LuxOptionType uint
const (
LuxOptionTypeWAN = 1
LuxOptionTypeWAN6 = 2
)
type LuxOption interface {
Read(rd *proto.LuxBuffer) error
Write(wd *proto.LuxBuffer)
Type() LuxOptionType
}
type LuxOptionWAN struct {
Addr4 net.IP
}
func (opt *LuxOptionWAN) Read(rd *proto.LuxBuffer) error {
octets, err := rd.ReadNext(4)
if err != nil {
return err
}
opt.Addr4 = net.IP(octets)
return nil
}
func (opt *LuxOptionWAN) Write(wd *proto.LuxBuffer) {
wd.WriteBytes(opt.Addr4.To4())
}
func (*LuxOptionWAN) Type() LuxOptionType {
return LuxOptionTypeWAN
}
type LuxOptionWAN6 struct {
Addr6 net.IP
}
func (opt *LuxOptionWAN6) Read(rd *proto.LuxBuffer) error {
octets, err := rd.ReadNext(16)
if err != nil {
return err
}
opt.Addr6 = net.IP(octets)
return nil
}
func (opt *LuxOptionWAN6) Write(wd *proto.LuxBuffer) {
wd.WriteBytes(opt.Addr6.To16())
}
func (*LuxOptionWAN6) Type() LuxOptionType {
return LuxOptionTypeWAN6
}

View file

@ -60,6 +60,8 @@ func (r *LuxRouter) addInboundChannel(ch LuxChannel) *LuxChannel {
return channel
}
// the ID is not destination, but rather peer associated for this route, like source ID.
// Destination router always know who is he, therefore we dont need target ID
func (r *LuxRouter) CreateOutboundRoute(id proto.LuxID, chType LuxChannelType, udpAddr string) error {
// we gonna look up key by id from key store
key, ok := r.keyStore.Get(id)