mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-21 19:13:20 +08:00
move router into link
This commit is contained in:
@@ -29,8 +29,6 @@ type Me struct {
|
||||
connmapmu sync.RWMutex
|
||||
// 本机监听的 endpoint
|
||||
myconn *net.UDPConn
|
||||
// 本机路由表
|
||||
router *Router
|
||||
// 不分目的 link 的接收队列
|
||||
pipe chan *head.Packet
|
||||
}
|
||||
@@ -54,15 +52,9 @@ func NewMe(privateKey *[32]byte, myipwithmask string, myEndpoint string, nopipei
|
||||
panic(err)
|
||||
}
|
||||
m.connections = make(map[string]*Link)
|
||||
m.router = &Router{
|
||||
list: make([]*net.IPNet, 1, 16),
|
||||
table: make(map[string]*Link, 16),
|
||||
}
|
||||
m.router.SetDefault(nil)
|
||||
if nopipeinlink {
|
||||
m.pipe = make(chan *head.Packet, 32)
|
||||
}
|
||||
m.AddPeer(m.me.String(), nil, "127.0.0.1:56789", []string{myipwithmask, "127.0.0.0/8"}, 0, false, nopipeinlink)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,8 @@ type Link struct {
|
||||
key *[32]byte
|
||||
// 本机信息
|
||||
me *Me
|
||||
// 本连接路由表
|
||||
router *Router
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -88,7 +90,7 @@ func (l *Link) Write(p *head.Packet, istransfer bool) (n int, err error) {
|
||||
}
|
||||
logrus.Debugln("[link] write", len(d), "bytes data")
|
||||
if err == nil {
|
||||
peerlink := l.me.router.NextHop(l.peerip.String())
|
||||
peerlink := l.router.NextHop(l.peerip.String())
|
||||
if peerlink != nil {
|
||||
peerep := peerlink.endpoint
|
||||
if peerep == nil {
|
||||
@@ -106,7 +108,7 @@ func (l *Link) Write(p *head.Packet, istransfer bool) (n int, err error) {
|
||||
func (l *Link) String() (n string) {
|
||||
n = "default"
|
||||
if l.pubk != nil {
|
||||
b, err := base14.UTF16be2utf8(base14.Encode(l.pubk[:21]))
|
||||
b, err := base14.UTF16be2utf8(base14.Encode(l.pubk[:10]))
|
||||
if err == nil {
|
||||
n = helper.BytesToString(b)
|
||||
} else {
|
||||
|
||||
@@ -23,7 +23,13 @@ func (m *Me) AddPeer(peerip string, pubicKey *[32]byte, endPoint string, allowed
|
||||
peerip: net.ParseIP(peerip),
|
||||
allowtrans: allowTrans,
|
||||
me: m,
|
||||
router: &Router{
|
||||
list: make([]*net.IPNet, 1, 16),
|
||||
table: make(map[string]*Link, 16),
|
||||
},
|
||||
}
|
||||
l.router.SetDefault(l)
|
||||
|
||||
if !nopipe {
|
||||
l.pipe = make(chan *head.Packet, 32)
|
||||
}
|
||||
@@ -48,7 +54,7 @@ func (m *Me) AddPeer(peerip string, pubicKey *[32]byte, endPoint string, allowed
|
||||
_, cidr, err := net.ParseCIDR(ipnet)
|
||||
if err == nil {
|
||||
l.allowedips = append(l.allowedips, cidr)
|
||||
l.me.router.SetItem(cidr, l)
|
||||
l.router.SetItem(cidr, l)
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ func (r *Router) SetItem(ip *net.IPNet, l *Link) {
|
||||
copy(r.list[i+1:], r.list[i:len(r.list)-1])
|
||||
r.list[i] = ip
|
||||
r.table[ip.String()] = l
|
||||
logrus.Infoln("[router] add route in link", l, "to", ip, "-->", l.peerip)
|
||||
logrus.Infoln("[router] add route in link", l, ip, "-->", l.peerip)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user