You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.4 KiB

4 years ago
package tunnel
import (
"errors"
4 years ago
"io"
"net"
"sync"
"syscall"
4 years ago
"time"
"github.com/xjasonlyu/tun2socks/v2/common/pool"
"github.com/xjasonlyu/tun2socks/v2/core/adapter"
"github.com/xjasonlyu/tun2socks/v2/log"
M "github.com/xjasonlyu/tun2socks/v2/metadata"
"github.com/xjasonlyu/tun2socks/v2/proxy"
"github.com/xjasonlyu/tun2socks/v2/tunnel/statistic"
4 years ago
)
const (
4 years ago
tcpWaitTimeout = 5 * time.Second
4 years ago
)
func newTCPTracker(conn net.Conn, metadata *M.Metadata) net.Conn {
return statistic.NewTCPTracker(conn, metadata, statistic.DefaultManager)
}
func handleTCPConn(localConn adapter.TCPConn) {
4 years ago
defer localConn.Close()
id := localConn.ID()
metadata := &M.Metadata{
Network: M.TCP,
SrcIP: net.IP(id.RemoteAddress),
SrcPort: id.RemotePort,
DstIP: net.IP(id.LocalAddress),
DstPort: id.LocalPort,
}
4 years ago
targetConn, err := proxy.Dial(metadata)
if err != nil {
log.Warnf("[TCP] dial %s: %v", metadata.DestinationAddress(), err)
4 years ago
return
}
metadata.MidIP, metadata.MidPort = parseAddr(targetConn.LocalAddr())
4 years ago
4 years ago
targetConn = newTCPTracker(targetConn, metadata)
4 years ago
defer targetConn.Close()
4 years ago
log.Infof("[TCP] %s <-> %s", metadata.SourceAddress(), metadata.DestinationAddress())
if err = relay(localConn, targetConn); err != nil {
log.Warnf("[TCP] %s <-> %s: %v", metadata.SourceAddress(), metadata.DestinationAddress(), err)
}
4 years ago
}
// relay copies between left and right bidirectionally.
func relay(left, right net.Conn) error {
4 years ago
wg := sync.WaitGroup{}
4 years ago
wg.Add(2)
var leftErr, rightErr error
4 years ago
go func() {
defer wg.Done()
if err := copyBuffer(right, left); err != nil {
leftErr = errors.Join(leftErr, err)
}
4 years ago
right.SetReadDeadline(time.Now().Add(tcpWaitTimeout))
}()
go func() {
defer wg.Done()
if err := copyBuffer(left, right); err != nil {
rightErr = errors.Join(rightErr, err)
}
4 years ago
left.SetReadDeadline(time.Now().Add(tcpWaitTimeout))
}()
wg.Wait()
return errors.Join(leftErr, rightErr)
4 years ago
}
func copyBuffer(dst io.Writer, src io.Reader) error {
4 years ago
buf := pool.Get(pool.RelayBufferSize)
4 years ago
defer pool.Put(buf)
_, err := io.CopyBuffer(dst, src, buf)
if err != nil && !isIgnorable(err) {
return err
}
return nil
}
func isIgnorable(err error) bool {
if ne, ok := err.(net.Error); ok && ne.Timeout() {
return true /* ignore I/O timeout */
} else if errors.Is(err, syscall.EPIPE) {
return true /* ignore broken pipe */
} else if errors.Is(err, syscall.ECONNRESET) {
return true /* ignore connection reset by peer */
}
return false
4 years ago
}