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
}
func (p *Client) Run() {
func (p *Client) Run() error {
conn, err := icmp.ListenPacket("ip4:icmp", "")
if err != nil {
loggo.Error("Error listening for ICMP packets: %s", err.Error())
return
return err
}
defer conn.Close()
p.conn = conn
@ -154,7 +154,7 @@ func (p *Client) Run() {
tcplistenConn, err := net.ListenTCP("tcp", p.tcpaddr)
if err != nil {
loggo.Error("Error listening for tcp packets: %s", err.Error())
return
return err
}
defer tcplistenConn.Close()
p.tcplistenConn = tcplistenConn
@ -162,7 +162,7 @@ func (p *Client) Run() {
listener, err := net.ListenUDP("udp", p.ipaddr)
if err != nil {
loggo.Error("Error listening for udp packets: %s", err.Error())
return
return err
}
defer listener.Close()
p.listenConn = listener
@ -183,16 +183,20 @@ func (p *Client) Run() {
interval := time.NewTicker(time.Second)
defer interval.Stop()
for !p.exit {
select {
case <-interval.C:
p.checkTimeoutConn()
p.ping()
p.showNet()
case r := <-recv:
p.processPacket(r)
go func() {
for !p.exit {
select {
case <-interval.C:
p.checkTimeoutConn()
p.ping()
p.showNet()
case r := <-recv:
p.processPacket(r)
}
}
}
}()
return nil
}
func (p *Client) Stop() {

View File

@ -6,6 +6,7 @@ import (
"github.com/esrrhs/go-engine/src/loggo"
"github.com/esrrhs/pingtunnel"
"strconv"
"time"
)
var usage = `
@ -137,7 +138,11 @@ func main() {
return
}
loggo.Info("Server start")
s.Run()
err = s.Run()
if err != nil {
loggo.Error("Run ERROR: %s", err.Error())
return
}
}
if *t == "client" {
@ -163,6 +168,13 @@ func main() {
}
loggo.Info("Client Listen %s (%s) Server %s (%s) TargetPort %s:", c.Addr(), c.IPAddr(),
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,12 +11,14 @@ import (
func NewServer(key int) (*Server, error) {
return &Server{
key: key,
exit: false,
key: key,
}, nil
}
type Server struct {
key int
exit bool
key int
conn *icmp.PacketConn
@ -46,12 +48,12 @@ type ServerConn struct {
tcpmode int
}
func (p *Server) Run() {
func (p *Server) Run() error {
conn, err := icmp.ListenPacket("ip4:icmp", "")
if err != nil {
loggo.Error("Error listening for ICMP packets: %s", err.Error())
return
return err
}
p.conn = conn
@ -63,15 +65,19 @@ func (p *Server) Run() {
interval := time.NewTicker(time.Second)
defer interval.Stop()
for {
select {
case <-interval.C:
p.checkTimeoutConn()
p.showNet()
case r := <-recv:
p.processPacket(r)
go func() {
for !p.exit {
select {
case <-interval.C:
p.checkTimeoutConn()
p.showNet()
case r := <-recv:
p.processPacket(r)
}
}
}
}()
return nil
}
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())
startConnectTime := time.Now()
for {
for !p.exit {
if conn.fm.IsConnected() {
break
}
@ -218,7 +224,7 @@ func (p *Server) RecvTCP(conn *ServerConn, id string, src *net.IPAddr) {
tcpActiveRecvTime := time.Now()
tcpActiveSendTime := time.Now()
for {
for !p.exit {
now := time.Now()
sleep := true
@ -305,7 +311,7 @@ func (p *Server) RecvTCP(conn *ServerConn, id string, src *net.IPAddr) {
}
startCloseTime := time.Now()
for {
for !p.exit {
now := time.Now()
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())
for {
for !p.exit {
bytes := make([]byte, 2000)
conn.conn.SetReadDeadline(time.Now().Add(time.Millisecond * 100))