1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-07 00:40:26 +08:00

init complete

This commit is contained in:
fumiama
2021-10-25 01:01:22 +08:00
parent d9138df3cd
commit e29d5b2f48
9 changed files with 115 additions and 38 deletions

7
gold/head/nat.go Normal file
View File

@@ -0,0 +1,7 @@
package head
// map[peerip]endpoint
type Notify map[string]string
// peerips array
type Query []string

View File

@@ -2,6 +2,7 @@ package head
const (
ProtoHello uint8 = iota
ProtoHelloAck
ProtoNotify
ProtoQuery
ProtoData
)

31
gold/link/crypto.go Normal file
View File

@@ -0,0 +1,31 @@
package link
import "net"
var (
privKey [32]byte
me net.IP
myend *net.UDPAddr
)
func SetMyself(privateKey [32]byte, myIP string, myEndpoint string) {
privKey = privateKey
var err error
myend, err = net.ResolveUDPAddr("udp", myEndpoint)
if err != nil {
panic(err)
}
me = net.ParseIP(myIP)
myconn, err = listen()
if err != nil {
panic(err)
}
}
func (l *Link) Encode(b []byte) (eb []byte, err error) {
return b, nil
}
func (l *Link) Decode(b []byte) (db []byte, err error) {
return b, nil
}

View File

@@ -16,9 +16,17 @@ type Link struct {
pipe chan *head.Packet
peerip net.IP
endpoint *net.UDPAddr
allowedips []*net.IPNet
hasKeepRuning bool
status int
}
const (
LINK_STATUS_DOWN = iota
LINK_STATUS_HALFUP
LINK_STATUS_UP
)
var (
connections = make(map[string]*Link)
connmapmu sync.RWMutex
@@ -38,6 +46,7 @@ func (l *Link) Close() {
connmapmu.Lock()
delete(connections, l.peerip.String())
connmapmu.Unlock()
l.status = LINK_STATUS_DOWN
}
func (l *Link) Read() *head.Packet {

View File

@@ -7,34 +7,6 @@ import (
"github.com/sirupsen/logrus"
)
var (
privKey [32]byte
me net.IP
myend *net.UDPAddr
)
func SetMyself(privateKey [32]byte, myIP string, myEndpoint string) {
privKey = privateKey
var err error
myend, err = net.ResolveUDPAddr("udp", myEndpoint)
if err != nil {
panic(err)
}
me = net.ParseIP(myIP)
myconn, err = listen()
if err != nil {
panic(err)
}
}
func (l *Link) Encode(b []byte) (eb []byte, err error) {
return b, nil
}
func (l *Link) Decode(b []byte) (db []byte, err error) {
return b, nil
}
func listen() (conn *net.UDPConn, err error) {
conn, err = net.ListenUDP("udp", myend)
if err == nil {
@@ -58,17 +30,41 @@ func listen() (conn *net.UDPConn, err error) {
p, ok := IsInPeer(packet.Src)
logrus.Infoln("[link] recv from endpoint", addr, "src", packet.Src, "dst", packet.Dst)
logrus.Debugln("[link] recv:", string(lbf))
if ok {
if p.EndPoint == "" || p.EndPoint != addr.String() {
logrus.Infoln("[link] set endpoint of peer", p.peerip, "to", addr.String())
p.endpoint = addr
p.EndPoint = addr.String()
}
if ok && p.Accept(net.IP(packet.Dst)) {
packet.Data, err = p.Decode(packet.Data)
if err == nil {
logrus.Infoln("[link] deliver to", p.peerip)
if p.EndPoint == "" {
logrus.Infoln("[link] set endpoint of peer", p.peerip, "to", addr.String())
p.endpoint = addr
p.EndPoint = addr.String()
switch packet.Proto {
case head.ProtoHello:
switch p.status {
case LINK_STATUS_DOWN:
_, _ = p.Write(head.NewPacket(head.ProtoHello, 0, 0, nil))
logrus.Infoln("[link] send hello ack packet")
p.status = LINK_STATUS_HALFUP
case LINK_STATUS_HALFUP:
p.status = LINK_STATUS_UP
case LINK_STATUS_UP:
break
}
case head.ProtoNotify:
logrus.Infoln("[link] recv notify")
onNotify(&packet)
case head.ProtoQuery:
logrus.Infoln("[link] recv query")
onQuery(&packet)
case head.ProtoData:
logrus.Infoln("[link] deliver to", p.peerip)
p.pipe <- &packet
default:
break
}
p.pipe <- &packet
}
} else {
logrus.Infoln("[link] packet to", packet.Dst, "is refused")
}
}
}

View File

@@ -21,3 +21,11 @@ func (l *Link) keepAlive() {
logrus.Infoln("[link.nat] start to keep alive")
}
}
func onQuery(packet *head.Packet) {
// TODO: 完成data解包与notify分发
}
func onNotify(packet *head.Packet) {
// TODO: 完成data解包与endpoint注册
}

View File

@@ -6,7 +6,7 @@ import (
"github.com/fumiama/WireGold/gold/head"
)
func AddPeer(peerip string, pubicKey [32]byte, endPoint string, keepAlive int64) (l *Link) {
func AddPeer(peerip string, pubicKey [32]byte, endPoint string, allowedIPs []string, keepAlive int64) (l *Link) {
peerip = net.ParseIP(peerip).String()
var ok bool
l, ok = IsInPeer(peerip)
@@ -27,6 +27,15 @@ func AddPeer(peerip string, pubicKey [32]byte, endPoint string, keepAlive int64)
l.EndPoint = endPoint
l.endpoint = e
}
if allowedIPs != nil {
l.allowedips = make([]*net.IPNet, len(allowedIPs))
for _, ipnet := range allowedIPs {
_, cidr, err := net.ParseCIDR(ipnet)
if err != nil {
l.allowedips = append(l.allowedips, cidr)
}
}
}
connmapmu.Lock()
connections[peerip] = l
connmapmu.Unlock()

16
gold/link/router.go Normal file
View File

@@ -0,0 +1,16 @@
package link
import "net"
func (l *Link) Accept(ip net.IP) bool {
for _, cidr := range l.allowedips {
if cidr.Contains(ip) {
return true
}
}
return false
}
func NextHop(ip net.IP) *Link {
return nil
}

View File

@@ -10,7 +10,7 @@ import (
func TestTunnel(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
link.SetMyself([32]byte{}, "192.168.1.2", "127.0.0.1:1236")
link.AddPeer("192.168.1.2", [32]byte{}, "127.0.0.1:1236", 0)
link.AddPeer("192.168.1.2", [32]byte{}, "127.0.0.1:1236", nil, 0)
tunn, err := Create("192.168.1.2", 1, 1)
if err != nil {
t.Error(err)