master
兔子 9 months ago
parent 2f40158945
commit 572a6059bb

@ -141,6 +141,10 @@ func (n *NetForward) runTCP() error {
} }
log := starlog.Std.NewFlag() log := starlog.Std.NewFlag()
log.Infof("Accept New TCP Conn from %v\n", conn.RemoteAddr().String()) log.Infof("Accept New TCP Conn from %v\n", conn.RemoteAddr().String())
if n.DelayMilSec > 0 && (n.DelayToward == 0 || n.DelayToward == 1) {
log.Infof("Delay %d ms\n", n.DelayMilSec)
time.Sleep(time.Millisecond * time.Duration(n.DelayMilSec))
}
go func(conn net.Conn) { go func(conn net.Conn) {
rmt, err := net.DialTimeout("tcp", n.RemoteURI, n.DialTimeout) rmt, err := net.DialTimeout("tcp", n.RemoteURI, n.DialTimeout)
if err != nil { if err != nil {
@ -283,42 +287,42 @@ func (n *NetForward) copy(dst, src net.Conn) {
defer wg.Done() defer wg.Done()
bufsize := make([]byte, 32*1024) bufsize := make([]byte, 32*1024)
for { for {
if n.DelayMilSec > 0 && (n.DelayToward == 0 || n.DelayToward == 1) { count, err := src.Read(bufsize)
time.Sleep(time.Millisecond * time.Duration(n.DelayMilSec))
}
n, err := src.Read(bufsize)
if err != nil { if err != nil {
dst.Close() dst.Close()
src.Close() src.Close()
return return
} }
_, err = dst.Write(bufsize[:n]) _, err = dst.Write(bufsize[:count])
if err != nil { if err != nil {
src.Close() src.Close()
dst.Close() dst.Close()
return return
} }
if n.DelayMilSec > 0 && (n.DelayToward == 0 || n.DelayToward == 1) {
time.Sleep(time.Millisecond * time.Duration(n.DelayMilSec))
}
} }
}() }()
go func() { go func() {
defer wg.Done() defer wg.Done()
bufsize := make([]byte, 32*1024) bufsize := make([]byte, 32*1024)
for { for {
if n.DelayMilSec > 0 && (n.DelayToward == 0 || n.DelayToward == 2) { count, err := dst.Read(bufsize)
time.Sleep(time.Millisecond * time.Duration(n.DelayMilSec))
}
n, err := dst.Read(bufsize)
if err != nil { if err != nil {
src.Close() src.Close()
dst.Close() dst.Close()
return return
} }
_, err = src.Write(bufsize[:n]) _, err = src.Write(bufsize[:count])
if err != nil { if err != nil {
src.Close() src.Close()
dst.Close() dst.Close()
return return
} }
if n.DelayMilSec > 0 && (n.DelayToward == 0 || n.DelayToward == 2) {
time.Sleep(time.Millisecond * time.Duration(n.DelayMilSec))
}
} }
}() }()
wg.Wait() wg.Wait()

Loading…
Cancel
Save