57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
package net
|
|
|
|
import (
|
|
"errors"
|
|
"lux/crypto"
|
|
"lux/proto"
|
|
"net"
|
|
)
|
|
|
|
type LuxRoute struct {
|
|
Key crypto.LuxKey
|
|
Destination *net.UDPAddr
|
|
Associated *LuxChannel
|
|
}
|
|
|
|
type LuxRouter struct {
|
|
keyStore crypto.LuxKeyStore
|
|
routes map[proto.LuxID]LuxRoute
|
|
|
|
outbound []LuxChannel
|
|
inbound []LuxChannel
|
|
}
|
|
|
|
func NewLuxRouter(ks crypto.LuxKeyStore) LuxRouter {
|
|
return LuxRouter{
|
|
keyStore: ks,
|
|
routes: make(map[proto.LuxID]LuxRoute),
|
|
outbound: make([]LuxChannel, 0),
|
|
inbound: make([]LuxChannel, 0),
|
|
}
|
|
}
|
|
|
|
func (r *LuxRouter) addOutboundChannel(ch LuxChannel) *LuxChannel {
|
|
r.outbound = append(r.outbound, ch)
|
|
return &r.outbound[len(r.outbound)-1]
|
|
}
|
|
|
|
func (r *LuxRouter) AddOutboundRoute(id proto.LuxID, chType LuxChannelType, udpAddr string) error {
|
|
// we gonna look up key by id from key store
|
|
key, ok := r.keyStore.Get(id)
|
|
if !ok {
|
|
return errors.New("key not found")
|
|
}
|
|
|
|
// create outbound channel
|
|
channel, err := NewLuxOutboundChannel(udpAddr, chType)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
r.routes[id] = LuxRoute{
|
|
Key: key,
|
|
Destination: channel.Address,
|
|
Associated: r.addOutboundChannel(channel),
|
|
}
|
|
return nil
|
|
}
|