Skip to content

Instantly share code, notes, and snippets.

@messyidea
Last active June 7, 2016 04:48
Show Gist options
  • Save messyidea/4c01dafb0facfa2c4d81522f3cb524db to your computer and use it in GitHub Desktop.
Save messyidea/4c01dafb0facfa2c4d81522f3cb524db to your computer and use it in GitHub Desktop.

Revisions

  1. messyidea revised this gist Jun 7, 2016. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions grproxy.go
    Original file line number Diff line number Diff line change
    @@ -9,9 +9,10 @@ import (
    "strings"
    )


    func main() {

    target, _ := url.Parse("http://www.google.com")
    target, _ := url.Parse("https://www.google.com")
    r := &httputil.ReverseProxy{
    Director: func(req *http.Request) {
    req.URL.Scheme = target.Scheme
    @@ -27,7 +28,7 @@ func main() {
    }

    http.Handle("/", r)
    http.ListenAndServe(":80", nil)
    http.ListenAndServeTLS(":443", "public.crt", "private.pem", nil)
    }

    func singleJoiningSlash(a, b string) string {
    @@ -41,3 +42,6 @@ func singleJoiningSlash(a, b string) string {
    }
    return a + b
    }

    // openssl genrsa -out private.pem 2048
    // openssl req -new -x509 -sha256 -key private.pem -out public.crt -days 99999
  2. @horsley horsley created this gist Oct 4, 2014.
    43 changes: 43 additions & 0 deletions grproxy.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    // grproxy project main.go
    package main

    import (
    "net/http"
    "net/http/httputil"
    "net/url"

    "strings"
    )

    func main() {

    target, _ := url.Parse("http://www.google.com")
    r := &httputil.ReverseProxy{
    Director: func(req *http.Request) {
    req.URL.Scheme = target.Scheme
    req.URL.Host = target.Host
    req.Host = target.Host //这才是关键
    req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
    if target.RawQuery == "" || req.URL.RawQuery == "" {
    req.URL.RawQuery = target.RawQuery + req.URL.RawQuery
    } else {
    req.URL.RawQuery = target.RawQuery + "&" + req.URL.RawQuery
    }
    },
    }

    http.Handle("/", r)
    http.ListenAndServe(":80", nil)
    }

    func singleJoiningSlash(a, b string) string {
    aslash := strings.HasSuffix(a, "/")
    bslash := strings.HasPrefix(b, "/")
    switch {
    case aslash && bslash:
    return a + b[1:]
    case !aslash && !bslash:
    return a + "/" + b
    }
    return a + b
    }