This repository has been archived by the owner on Jan 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
1) Add websocket support, and 2) make sure to redirect to where we came from #64
Open
jenshz
wants to merge
6
commits into
bitly:master
Choose a base branch
from
treatwell:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
463bc90
Add ability to proxy websocket connections as well as normal HTTP req…
airhorns c674656
Merge remote-tracking branch 'gap2/proxy_websockets' into proxy-web-s…
jenshz 68db406
Make redirection work properly
jenshz e8d8b6b
Include the redirect everywhere
jenshz ea47935
Forward basic auth, if other means were used for authenticating
jenshz 6f6eb28
Update the Google API keys to new Treatwell project keys
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
## Google Auth Proxy Config File | ||
## https://github.com/bitly/google_auth_proxy | ||
|
||
## <addr>:<port> to listen on for HTTP clients | ||
# http_address = "127.0.0.1:4180" | ||
|
||
## the OAuth Redirect URL. | ||
redirect_url = "https://auth.int.treatwell.com/oauth2/callback" | ||
|
||
## the http url(s) of the upstream endpoint. If multiple, routing is based on path | ||
upstreams = [ | ||
"http://127.0.0.1:8080/" | ||
] | ||
|
||
## pass HTTP Basic Auth, X-Forwarded-User and X-Forwarded-Email information to upstream | ||
# pass_basic_auth = true | ||
|
||
## Google Apps Domains to allow authentication for | ||
google_apps_domains = [ | ||
"treatwell.nl" | ||
] | ||
|
||
|
||
## The Google OAuth Client ID, Secret | ||
client_id = "598239317768-0i5vee2o45qpjqj4ivimdutnff79lqou.apps.googleusercontent.com" | ||
client_secret = "sSzmqIahuWFKt9ghdYcE0zjr" | ||
|
||
## Authenticated Email Addresses File (one email per line) | ||
# authenticated_emails_file = "" | ||
|
||
## Htpasswd File (optional) | ||
## Additionally authenticate against a htpasswd file. Entries must be created with "htpasswd -s" for SHA encryption | ||
## enabling exposes a username/login signin form | ||
#htpasswd_file = "/opt/htpasswd" | ||
|
||
|
||
## Cookie Settings | ||
## Secret - the seed string for secure cookies | ||
## Domain - optional cookie domain to force cookies to (ie: .yourcompany.com) | ||
## Expire - expire timeframe for cookie | ||
# cookie_secret = "" | ||
cookie_domain = "int.treatwell.com" | ||
cookie_expire = "168h" | ||
# cookie_https_only = true | ||
# cookie_httponly = true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"log" | ||
"net" | ||
"net/http" | ||
"net/http/httputil" | ||
"net/url" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
type WebsocketReverseProxy struct { | ||
Proxy *httputil.ReverseProxy | ||
Upstream string | ||
} | ||
|
||
func NewWebsocketReverseProxy(target *url.URL) *WebsocketReverseProxy { | ||
proxy := httputil.NewSingleHostReverseProxy(target) | ||
return &WebsocketReverseProxy{Proxy: proxy, Upstream: target.Host} | ||
} | ||
|
||
func (p *WebsocketReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) { | ||
if websocketUpgradeRequest(req) { | ||
p.hijackWebsocket(rw, req) | ||
} else { | ||
p.Proxy.ServeHTTP(rw, req) | ||
} | ||
} | ||
|
||
func (p *WebsocketReverseProxy) hijackWebsocket(rw http.ResponseWriter, req *http.Request) { | ||
highjacker, ok := rw.(http.Hijacker) | ||
|
||
if !ok { | ||
http.Error(rw, "webserver doesn't support hijacking", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
conn, bufrw, err := highjacker.Hijack() | ||
defer conn.Close() | ||
|
||
conn2, err := net.Dial("tcp", p.Upstream) | ||
if err != nil { | ||
log.Printf("couldn't connect to backend websocket server: %v", err) | ||
http.Error(rw, "couldn't connect to backend server", http.StatusServiceUnavailable) | ||
return | ||
} | ||
defer conn2.Close() | ||
|
||
err = req.Write(conn2) | ||
if err != nil { | ||
log.Printf("writing WebSocket request to backend server failed: %v", err) | ||
return | ||
} | ||
|
||
bufferedBidirCopy(conn, bufrw, conn2, bufio.NewReadWriter(bufio.NewReader(conn2), bufio.NewWriter(conn2))) | ||
} | ||
|
||
func websocketUpgradeRequest(req *http.Request) bool { | ||
connection_headers, ok := req.Header["Connection"] | ||
if !ok || len(connection_headers) <= 0 { | ||
return false | ||
} | ||
|
||
connection_header := connection_headers[0] | ||
if strings.ToLower(connection_header) != "upgrade" { | ||
return false | ||
} | ||
|
||
upgrade_headers, ok := req.Header["Upgrade"] | ||
if !ok || len(upgrade_headers) <= 0 { | ||
return false | ||
} | ||
|
||
return strings.ToLower(upgrade_headers[0]) == "websocket" | ||
} | ||
|
||
func bufferedCopy(dest *bufio.ReadWriter, src *bufio.ReadWriter) { | ||
buf := make([]byte, 40*1024) | ||
for { | ||
n, err := src.Read(buf) | ||
if err != nil && err != io.EOF { | ||
log.Printf("Upstream read failed: %v", err) | ||
return | ||
} | ||
if n == 0 { | ||
return | ||
} | ||
n, err = dest.Write(buf[0:n]) | ||
if err != nil && err != io.EOF { | ||
log.Printf("Downstream write failed: %v", err) | ||
return | ||
} | ||
|
||
err = dest.Flush() | ||
if err != nil { | ||
log.Printf("Downstream write flush failed: %v", err) | ||
return | ||
} | ||
} | ||
} | ||
|
||
func bufferedBidirCopy(conn1 io.ReadWriteCloser, rw1 *bufio.ReadWriter, conn2 io.ReadWriteCloser, rw2 *bufio.ReadWriter) { | ||
wg := sync.WaitGroup{} | ||
|
||
copier := func(wg *sync.WaitGroup, rw1 *bufio.ReadWriter, rw2 *bufio.ReadWriter) { | ||
defer wg.Done() | ||
bufferedCopy(rw2, rw1) | ||
} | ||
|
||
wg.Add(2) | ||
go copier(&wg, rw1, rw2) | ||
go copier(&wg, rw2, rw1) | ||
wg.Wait() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yum, Yum secret keys used by bitcoin crawlers..