fuckgfw.appspot.com是你的app地址,需要在你的hosts中将其指定到一个你能访问到的google服务器IP
本地代理端口是8888
//GAE app的主体 | |
package hello | |
import ( | |
"appengine" | |
"appengine/urlfetch" | |
"bytes" | |
"encoding/gob" | |
"io" | |
"net/http" | |
) | |
func init() { | |
http.HandleFunc("/", handler) | |
} | |
type Req struct { | |
Url string | |
Method string | |
Header http.Header | |
Body []byte | |
} | |
type Rep struct { | |
StatusCode int | |
Header http.Header | |
Body []byte | |
} | |
func handler(w http.ResponseWriter, r *http.Request) { | |
c := appengine.NewContext(r) | |
client := urlfetch.Client(c) | |
var req Req | |
dec := gob.NewDecoder(r.Body) | |
if err := dec.Decode(&req); err != nil { | |
c.Infof("decode error: %s", err.Error()) | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
preq, _ := http.NewRequest(req.Method, req.Url, bytes.NewBuffer(req.Body)) | |
preq.Header = req.Header | |
if rep, err := client.Do(preq); err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
} else { | |
var ret Rep | |
ret.StatusCode = rep.StatusCode | |
ret.Header = rep.Header | |
ret.Body = make([]byte, rep.ContentLength) | |
io.ReadFull(rep.Body, ret.Body) | |
buf := new(bytes.Buffer) | |
enc := gob.NewEncoder(buf) | |
enc.Encode(ret) | |
w.Write(buf.Bytes()) | |
} | |
} |
//本地代理 | |
package main | |
import ( | |
"bytes" | |
"encoding/gob" | |
"io" | |
"log" | |
"net/http" | |
) | |
type Req struct { | |
Url string | |
Method string | |
Header http.Header | |
Body []byte | |
} | |
type Rep struct { | |
StatusCode int | |
Header http.Header | |
Body []byte | |
} | |
func handle(w http.ResponseWriter, r *http.Request) { | |
var req Req | |
req.Url = r.RequestURI | |
req.Method = r.Method | |
req.Header = r.Header | |
req.Body = make([]byte, r.ContentLength) | |
io.ReadFull(r.Body, req.Body) | |
buf := new(bytes.Buffer) | |
enc := gob.NewEncoder(buf) | |
if err := enc.Encode(req); err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
rep, err := http.Post("https://fuckgfw.appspot.com/", "application/bin", buf) | |
if err != nil { | |
log.Println("post err: ", err.Error()) | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
} else { | |
var ret Rep | |
dec := gob.NewDecoder(rep.Body) | |
if err := dec.Decode(&ret); err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
h := w.Header() | |
for k, v := range ret.Header { | |
h[k] = v | |
} | |
w.WriteHeader(ret.StatusCode) | |
w.Write(ret.Body) | |
} | |
} | |
func main() { | |
http.HandleFunc("/", handle) | |
log.Fatal(http.ListenAndServe(":8888", nil)) | |
} |