Skip to content

Commit

Permalink
intra/tcp: account for gonet.TCPConn
Browse files Browse the repository at this point in the history
  • Loading branch information
ignoramous committed Mar 21, 2023
1 parent 087bed2 commit 2348ff5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 27 deletions.
20 changes: 0 additions & 20 deletions intra/core/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,6 @@ import (
// in the lwIP thread when they are called, that is, they are holding
// the lwipMutex.
type TCPConn interface {
// Sent will be called when sent data has been acknowledged by peer.
Sent(len uint16) error

// Receive will be called when data arrives from TUN.
Receive(data []byte) error

// Err will be called when a fatal error has occurred on the connection.
// The corresponding pcb is already freed when this callback is called
Err(err error)

// LocalClosed will be called when lwIP receives a FIN segment on a
// connection.
LocalClosed() error

// Poll will be periodically called by TCP timers.
Poll() error

// RemoteAddr returns the destination network address.
RemoteAddr() net.Addr

Expand Down Expand Up @@ -63,9 +46,6 @@ type TCPConn interface {
// read more from TUN.
CloseRead() error

// Abort aborts the connection by sending a RST segment.
Abort()

SetDeadline(t time.Time) error
SetReadDeadline(t time.Time) error
SetWriteDeadline(t time.Time) error
Expand Down
2 changes: 2 additions & 0 deletions intra/ipn/wg.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ func (h *wgtun) Dial(network, address string) (c Conn, err error) {
} else {
h.status = TOK
}

log.I("wg: dial: %s %s; err %v", network, address, err)
return
}

Expand Down
18 changes: 11 additions & 7 deletions intra/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (

"github.com/celzero/firestack/intra/dnsx"
"github.com/celzero/firestack/intra/log"
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"

"github.com/celzero/firestack/intra/core"
"github.com/celzero/firestack/intra/ipn"
Expand Down Expand Up @@ -107,7 +108,7 @@ func NewTCPHandler(resolver dnsx.Resolver, pt ipn.NatPt, ctl protect.Controller,
}

// TODO: Propagate TCP RST using local.Abort(), on appropriate errors.
func (h *tcpHandler) handleUpload(local core.TCPConn, remote split.DuplexConn, upload chan int64) {
func (h *tcpHandler) handleUpload(local core.TCPConn, remote core.TCPConn, upload chan int64) {
ci := conn2str(local, remote)

// io.copy does remote.ReadFrom(local)
Expand All @@ -127,7 +128,7 @@ func conn2str(a net.Conn, b net.Conn) string {
return fmt.Sprintf("a(%v->%v) => b(%v<-%v)", al, ar, bl, br)
}

func (h *tcpHandler) handleDownload(local core.TCPConn, remote split.DuplexConn) (bytes int64, err error) {
func (h *tcpHandler) handleDownload(local core.TCPConn, remote core.TCPConn) (bytes int64, err error) {
ci := conn2str(local, remote)

bytes, err = io.Copy(local, remote)
Expand All @@ -138,14 +139,15 @@ func (h *tcpHandler) handleDownload(local core.TCPConn, remote split.DuplexConn)
return
}

func (h *tcpHandler) forward(local net.Conn, remote split.DuplexConn, summary *TCPSocketSummary) {
localtcp := local.(core.TCPConn)
func (h *tcpHandler) forward(local net.Conn, remote net.Conn, summary *TCPSocketSummary) {
localtcp := local.(core.TCPConn) // conforms to net.TCPConn
remotetcp := remote.(core.TCPConn) // conforms to net.TCPConn
upload := make(chan int64)
start := time.Now()

go h.handleUpload(localtcp, remote, upload)
go h.handleUpload(localtcp, remotetcp, upload)

download, _ := h.handleDownload(localtcp, remote)
download, _ := h.handleDownload(localtcp, remotetcp)

summary.DownloadBytes = download
summary.UploadBytes = <-upload
Expand Down Expand Up @@ -275,7 +277,7 @@ func (h *tcpHandler) Handle(conn net.Conn, target *net.TCPAddr, decision string)
summary.ServerPort = filteredPort(target)
summary.ID = cid // may be an empty string
start := time.Now()
var c split.DuplexConn
var c net.Conn

// Ref: stackoverflow.com/questions/63656117
// Ref: stackoverflow.com/questions/40328025
Expand All @@ -285,6 +287,8 @@ func (h *tcpHandler) Handle(conn net.Conn, target *net.TCPAddr, decision string)
// underlying conn must specifically be a tcp-conn
case *net.TCPConn:
c = uc
case *gonet.TCPConn:
c = uc
default:
err = errTcpSetupConn
}
Expand Down

1 comment on commit 2348ff5

@ignoramous
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.