mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-07 09:41:09 +08:00
47 lines
859 B
Go
47 lines
859 B
Go
package direct
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/xjasonlyu/tun2socks/common/log"
|
|
"github.com/xjasonlyu/tun2socks/core"
|
|
)
|
|
|
|
type tcpHandler struct{}
|
|
|
|
func NewTCPHandler() core.TCPConnHandler {
|
|
return &tcpHandler{}
|
|
}
|
|
|
|
func (h *tcpHandler) handleInput(conn net.Conn, input io.ReadCloser) {
|
|
defer func() {
|
|
conn.Close()
|
|
input.Close()
|
|
}()
|
|
io.Copy(conn, input)
|
|
}
|
|
|
|
func (h *tcpHandler) handleOutput(conn net.Conn, output io.WriteCloser) {
|
|
defer func() {
|
|
conn.Close()
|
|
output.Close()
|
|
}()
|
|
io.Copy(output, conn)
|
|
}
|
|
|
|
func (h *tcpHandler) Handle(conn net.Conn, target *net.TCPAddr) error {
|
|
c, err := net.DialTCP("tcp", nil, target)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
go h.handleInput(conn, c)
|
|
go h.handleOutput(conn, c)
|
|
log.Infof("new proxy connection for target: %s:%s", target.Network(), target.String())
|
|
return nil
|
|
}
|