| skipped 21 lines |
22 | 22 | | _tcpWaitTimeout = t |
23 | 23 | | } |
24 | 24 | | |
25 | | - | func handleTCPConn(localConn adapter.TCPConn) { |
26 | | - | defer localConn.Close() |
| 25 | + | func handleTCPConn(originConn adapter.TCPConn) { |
| 26 | + | defer originConn.Close() |
27 | 27 | | |
28 | | - | id := localConn.ID() |
| 28 | + | id := originConn.ID() |
29 | 29 | | metadata := &M.Metadata{ |
30 | 30 | | Network: M.TCP, |
31 | 31 | | SrcIP: net.IP(id.RemoteAddress), |
| skipped 2 lines |
34 | 34 | | DstPort: id.LocalPort, |
35 | 35 | | } |
36 | 36 | | |
37 | | - | targetConn, err := proxy.Dial(metadata) |
| 37 | + | remoteConn, err := proxy.Dial(metadata) |
38 | 38 | | if err != nil { |
39 | 39 | | log.Warnf("[TCP] dial %s: %v", metadata.DestinationAddress(), err) |
40 | 40 | | return |
41 | 41 | | } |
42 | | - | metadata.MidIP, metadata.MidPort = parseAddr(targetConn.LocalAddr()) |
| 42 | + | metadata.MidIP, metadata.MidPort = parseAddr(remoteConn.LocalAddr()) |
43 | 43 | | |
44 | | - | targetConn = statistic.DefaultTCPTracker(targetConn, metadata) |
45 | | - | defer targetConn.Close() |
| 44 | + | remoteConn = statistic.DefaultTCPTracker(remoteConn, metadata) |
| 45 | + | defer remoteConn.Close() |
46 | 46 | | |
47 | 47 | | log.Infof("[TCP] %s <-> %s", metadata.SourceAddress(), metadata.DestinationAddress()) |
48 | | - | if err = relay(localConn, targetConn); err != nil { |
| 48 | + | if err = pipe(originConn, remoteConn); err != nil { |
49 | 49 | | log.Debugf("[TCP] %s <-> %s: %v", metadata.SourceAddress(), metadata.DestinationAddress(), err) |
50 | 50 | | } |
51 | 51 | | } |
52 | 52 | | |
53 | | - | // relay copies between left and right bidirectionally. |
54 | | - | func relay(left, right net.Conn) error { |
| 53 | + | // pipe copies copy data to & from provided net.Conn(s) bidirectionally. |
| 54 | + | func pipe(origin, remote net.Conn) error { |
55 | 55 | | wg := sync.WaitGroup{} |
56 | 56 | | wg.Add(2) |
57 | 57 | | |
| skipped 1 lines |
59 | 59 | | |
60 | 60 | | go func() { |
61 | 61 | | defer wg.Done() |
62 | | - | if err := copyBuffer(right, left); err != nil { |
| 62 | + | if err := copyBuffer(remote, origin); err != nil { |
63 | 63 | | leftErr = errors.Join(leftErr, err) |
64 | 64 | | } |
65 | | - | right.SetReadDeadline(time.Now().Add(_tcpWaitTimeout)) |
| 65 | + | remote.SetReadDeadline(time.Now().Add(_tcpWaitTimeout)) |
66 | 66 | | }() |
67 | 67 | | |
68 | 68 | | go func() { |
69 | 69 | | defer wg.Done() |
70 | | - | if err := copyBuffer(left, right); err != nil { |
| 70 | + | if err := copyBuffer(origin, remote); err != nil { |
71 | 71 | | rightErr = errors.Join(rightErr, err) |
72 | 72 | | } |
73 | | - | left.SetReadDeadline(time.Now().Add(_tcpWaitTimeout)) |
| 73 | + | origin.SetReadDeadline(time.Now().Add(_tcpWaitTimeout)) |
74 | 74 | | }() |
75 | 75 | | |
76 | 76 | | wg.Wait() |
| skipped 11 lines |