Skip to content

Instantly share code, notes, and snippets.

@daemonchen
Forked from dojiong/hello.go
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daemonchen/e3fc755d264546d4cf01 to your computer and use it in GitHub Desktop.
Save daemonchen/e3fc755d264546d4cf01 to your computer and use it in GitHub Desktop.

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))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment