mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-04 23:40:26 +08:00
init
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -13,3 +13,5 @@
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
|
||||
.DS_Store
|
||||
5
go.mod
Normal file
5
go.mod
Normal file
@@ -0,0 +1,5 @@
|
||||
module github.com/fumiama/WireGold
|
||||
|
||||
go 1.16
|
||||
|
||||
require github.com/sirupsen/logrus v1.8.1
|
||||
10
go.sum
Normal file
10
go.sum
Normal file
@@ -0,0 +1,10 @@
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
||||
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
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")
|
||||
}
|
||||
}
|
||||
7
upper/data.go
Normal file
7
upper/data.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package upper
|
||||
|
||||
import "io"
|
||||
|
||||
type Service interface {
|
||||
io.ReadWriteCloser
|
||||
}
|
||||
46
upper/services/tunnel/tunnel.go
Normal file
46
upper/services/tunnel/tunnel.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package tunnel
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/fumiama/WireGold/gold/head"
|
||||
"github.com/fumiama/WireGold/gold/link"
|
||||
)
|
||||
|
||||
type Tunnel struct {
|
||||
l *link.Link
|
||||
In *chan []byte
|
||||
Out *chan []byte
|
||||
src uint16
|
||||
dest uint16
|
||||
}
|
||||
|
||||
func Create(peer string, srcport uint16, destport uint16) (s Tunnel, err error) {
|
||||
logrus.Infoln("[tunnel] create from", srcport, "to", destport)
|
||||
var l link.Link
|
||||
l, err = link.Connect(peer)
|
||||
if err == nil {
|
||||
s.l = &l
|
||||
s.In = new(chan []byte)
|
||||
s.Out = new(chan []byte)
|
||||
s.src = srcport
|
||||
s.dest = destport
|
||||
go s.handleWrite()
|
||||
} else {
|
||||
logrus.Errorln("[tunnel] create err:", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Tunnel) handleWrite() {
|
||||
for b := range *s.In {
|
||||
_, err := s.l.Write(head.NewPacket(head.ProtoData, s.src, s.dest, b))
|
||||
if err != nil {
|
||||
logrus.Errorln("[tunnel] write err:", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Tunnel) Handle(srcport uint16, destport uint16, data *[]byte) {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user