Comments (4)
Hey @faelp22
I've done it like this:
Create a writer that wraps the io.Writer
for the connection
type closureWriter struct {
fn func([]byte)
writer io.Writer
}
func newClosureWriter(fn func(p []byte), w io.Writer) io.Writer {
return closureWriter{fn: fn, writer: w}
}
var _ io.Writer = new(closureWriter)
func (cw closureWriter) Write(p []byte) (len int, err error) {
cw.fn(p)
return cw.writer.Write(p)
}
Then you can set up the proxy like this:
func main() {
flag.Parse()
fmt.Printf("Listening: %v\nProxying: %v\n\n", *localAddr, *remoteAddr)
listener, err := net.Listen("tcp", *localAddr)
if err != nil {
panic(err)
}
for {
conn, err := listener.Accept()
log.Println("New connection", conn.RemoteAddr())
if err != nil {
log.Println("error accepting connection", err)
continue
}
go func() {
defer conn.Close()
conn2, err := net.Dial("tcp", *remoteAddr)
if err != nil {
log.Println("error dialing remote addr", err)
return
}
defer conn2.Close()
closer := make(chan struct{}, 2)
go copy(closer, newClosureWriter(func(data []byte) {
fmt.Printf("Data from %s to %s %q\n", conn.RemoteAddr(), *remoteAddr, data)
}, conn2), conn)
go copy(closer, newClosureWriter(func(data []byte) {
fmt.Printf("Data from %s to %s %q\n", *remoteAddr, conn.RemoteAddr(), data)
}, conn), conn2)
<-closer
log.Println("Connection complete", conn.RemoteAddr())
}()
}
}
Full working gist: https://gist.github.com/maxmcd/bb34f1dd466fc9e599cafc7252ee983a
from tcp-proxy.
I did it that way but it didn't work
mensagem, _ := bufio.NewReader(conn).ReadString('\n')
fmt.Print("Mensagem recebida:", string(mensagem))
log.Println("New connection", conn.RemoteAddr())
if err != nil {
log.Println("error accepting connection", err)
continue
}
go func() {
defer conn.Close()
conn2, err := net.Dial("tcp", *remoteAddr)
mensagem2, _ := bufio.NewReader(conn2).ReadString('\n')
fmt.Print("Mensagem recebida2:", string(mensagem2))
from tcp-proxy.
going to close this issue, feel free to respond if you have other questions
from tcp-proxy.
Thanks a lot for the help!
from tcp-proxy.
Related Issues (1)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tcp-proxy.