```
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"log"
"net"
"net/url"
"strings"
)
func main() {
l, err := net.Listen("tcp", ":8082")
if err != nil {
log.Panic(err)
}
for {
client, err := l.Accept()
if err != nil {
log.Panic(err)
}
go handleClientRequest(client)
}
}
func handleClientRequest(client net.Conn) {
if client == nil {
return
}
defer client.Close()
br := bufio.NewReader(client)
line, err := br.ReadBytes('\n')
line = bytes.TrimRight(line, "\r\n")
if err != nil {
fmt.Println(err)
return
}
lines := strings.Split(string(line), " ")
var addr string
url, _ := url.Parse(lines[1])
if lines[0] != "CONNECT" {
if strings.Index(url.Host, ":") == -1 {
addr = url.Host + ":80"
} else {
addr = url.Host
}
} else {
addr = lines[1]
}
server, err := net.Dial("tcp", addr)
if err != nil {
return
}
if lines[0] == "CONNECT" {
fmt.Fprint(client, "HTTP/1.1 200 Connection established\r\n\r\n")
} else {
//server.Write(b[:n]) //这一步是干啥呢,不知道怎么写
}
//进行转发
go io.Copy(server, client)
io.Copy(client, server)
}
```
参考 https://www.flysnow.org/2016/12/24/golang-http-proxy.html
有疑问加站长微信联系(非本文作者)