加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

用Go写了一个类似Proxy的小程序,可以用来访问goolge个人使用还是

发布时间:2020-12-16 18:40:57 所属栏目:大数据 来源:网络整理
导读:package mainimport ("fmt""io""net/http")func main() {http.HandleFunc("/",route)e := http.ListenAndServe(":80",nil)if e != nil {fmt.Println(e)}}func route(w http.ResponseWriter,r *http.Request) {req,_ := http.NewRequest(r.Method,"",r.Body)r
package main

import (
	"fmt"
	"io"
	"net/http"
)

func main() {
	http.HandleFunc("/",route)
	e := http.ListenAndServe(":80",nil)
	if e != nil {
		fmt.Println(e)
	}
}

func route(w http.ResponseWriter,r *http.Request) {
	req,_ := http.NewRequest(r.Method,"",r.Body)
	req.URL = r.URL
	req.URL.Host = r.Host //"www.qq.com"
	req.URL.Scheme = "http"
	for _,v := range r.Cookies() {
		req.AddCookie(v)
	}
	//req.Header = r.Header 这里的Header就不要使用了,使用的话他会自动跳转到https,代理就出问题了.
	resp,err := http.DefaultClient.Do(req)
	if err != nil {
		fmt.Println("Here:",err)
		return
	}
	for k,v := range resp.Header {
		for _,value := range v {
			w.Header().Add(k,value)
		}
	}
	for _,cookie := range resp.Cookies() {
		w.Header().Add("Set-Cookie",cookie.Raw)
	}
	w.WriteHeader(resp.StatusCode)
	io.Copy(w,resp.Body)
	resp.Body.Close()
	r.Body.Close()
}
这个用来访问海外网站的话,要在本地host,把 域名绑定到IP,例如:1.1.1.1www.google.com

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读