Files
tun2socks/proxy/direct/tcp.go
2019-07-16 11:37:52 +08:00

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
}