Skip to content

Instantly share code, notes, and snippets.

@techninja1008
Forked from yowu/HttpProxy.go
Last active March 30, 2018 18:32
Show Gist options
  • Select an option

  • Save techninja1008/a0fbd30f7dec954f469c37a8e53db148 to your computer and use it in GitHub Desktop.

Select an option

Save techninja1008/a0fbd30f7dec954f469c37a8e53db148 to your computer and use it in GitHub Desktop.

Revisions

  1. techninja1008 revised this gist Mar 30, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion HttpProxy.go
    Original file line number Diff line number Diff line change
    @@ -68,7 +68,7 @@ func (p *proxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
    //http://golang.org/src/pkg/net/http/client.go
    req.RequestURI = ""

    u, err := url.Parse("http://127.0.0.1:8001/api/v1/namespaces/" + p.ns + "/services/" + p.svc + "/proxy" + req.URL)
    u, err := url.Parse("http://127.0.0.1:8001/api/v1/namespaces/" + p.ns + "/services/" + p.svc + "/proxy" + req.URL.RequestURI())
    if err != nil {
    http.Error(wr, "Server Error", http.StatusInternalServerError)
    log.Fatal("ServeHTTP:", err)
  2. techninja1008 revised this gist Mar 30, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion HttpProxy.go
    Original file line number Diff line number Diff line change
    @@ -68,7 +68,7 @@ func (p *proxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
    //http://golang.org/src/pkg/net/http/client.go
    req.RequestURI = ""

    u, err := url.Parse("http://127.0.0.1:8001/api/v1/namespaces/" + p.ns + "/services/" + p.svc + "/proxy" + req.URL.Path)
    u, err := url.Parse("http://127.0.0.1:8001/api/v1/namespaces/" + p.ns + "/services/" + p.svc + "/proxy" + req.URL)
    if err != nil {
    http.Error(wr, "Server Error", http.StatusInternalServerError)
    log.Fatal("ServeHTTP:", err)
  3. techninja1008 revised this gist Mar 30, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion HttpProxy.go
    Original file line number Diff line number Diff line change
    @@ -99,7 +99,7 @@ func (p *proxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
    }

    func main() {
    var addr = flag.String("addr", "127.0.0.1:8001", "The addr of the application.")
    var addr = flag.String("addr", "127.0.0.1:8002", "The addr of the application.")
    var namespace = flag.String("ns", "longhorn-system", "Kube namespace")
    var service = flag.String("svc", "longhorn-frontend:longhorn-ui", "Kube service")
    flag.Parse()
  4. techninja1008 revised this gist Mar 30, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions HttpProxy.go
    Original file line number Diff line number Diff line change
    @@ -53,14 +53,14 @@ type proxy struct {
    }

    func (p *proxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
    log.Println(req.URL.Scheme, " ", req.RemoteAddr, " ", req.Method, " ", req.URL)
    log.Println(req.RemoteAddr, " ", req.Method, " ", req.URL)

    if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
    /*if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
    msg := "unsupported protocal scheme "+req.URL.Scheme
    http.Error(wr, msg, http.StatusBadRequest)
    log.Println(msg)
    return
    }
    }*/

    client := &http.Client{}

  5. techninja1008 revised this gist Mar 30, 2018. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions HttpProxy.go
    Original file line number Diff line number Diff line change
    @@ -68,8 +68,14 @@ func (p *proxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
    //http://golang.org/src/pkg/net/http/client.go
    req.RequestURI = ""

    req.URL = url.Parse("http://127.0.0.1:8001/api/v1/namespaces/" + p.ns + "/services/" + p.svc + "/proxy" + req.URL.Path)

    u, err := url.Parse("http://127.0.0.1:8001/api/v1/namespaces/" + p.ns + "/services/" + p.svc + "/proxy" + req.URL.Path)
    if err != nil {
    http.Error(wr, "Server Error", http.StatusInternalServerError)
    log.Fatal("ServeHTTP:", err)
    }

    req.URL = u

    delHopHeaders(req.Header)

    if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
  6. techninja1008 revised this gist Mar 30, 2018. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions HttpProxy.go
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,7 @@ import (
    "net"
    "net/http"
    "strings"
    "net/url"
    )

    // Hop-by-hop headers. These are removed when sent to the backend.
    @@ -52,7 +53,7 @@ type proxy struct {
    }

    func (p *proxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
    log.Println(req.RemoteAddr, " ", req.Method, " ", req.URL)
    log.Println(req.URL.Scheme, " ", req.RemoteAddr, " ", req.Method, " ", req.URL)

    if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
    msg := "unsupported protocal scheme "+req.URL.Scheme
    @@ -67,7 +68,7 @@ func (p *proxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
    //http://golang.org/src/pkg/net/http/client.go
    req.RequestURI = ""

    req.URL.Path = "/api/v1/namespaces/" + p.ns + "/services/" + p.svc + "/proxy" + req.URL.Path
    req.URL = url.Parse("http://127.0.0.1:8001/api/v1/namespaces/" + p.ns + "/services/" + p.svc + "/proxy" + req.URL.Path)

    delHopHeaders(req.Header)

  7. techninja1008 revised this gist Mar 30, 2018. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions HttpProxy.go
    Original file line number Diff line number Diff line change
    @@ -47,6 +47,8 @@ func appendHostToXForwardHeader(header http.Header, host string) {
    }

    type proxy struct {
    ns string
    svc string
    }

    func (p *proxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
    @@ -64,6 +66,8 @@ func (p *proxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
    //http: Request.RequestURI can't be set in client requests.
    //http://golang.org/src/pkg/net/http/client.go
    req.RequestURI = ""

    req.URL.Path = "/api/v1/namespaces/" + p.ns + "/services/" + p.svc + "/proxy" + req.URL.Path

    delHopHeaders(req.Header)

    @@ -88,10 +92,12 @@ func (p *proxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
    }

    func main() {
    var addr = flag.String("addr", "127.0.0.1:8080", "The addr of the application.")
    var addr = flag.String("addr", "127.0.0.1:8001", "The addr of the application.")
    var namespace = flag.String("ns", "longhorn-system", "Kube namespace")
    var service = flag.String("svc", "longhorn-frontend:longhorn-ui", "Kube service")
    flag.Parse()

    handler := &proxy{}
    handler := &proxy{ns: *namespace, svc: *service}

    log.Println("Starting proxy server on", *addr)
    if err := http.ListenAndServe(*addr, handler); err != nil {
  8. dbgwarn revised this gist Aug 27, 2015. No changes.
  9. dbgwarn revised this gist Jul 28, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion HttpProxy.go
    Original file line number Diff line number Diff line change
    @@ -53,7 +53,7 @@ func (p *proxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
    log.Println(req.RemoteAddr, " ", req.Method, " ", req.URL)

    if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
    msg := "unsupported protocal scheme "+req.URL.Scheme
    msg := "unsupported protocal scheme "+req.URL.Scheme
    http.Error(wr, msg, http.StatusBadRequest)
    log.Println(msg)
    return
  10. dbgwarn revised this gist Jul 28, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion HttpProxy.go
    Original file line number Diff line number Diff line change
    @@ -88,7 +88,7 @@ func (p *proxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
    }

    func main() {
    var addr = flag.String("addr", ":8080", "The addr of the application.")
    var addr = flag.String("addr", "127.0.0.1:8080", "The addr of the application.")
    flag.Parse()

    handler := &proxy{}
  11. dbgwarn revised this gist Jul 28, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion HttpProxy.go
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,6 @@ import (
    "log"
    "net"
    "net/http"
    "net/url"
    "strings"
    )

  12. dbgwarn created this gist Jul 28, 2015.
    101 changes: 101 additions & 0 deletions HttpProxy.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,101 @@
    package main

    import (
    "flag"
    "io"
    "log"
    "net"
    "net/http"
    "net/url"
    "strings"
    )

    // Hop-by-hop headers. These are removed when sent to the backend.
    // http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
    var hopHeaders = []string{
    "Connection",
    "Keep-Alive",
    "Proxy-Authenticate",
    "Proxy-Authorization",
    "Te", // canonicalized version of "TE"
    "Trailers",
    "Transfer-Encoding",
    "Upgrade",
    }

    func copyHeader(dst, src http.Header) {
    for k, vv := range src {
    for _, v := range vv {
    dst.Add(k, v)
    }
    }
    }

    func delHopHeaders(header http.Header) {
    for _, h := range hopHeaders {
    header.Del(h)
    }
    }

    func appendHostToXForwardHeader(header http.Header, host string) {
    // If we aren't the first proxy retain prior
    // X-Forwarded-For information as a comma+space
    // separated list and fold multiple headers into one.
    if prior, ok := header["X-Forwarded-For"]; ok {
    host = strings.Join(prior, ", ") + ", " + host
    }
    header.Set("X-Forwarded-For", host)
    }

    type proxy struct {
    }

    func (p *proxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
    log.Println(req.RemoteAddr, " ", req.Method, " ", req.URL)

    if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
    msg := "unsupported protocal scheme "+req.URL.Scheme
    http.Error(wr, msg, http.StatusBadRequest)
    log.Println(msg)
    return
    }

    client := &http.Client{}

    //http: Request.RequestURI can't be set in client requests.
    //http://golang.org/src/pkg/net/http/client.go
    req.RequestURI = ""

    delHopHeaders(req.Header)

    if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
    appendHostToXForwardHeader(req.Header, clientIP)
    }

    resp, err := client.Do(req)
    if err != nil {
    http.Error(wr, "Server Error", http.StatusInternalServerError)
    log.Fatal("ServeHTTP:", err)
    }
    defer resp.Body.Close()

    log.Println(req.RemoteAddr, " ", resp.Status)

    delHopHeaders(resp.Header)

    copyHeader(wr.Header(), resp.Header)
    wr.WriteHeader(resp.StatusCode)
    io.Copy(wr, resp.Body)
    }

    func main() {
    var addr = flag.String("addr", ":8080", "The addr of the application.")
    flag.Parse()

    handler := &proxy{}

    log.Println("Starting proxy server on", *addr)
    if err := http.ListenAndServe(*addr, handler); err != nil {
    log.Fatal("ListenAndServe:", err)
    }
    }