fix
This commit is contained in:
parent
5d26cd81ec
commit
0117c80ec8
17
client.go
17
client.go
@ -9,7 +9,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewClient(addr string, server string, target string, timeout int, sproto int, rproto int, catch int) (*Client, error) {
|
func NewClient(addr string, server string, target string, timeout int, sproto int, rproto int, catch int, key int) (*Client, error) {
|
||||||
|
|
||||||
ipaddr, err := net.ResolveUDPAddr("udp", addr)
|
ipaddr, err := net.ResolveUDPAddr("udp", addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -33,6 +33,7 @@ func NewClient(addr string, server string, target string, timeout int, sproto in
|
|||||||
sproto: sproto,
|
sproto: sproto,
|
||||||
rproto: rproto,
|
rproto: rproto,
|
||||||
catch: catch,
|
catch: catch,
|
||||||
|
key: key,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,6 +45,7 @@ type Client struct {
|
|||||||
sproto int
|
sproto int
|
||||||
rproto int
|
rproto int
|
||||||
catch int
|
catch int
|
||||||
|
key int
|
||||||
|
|
||||||
ipaddr *net.UDPAddr
|
ipaddr *net.UDPAddr
|
||||||
addr string
|
addr string
|
||||||
@ -180,7 +182,8 @@ func (p *Client) Accept() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
clientConn.activeTime = now
|
clientConn.activeTime = now
|
||||||
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(DATA), bytes[:n], p.sproto, p.rproto, p.catch)
|
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(DATA), bytes[:n],
|
||||||
|
p.sproto, p.rproto, p.catch, p.key)
|
||||||
|
|
||||||
p.sequence++
|
p.sequence++
|
||||||
|
|
||||||
@ -195,6 +198,10 @@ func (p *Client) processPacket(packet *Packet) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if packet.key != p.key {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if packet.msgType == PING {
|
if packet.msgType == PING {
|
||||||
t := time.Time{}
|
t := time.Time{}
|
||||||
t.UnmarshalBinary(packet.data)
|
t.UnmarshalBinary(packet.data)
|
||||||
@ -259,7 +266,8 @@ func (p *Client) ping() {
|
|||||||
if p.sendPacket == 0 {
|
if p.sendPacket == 0 {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
b, _ := now.MarshalBinary()
|
b, _ := now.MarshalBinary()
|
||||||
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, "", (uint32)(PING), b, p.sproto, p.rproto, p.catch)
|
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, "", (uint32)(PING), b,
|
||||||
|
p.sproto, p.rproto, p.catch, p.key)
|
||||||
fmt.Printf("ping %s %s %d %d %d %d\n", p.addrServer, now.String(), p.sproto, p.rproto, p.id, p.sequence)
|
fmt.Printf("ping %s %s %d %d %d %d\n", p.addrServer, now.String(), p.sproto, p.rproto, p.id, p.sequence)
|
||||||
p.sequence++
|
p.sequence++
|
||||||
}
|
}
|
||||||
@ -279,7 +287,8 @@ func (p *Client) showNet() {
|
|||||||
func (p *Client) sendCatch() {
|
func (p *Client) sendCatch() {
|
||||||
if p.catch > 0 {
|
if p.catch > 0 {
|
||||||
for _, conn := range p.localIdToConnMap {
|
for _, conn := range p.localIdToConnMap {
|
||||||
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, conn.id, (uint32)(CATCH), make([]byte, 0), p.sproto, p.rproto, p.catch)
|
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, conn.id, (uint32)(CATCH), make([]byte, 0),
|
||||||
|
p.sproto, p.rproto, p.catch, p.key)
|
||||||
p.sequence++
|
p.sequence++
|
||||||
p.sendCatchPacket++
|
p.sendCatchPacket++
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,7 @@ type MyMsg struct {
|
|||||||
Data []byte
|
Data []byte
|
||||||
RPROTO uint16
|
RPROTO uint16
|
||||||
CATCH uint16
|
CATCH uint16
|
||||||
|
KEY uint32
|
||||||
ENDTYPE uint32
|
ENDTYPE uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,7 +38,7 @@ func (p *MyMsg) Len(proto int) int {
|
|||||||
if p == nil {
|
if p == nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return 4 + p.LenString(p.ID) + p.LenString(p.TARGET) + p.LenData(p.Data) + 2 + 2 + 4
|
return 4 + p.LenString(p.ID) + p.LenString(p.TARGET) + p.LenData(p.Data) + 2 + 2 + 4 + 4
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *MyMsg) LenString(s string) int {
|
func (p *MyMsg) LenString(s string) int {
|
||||||
@ -68,7 +69,9 @@ func (p *MyMsg) Marshal(proto int) ([]byte, error) {
|
|||||||
|
|
||||||
binary.BigEndian.PutUint16(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+2:], uint16(p.CATCH))
|
binary.BigEndian.PutUint16(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+2:], uint16(p.CATCH))
|
||||||
|
|
||||||
binary.BigEndian.PutUint32(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+4:], uint32(p.ENDTYPE))
|
binary.BigEndian.PutUint32(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+4:], uint32(p.KEY))
|
||||||
|
|
||||||
|
binary.BigEndian.PutUint32(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+8:], uint32(p.ENDTYPE))
|
||||||
|
|
||||||
return b, nil
|
return b, nil
|
||||||
}
|
}
|
||||||
@ -105,7 +108,9 @@ func (p *MyMsg) Unmarshal(b []byte) error {
|
|||||||
|
|
||||||
p.CATCH = binary.BigEndian.Uint16(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+2:])
|
p.CATCH = binary.BigEndian.Uint16(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+2:])
|
||||||
|
|
||||||
p.ENDTYPE = binary.BigEndian.Uint32(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+4:])
|
p.KEY = binary.BigEndian.Uint32(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+4:])
|
||||||
|
|
||||||
|
p.ENDTYPE = binary.BigEndian.Uint32(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+8:])
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -131,7 +136,7 @@ func (p *MyMsg) UnmarshalData(b []byte) []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func sendICMP(id int, sequence int, conn icmp.PacketConn, server *net.IPAddr, target string,
|
func sendICMP(id int, sequence int, conn icmp.PacketConn, server *net.IPAddr, target string,
|
||||||
connId string, msgType uint32, data []byte, sproto int, rproto int, catch int) {
|
connId string, msgType uint32, data []byte, sproto int, rproto int, catch int, key int) {
|
||||||
|
|
||||||
m := &MyMsg{
|
m := &MyMsg{
|
||||||
ID: connId,
|
ID: connId,
|
||||||
@ -140,6 +145,7 @@ func sendICMP(id int, sequence int, conn icmp.PacketConn, server *net.IPAddr, ta
|
|||||||
Data: data,
|
Data: data,
|
||||||
RPROTO: (uint16)(rproto),
|
RPROTO: (uint16)(rproto),
|
||||||
CATCH: (uint16)(catch),
|
CATCH: (uint16)(catch),
|
||||||
|
KEY: (uint32)(key),
|
||||||
ENDTYPE: END,
|
ENDTYPE: END,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -217,7 +223,8 @@ func recvICMP(conn icmp.PacketConn, recv chan<- *Packet) {
|
|||||||
|
|
||||||
recv <- &Packet{msgType: my.TYPE, data: my.Data, id: my.ID, target: my.TARGET,
|
recv <- &Packet{msgType: my.TYPE, data: my.Data, id: my.ID, target: my.TARGET,
|
||||||
src: srcaddr.(*net.IPAddr), rproto: (int)((int16)(my.RPROTO)),
|
src: srcaddr.(*net.IPAddr), rproto: (int)((int16)(my.RPROTO)),
|
||||||
echoId: echoId, echoSeq: echoSeq, catch: (int)((int16)(my.CATCH))}
|
echoId: echoId, echoSeq: echoSeq, catch: (int)((int16)(my.CATCH)),
|
||||||
|
key: (int)(my.KEY)}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -231,6 +238,7 @@ type Packet struct {
|
|||||||
echoId int
|
echoId int
|
||||||
echoSeq int
|
echoSeq int
|
||||||
catch int
|
catch int
|
||||||
|
key int
|
||||||
}
|
}
|
||||||
|
|
||||||
func UniqueId() string {
|
func UniqueId() string {
|
||||||
|
17
server.go
17
server.go
@ -7,14 +7,16 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewServer(timeout int) (*Server, error) {
|
func NewServer(timeout int, key int) (*Server, error) {
|
||||||
return &Server{
|
return &Server{
|
||||||
timeout: timeout,
|
timeout: timeout,
|
||||||
|
key: key,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
timeout int
|
timeout int
|
||||||
|
key int
|
||||||
|
|
||||||
conn *icmp.PacketConn
|
conn *icmp.PacketConn
|
||||||
|
|
||||||
@ -73,6 +75,10 @@ func (p *Server) Run() {
|
|||||||
|
|
||||||
func (p *Server) processPacket(packet *Packet) {
|
func (p *Server) processPacket(packet *Packet) {
|
||||||
|
|
||||||
|
if packet.key != p.key {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
p.echoId = packet.echoId
|
p.echoId = packet.echoId
|
||||||
p.echoSeq = packet.echoSeq
|
p.echoSeq = packet.echoSeq
|
||||||
|
|
||||||
@ -80,7 +86,8 @@ func (p *Server) processPacket(packet *Packet) {
|
|||||||
t := time.Time{}
|
t := time.Time{}
|
||||||
t.UnmarshalBinary(packet.data)
|
t.UnmarshalBinary(packet.data)
|
||||||
fmt.Printf("ping from %s %s %d %d %d\n", packet.src.String(), t.String(), packet.rproto, packet.echoId, packet.echoSeq)
|
fmt.Printf("ping from %s %s %d %d %d\n", packet.src.String(), t.String(), packet.rproto, packet.echoId, packet.echoSeq)
|
||||||
sendICMP(packet.echoId, packet.echoSeq, *p.conn, packet.src, "", "", (uint32)(PING), packet.data, packet.rproto, -1, 0)
|
sendICMP(packet.echoId, packet.echoSeq, *p.conn, packet.src, "", "", (uint32)(PING), packet.data,
|
||||||
|
packet.rproto, -1, 0, p.key)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,7 +128,8 @@ func (p *Server) processPacket(packet *Packet) {
|
|||||||
if packet.msgType == CATCH {
|
if packet.msgType == CATCH {
|
||||||
select {
|
select {
|
||||||
case re := <-udpConn.catchQueue:
|
case re := <-udpConn.catchQueue:
|
||||||
sendICMP(packet.echoId, packet.echoSeq, *p.conn, re.src, "", re.id, (uint32)(CATCH), re.data, re.conn.rproto, -1, 0)
|
sendICMP(packet.echoId, packet.echoSeq, *p.conn, re.src, "", re.id, (uint32)(CATCH), re.data,
|
||||||
|
re.conn.rproto, -1, 0, p.key)
|
||||||
p.sendCatchPacket++
|
p.sendCatchPacket++
|
||||||
case <-time.After(time.Duration(1) * time.Millisecond):
|
case <-time.After(time.Duration(1) * time.Millisecond):
|
||||||
}
|
}
|
||||||
@ -174,7 +182,8 @@ func (p *Server) Recv(conn *ServerConn, id string, src *net.IPAddr) {
|
|||||||
case <-time.After(time.Duration(10) * time.Millisecond):
|
case <-time.After(time.Duration(10) * time.Millisecond):
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
sendICMP(p.echoId, p.echoSeq, *p.conn, src, "", id, (uint32)(DATA), bytes[:n], conn.rproto, -1, 0)
|
sendICMP(p.echoId, p.echoSeq, *p.conn, src, "", id, (uint32)(DATA), bytes[:n],
|
||||||
|
conn.rproto, -1, 0, p.key)
|
||||||
}
|
}
|
||||||
|
|
||||||
p.sendPacket++
|
p.sendPacket++
|
||||||
|
Loading…
Reference in New Issue
Block a user