mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-13 05:31:08 +08:00
init complete
This commit is contained in:
7
gold/head/nat.go
Normal file
7
gold/head/nat.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package head
|
||||||
|
|
||||||
|
// map[peerip]endpoint
|
||||||
|
type Notify map[string]string
|
||||||
|
|
||||||
|
// peerips array
|
||||||
|
type Query []string
|
||||||
@@ -2,6 +2,7 @@ package head
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
ProtoHello uint8 = iota
|
ProtoHello uint8 = iota
|
||||||
ProtoHelloAck
|
ProtoNotify
|
||||||
|
ProtoQuery
|
||||||
ProtoData
|
ProtoData
|
||||||
)
|
)
|
||||||
|
|||||||
31
gold/link/crypto.go
Normal file
31
gold/link/crypto.go
Normal 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
|
||||||
|
}
|
||||||
@@ -16,9 +16,17 @@ type Link struct {
|
|||||||
pipe chan *head.Packet
|
pipe chan *head.Packet
|
||||||
peerip net.IP
|
peerip net.IP
|
||||||
endpoint *net.UDPAddr
|
endpoint *net.UDPAddr
|
||||||
|
allowedips []*net.IPNet
|
||||||
hasKeepRuning bool
|
hasKeepRuning bool
|
||||||
|
status int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
LINK_STATUS_DOWN = iota
|
||||||
|
LINK_STATUS_HALFUP
|
||||||
|
LINK_STATUS_UP
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
connections = make(map[string]*Link)
|
connections = make(map[string]*Link)
|
||||||
connmapmu sync.RWMutex
|
connmapmu sync.RWMutex
|
||||||
@@ -38,6 +46,7 @@ func (l *Link) Close() {
|
|||||||
connmapmu.Lock()
|
connmapmu.Lock()
|
||||||
delete(connections, l.peerip.String())
|
delete(connections, l.peerip.String())
|
||||||
connmapmu.Unlock()
|
connmapmu.Unlock()
|
||||||
|
l.status = LINK_STATUS_DOWN
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Link) Read() *head.Packet {
|
func (l *Link) Read() *head.Packet {
|
||||||
|
|||||||
@@ -7,34 +7,6 @@ import (
|
|||||||
"github.com/sirupsen/logrus"
|
"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) {
|
func listen() (conn *net.UDPConn, err error) {
|
||||||
conn, err = net.ListenUDP("udp", myend)
|
conn, err = net.ListenUDP("udp", myend)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -58,17 +30,41 @@ func listen() (conn *net.UDPConn, err error) {
|
|||||||
p, ok := IsInPeer(packet.Src)
|
p, ok := IsInPeer(packet.Src)
|
||||||
logrus.Infoln("[link] recv from endpoint", addr, "src", packet.Src, "dst", packet.Dst)
|
logrus.Infoln("[link] recv from endpoint", addr, "src", packet.Src, "dst", packet.Dst)
|
||||||
logrus.Debugln("[link] recv:", string(lbf))
|
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)
|
packet.Data, err = p.Decode(packet.Data)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
logrus.Infoln("[link] deliver to", p.peerip)
|
switch packet.Proto {
|
||||||
if p.EndPoint == "" {
|
case head.ProtoHello:
|
||||||
logrus.Infoln("[link] set endpoint of peer", p.peerip, "to", addr.String())
|
switch p.status {
|
||||||
p.endpoint = addr
|
case LINK_STATUS_DOWN:
|
||||||
p.EndPoint = addr.String()
|
_, _ = 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")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -21,3 +21,11 @@ func (l *Link) keepAlive() {
|
|||||||
logrus.Infoln("[link.nat] start to keep alive")
|
logrus.Infoln("[link.nat] start to keep alive")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func onQuery(packet *head.Packet) {
|
||||||
|
// TODO: 完成data解包与notify分发
|
||||||
|
}
|
||||||
|
|
||||||
|
func onNotify(packet *head.Packet) {
|
||||||
|
// TODO: 完成data解包与endpoint注册
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"github.com/fumiama/WireGold/gold/head"
|
"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()
|
peerip = net.ParseIP(peerip).String()
|
||||||
var ok bool
|
var ok bool
|
||||||
l, ok = IsInPeer(peerip)
|
l, ok = IsInPeer(peerip)
|
||||||
@@ -27,6 +27,15 @@ func AddPeer(peerip string, pubicKey [32]byte, endPoint string, keepAlive int64)
|
|||||||
l.EndPoint = endPoint
|
l.EndPoint = endPoint
|
||||||
l.endpoint = e
|
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()
|
connmapmu.Lock()
|
||||||
connections[peerip] = l
|
connections[peerip] = l
|
||||||
connmapmu.Unlock()
|
connmapmu.Unlock()
|
||||||
|
|||||||
16
gold/link/router.go
Normal file
16
gold/link/router.go
Normal 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
|
||||||
|
}
|
||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
func TestTunnel(t *testing.T) {
|
func TestTunnel(t *testing.T) {
|
||||||
logrus.SetLevel(logrus.DebugLevel)
|
logrus.SetLevel(logrus.DebugLevel)
|
||||||
link.SetMyself([32]byte{}, "192.168.1.2", "127.0.0.1:1236")
|
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)
|
tunn, err := Create("192.168.1.2", 1, 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
|
|||||||
Reference in New Issue
Block a user