diff --git a/common/community_client.go b/common/community_client.go index 29f528ab..c0094e62 100644 --- a/common/community_client.go +++ b/common/community_client.go @@ -7,7 +7,7 @@ import ( "encoding/pem" "fmt" "io/ioutil" - "net/url" + neturl "net/url" "strings" "time" @@ -85,8 +85,11 @@ func NewMongoCommunityConn(url string, connectMode string, timeout bool, readCon } l.Logger.Debugf("encodedURL:[%v]", encodedURL) + isSrvURI := strings.HasPrefix(url, "mongodb+srv://") clientOps := options.Client().ApplyURI(encodedURL) - //clientOps := options.Client().ApplyURI(url) + if !isSrvURI { + clientOps.SetDirect(true) + } // tls tlsInsecure + tlsCaFile if sslRootFile != "" { @@ -146,13 +149,10 @@ func NewMongoCommunityConn(url string, connectMode string, timeout bool, readCon ctx := context.Background() // connect - client, err := mongo.NewClient(clientOps) + client, err := mongo.Connect(ctx, clientOps) if err != nil { return nil, fmt.Errorf("new client failed: %v", err) } - if err := client.Connect(ctx); err != nil { - return nil, fmt.Errorf("connect to %s failed: %v", BlockMongoUrlPassword(url, "***"), err) - } // ping if err = client.Ping(ctx, clientOps.ReadPreference); err != nil { @@ -287,7 +287,7 @@ func EncodeMongoURI(uri string) (string, error) { return "", fmt.Errorf("invalid URI scheme") } scheme := parts[0] - if scheme != "mongodb" { + if scheme != "mongodb" && scheme != "mongodb+srv" { return "", fmt.Errorf("unsupported scheme: %s", scheme) } rest := parts[1][2:] // remove "//" @@ -315,19 +315,25 @@ func EncodeMongoURI(uri string) (string, error) { // split hosts and path hostPathParts := strings.SplitN(afterUserInfo, "/", 2) host := hostPathParts[0] - var path string + + var path, rawQuery string if len(hostPathParts) > 1 { - path = "/" + hostPathParts[1] - } else { - path = "" + pathAndQuery := hostPathParts[1] + // split path and query + pathQueryParts := strings.SplitN(pathAndQuery, "?", 2) + path = "/" + pathQueryParts[0] + if len(pathQueryParts) > 1 { + rawQuery = pathQueryParts[1] + } } // generate URL - u := &url.URL{ - Scheme: scheme, - User: url.UserPassword(username, password), - Host: host, - Path: path, + u := &neturl.URL{ + Scheme: scheme, + User: neturl.UserPassword(username, password), + Host: host, + Path: path, + RawQuery: rawQuery, } return u.String(), nil diff --git a/logs/mongoshake.pid b/logs/mongoshake.pid new file mode 100644 index 00000000..5728d18e --- /dev/null +++ b/logs/mongoshake.pid @@ -0,0 +1 @@ +144794 diff --git a/mongoshake.conf b/mongoshake.conf new file mode 100644 index 00000000..d96303a1 --- /dev/null +++ b/mongoshake.conf @@ -0,0 +1,19 @@ +#conf.version = 11 + +#mongo_urls = mongodb://root:O249MCkl.~laPeNe@10.2.0.216:30310/?directConnection=true +#mongo_urls = mongodb://root:O249MCkl.~laPeNe@10.42.0.96:27017,10.42.0.97:27017/?replicaSet=rs1 +#mongo_urls = mongodb://root:O249MCkl.~laPeNe@127.0.0.1:27017/admin?directConnection=true +#mongo_urls = mongodb://msuser:MySafePassword123!@127.0.0.1:27018/admin?authSource=admin&directConnection=true +#tunnel.address = mongodb://root:b841Pt6cOrMppzmX@10.2.0.216:32255 + +#filter.namespace.black = config,local + +#sync_mode = all + + +conf.version = 11 +mongo_urls = +tunnel.address= mongodb://root:YcYbhFKuCwso~CxL@10.2.0.216:30378 +sync_mode = all +full_sync.executor.insert_on_dup_update = true +