add
This commit is contained in:
parent
e7fecd4e1c
commit
75edf4c3c1
12
client.go
12
client.go
@ -3,6 +3,8 @@ package pingtunnel
|
||||
import (
|
||||
"fmt"
|
||||
"golang.org/x/net/icmp"
|
||||
"math"
|
||||
"math/rand"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
@ -19,7 +21,10 @@ func NewClient(addr string, server string, target string, timeout int, sproto in
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
return &Client{
|
||||
id: r.Intn(math.MaxInt16),
|
||||
r: r,
|
||||
ipaddr: ipaddr,
|
||||
addr: addr,
|
||||
ipaddrServer: ipaddrServer,
|
||||
@ -32,6 +37,9 @@ func NewClient(addr string, server string, target string, timeout int, sproto in
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
id int
|
||||
r *rand.Rand
|
||||
|
||||
timeout int
|
||||
sproto int
|
||||
rproto int
|
||||
@ -156,7 +164,7 @@ func (p *Client) Accept() error {
|
||||
}
|
||||
|
||||
clientConn.activeTime = now
|
||||
sendICMP(*p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(DATA), bytes[:n], p.sproto, p.rproto)
|
||||
sendICMP(p.id, p.r.Intn(math.MaxInt16), *p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(DATA), bytes[:n], p.sproto, p.rproto)
|
||||
|
||||
p.sendPacket++
|
||||
p.sendPacketSize += (uint64)(n)
|
||||
@ -229,7 +237,7 @@ func (p *Client) ping() {
|
||||
if p.sendPacket == 0 && p.recvPacket == 0 {
|
||||
now := time.Now()
|
||||
b, _ := now.MarshalBinary()
|
||||
sendICMP(*p.conn, p.ipaddrServer, p.targetAddr, "", (uint32)(PING), b, p.sproto, p.rproto)
|
||||
sendICMP(p.id, p.r.Intn(math.MaxInt16), *p.conn, p.ipaddrServer, p.targetAddr, "", (uint32)(PING), b, p.sproto, p.rproto)
|
||||
fmt.Printf("ping %s %s %d %d\n", p.addrServer, now.String(), p.sproto, p.rproto)
|
||||
}
|
||||
}
|
||||
|
@ -125,7 +125,7 @@ func (p *MyMsg) UnmarshalData(b []byte) []byte {
|
||||
return data
|
||||
}
|
||||
|
||||
func sendICMP(conn icmp.PacketConn, server *net.IPAddr, target string, connId string, msgType uint32, data []byte, sproto int, rproto int) {
|
||||
func sendICMP(id int, sequence int, conn icmp.PacketConn, server *net.IPAddr, target string, connId string, msgType uint32, data []byte, sproto int, rproto int) {
|
||||
|
||||
m := &MyMsg{
|
||||
ID: connId,
|
||||
@ -136,10 +136,18 @@ func sendICMP(conn icmp.PacketConn, server *net.IPAddr, target string, connId st
|
||||
ENDTYPE: END,
|
||||
}
|
||||
|
||||
mb, err := m.Marshal(0)
|
||||
|
||||
body := &icmp.Echo{
|
||||
ID: id,
|
||||
Seq: sequence,
|
||||
Data: mb,
|
||||
}
|
||||
|
||||
msg := &icmp.Message{
|
||||
Type: (ipv4.ICMPType)(sproto),
|
||||
Code: 0,
|
||||
Body: m,
|
||||
Body: body,
|
||||
}
|
||||
|
||||
bytes, err := msg.Marshal(nil)
|
||||
@ -184,7 +192,7 @@ func recvICMP(conn icmp.PacketConn, recv chan<- *Packet) {
|
||||
|
||||
my := &MyMsg{
|
||||
}
|
||||
my.Unmarshal(bytes[4:n])
|
||||
my.Unmarshal(bytes[8:n])
|
||||
|
||||
if (my.TYPE != (uint32)(DATA) && my.TYPE != (uint32)(PING)) || my.ENDTYPE != (uint32)(END) {
|
||||
//fmt.Printf("processPacket diff type %s %d %d \n", my.ID, my.TYPE, my.ENDTYPE)
|
||||
|
12
server.go
12
server.go
@ -3,17 +3,25 @@ package pingtunnel
|
||||
import (
|
||||
"fmt"
|
||||
"golang.org/x/net/icmp"
|
||||
"math"
|
||||
"math/rand"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewServer(timeout int) (*Server, error) {
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
return &Server{
|
||||
id: r.Intn(math.MaxInt16),
|
||||
r: r,
|
||||
timeout: timeout,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
id int
|
||||
r *rand.Rand
|
||||
|
||||
timeout int
|
||||
|
||||
conn *icmp.PacketConn
|
||||
@ -69,7 +77,7 @@ func (p *Server) processPacket(packet *Packet) {
|
||||
t := time.Time{}
|
||||
t.UnmarshalBinary(packet.data)
|
||||
fmt.Printf("ping from %s %s %d\n", packet.src.String(), t.String(), packet.rproto)
|
||||
sendICMP(*p.conn, packet.src, "", "", (uint32)(PING), packet.data, packet.rproto, -1)
|
||||
sendICMP(p.id, p.r.Intn(math.MaxInt16), *p.conn, packet.src, "", "", (uint32)(PING), packet.data, packet.rproto, -1)
|
||||
return
|
||||
}
|
||||
|
||||
@ -136,7 +144,7 @@ func (p *Server) Recv(conn *ServerConn, id string, src *net.IPAddr) {
|
||||
now := time.Now()
|
||||
conn.activeTime = now
|
||||
|
||||
sendICMP(*p.conn, src, "", id, (uint32)(DATA), bytes[:n], conn.rproto, -1)
|
||||
sendICMP(p.id, p.r.Intn(math.MaxInt16), *p.conn, src, "", id, (uint32)(DATA), bytes[:n], conn.rproto, -1)
|
||||
|
||||
p.sendPacket++
|
||||
p.sendPacketSize += (uint64)(n)
|
||||
|
Loading…
Reference in New Issue
Block a user