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