Skip to content

Commit 6538857

Browse files
committed
Clean dir && add traffic stat
1 parent b1ceea8 commit 6538857

25 files changed

+74
-9647
lines changed

client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func NewClient(localaddr string, config *ClientConfig) error {
8282
return
8383
}
8484

85-
Bridge(downconn, upconn, nil)
85+
Bridge(upconn, downconn, nil, config.Stat)
8686
}(conn)
8787
}
8888
}

cmd/goflyway/main.go

+22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"strconv"
88
"strings"
9+
"time"
910

1011
"github.com/coyove/common/sched"
1112
"github.com/coyove/goflyway"
@@ -128,6 +129,27 @@ func main() {
128129
if localAddr != "" && remoteAddr != "" {
129130
cconfig.Bind = remoteAddr
130131
cconfig.Upstream = addr
132+
cconfig.Stat = &goflyway.Traffic{}
133+
134+
go func() {
135+
p := func(v int64) string {
136+
if v > 1024*1024*1024*10 {
137+
return fmt.Sprintf("%.3fG", float64(v)/1024/1024/1024)
138+
}
139+
return fmt.Sprintf("%.3fM", float64(v)/1024/1024)
140+
}
141+
142+
var lastSent, lastRecv int64
143+
144+
for range time.Tick(time.Second * 5) {
145+
s, r := *cconfig.Stat.Sent(), *cconfig.Stat.Recv()
146+
ss := strconv.FormatFloat(float64(s-lastSent)/1024/1024/5, 'f', 3, 64)
147+
rs := strconv.FormatFloat(float64(r-lastRecv)/1024/1024/5, 'f', 3, 64)
148+
lastSent, lastRecv = s, r
149+
150+
v.Vprint("client send: ", p(s), " (", ss, "M/s), recv: ", p(r), " (", rs, "M/s)")
151+
}
152+
}()
131153

132154
fmt.Println("goflyway client binds", remoteAddr, "at", addr, "to", localAddr, with)
133155

pkg/fd/fd.go fd/fd.go

File renamed without changes.

pkg/fd/fd_go19.go fd/fd_go19.go

File renamed without changes.
File renamed without changes.

pkg/fd/fd_unix.go fd/fd_unix.go

File renamed without changes.
File renamed without changes.

io.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@ package goflyway
33
import (
44
"io"
55
"net"
6+
"sync/atomic"
67

78
. "github.com/coyove/goflyway/v"
89
)
910

10-
func Bridge(target, source net.Conn, timeout *TokenBucket) {
11+
func Bridge(target, source net.Conn, timeout *TokenBucket, stat *Traffic) {
1112
go func() {
12-
if _, err := ioCopy(target, source, timeout); err != nil {
13+
if err := ioCopy(target, source, timeout, stat.Sent()); err != nil {
1314
Vprint("Bridge: ", err)
1415
}
1516
target.Close()
1617
source.Close()
1718
}()
1819

19-
if _, err := ioCopy(source, target, timeout); err != nil {
20+
if err := ioCopy(source, target, timeout, stat.Recv()); err != nil {
2021
Vprint("Bridge: ", err)
2122
}
2223

@@ -25,7 +26,7 @@ func Bridge(target, source net.Conn, timeout *TokenBucket) {
2526
source.Close()
2627
}
2728

28-
func ioCopy(dst io.WriteCloser, src io.ReadCloser, bk *TokenBucket) (written int64, err error) {
29+
func ioCopy(dst io.WriteCloser, src io.ReadCloser, bk *TokenBucket, bytes *int64) (err error) {
2930
buf := make([]byte, 32*1024)
3031

3132
for {
@@ -37,8 +38,9 @@ func ioCopy(dst io.WriteCloser, src io.ReadCloser, bk *TokenBucket) (written int
3738
}
3839

3940
nw, ew := dst.Write(buf[0:nr])
40-
if nw > 0 {
41-
written += int64(nw)
41+
42+
if nw > 0 && bytes != nil {
43+
atomic.AddInt64(bytes, int64(nw))
4244
}
4345

4446
if ew != nil {
@@ -52,8 +54,6 @@ func ioCopy(dst io.WriteCloser, src io.ReadCloser, bk *TokenBucket) (written int
5254
err = io.ErrShortWrite
5355
break
5456
}
55-
56-
// retries = 0
5757
}
5858

5959
if er != nil {
@@ -64,5 +64,5 @@ func ioCopy(dst io.WriteCloser, src io.ReadCloser, bk *TokenBucket) (written int
6464
}
6565
}
6666

67-
return written, err
67+
return err
6868
}

pkg/aclrouter/ip_test.go

-97
This file was deleted.

0 commit comments

Comments
 (0)