This commit is contained in:
esrrhs 2019-10-30 19:14:27 +08:00
parent 9b86a95dfc
commit a43e5c435f
3 changed files with 53 additions and 31 deletions

View File

@ -140,12 +140,12 @@ func (p *Client) RTT() time.Duration {
return p.rtt return p.rtt
} }
func (p *Client) Run() { func (p *Client) Run() error {
conn, err := icmp.ListenPacket("ip4:icmp", "") conn, err := icmp.ListenPacket("ip4:icmp", "")
if err != nil { if err != nil {
loggo.Error("Error listening for ICMP packets: %s", err.Error()) loggo.Error("Error listening for ICMP packets: %s", err.Error())
return return err
} }
defer conn.Close() defer conn.Close()
p.conn = conn p.conn = conn
@ -154,7 +154,7 @@ func (p *Client) Run() {
tcplistenConn, err := net.ListenTCP("tcp", p.tcpaddr) tcplistenConn, err := net.ListenTCP("tcp", p.tcpaddr)
if err != nil { if err != nil {
loggo.Error("Error listening for tcp packets: %s", err.Error()) loggo.Error("Error listening for tcp packets: %s", err.Error())
return return err
} }
defer tcplistenConn.Close() defer tcplistenConn.Close()
p.tcplistenConn = tcplistenConn p.tcplistenConn = tcplistenConn
@ -162,7 +162,7 @@ func (p *Client) Run() {
listener, err := net.ListenUDP("udp", p.ipaddr) listener, err := net.ListenUDP("udp", p.ipaddr)
if err != nil { if err != nil {
loggo.Error("Error listening for udp packets: %s", err.Error()) loggo.Error("Error listening for udp packets: %s", err.Error())
return return err
} }
defer listener.Close() defer listener.Close()
p.listenConn = listener p.listenConn = listener
@ -183,6 +183,7 @@ func (p *Client) Run() {
interval := time.NewTicker(time.Second) interval := time.NewTicker(time.Second)
defer interval.Stop() defer interval.Stop()
go func() {
for !p.exit { for !p.exit {
select { select {
case <-interval.C: case <-interval.C:
@ -193,6 +194,9 @@ func (p *Client) Run() {
p.processPacket(r) p.processPacket(r)
} }
} }
}()
return nil
} }
func (p *Client) Stop() { func (p *Client) Stop() {

View File

@ -6,6 +6,7 @@ import (
"github.com/esrrhs/go-engine/src/loggo" "github.com/esrrhs/go-engine/src/loggo"
"github.com/esrrhs/pingtunnel" "github.com/esrrhs/pingtunnel"
"strconv" "strconv"
"time"
) )
var usage = ` var usage = `
@ -137,7 +138,11 @@ func main() {
return return
} }
loggo.Info("Server start") loggo.Info("Server start")
s.Run() err = s.Run()
if err != nil {
loggo.Error("Run ERROR: %s", err.Error())
return
}
} }
if *t == "client" { if *t == "client" {
@ -163,6 +168,13 @@ func main() {
} }
loggo.Info("Client Listen %s (%s) Server %s (%s) TargetPort %s:", c.Addr(), c.IPAddr(), loggo.Info("Client Listen %s (%s) Server %s (%s) TargetPort %s:", c.Addr(), c.IPAddr(),
c.ServerAddr(), c.ServerIPAddr(), c.TargetAddr()) c.ServerAddr(), c.ServerIPAddr(), c.TargetAddr())
c.Run() err = c.Run()
if err != nil {
loggo.Error("Run ERROR: %s", err.Error())
return
}
}
for {
time.Sleep(time.Hour)
} }
} }

View File

@ -11,11 +11,13 @@ import (
func NewServer(key int) (*Server, error) { func NewServer(key int) (*Server, error) {
return &Server{ return &Server{
exit: false,
key: key, key: key,
}, nil }, nil
} }
type Server struct { type Server struct {
exit bool
key int key int
conn *icmp.PacketConn conn *icmp.PacketConn
@ -46,12 +48,12 @@ type ServerConn struct {
tcpmode int tcpmode int
} }
func (p *Server) Run() { func (p *Server) Run() error {
conn, err := icmp.ListenPacket("ip4:icmp", "") conn, err := icmp.ListenPacket("ip4:icmp", "")
if err != nil { if err != nil {
loggo.Error("Error listening for ICMP packets: %s", err.Error()) loggo.Error("Error listening for ICMP packets: %s", err.Error())
return return err
} }
p.conn = conn p.conn = conn
@ -63,7 +65,8 @@ func (p *Server) Run() {
interval := time.NewTicker(time.Second) interval := time.NewTicker(time.Second)
defer interval.Stop() defer interval.Stop()
for { go func() {
for !p.exit {
select { select {
case <-interval.C: case <-interval.C:
p.checkTimeoutConn() p.checkTimeoutConn()
@ -72,6 +75,9 @@ func (p *Server) Run() {
p.processPacket(r) p.processPacket(r)
} }
} }
}()
return nil
} }
func (p *Server) processPacket(packet *Packet) { func (p *Server) processPacket(packet *Packet) {
@ -185,7 +191,7 @@ func (p *Server) RecvTCP(conn *ServerConn, id string, src *net.IPAddr) {
loggo.Info("start wait remote connect tcp %s %s", conn.id, conn.tcpaddrTarget.String()) loggo.Info("start wait remote connect tcp %s %s", conn.id, conn.tcpaddrTarget.String())
startConnectTime := time.Now() startConnectTime := time.Now()
for { for !p.exit {
if conn.fm.IsConnected() { if conn.fm.IsConnected() {
break break
} }
@ -218,7 +224,7 @@ func (p *Server) RecvTCP(conn *ServerConn, id string, src *net.IPAddr) {
tcpActiveRecvTime := time.Now() tcpActiveRecvTime := time.Now()
tcpActiveSendTime := time.Now() tcpActiveSendTime := time.Now()
for { for !p.exit {
now := time.Now() now := time.Now()
sleep := true sleep := true
@ -305,7 +311,7 @@ func (p *Server) RecvTCP(conn *ServerConn, id string, src *net.IPAddr) {
} }
startCloseTime := time.Now() startCloseTime := time.Now()
for { for !p.exit {
now := time.Now() now := time.Now()
conn.fm.Update() conn.fm.Update()
@ -360,7 +366,7 @@ func (p *Server) Recv(conn *ServerConn, id string, src *net.IPAddr) {
loggo.Info("server waiting target response %s -> %s %s", conn.ipaddrTarget.String(), conn.id, conn.conn.LocalAddr().String()) loggo.Info("server waiting target response %s -> %s %s", conn.ipaddrTarget.String(), conn.id, conn.conn.LocalAddr().String())
for { for !p.exit {
bytes := make([]byte, 2000) bytes := make([]byte, 2000)
conn.conn.SetReadDeadline(time.Now().Add(time.Millisecond * 100)) conn.conn.SetReadDeadline(time.Now().Add(time.Millisecond * 100))