Skip to content

Instantly share code, notes, and snippets.

@inotnako
Last active December 15, 2017 05:42
Show Gist options
  • Save inotnako/dff2d2ac302df1f7a0844692218af250 to your computer and use it in GitHub Desktop.
Save inotnako/dff2d2ac302df1f7a0844692218af250 to your computer and use it in GitHub Desktop.
auth_proxy.go
package main
import (
"flag"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
var (
direction = flag.String(`direction`, `https://getacorn.com/`, `set your url`)
addr = flag.String(`addr`, `localhost:8888`, `listen address`)
)
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
}
func main() {
flag.Parse()
target, err := url.Parse(*direction)
if err != nil {
panic(err)
}
targetQuery := target.RawQuery
proxy := &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 targetQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = targetQuery + req.URL.RawQuery
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
if _, ok := req.Header["User-Agent"]; !ok {
// explicitly disable User-Agent so it's not set to default value
req.Header.Set("User-Agent", "")
}
},
ModifyResponse: func(resp *http.Response) error {
// here we can accept all and req / and resp
dumpReq, _ := httputil.DumpRequestOut(resp.Request, true)
var printBodyResponse bool
if strings.Contains(resp.Header.Get(`Content-Type`), `application/json`) {
printBodyResponse = true
}
dumpResp, _ := httputil.DumpResponse(resp, printBodyResponse)
println(`>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>`)
println(string(dumpReq))
println(`<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<`)
println(string(dumpResp))
return nil
},
}
http.ListenAndServe(*addr, proxy)
}
@inotnako
Copy link
Author

screen shot 2017-12-15 at 08 41 55

logs

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
GET / HTTP/1.1
Host: getacorn.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7
Cache-Control: max-age=0
Cookie: Gogland-2d775c86=9b062f3b-252f-4e41-a066-cc001268bef8; _ga=GA1.1.1784250633.1513316215; _gid=GA1.1.1652876936.1513316215
Upgrade-Insecure-Requests: 1
X-B3-Flags: 1
X-B3-Sampled: 1
X-B3-Spanid: 4484cef03d0ddf58
X-B3-Traceid: 4484cef03d0ddf58
X-Forwarded-For: 127.0.0.1
X-Zipkin-Extension: 1


<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
HTTP/2.0 200 OK
Connection: close
Cf-Ray: 3cd70f9dda725af7-HEL
Content-Encoding: br
Content-Type: text/html; charset=utf-8
Date: Fri, 15 Dec 2017 05:39:11 GMT
Server: cloudflare-nginx
Set-Cookie: __cfduid=d71060fd435e4dd68a9247202e9d0369c1513316351; expires=Sat, 15-Dec-18 05:39:11 GMT; path=/; domain=.getacorn.com; HttpOnly
X-Powered-By: Express


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
GET /style/images/icons/favicon-32x32.png HTTP/1.1
Host: getacorn.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36
Accept: image/webp,image/apng,image/*,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7
Cache-Control: no-cache
Cookie: Gogland-2d775c86=9b062f3b-252f-4e41-a066-cc001268bef8; _ga=GA1.1.1784250633.1513316215; _gid=GA1.1.1652876936.1513316215; _gat=1
Pragma: no-cache
Referer: http://localhost:8888/
X-B3-Flags: 1
X-B3-Sampled: 1
X-B3-Spanid: 194c207c6ed8f614
X-B3-Traceid: 194c207c6ed8f614
X-Forwarded-For: 127.0.0.1
X-Zipkin-Extension: 1


<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
HTTP/2.0 200 OK
Content-Length: 1951
Accept-Ranges: bytes
Cache-Control: public, max-age=14400
Cf-Cache-Status: MISS
Cf-Ray: 3cd70fa33c6a5af7-HEL
Content-Type: image/png
Date: Fri, 15 Dec 2017 05:39:12 GMT
Etag: W/"79f-15f581a8d40"
Expires: Fri, 15 Dec 2017 09:39:12 GMT
Last-Modified: Thu, 26 Oct 2017 09:54:16 GMT
Server: cloudflare-nginx
Set-Cookie: __cfduid=d1e15d8646cb8fde32c8ea577732dbc441513316352; expires=Sat, 15-Dec-18 05:39:12 GMT; path=/; domain=.getacorn.com; HttpOnly
Vary: Accept-Encoding
X-Powered-By: Express

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment