Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions cmd/devd/devd.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func main() {

hdrs := make(http.Header)
if *cors {
hdrs.Set("Access-Control-Allow-Origin", "*")
hdrs.Set("Access-Control-Allow-Credentials", "true")
}

var servingScheme string
Expand All @@ -196,9 +196,9 @@ func main() {

dd := devd.Devd{
// Shaping
Latency: *latency,
DownKbps: *downKbps,
UpKbps: *upKbps,
Latency: *latency,
DownKbps: *downKbps,
UpKbps: *upKbps,
ServingScheme: servingScheme,

AddHeaders: &hdrs,
Expand All @@ -209,6 +209,8 @@ func main() {
WatchPaths: *watch,
Excludes: *excludes,

Cors: *cors,

Credentials: creds,
}

Expand Down
11 changes: 11 additions & 0 deletions httpctx/httpctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,14 @@ func StripPrefix(prefix string, h Handler) Handler {
}
})
}

func RouteWebsockets(httpHandler, wsHandler Handler) Handler {
return HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
upgrade := r.Header.Get("Upgrade")
if upgrade == "websocket" {
wsHandler.ServeHTTPContext(ctx, w, r)
} else {
httpHandler.ServeHTTPContext(ctx, w, r)
}
})
}
6 changes: 6 additions & 0 deletions responselogger.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package devd

import (
"bufio"
"fmt"
"net"
"net/http"
"strconv"

Expand All @@ -15,6 +17,7 @@ import (
type ResponseLogWriter struct {
Log termlog.Logger
Resp http.ResponseWriter
Hijacker http.Hijacker
Flusher http.Flusher
Timer *timer.Timer
wroteHeader bool
Expand Down Expand Up @@ -87,3 +90,6 @@ func (rl *ResponseLogWriter) Flush() {
rl.Flusher.Flush()
}
}
func (rl *ResponseLogWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return rl.Hijacker.Hijack()
}
17 changes: 16 additions & 1 deletion route.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/cortesi/devd/inject"
"github.com/cortesi/devd/reverseproxy"
"github.com/cortesi/devd/routespec"
"github.com/cortesi/devd/websocketproxy"
"github.com/gorilla/websocket"
)

// Endpoint is the destination of a Route - either on the filesystem or
Expand All @@ -33,7 +35,20 @@ func (ep forwardEndpoint) Handler(prefix string, templates *template.Template, c
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
rp.FlushInterval = 200 * time.Millisecond
return httpctx.StripPrefix(prefix, rp)

wsURL := url.URL(ep)
switch wsURL.Scheme {
case "http":
wsURL.Scheme = "ws"
case "https":
wsURL.Scheme = "wss"
}
ws := websocketproxy.NewProxy(&wsURL)
ws.Dialer = &websocket.Dialer{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}

return httpctx.StripPrefix(prefix, httpctx.RouteWebsockets(rp, ws))
}

func newForwardEndpoint(path string) (*forwardEndpoint, error) {
Expand Down
17 changes: 16 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ type Devd struct {
WatchPaths []string
Excludes []string

// Add Access-Control-Allow-Origin header
Cors bool

// Logging
IgnoreLogs []*regexp.Regexp

Expand Down Expand Up @@ -194,10 +197,22 @@ func (dd *Devd) WrapHandler(log termlog.TermLog, next httpctx.Handler) http.Hand
}
}
}
if dd.Cors {
origin := r.Header.Get("Origin")
if origin == "" {
origin = "*"
}
w.Header().Set("Access-Control-Allow-Origin", origin)
requestHeaders := r.Header.Get("Access-Control-Request-Headers")
if requestHeaders != "" {
w.Header().Set("Access-Control-Allow-Headers", requestHeaders)
}
}
flusher, _ := w.(http.Flusher)
hijacker, _ := w.(http.Hijacker)
next.ServeHTTPContext(
ctx,
&ResponseLogWriter{Log: sublog, Resp: w, Flusher: flusher, Timer: &timr},
&ResponseLogWriter{Log: sublog, Resp: w, Hijacker: hijacker, Flusher: flusher, Timer: &timr},
r,
)
})
Expand Down
2 changes: 2 additions & 0 deletions websocketproxy/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
language: go
go: 1.8
20 changes: 20 additions & 0 deletions websocketproxy/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 Koding, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
54 changes: 54 additions & 0 deletions websocketproxy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# WebsocketProxy [![GoDoc](https://godoc.org/github.com/koding/websocketproxy?status.svg)](https://godoc.org/github.com/koding/websocketproxy) [![Build Status](https://travis-ci.org/koding/websocketproxy.svg)](https://travis-ci.org/koding/websocketproxy)

WebsocketProxy is an http.Handler interface build on top of
[gorilla/websocket](https://github.com/gorilla/websocket) that you can plug
into your existing Go webserver to provide WebSocket reverse proxy.

## Install

```bash
go get github.com/koding/websocketproxy
```

## Example

Below is a simple server that proxies to the given backend URL

```go
package main

import (
"flag"
"net/http"
"net/url"

"github.com/koding/websocketproxy"
)

var (
flagBackend = flag.String("backend", "", "Backend URL for proxying")
)

func main() {
u, err := url.Parse(*flagBackend)
if err != nil {
log.Fatalln(err)
}

err = http.ListenAndServe(":80", websocketproxy.NewProxy(u))
if err != nil {
log.Fatalln(err)
}
}
```

Save it as `proxy.go` and run as:

```bash
go run proxy.go -backend ws://example.com:3000
```

Now all incoming WebSocket requests coming to this server will be proxied to
`ws://example.com:3000`


Loading