mirror of
https://github.com/KusakabeShi/EtherGuard-VPN.git
synced 2024-11-22 23:33:09 +01:00
149 lines
3.5 KiB
Go
149 lines
3.5 KiB
Go
package path
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/gob"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/KusakabeSi/EtherGuardVPN/config"
|
|
)
|
|
|
|
func GetByte(structIn interface{}) (bb []byte, err error) {
|
|
var b bytes.Buffer
|
|
e := gob.NewEncoder(&b)
|
|
if err := e.Encode(structIn); err != nil {
|
|
panic(err)
|
|
}
|
|
bb = b.Bytes()
|
|
return
|
|
}
|
|
|
|
type RegisterMsg struct {
|
|
Node_id config.Vertex `struc:"uint32"`
|
|
Init bool
|
|
}
|
|
|
|
func (c *RegisterMsg) ToString() string {
|
|
return "RegisterMsg Node_id:" + strconv.Itoa(int(c.Node_id))
|
|
}
|
|
|
|
func ParseRegisterMsg(bin []byte) (StructPlace RegisterMsg, err error) {
|
|
var b bytes.Buffer
|
|
b.Write(bin)
|
|
d := gob.NewDecoder(&b)
|
|
err = d.Decode(&StructPlace)
|
|
return
|
|
}
|
|
|
|
type UpdatePeerMsg struct {
|
|
State_hash [32]byte `struc:"[32]uint8"`
|
|
}
|
|
|
|
func (c *UpdatePeerMsg) ToString() string {
|
|
return "UpdatePeerMsg State_hash:" + string(c.State_hash[:])
|
|
}
|
|
|
|
func ParseUpdatePeerMsg(bin []byte) (StructPlace UpdatePeerMsg, err error) {
|
|
var b bytes.Buffer
|
|
b.Write(bin)
|
|
d := gob.NewDecoder(&b)
|
|
err = d.Decode(&StructPlace)
|
|
return
|
|
}
|
|
|
|
type UpdateNhTableMsg struct {
|
|
State_hash [32]byte `struc:"[32]uint8"`
|
|
}
|
|
|
|
func (c *UpdateNhTableMsg) ToString() string {
|
|
return "UpdateNhTableMsg State_hash:" + string(c.State_hash[:])
|
|
}
|
|
|
|
func ParseUpdateNhTableMsg(bin []byte) (StructPlace UpdateNhTableMsg, err error) {
|
|
var b bytes.Buffer
|
|
b.Write(bin)
|
|
d := gob.NewDecoder(&b)
|
|
err = d.Decode(&StructPlace)
|
|
return
|
|
}
|
|
|
|
type PingMsg struct {
|
|
RequestID uint32 `struc:"uint32"`
|
|
Src_nodeID config.Vertex `struc:"uint32"`
|
|
Time time.Time `struc:"uint64"`
|
|
}
|
|
|
|
func (c *PingMsg) ToString() string {
|
|
return "PingMsg SID:" + strconv.Itoa(int(c.Src_nodeID)) + " Time:" + c.Time.String() + " RequestID:" + strconv.Itoa(int(c.RequestID))
|
|
}
|
|
|
|
func ParsePingMsg(bin []byte) (StructPlace PingMsg, err error) {
|
|
var b bytes.Buffer
|
|
b.Write(bin)
|
|
d := gob.NewDecoder(&b)
|
|
err = d.Decode(&StructPlace)
|
|
return
|
|
}
|
|
|
|
type PongMsg struct {
|
|
RequestID uint32
|
|
Src_nodeID config.Vertex `struc:"uint32"`
|
|
Dst_nodeID config.Vertex `struc:"uint32"`
|
|
Timediff time.Duration `struc:"int64"`
|
|
}
|
|
|
|
func (c *PongMsg) ToString() string {
|
|
return "PongMsg SID:" + strconv.Itoa(int(c.Src_nodeID)) + " DID:" + strconv.Itoa(int(c.Dst_nodeID)) + " Timediff:" + c.Timediff.String() + " RequestID:" + strconv.Itoa(int(c.RequestID))
|
|
}
|
|
|
|
func ParsePongMsg(bin []byte) (StructPlace PongMsg, err error) {
|
|
var b bytes.Buffer
|
|
b.Write(bin)
|
|
d := gob.NewDecoder(&b)
|
|
err = d.Decode(&StructPlace)
|
|
return
|
|
}
|
|
|
|
type RequestPeerMsg struct {
|
|
Request_ID uint32 `struc:"uint32"`
|
|
}
|
|
|
|
func (c *RequestPeerMsg) ToString() string {
|
|
return "RequestPeerMsg Request_ID:" + strconv.Itoa(int(c.Request_ID))
|
|
}
|
|
|
|
func ParseRequestPeerMsg(bin []byte) (StructPlace RequestPeerMsg, err error) {
|
|
var b bytes.Buffer
|
|
b.Write(bin)
|
|
d := gob.NewDecoder(&b)
|
|
err = d.Decode(&StructPlace)
|
|
return
|
|
}
|
|
|
|
type BoardcastPeerMsg struct {
|
|
Request_ID uint32 `struc:"uint32"`
|
|
NodeID config.Vertex `struc:"uint32"`
|
|
PubKey [32]byte `struc:"[32]uint8"`
|
|
PSKey [32]byte `struc:"[32]uint8"`
|
|
ConnURL string
|
|
}
|
|
|
|
func (c *BoardcastPeerMsg) ToString() string {
|
|
return "BoardcastPeerMsg Request_ID:" + strconv.Itoa(int(c.Request_ID)) + " NodeID:" + strconv.Itoa(int(c.NodeID)) + " ConnURL:" + c.ConnURL
|
|
}
|
|
|
|
func ParseBoardcastPeerMsg(bin []byte) (StructPlace BoardcastPeerMsg, err error) {
|
|
var b bytes.Buffer
|
|
b.Write(bin)
|
|
d := gob.NewDecoder(&b)
|
|
err = d.Decode(&StructPlace)
|
|
return
|
|
}
|
|
|
|
type SUPER_Events struct {
|
|
Event_server_pong chan PongMsg
|
|
Event_server_register chan RegisterMsg
|
|
Event_server_NhTable_changed chan struct{}
|
|
}
|