代码如下所示:
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
"strconv"
"sync"
)
func main() {
addr := flag.String("s", ":1080", "proxy server address")
flag.Parse()
if err := proxy(*addr); err != nil {
panic(err)
}
}
const netTCP = "tcp"
func proxy(addr string) error {
lc, err := net.Listen(netTCP, addr)
if err != nil {
return err
}
//goland:noinspection GoUnhandledErrorResult
defer lc.Close()
fmt.Printf("proxy server listening on %s\n", addr)
for {
if ac, ea := lc.Accept(); ea == nil {
go handleProxyFunc(ac)
}
}
}
type poolBuf struct {
buf0, buf1 []byte
buf *bufio.Reader
}
var bytePool = &sync.Pool{New: