mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-29 23:30:37 +08:00
模块化 me router
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package link
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"unsafe"
|
||||
|
||||
@@ -10,10 +11,10 @@ import (
|
||||
)
|
||||
|
||||
// AddPeer 添加一个 peer
|
||||
func AddPeer(peerip string, pubicKey *[32]byte, endPoint string, allowedIPs []string, keepAlive int64, allowTrans bool) (l *Link) {
|
||||
func (m *Me) AddPeer(peerip string, pubicKey *[32]byte, endPoint string, allowedIPs []string, keepAlive int64, allowTrans bool) (l *Link) {
|
||||
peerip = net.ParseIP(peerip).String()
|
||||
var ok bool
|
||||
l, ok = IsInPeer(peerip)
|
||||
l, ok = m.IsInPeer(peerip)
|
||||
if ok {
|
||||
return
|
||||
}
|
||||
@@ -23,11 +24,13 @@ func AddPeer(peerip string, pubicKey *[32]byte, endPoint string, allowedIPs []st
|
||||
pipe: make(chan *head.Packet, 32),
|
||||
peerip: net.ParseIP(peerip),
|
||||
allowtrans: allowTrans,
|
||||
me: m,
|
||||
}
|
||||
if pubicKey != nil {
|
||||
c := curve.Get(privKey[:])
|
||||
c := curve.Get(m.privKey[:])
|
||||
k, err := c.Shared(pubicKey)
|
||||
if err == nil {
|
||||
fmt.Println(len(k))
|
||||
l.key = (*[32]byte)(*(*unsafe.Pointer)(unsafe.Pointer(&k)))
|
||||
}
|
||||
}
|
||||
@@ -45,20 +48,20 @@ func AddPeer(peerip string, pubicKey *[32]byte, endPoint string, allowedIPs []st
|
||||
_, cidr, err := net.ParseCIDR(ipnet)
|
||||
if err == nil {
|
||||
l.allowedips = append(l.allowedips, cidr)
|
||||
routetable[cidr.String()] = append(routetable[cidr.String()], l)
|
||||
l.me.router.routetable[cidr.String()] = append(l.me.router.routetable[cidr.String()], l)
|
||||
}
|
||||
}
|
||||
}
|
||||
connmapmu.Lock()
|
||||
connections[peerip] = l
|
||||
connmapmu.Unlock()
|
||||
l.me.connmapmu.Lock()
|
||||
l.me.connections[peerip] = l
|
||||
l.me.connmapmu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
// IsInPeer 查找 peer 是否已经在册
|
||||
func IsInPeer(peer string) (p *Link, ok bool) {
|
||||
connmapmu.RLock()
|
||||
p, ok = connections[peer]
|
||||
connmapmu.RUnlock()
|
||||
func (m *Me) IsInPeer(peer string) (p *Link, ok bool) {
|
||||
m.connmapmu.RLock()
|
||||
p, ok = m.connections[peer]
|
||||
m.connmapmu.RUnlock()
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user