You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
2.5 KiB
Go
123 lines
2.5 KiB
Go
4 years ago
|
package proxy
|
||
|
|
||
|
import (
|
||
|
"io"
|
||
|
"log"
|
||
4 years ago
|
"net"
|
||
4 years ago
|
"net/http"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
4 years ago
|
type Options struct {
|
||
|
Addr string
|
||
|
}
|
||
|
|
||
4 years ago
|
type Proxy struct {
|
||
|
Server *http.Server
|
||
|
}
|
||
|
|
||
|
func (proxy *Proxy) Start() error {
|
||
4 years ago
|
log.Printf("Proxy start listen at %v\n", proxy.Server.Addr)
|
||
4 years ago
|
return proxy.Server.ListenAndServe()
|
||
|
}
|
||
|
|
||
|
func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
||
4 years ago
|
if req.Method == "CONNECT" {
|
||
|
proxy.handleConnect(res, req)
|
||
|
return
|
||
|
}
|
||
|
|
||
4 years ago
|
if !req.URL.IsAbs() || req.URL.Host == "" {
|
||
|
res.WriteHeader(400)
|
||
|
_, err := io.WriteString(res, "此为代理服务器,不能直接发起请求")
|
||
|
if err != nil {
|
||
4 years ago
|
log.Printf("error: %v, url: %v\n", err, req.URL.String())
|
||
4 years ago
|
}
|
||
4 years ago
|
return
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
start := time.Now()
|
||
4 years ago
|
|
||
4 years ago
|
proxyReq, err := http.NewRequest(req.Method, req.URL.String(), req.Body)
|
||
|
if err != nil {
|
||
4 years ago
|
log.Printf("error: %v, url: %v\n", err, req.URL.String())
|
||
4 years ago
|
res.WriteHeader(502)
|
||
|
return
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
// TODO: handle Proxy- header
|
||
|
for key, value := range req.Header {
|
||
|
proxyReq.Header[key] = value
|
||
|
}
|
||
4 years ago
|
proxyRes, err := http.DefaultClient.Do(proxyReq)
|
||
|
if err != nil {
|
||
4 years ago
|
log.Printf("error: %v, url: %v\n", err, req.URL.String())
|
||
4 years ago
|
res.WriteHeader(502)
|
||
|
return
|
||
|
}
|
||
|
defer proxyRes.Body.Close()
|
||
4 years ago
|
|
||
4 years ago
|
for key, value := range proxyRes.Header {
|
||
|
res.Header()[key] = value
|
||
|
}
|
||
|
res.WriteHeader(proxyRes.StatusCode)
|
||
4 years ago
|
_, err = io.Copy(res, proxyRes.Body)
|
||
4 years ago
|
if err != nil {
|
||
4 years ago
|
log.Printf("error: %v, url: %v\n", err, req.URL.String())
|
||
4 years ago
|
return
|
||
|
}
|
||
|
|
||
4 years ago
|
log.Printf("%v %v %v - %v ms", req.Method, req.URL.String(), proxyRes.StatusCode, time.Since(start).Milliseconds())
|
||
|
}
|
||
|
|
||
|
func (proxy *Proxy) handleConnect(res http.ResponseWriter, req *http.Request) {
|
||
|
log.Printf("CONNECT: %v\n", req.Host)
|
||
|
|
||
|
conn, err := net.Dial("tcp", req.Host)
|
||
4 years ago
|
if err != nil {
|
||
4 years ago
|
log.Printf("error: %v, host: %v\n", err, req.Host)
|
||
4 years ago
|
res.WriteHeader(502)
|
||
4 years ago
|
return
|
||
|
}
|
||
4 years ago
|
defer conn.Close()
|
||
4 years ago
|
|
||
4 years ago
|
cconn, _, err := res.(http.Hijacker).Hijack()
|
||
|
if err != nil {
|
||
4 years ago
|
log.Printf("error: %v, host: %v\n", err, req.Host)
|
||
4 years ago
|
res.WriteHeader(502)
|
||
|
return
|
||
|
}
|
||
|
defer cconn.Close()
|
||
|
|
||
|
_, err = io.WriteString(cconn, "HTTP/1.1 200 Connection Established\r\n\r\n")
|
||
|
if err != nil {
|
||
4 years ago
|
log.Printf("error: %v, host: %v\n", err, req.Host)
|
||
4 years ago
|
return
|
||
|
}
|
||
|
|
||
|
ch := make(chan bool)
|
||
|
go func() {
|
||
|
_, err := io.Copy(conn, cconn)
|
||
|
if err != nil {
|
||
4 years ago
|
log.Printf("error: %v, host: %v\n", err, req.Host)
|
||
4 years ago
|
}
|
||
|
ch <- true
|
||
|
}()
|
||
|
|
||
|
_, err = io.Copy(cconn, conn)
|
||
|
if err != nil {
|
||
4 years ago
|
log.Printf("error: %v, host: %v\n", err, req.Host)
|
||
4 years ago
|
}
|
||
|
|
||
|
<-ch
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
func NewProxy(opts *Options) *Proxy {
|
||
4 years ago
|
proxy := new(Proxy)
|
||
|
proxy.Server = &http.Server{
|
||
4 years ago
|
Addr: opts.Addr,
|
||
4 years ago
|
Handler: proxy,
|
||
|
}
|
||
|
return proxy
|
||
4 years ago
|
}
|