我正在构建代理,我也想处理https网址。
问题是当我尝试访问https://google.com
SSL received a record that exceeded the maximum permissible length.
(Error code: ssl_error_rx_record_too_long)
我的代理的代码是:
package main
import (
"crypto/tls"
"flag"
"io/ioutil"
"log"
"net/http"
"strings"
"unicode"
)
var (
listen = flag.String("listen", "localhost:1080", "listen on address")
logp = flag.Bool("log", true, "enable logging")
)
func main() {
flag.Parse()
proxyHandler := http.HandlerFunc(proxyHandlerFunc)
log.Fatal(http.ListenAndServe(*listen, proxyHandler))
//log.Fatal(http.ListenAndServeTLS("localhost:1080", "cert.pem","key.pem",proxyHandler))
}
func copyHeaders(dst, src http.Header) {
for k, _ := range dst {
dst.Del(k)
}
for k, vs := range src {
for _, v := range vs {
dst.Add(k, v)
}
}
}
func proxyHandlerFunc(w http.ResponseWriter, r *http.Request) {
log.Println(r.Method)
if r.Method == "CONNECT" && 1 == 2 {
} else {
// Log if requested
if *logp {
log.Println(r.URL)
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
// We'll want to use a new client for every request.
client := &http.Client{Transport: tr}
// Tweak the request as appropriate:
// RequestURI may not be sent to client
// URL.Scheme must be lower-case
r.RequestURI = ""
r.URL.Scheme = strings.Map(unicode.ToLower, r.URL.Scheme)
if r.URL.Scheme == "" {
r.URL.Scheme = "https"
}
log.Println(r.Host)
// And proxy
resp, err := client.Do(r)
if err != nil {
log.Fatal(err)
}
for n, v := range resp.Header {
log.Printf("%s %s", n, v[0])
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
copyHeaders(w.Header(), resp.Header)
w.Write(bodyBytes)
log.Print("----------")
}
}
上面的代码工作正常网址(http),我应该做什么 获取https网址也是?
修改 我更新了代码。