mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-29 23:30:37 +08:00
init
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package head
|
package head
|
||||||
|
|
||||||
|
import "encoding/json"
|
||||||
|
|
||||||
type Packet struct {
|
type Packet struct {
|
||||||
DataSZ uint32
|
DataSZ uint32
|
||||||
Proto uint8
|
Proto uint8
|
||||||
@@ -21,10 +23,13 @@ func NewPacket(proto uint8, srcPort uint16, dstPort uint16, data []byte) *Packet
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Packet) UnMashal(data []byte) {
|
func (p *Packet) UnMashal(data []byte) error {
|
||||||
|
return json.Unmarshal(data, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Packet) Mashal(src string, dst string) []byte {
|
func (p *Packet) Mashal(src string, dst string) ([]byte, error) {
|
||||||
return nil
|
p.DataSZ = uint32(len(p.Data))
|
||||||
|
p.Src = src
|
||||||
|
p.Dst = dst
|
||||||
|
return json.Marshal(p)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,59 +6,53 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/fumiama/WireGold/gold/head"
|
"github.com/fumiama/WireGold/gold/head"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Link struct {
|
type Link struct {
|
||||||
conn net.Conn
|
PubicKey [32]byte
|
||||||
peer *Identity
|
EndPoint string
|
||||||
|
KeepAlive int64
|
||||||
|
pipe chan *head.Packet
|
||||||
peerip net.IP
|
peerip net.IP
|
||||||
|
endpoint *net.UDPAddr
|
||||||
hasKeepRuning bool
|
hasKeepRuning bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
connections = make(map[string]*Link)
|
connections = make(map[string]*Link)
|
||||||
connmapmu sync.RWMutex
|
connmapmu sync.RWMutex
|
||||||
|
myconn *net.UDPConn
|
||||||
)
|
)
|
||||||
|
|
||||||
func Connect(peer string) (l Link, err error) {
|
func Connect(peer string) (*Link, error) {
|
||||||
peer = net.ParseIP(peer).String()
|
p, ok := IsInPeer(net.ParseIP(peer).String())
|
||||||
p, ok := IsInPeer(peer)
|
|
||||||
if ok {
|
if ok {
|
||||||
connmapmu.RLock()
|
p.keepAlive()
|
||||||
lnk, ok := connections[peer]
|
return p, nil
|
||||||
connmapmu.RUnlock()
|
|
||||||
if ok {
|
|
||||||
return *lnk, nil
|
|
||||||
}
|
|
||||||
l.conn, err = net.Dial("udp", p.EndPoint)
|
|
||||||
l.peer = p
|
|
||||||
l.peerip = net.ParseIP(peer)
|
|
||||||
connmapmu.Lock()
|
|
||||||
connections[l.peerip.String()] = &l
|
|
||||||
connmapmu.Unlock()
|
|
||||||
l.keepAlive()
|
|
||||||
} else {
|
|
||||||
err = errors.New("peer not exist")
|
|
||||||
}
|
}
|
||||||
return
|
return nil, errors.New("peer not exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Link) Close() {
|
func (l *Link) Close() {
|
||||||
l.conn.Close()
|
|
||||||
connmapmu.Lock()
|
connmapmu.Lock()
|
||||||
delete(connections, l.peerip.String())
|
delete(connections, l.peerip.String())
|
||||||
connmapmu.Unlock()
|
connmapmu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Link) Read() *head.Packet {
|
func (l *Link) Read() *head.Packet {
|
||||||
return <-l.peer.pipe
|
return <-l.pipe
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Link) Write(p *head.Packet) (n int, err error) {
|
func (l *Link) Write(p *head.Packet) (n int, err error) {
|
||||||
d := p.Mashal(me.String(), l.peerip.String())
|
var d []byte
|
||||||
d, err = l.peer.Encode(d)
|
d, err = p.Mashal(me.String(), l.peerip.String())
|
||||||
|
logrus.Debugln("[link] write data", string(d))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
n, err = l.conn.Write(d)
|
d, err = l.Encode(d)
|
||||||
|
if err == nil {
|
||||||
|
n, err = myconn.WriteToUDP(d, l.endpoint)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
31
gold/link/link_test.go
Normal file
31
gold/link/link_test.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package link
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUDP(t *testing.T) {
|
||||||
|
t.Log("test start")
|
||||||
|
lconn, err := net.ListenUDP("udp", &net.UDPAddr{Port: 1234})
|
||||||
|
if err == nil {
|
||||||
|
dconn, err := net.DialUDP("udp", &net.UDPAddr{Port: 1235}, &net.UDPAddr{Port: 1234})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
_, err = dconn.Write(([]byte)("1234567890"))
|
||||||
|
t.Log("write succ")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
d := make([]byte, 10)
|
||||||
|
_, err = lconn.Read(d)
|
||||||
|
t.Log("read succ")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log(d)
|
||||||
|
} else {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,33 +10,47 @@ import (
|
|||||||
var (
|
var (
|
||||||
privKey [32]byte
|
privKey [32]byte
|
||||||
me net.IP
|
me net.IP
|
||||||
|
myend *net.UDPAddr
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetMyself(privateKey [32]byte, myIP string) {
|
func SetMyself(privateKey [32]byte, myIP string, myEndpoint string) {
|
||||||
privKey = privateKey
|
privKey = privateKey
|
||||||
|
var err error
|
||||||
|
myend, err = net.ResolveUDPAddr("udp", myEndpoint)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
me = net.ParseIP(myIP)
|
me = net.ParseIP(myIP)
|
||||||
|
myconn, err = listen()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (id *Identity) Encode(b []byte) (eb []byte, err error) {
|
func (l *Link) Encode(b []byte) (eb []byte, err error) {
|
||||||
return b, nil
|
return b, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (id *Identity) Decode(b []byte) (db []byte, err error) {
|
func (l *Link) Decode(b []byte) (db []byte, err error) {
|
||||||
return b, nil
|
return b, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Listen(endpoint string) error {
|
func listen() (conn *net.UDPConn, err error) {
|
||||||
conn, err := net.ListenPacket("udp", endpoint)
|
conn, err = net.ListenUDP("udp", myend)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
go func() {
|
go func() {
|
||||||
listenbuff := make([]byte, 65536)
|
listenbuff := make([]byte, 65536)
|
||||||
for {
|
for {
|
||||||
_, addr, err := conn.ReadFrom(listenbuff)
|
lbf := listenbuff
|
||||||
|
n, addr, err := conn.ReadFromUDP(lbf)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
p, ok := IsInPeer(addr.String())
|
lbf = lbf[:n]
|
||||||
|
p, ok := IsEndpointInPeer(addr.String())
|
||||||
|
logrus.Infoln("[link] recv from endpoint", addr)
|
||||||
|
logrus.Debugln("[link] recv:", string(lbf))
|
||||||
if ok {
|
if ok {
|
||||||
packet := head.Packet{}
|
packet := head.Packet{}
|
||||||
d, err := p.Decode(listenbuff)
|
d, err := p.Decode(lbf)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
packet.UnMashal(d)
|
packet.UnMashal(d)
|
||||||
r := packet.DataSZ - uint32(len(packet.Data))
|
r := packet.DataSZ - uint32(len(packet.Data))
|
||||||
@@ -45,7 +59,7 @@ func Listen(endpoint string) error {
|
|||||||
n := 0
|
n := 0
|
||||||
remain := make([]byte, r)
|
remain := make([]byte, r)
|
||||||
for r > 0 {
|
for r > 0 {
|
||||||
n, _, err = conn.ReadFrom(remain[i:])
|
n, _, err = conn.ReadFromUDP(remain[i:])
|
||||||
if err == nil {
|
if err == nil {
|
||||||
i += n
|
i += n
|
||||||
r -= uint32(n)
|
r -= uint32(n)
|
||||||
@@ -56,6 +70,7 @@ func Listen(endpoint string) error {
|
|||||||
}
|
}
|
||||||
packet.Data = append(packet.Data, remain...)
|
packet.Data = append(packet.Data, remain...)
|
||||||
}
|
}
|
||||||
|
logrus.Infoln("[link] deliver to", p.peerip)
|
||||||
p.pipe <- &packet
|
p.pipe <- &packet
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -63,5 +78,5 @@ func Listen(endpoint string) error {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (l *Link) keepAlive() {
|
func (l *Link) keepAlive() {
|
||||||
if l.peer.KeepAlive > 0 && !l.hasKeepRuning {
|
if l.KeepAlive > 0 && !l.hasKeepRuning {
|
||||||
l.hasKeepRuning = true
|
l.hasKeepRuning = true
|
||||||
go func() {
|
go func() {
|
||||||
t := time.NewTicker(time.Second * time.Duration(l.peer.KeepAlive))
|
t := time.NewTicker(time.Second * time.Duration(l.KeepAlive))
|
||||||
for range t.C {
|
for range t.C {
|
||||||
_, _ = l.Write(head.NewPacket(head.ProtoHello, 0, 0, nil))
|
_, _ = l.Write(head.NewPacket(head.ProtoHello, 0, 0, nil))
|
||||||
logrus.Infoln("[link.nat] send keep alive packet")
|
logrus.Infoln("[link.nat] send keep alive packet")
|
||||||
|
|||||||
55
gold/link/peer.go
Normal file
55
gold/link/peer.go
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package link
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/fumiama/WireGold/gold/head"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
eps = make(map[string]*Link)
|
||||||
|
epmu sync.RWMutex
|
||||||
|
)
|
||||||
|
|
||||||
|
func AddPeer(peerip string, pubicKey [32]byte, endPoint string, keepAlive int64) (l *Link) {
|
||||||
|
peerip = net.ParseIP(peerip).String()
|
||||||
|
var ok bool
|
||||||
|
l, ok = IsInPeer(peerip)
|
||||||
|
if ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
e, err := net.ResolveUDPAddr("udp", endPoint)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
l = &Link{
|
||||||
|
PubicKey: pubicKey,
|
||||||
|
EndPoint: endPoint,
|
||||||
|
KeepAlive: keepAlive,
|
||||||
|
pipe: make(chan *head.Packet, 32),
|
||||||
|
peerip: net.ParseIP(peerip),
|
||||||
|
endpoint: e,
|
||||||
|
}
|
||||||
|
connmapmu.Lock()
|
||||||
|
epmu.Lock()
|
||||||
|
connections[peerip] = l
|
||||||
|
eps[endPoint] = l
|
||||||
|
connmapmu.Unlock()
|
||||||
|
epmu.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsInPeer(peer string) (p *Link, ok bool) {
|
||||||
|
connmapmu.RLock()
|
||||||
|
p, ok = connections[peer]
|
||||||
|
connmapmu.RUnlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsEndpointInPeer(ep string) (p *Link, ok bool) {
|
||||||
|
epmu.RLock()
|
||||||
|
p, ok = eps[ep]
|
||||||
|
epmu.RUnlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
package link
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/fumiama/WireGold/gold/head"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Identity struct {
|
|
||||||
PubicKey [32]byte
|
|
||||||
EndPoint string
|
|
||||||
KeepAlive int64
|
|
||||||
pipe chan *head.Packet
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
peers = make(map[string]*Identity)
|
|
||||||
peersmu sync.RWMutex
|
|
||||||
)
|
|
||||||
|
|
||||||
func AddPeer(peerip string, pubicKey [32]byte, endPoint string, keepAlive int64) (i *Identity) {
|
|
||||||
peerip = net.ParseIP(peerip).String()
|
|
||||||
var ok bool
|
|
||||||
peersmu.RLock()
|
|
||||||
i, ok = peers[peerip]
|
|
||||||
peersmu.RUnlock()
|
|
||||||
if ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
i = &Identity{
|
|
||||||
PubicKey: pubicKey,
|
|
||||||
EndPoint: endPoint,
|
|
||||||
KeepAlive: keepAlive,
|
|
||||||
pipe: make(chan *head.Packet, 32),
|
|
||||||
}
|
|
||||||
peersmu.Lock()
|
|
||||||
peers[peerip] = i
|
|
||||||
peersmu.Unlock()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsInPeer(peer string) (p *Identity, ok bool) {
|
|
||||||
peersmu.RLock()
|
|
||||||
p, ok = peers[peer]
|
|
||||||
peersmu.RUnlock()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
@@ -20,10 +20,8 @@ type Tunnel struct {
|
|||||||
|
|
||||||
func Create(peer string, srcport uint16, destport uint16) (s Tunnel, err error) {
|
func Create(peer string, srcport uint16, destport uint16) (s Tunnel, err error) {
|
||||||
logrus.Infoln("[tunnel] create from", srcport, "to", destport)
|
logrus.Infoln("[tunnel] create from", srcport, "to", destport)
|
||||||
var l link.Link
|
s.l, err = link.Connect(peer)
|
||||||
l, err = link.Connect(peer)
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
s.l = &l
|
|
||||||
s.in = make(chan []byte, 4)
|
s.in = make(chan []byte, 4)
|
||||||
s.out = make(chan []byte, 4)
|
s.out = make(chan []byte, 4)
|
||||||
s.src = srcport
|
s.src = srcport
|
||||||
@@ -68,7 +66,9 @@ func (s *Tunnel) Close() error {
|
|||||||
|
|
||||||
func (s *Tunnel) handleWrite() {
|
func (s *Tunnel) handleWrite() {
|
||||||
for b := range s.in {
|
for b := range s.in {
|
||||||
|
logrus.Debugln("[tunnel] write recv", b)
|
||||||
if b == nil {
|
if b == nil {
|
||||||
|
logrus.Errorln("[tunnel] write recv nil")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
logrus.Debugln("[tunnel] writing", len(b), "bytes...")
|
logrus.Debugln("[tunnel] writing", len(b), "bytes...")
|
||||||
@@ -86,8 +86,10 @@ func (s *Tunnel) handleRead() {
|
|||||||
for {
|
for {
|
||||||
p := s.l.Read()
|
p := s.l.Read()
|
||||||
if p == nil {
|
if p == nil {
|
||||||
|
logrus.Errorln("[tunnel] read recv nil")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
logrus.Debugln("[tunnel] read recv", p.Data)
|
||||||
s.out <- p.Data
|
s.out <- p.Data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,20 +4,24 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/fumiama/WireGold/gold/link"
|
"github.com/fumiama/WireGold/gold/link"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTunnel(t *testing.T) {
|
func TestTunnel(t *testing.T) {
|
||||||
link.SetMyself([32]byte{}, "127.0.0.1:1234")
|
logrus.SetLevel(logrus.DebugLevel)
|
||||||
link.AddPeer("192.168.1.1", [32]byte{}, "127.0.0.1:1235", 0)
|
link.SetMyself([32]byte{}, "192.168.1.2", "127.0.0.1:1236")
|
||||||
link.Listen("127.0.0.1:1234")
|
link.AddPeer("192.168.1.2", [32]byte{}, "127.0.0.1:1236", 0)
|
||||||
link.Listen("127.0.0.1:1235")
|
tunn, err := Create("192.168.1.2", 1, 1)
|
||||||
tunn, err := Create("192.168.1.1", 1, 1)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
} else {
|
} else {
|
||||||
tunn.Write(([]byte)("1234"))
|
sendb := ([]byte)("1234")
|
||||||
|
tunn.Write(sendb)
|
||||||
p := make([]byte, 4)
|
p := make([]byte, 4)
|
||||||
tunn.Read(p)
|
tunn.Read(p)
|
||||||
t.Log(p)
|
if string(sendb) != string(p) {
|
||||||
|
t.Log("error: recv", p)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user