{"id":4361,"date":"2021-11-18T21:49:29","date_gmt":"2021-11-18T13:49:29","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=4361"},"modified":"2021-11-18T21:53:36","modified_gmt":"2021-11-18T13:53:36","slug":"socks%e8%bd%achttp%e4%bb%a3%e7%90%86","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=4361","title":{"rendered":"socks\u8f6chttp\u4ee3\u7406"},"content":{"rendered":"\n<p><a href=\"https:\/\/github.com\/zenhack\/socks2http\">https:\/\/github.com\/zenhack\/socks2http<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"flag\"\n\t\"golang.org\/x\/net\/proxy\"\n\t\"io\"\n\t\"log\"\n\t\"net\"\n\t\"net\/http\"\n)\n\nvar (\n\tladdr = flag.String(\"laddr\", \":8000\", \"Address to listen on\")\n\traddr = flag.String(\"raddr\", \":1080\", \"Socks proxy address to connect to.\")\n)\n\nvar (\n\tsocks5proxy proxy.Dialer\n\tclient      *http.Client\n)\n\nfunc newClient(dialer proxy.Dialer) *http.Client {\n\treturn &amp;http.Client{\n\t\tTransport: &amp;http.Transport{\n\t\t\tDial: func(net, addr string) (net.Conn, error) {\n\t\t\t\treturn socks5proxy.Dial(net, addr)\n\t\t\t},\n\t\t},\n\t}\n}\n\nfunc main() {\n\tflag.Parse()\n\tvar err error\n\tsocks5proxy, err = proxy.SOCKS5(\"tcp\", *raddr, nil, proxy.Direct)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tclient = newClient(socks5proxy)\n\thndl := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {\n\t\tif req.Method == \"CONNECT\" {\n\t\t\tserverConn, err := socks5proxy.Dial(\"tcp\", req.Host)\n\t\t\tif err != nil {\n\t\t\t\tw.WriteHeader(500)\n\t\t\t\tw.Write([]byte(err.Error() + \"\\n\"))\n\t\t\t\treturn\n\t\t\t}\n\t\t\thijacker, ok := w.(http.Hijacker)\n\t\t\tif !ok {\n\t\t\t\tserverConn.Close()\n\t\t\t\tw.WriteHeader(500)\n\t\t\t\tw.Write([]byte(\"Failed cast to Hijacker\\n\"))\n\t\t\t\treturn\n\t\t\t}\n\t\t\tw.WriteHeader(200)\n\t\t\t_, bio, err := hijacker.Hijack()\n\t\t\tif err != nil {\n\t\t\t\tw.WriteHeader(500)\n\t\t\t\tw.Write([]byte(err.Error() + \"\\n\"))\n\t\t\t\tserverConn.Close()\n\t\t\t\treturn\n\t\t\t}\n\t\t\tgo io.Copy(serverConn, bio)\n\t\t\tgo io.Copy(bio, serverConn)\n\t\t} else {\n\t\t\t\/\/ Server-Only field; we get an error fi we pass this to `client.Do`.\n\t\t\treq.RequestURI = \"\"\n\n\t\t\tresp, err := client.Do(req)\n\t\t\tif err != nil {\n\t\t\t\tw.WriteHeader(500)\n\t\t\t\tw.Write([]byte(err.Error() + \"\\n\"))\n\t\t\t\treturn\n\t\t\t}\n\t\t\thdr := w.Header()\n\t\t\tfor k, v := range resp.Header {\n\t\t\t\thdr[k] = v\n\t\t\t}\n\t\t\tw.WriteHeader(resp.StatusCode)\n\t\t\tio.Copy(w, resp.Body)\n\t\t}\n\t})\n\tlog.Fatal(http.ListenAndServe(*laddr, hndl))\n}\n<\/code><\/pre>\n\n\n\n<p>exe\u4e0b\u8f7d<br>\u94fe\u63a5: https:\/\/pan.baidu.com\/s\/1cGOJ1AJXzdl2GwKLSKTE2g<br>\u63d0\u53d6\u7801: 24ru<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/github.com\/zenhack\/socks2http exe\u4e0b\u8f7d\u94fe\u63a5: https:\/\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/4361"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4361"}],"version-history":[{"count":3,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/4361\/revisions"}],"predecessor-version":[{"id":4364,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/4361\/revisions\/4364"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}