79 lines
1.6 KiB
Go
79 lines
1.6 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 {
|
|
thisKey crypto.LuxKey
|
|
keyStore crypto.LuxKeyStore
|
|
|
|
routes []LuxRoute
|
|
|
|
outbound []LuxChannel
|
|
inbound []LuxChannel
|
|
}
|
|
|
|
func NewLuxRouter(key crypto.LuxKey, ks crypto.LuxKeyStore) LuxRouter {
|
|
return LuxRouter{
|
|
thisKey: key,
|
|
keyStore: ks,
|
|
routes: make([]LuxRoute, 0),
|
|
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) addInboundChannel(ch LuxChannel) *LuxChannel {
|
|
r.inbound = append(r.inbound, ch)
|
|
return &r.inbound[len(r.inbound)-1]
|
|
}
|
|
|
|
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)
|
|
if !ok {
|
|
return errors.New("key not found")
|
|
}
|
|
|
|
// create outbound channel
|
|
channel, err := NewLuxOutboundChannel(udpAddr, chType)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
r.routes = append(r.routes, LuxRoute{
|
|
Key: key,
|
|
Destination: channel.Address,
|
|
Associated: r.addOutboundChannel(channel),
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func (r *LuxRouter) CreateInboundChannel(chType LuxChannelType, udpAddr string) error {
|
|
channel, err := NewLuxInboundChannel(udpAddr, chType)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
r.routes = append(r.routes, LuxRoute{
|
|
Key: r.thisKey,
|
|
Destination: channel.Address,
|
|
Associated: r.addInboundChannel(channel),
|
|
})
|
|
return nil
|
|
}
|