mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-24 04:27:08 +08:00
完善主程序
This commit is contained in:
@@ -6,6 +6,8 @@ import (
|
||||
"unsafe"
|
||||
|
||||
tea "github.com/fumiama/gofastTEA"
|
||||
|
||||
"github.com/fumiama/WireGold/gold/head"
|
||||
)
|
||||
|
||||
// Me 是本机的抽象
|
||||
@@ -29,10 +31,12 @@ type Me struct {
|
||||
myconn *net.UDPConn
|
||||
// 本机路由表
|
||||
router *Router
|
||||
// 不分目的 link 的接收队列
|
||||
pipe chan *head.Packet
|
||||
}
|
||||
|
||||
// NewMe 设置本机参数
|
||||
func NewMe(privateKey *[32]byte, myipwithmask string, myEndpoint string) (m Me) {
|
||||
func NewMe(privateKey *[32]byte, myipwithmask string, myEndpoint string, nopipeinlink bool) (m Me) {
|
||||
m.privKey = *privateKey
|
||||
var err error
|
||||
m.myend, err = net.ResolveUDPAddr("udp", myEndpoint)
|
||||
@@ -55,6 +59,10 @@ func NewMe(privateKey *[32]byte, myipwithmask string, myEndpoint string) (m Me)
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
@@ -56,10 +56,14 @@ func (m *Me) Connect(peer string) (*Link, error) {
|
||||
|
||||
// Close 关闭到 peer 的连接
|
||||
func (l *Link) Close() {
|
||||
l.status = LINK_STATUS_DOWN
|
||||
}
|
||||
|
||||
// Destroy 从 connections 移除 peer
|
||||
func (l *Link) Destroy() {
|
||||
l.me.connmapmu.Lock()
|
||||
delete(l.me.connections, l.peerip.String())
|
||||
l.me.connmapmu.Unlock()
|
||||
l.status = LINK_STATUS_DOWN
|
||||
}
|
||||
|
||||
// Read 从 peer 收包
|
||||
|
||||
@@ -61,7 +61,11 @@ func (m *Me) listen() (conn *net.UDPConn, err error) {
|
||||
p.onQuery(&packet)
|
||||
case head.ProtoData:
|
||||
logrus.Infoln("[link] deliver to", p.peerip)
|
||||
p.pipe <- &packet
|
||||
if p.pipe != nil {
|
||||
p.pipe <- &packet
|
||||
} else {
|
||||
m.pipe <- &packet
|
||||
}
|
||||
default:
|
||||
break
|
||||
}
|
||||
@@ -84,6 +88,12 @@ func (m *Me) listen() (conn *net.UDPConn, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Read 接收所有发送给本机的报文
|
||||
// 需要开启 nopipe
|
||||
func (m *Me) Read() *head.Packet {
|
||||
return <-m.pipe
|
||||
}
|
||||
|
||||
// 从 conn 读取 sz 字节数据
|
||||
func readAll(conn *net.UDPConn, sz int) ([]byte, error) {
|
||||
i := 0
|
||||
|
||||
@@ -4,13 +4,13 @@ import (
|
||||
"net"
|
||||
"unsafe"
|
||||
|
||||
curve "github.com/fumiama/go-x25519"
|
||||
|
||||
"github.com/fumiama/WireGold/gold/head"
|
||||
curve "github.com/fumiama/go-x25519"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// AddPeer 添加一个 peer
|
||||
func (m *Me) 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, nopipe bool) (l *Link) {
|
||||
peerip = net.ParseIP(peerip).String()
|
||||
var ok bool
|
||||
l, ok = m.IsInPeer(peerip)
|
||||
@@ -20,11 +20,13 @@ func (m *Me) AddPeer(peerip string, pubicKey *[32]byte, endPoint string, allowed
|
||||
l = &Link{
|
||||
pubk: pubicKey,
|
||||
keepalive: keepAlive,
|
||||
pipe: make(chan *head.Packet, 32),
|
||||
peerip: net.ParseIP(peerip),
|
||||
allowtrans: allowTrans,
|
||||
me: m,
|
||||
}
|
||||
if !nopipe {
|
||||
l.pipe = make(chan *head.Packet, 32)
|
||||
}
|
||||
if pubicKey != nil {
|
||||
c := curve.Get(m.privKey[:])
|
||||
k, err := c.Shared(pubicKey)
|
||||
@@ -53,6 +55,7 @@ func (m *Me) AddPeer(peerip string, pubicKey *[32]byte, endPoint string, allowed
|
||||
l.me.connmapmu.Lock()
|
||||
l.me.connections[peerip] = l
|
||||
l.me.connmapmu.Unlock()
|
||||
logrus.Infoln("[peer] add peer:", peerip, "allow:", allowedIPs)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user