This repository has been archived by the owner on Apr 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d49ed1
commit 643323f
Showing
9 changed files
with
163 additions
and
388 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,26 @@ | ||
package echo | ||
|
||
import ( | ||
"io" | ||
"net" | ||
|
||
"github.com/eycorsican/go-tun2socks/core" | ||
) | ||
|
||
var bufSize = 10 * 1024 | ||
|
||
type connEntry struct { | ||
data []byte | ||
conn core.TCPConn | ||
} | ||
|
||
// An echo proxy, do nothing but echo back data to the sender, the handler was | ||
// created for testing purposes, it may causes issues when more than one clients | ||
// are connecting the handler simultaneously. | ||
type tcpHandler struct { | ||
buf chan *connEntry | ||
} | ||
type tcpHandler struct{} | ||
|
||
func NewTCPHandler() core.TCPConnHandler { | ||
handler := &tcpHandler{ | ||
buf: make(chan *connEntry, bufSize), | ||
} | ||
go handler.echoBack() | ||
return handler | ||
return &tcpHandler{} | ||
} | ||
|
||
func (h *tcpHandler) echoBack() { | ||
for { | ||
e := <-h.buf | ||
_, err := e.conn.Write(e.data) | ||
if err != nil { | ||
e.conn.Close() | ||
} | ||
} | ||
func (h *tcpHandler) echoBack(conn net.Conn) { | ||
io.Copy(conn, conn) | ||
} | ||
|
||
func (h *tcpHandler) Connect(conn core.TCPConn, target net.Addr) error { | ||
func (h *tcpHandler) Handle(conn net.Conn, target net.Addr) error { | ||
go h.echoBack(conn) | ||
return nil | ||
} | ||
|
||
func (h *tcpHandler) DidReceive(conn core.TCPConn, data []byte) error { | ||
payload := append([]byte(nil), data...) | ||
// This function runs in lwIP thread, we can't block, so discarding data if | ||
// buf if full. | ||
select { | ||
case h.buf <- &connEntry{data: payload, conn: conn}: | ||
default: | ||
} | ||
return nil | ||
} | ||
|
||
func (h *tcpHandler) DidClose(conn core.TCPConn) { | ||
} | ||
|
||
func (h *tcpHandler) LocalDidClose(conn core.TCPConn) { | ||
} |
Oops, something went wrong.