Created
November 4, 2023 06:58
-
-
Save biningo/7c1bb1e03e876b93fae77e49df2d013f to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"flag" | |
"fmt" | |
"io" | |
"net" | |
"net/http" | |
"strings" | |
) | |
var ( | |
lAddr = flag.String("l", ":7001", "listen addr") | |
) | |
func handle(client net.Conn) { | |
defer client.Close() | |
fmt.Printf("remote addr: %v\n", client.RemoteAddr()) | |
buf := make([]byte, 1024) | |
n, err := client.Read(buf) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
s := string(buf) | |
lines := strings.Split(s, "\r\n") | |
firtstLineParts := strings.Split(lines[0], " ") | |
method, path := firtstLineParts[0], firtstLineParts[1] | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
address := "" | |
if method == http.MethodConnect { | |
client.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n")) | |
address = path | |
} else { | |
address = strings.Split(strings.TrimPrefix(path, "http://"), "/")[0] | |
} | |
if strings.Index(address, ":") == -1 { | |
address += ":80" | |
} | |
server, err := net.Dial("tcp", address) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
if method != http.MethodConnect { | |
server.Write(buf[:n]) | |
} | |
go io.Copy(client, server) | |
io.Copy(server, client) | |
} | |
func main() { | |
flag.Parse() | |
listen, _ := net.Listen("tcp", *lAddr) | |
for { | |
conn, _ := listen.Accept() | |
go handle(conn) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment