Created
May 21, 2022 05:08
-
-
Save habibiefaried/2a4c887f80a19a21d64e3a36419a2eab to your computer and use it in GitHub Desktop.
simpler easy peasy proxy
This file contains hidden or 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
package main | |
import ( | |
"flag" | |
"fmt" | |
"io" | |
"log" | |
"net" | |
) | |
var localAddr *string = flag.String("l", "localhost:9999", "local address") | |
var remoteAddr *string = flag.String("r", "localhost:80", "remote address") | |
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() | |
if err != nil { | |
log.Println("error accepting connection", err) | |
continue | |
} | |
go func() { | |
conn2, err := net.Dial("tcp", *remoteAddr) | |
if err != nil { | |
log.Println("error dialing remote addr", err) | |
return | |
} | |
go io.Copy(conn2, conn) | |
io.Copy(conn, conn2) | |
conn2.Close() | |
conn.Close() | |
}() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment