Skip to content

CBG-1901: Update ISGR/Blip to nhooyr.io/websocket #5421

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

Merged
merged 4 commits into from
Feb 15, 2022
Merged
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
21 changes: 7 additions & 14 deletions db/active_replicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ package db

import (
"context"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
Expand All @@ -21,7 +20,6 @@ import (

"github.com/couchbase/go-blip"
"github.com/couchbase/sync_gateway/base"
"golang.org/x/net/websocket"
)

// ActiveReplicator is a wrapper to encapsulate separate push and pull active replicators.
Expand Down Expand Up @@ -263,23 +261,18 @@ func blipSync(target url.URL, blipContext *blip.Context, insecureSkipVerify bool
target.User = nil
}

config, err := websocket.NewConfig(target.String()+"/_blipsync?"+BLIPSyncClientTypeQueryParam+"="+string(BLIPClientTypeSGR2), "http://localhost")
if err != nil {
return nil, err
}

if insecureSkipVerify {
if config.TlsConfig == nil {
config.TlsConfig = new(tls.Config)
}
config.TlsConfig.InsecureSkipVerify = true
config := blip.DialOptions{
URL: target.String() + "/_blipsync?" + BLIPSyncClientTypeQueryParam + "=" + string(BLIPClientTypeSGR2),
HTTPClient: client,
}

if basicAuthCreds != nil {
config.Header.Add("Authorization", "Basic "+base64UserInfo(basicAuthCreds))
config.HTTPHeader = http.Header{
"Authorization": []string{"Basic " + base64UserInfo(basicAuthCreds)},
}
}

return blipContext.DialConfig(config)
return blipContext.DialConfig(&config)
}

// base64UserInfo returns the base64 encoded version of the given UserInfo.
Expand Down
5 changes: 4 additions & 1 deletion manifest/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ licenses/APL2.txt.

<project name="goutils" path="godeps/src/github.com/couchbase/goutils" remote="couchbase" revision="f98adca8eb365032cab838ef4d99453931afa112"/>

<project name="go-blip" path="godeps/src/github.com/couchbase/go-blip" remote="couchbase" revision="3c4edeb16c47cdb3bcd1eeab3b3c5c87832d4923"/>
<project name="go-blip" path="godeps/src/github.com/couchbase/go-blip" remote="couchbase" revision="746a6c2c23026766d1a3ace9cca85c5a01f11408"/>

<project name="errors" path="godeps/src/github.com/pkg/errors" remote="couchbasedeps" revision="f15c970de5b76fac0b59abb32d62c17cc7bed265"/>

Expand Down Expand Up @@ -156,4 +156,7 @@ licenses/APL2.txt.

<project name="mergo" path="godeps/src/github.com/imdario/mergo" remote="couchbasedeps" revision="b968a17bce53738e37f30fd352eb2f7f69ed58a7"/>

<project name="websocket" path="godeps/src/nhooyr.io/websocket" remote="couchbasedeps" revision="8dee580a7f74cf1713400307b4eee514b927870f"/>
<project name="compress" path="godeps/src/github.com/klauspost/compress" remote="couchbasedeps" revision="a1a9cfc821f00faf2f5231beaa96244344d50391"/>

</manifest>
20 changes: 10 additions & 10 deletions rest/blip_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

"github.com/couchbase/go-blip"
"github.com/couchbase/sync_gateway/base"
"golang.org/x/net/websocket"
)

// HTTP handler for incoming BLIP sync WebSocket request (/db/_blipsync)
Expand Down Expand Up @@ -61,16 +60,17 @@ func (h *handler) handleBLIPSync() error {

// Create a BLIP WebSocket handler and have it handle the request:
server := blipContext.WebSocketServer()
defaultHandler := server.Handler
server.Handler = func(conn *websocket.Conn) {
h.logStatus(http.StatusSwitchingProtocols, fmt.Sprintf("[%s] Upgraded to WebSocket protocol %s+%s%s", blipContext.ID, blip.WebSocketSubProtocolPrefix, blipContext.ActiveSubprotocol(), h.formattedEffectiveUserName()))
defer func() {
_ = conn.Close() // in case it wasn't closed already
base.InfofCtx(h.db.Ctx, base.KeyHTTP, "%s: --> BLIP+WebSocket connection closed", h.formatSerialNumber())
}()
defaultHandler(conn)

middleware := func(next http.Handler) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
h.logStatus(http.StatusSwitchingProtocols, fmt.Sprintf("[%s] Upgraded to WebSocket protocol %s+%s%s", blipContext.ID, blip.WebSocketSubProtocolPrefix, blipContext.ActiveSubprotocol(), h.formattedEffectiveUserName()))
defer base.InfofCtx(h.db.Ctx, base.KeyHTTP, "%s: --> BLIP+WebSocket connection closed", h.formatSerialNumber())
next.ServeHTTP(w, r)
})
}

server.ServeHTTP(h.response, h.rq)
middleware(server).ServeHTTP(h.response, h.rq)

return nil
}
12 changes: 4 additions & 8 deletions rest/utilities_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/bcrypt"
"golang.org/x/net/websocket"
)

// Testing utilities that have been included in the rest package so that they
Expand Down Expand Up @@ -986,20 +985,17 @@ func createBlipTesterWithSpec(tb testing.TB, spec BlipTesterSpec, rt *RestTester
return nil, err
}

origin := "http://localhost" // TODO: what should be used here?

config, err := websocket.NewConfig(u.String(), origin)
if err != nil {
return nil, err
config := blip.DialOptions{
URL: u.String(),
}

if len(spec.connectingUsername) > 0 {
config.Header = http.Header{
config.HTTPHeader = http.Header{
"Authorization": {"Basic " + base64.StdEncoding.EncodeToString([]byte(spec.connectingUsername+":"+spec.connectingPassword))},
}
}

bt.sender, err = bt.blipContext.DialConfig(config)
bt.sender, err = bt.blipContext.DialConfig(&config)
if err != nil {
return nil, err
}
Expand Down