mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-13 05:31:08 +08:00
init
This commit is contained in:
27
gold/head/packet.go
Normal file
27
gold/head/packet.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package head
|
||||
|
||||
type Packet struct {
|
||||
Proto uint8
|
||||
SrcPort uint16
|
||||
DstPort uint16
|
||||
TTL uint8
|
||||
Data []byte
|
||||
}
|
||||
|
||||
func NewPacket(proto uint8, srcPort uint16, dstPort uint16, data []byte) *Packet {
|
||||
return &Packet{
|
||||
Proto: proto,
|
||||
SrcPort: srcPort,
|
||||
DstPort: dstPort,
|
||||
TTL: 255,
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Packet) UnMashal(data []byte) {
|
||||
|
||||
}
|
||||
|
||||
func (p *Packet) Mashal() []byte {
|
||||
return nil
|
||||
}
|
||||
7
gold/head/protos.go
Normal file
7
gold/head/protos.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package head
|
||||
|
||||
const (
|
||||
ProtoHello uint8 = iota
|
||||
ProtoHelloAck
|
||||
ProtoData
|
||||
)
|
||||
23
gold/link/crypto.go
Normal file
23
gold/link/crypto.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package link
|
||||
|
||||
import "net"
|
||||
|
||||
var (
|
||||
privKey [32]byte
|
||||
myip string
|
||||
me net.IP
|
||||
)
|
||||
|
||||
func SetMyself(privateKey [32]byte, myIP string) {
|
||||
privKey = privateKey
|
||||
myip = myIP
|
||||
me = net.ParseIP(myIP)
|
||||
}
|
||||
|
||||
func (id *Identity) Encode(b []byte) (n int, err error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (id *Identity) Decode(b []byte) (n int, err error) {
|
||||
return 0, nil
|
||||
}
|
||||
9
gold/link/identify.go
Normal file
9
gold/link/identify.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package link
|
||||
|
||||
type Identity struct {
|
||||
PubicKey [32]byte
|
||||
EndPoint string
|
||||
KeepAlive int64
|
||||
}
|
||||
|
||||
var peers = make(map[string]*Identity)
|
||||
50
gold/link/link.go
Normal file
50
gold/link/link.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package link
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
|
||||
"github.com/fumiama/WireGold/gold/head"
|
||||
)
|
||||
|
||||
type Link struct {
|
||||
conn net.Conn
|
||||
peer *Identity
|
||||
hasKeepRuning bool
|
||||
}
|
||||
|
||||
func Connect(peer string) (l Link, err error) {
|
||||
p, ok := peers[peer]
|
||||
if ok {
|
||||
l.conn, err = net.Dial("udp", peer)
|
||||
l.peer = p
|
||||
} else {
|
||||
err = errors.New("peer not exist")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (l *Link) Close() {
|
||||
l.conn.Close()
|
||||
}
|
||||
|
||||
func (l *Link) Read(p *head.Packet) (n int, err error) {
|
||||
d := make([]byte, 1024)
|
||||
n, err = l.conn.Read(d)
|
||||
if err == nil {
|
||||
n, err = l.peer.Decode(d)
|
||||
if err == nil {
|
||||
p.UnMashal(d)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (l *Link) Write(p *head.Packet) (n int, err error) {
|
||||
d := p.Mashal()
|
||||
_, err = l.peer.Encode(d)
|
||||
if err == nil {
|
||||
n, err = l.conn.Write(d)
|
||||
}
|
||||
return
|
||||
}
|
||||
23
gold/link/nat.go
Normal file
23
gold/link/nat.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package link
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/fumiama/WireGold/gold/head"
|
||||
)
|
||||
|
||||
func (l *Link) KeepAlive() {
|
||||
if l.peer.KeepAlive > 0 && !l.hasKeepRuning {
|
||||
l.hasKeepRuning = true
|
||||
go func() {
|
||||
t := time.NewTicker(time.Second * time.Duration(l.peer.KeepAlive))
|
||||
for range t.C {
|
||||
_, _ = l.Write(head.NewPacket(head.ProtoHello, 0, 0, nil))
|
||||
logrus.Infoln("[link.nat] send keep alive packet")
|
||||
}
|
||||
}()
|
||||
logrus.Infoln("[link.nat] start to keep alive")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user