This commit is contained in:
esrrhs 2018-12-21 16:09:54 +08:00
parent 74b9105fbf
commit d72a7ff774
3 changed files with 24 additions and 19 deletions

View File

@ -24,7 +24,6 @@ func NewClient(addr string, server string, target string, timeout int, sproto in
r := rand.New(rand.NewSource(time.Now().UnixNano())) r := rand.New(rand.NewSource(time.Now().UnixNano()))
return &Client{ return &Client{
id: r.Intn(math.MaxInt16), id: r.Intn(math.MaxInt16),
r: r,
ipaddr: ipaddr, ipaddr: ipaddr,
addr: addr, addr: addr,
ipaddrServer: ipaddrServer, ipaddrServer: ipaddrServer,
@ -38,7 +37,7 @@ func NewClient(addr string, server string, target string, timeout int, sproto in
type Client struct { type Client struct {
id int id int
r *rand.Rand sequence int
timeout int timeout int
sproto int sproto int
@ -164,7 +163,9 @@ func (p *Client) Accept() error {
} }
clientConn.activeTime = now clientConn.activeTime = now
sendICMP(p.id, p.r.Intn(math.MaxInt16), *p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(DATA), bytes[:n], p.sproto, p.rproto) sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(DATA), bytes[:n], p.sproto, p.rproto)
p.sequence++
p.sendPacket++ p.sendPacket++
p.sendPacketSize += (uint64)(n) p.sendPacketSize += (uint64)(n)
@ -234,11 +235,12 @@ func (p *Client) checkTimeoutConn() {
} }
func (p *Client) ping() { func (p *Client) ping() {
if p.sendPacket == 0 && p.recvPacket == 0 { if p.sendPacket == 0 {
now := time.Now() now := time.Now()
b, _ := now.MarshalBinary() b, _ := now.MarshalBinary()
sendICMP(p.id, p.r.Intn(math.MaxInt16), *p.conn, p.ipaddrServer, p.targetAddr, "", (uint32)(PING), b, p.sproto, p.rproto) sendICMP(p.id, p.sequence, *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) fmt.Printf("ping %s %s %d %d %d %d\n", p.addrServer, now.String(), p.sproto, p.rproto, p.id, p.sequence)
p.sequence++
} }
} }

View File

@ -190,6 +190,9 @@ func recvICMP(conn icmp.PacketConn, recv chan<- *Packet) {
} }
} }
echoId := int(binary.BigEndian.Uint16(bytes[4:6]))
echoSeq := int(binary.BigEndian.Uint16(bytes[6:8]))
my := &MyMsg{ my := &MyMsg{
} }
my.Unmarshal(bytes[8:n]) my.Unmarshal(bytes[8:n])
@ -204,7 +207,9 @@ func recvICMP(conn icmp.PacketConn, recv chan<- *Packet) {
return return
} }
recv <- &Packet{msgType: my.TYPE, data: my.Data, id: my.ID, target: my.TARGET, src: srcaddr.(*net.IPAddr), rproto: (int)((int16)(my.RPROTO))} recv <- &Packet{msgType: my.TYPE, data: my.Data, id: my.ID, target: my.TARGET,
src: srcaddr.(*net.IPAddr), rproto: (int)((int16)(my.RPROTO)),
echoId: echoId, echoSeq: echoSeq}
} }
} }
@ -215,6 +220,8 @@ type Packet struct {
target string target string
src *net.IPAddr src *net.IPAddr
rproto int rproto int
echoId int
echoSeq int
} }
func UniqueId() string { func UniqueId() string {

View File

@ -3,25 +3,17 @@ package pingtunnel
import ( import (
"fmt" "fmt"
"golang.org/x/net/icmp" "golang.org/x/net/icmp"
"math"
"math/rand"
"net" "net"
"time" "time"
) )
func NewServer(timeout int) (*Server, error) { func NewServer(timeout int) (*Server, error) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return &Server{ return &Server{
id: r.Intn(math.MaxInt16),
r: r,
timeout: timeout, timeout: timeout,
}, nil }, nil
} }
type Server struct { type Server struct {
id int
r *rand.Rand
timeout int timeout int
conn *icmp.PacketConn conn *icmp.PacketConn
@ -41,6 +33,8 @@ type ServerConn struct {
activeTime time.Time activeTime time.Time
close bool close bool
rproto int rproto int
echoId int
echoSeq int
} }
func (p *Server) Run() { func (p *Server) Run() {
@ -76,8 +70,8 @@ func (p *Server) processPacket(packet *Packet) {
if packet.msgType == PING { if packet.msgType == PING {
t := time.Time{} t := time.Time{}
t.UnmarshalBinary(packet.data) t.UnmarshalBinary(packet.data)
fmt.Printf("ping from %s %s %d\n", packet.src.String(), t.String(), packet.rproto) fmt.Printf("ping from %s %s %d %d %d\n", packet.src.String(), t.String(), packet.rproto, packet.echoId, packet.echoSeq)
sendICMP(p.id, p.r.Intn(math.MaxInt16), *p.conn, packet.src, "", "", (uint32)(PING), packet.data, packet.rproto, -1) sendICMP(packet.echoId, packet.echoSeq, *p.conn, packet.src, "", "", (uint32)(PING), packet.data, packet.rproto, -1)
return return
} }
@ -107,6 +101,8 @@ func (p *Server) processPacket(packet *Packet) {
} }
udpConn.activeTime = now udpConn.activeTime = now
udpConn.echoId = packet.echoId
udpConn.echoSeq = packet.echoSeq
_, err := udpConn.conn.Write(packet.data) _, err := udpConn.conn.Write(packet.data)
if err != nil { if err != nil {
@ -144,7 +140,7 @@ func (p *Server) Recv(conn *ServerConn, id string, src *net.IPAddr) {
now := time.Now() now := time.Now()
conn.activeTime = now conn.activeTime = now
sendICMP(p.id, p.r.Intn(math.MaxInt16), *p.conn, src, "", id, (uint32)(DATA), bytes[:n], conn.rproto, -1) sendICMP(conn.echoId, conn.echoSeq, *p.conn, src, "", id, (uint32)(DATA), bytes[:n], conn.rproto, -1)
p.sendPacket++ p.sendPacket++
p.sendPacketSize += (uint64)(n) p.sendPacketSize += (uint64)(n)