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