Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement current state of WebSocketAPI #6

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,19 @@ To run it, just do:
To rebuild, use:

go build github.com/matrix-org/matrix-websockets-proxy

To update to the latest state, run:

go get -u github.com/matrix-org/matrix-websockets-proxy

### nginx-integration
To make this work you can integrate this proxy using nginx as follows:
```
location /_matrix/client/unstable/stream {
proxy_pass http://127.0.0.1:8009/stream;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Origin "";
}
```
38 changes: 16 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,11 @@ import (
"net/http"
"path/filepath"
"runtime"
"time"

"github.com/gorilla/websocket"
"github.com/matrix-org/matrix-websockets-proxy/proxy"
)

const (
// timeout for upstream /sync requests (after which it will send back
// an empty response)
syncTimeout = 60 * time.Second
)

var port = flag.Int("port", 8009, "TCP port to listen on")
var upstreamURL = flag.String("upstream", "http://localhost:8008/", "URL of upstream server")
var testHTML *string
Expand Down Expand Up @@ -64,28 +57,23 @@ func serveStream(w http.ResponseWriter, r *http.Request) {
return
}

r.URL.Query().Set("timeout", "0")
syncer := &proxy.Syncer{
UpstreamURL: *upstreamURL + "_matrix/client/v2_alpha/sync",
SyncParams: r.URL.Query(),
}
client := proxy.NewClient(*upstreamURL, r.URL.Query().Get("access_token"))
client.NextSyncBatch = r.URL.Query().Get("since")
client.Filter = r.URL.Query().Get("filter")

msg, err := syncer.MakeRequest()
msg, err := client.Sync(false)
if err != nil {
log.Println("Error in sync", err)
switch err.(type) {
case *proxy.SyncError:
errp := err.(*proxy.SyncError)
log.Println("sync failed:", string(errp.Body))
w.Header().Set("Content-Type", errp.ContentType)
w.WriteHeader(errp.StatusCode)
w.Write(errp.Body)
case *proxy.MatrixError:
handleHTTPError(w, err.(*proxy.MatrixError).HTTPError)
case *proxy.HTTPError:
handleHTTPError(w, *(err.(*proxy.HTTPError)))
default:
log.Println("Error in sync", err)
httpError(w, http.StatusInternalServerError)
}
return
}
syncer.SyncParams.Set("timeout", fmt.Sprintf("%d", syncTimeout/time.Millisecond))

upgrader := websocket.Upgrader{
Subprotocols: []string{"m.json"},
Expand All @@ -96,11 +84,17 @@ func serveStream(w http.ResponseWriter, r *http.Request) {
return
}

c := proxy.New(syncer, ws)
c := proxy.New(client, ws)
c.SendMessage(msg)
c.Start()
}

func httpError(w http.ResponseWriter, status int) {
http.Error(w, http.StatusText(status), status)
}

func handleHTTPError(w http.ResponseWriter, errp proxy.HTTPError) {
w.Header().Set("Content-Type", errp.ContentType)
w.WriteHeader(errp.StatusCode)
w.Write(errp.Body)
}
Loading