From 1c17501cf285198e9a2289ad28a0576b9bb49a2d Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Mon, 15 Apr 2024 14:23:11 +0300 Subject: [PATCH] Ability to connect to http:// entrypoints --- cmd/ch-leave/main.go | 12 ++++++++---- cmd/mmc/main.go | 21 +++++++++++++++++---- cmd/sb/main.go | 13 +++++++++---- common.go | 2 +- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/cmd/ch-leave/main.go b/cmd/ch-leave/main.go index cdd699d..d9e3385 100644 --- a/cmd/ch-leave/main.go +++ b/cmd/ch-leave/main.go @@ -19,6 +19,7 @@ package main import ( "flag" "log" + "net/url" "github.com/mattermost/mattermost-server/v6/model" "go.cypherpunks.ru/netrc" @@ -29,13 +30,16 @@ func main() { entrypoint := flag.String("entrypoint", mmc.GetEntrypoint(), "Entrypoint") flag.Parse() log.SetFlags(log.Lshortfile) - chId := flag.Arg(0) - login, password := netrc.Find(*entrypoint) + entrypointURL, err := url.Parse(*entrypoint) + if err != nil { + log.Fatalln(err) + } + login, password := netrc.Find(entrypointURL.Hostname()) if login == "" || password == "" { - log.Fatalln("no credentials found for:", *entrypoint) + log.Fatalln("no credentials found for:", entrypointURL.Hostname()) } - c := model.NewAPIv4Client("https://" + *entrypoint) + c := model.NewAPIv4Client(*entrypoint) c.Login(login, password) me, _, err := c.GetMe("") if err != nil { diff --git a/cmd/mmc/main.go b/cmd/mmc/main.go index 9bf3fb2..79a850a 100644 --- a/cmd/mmc/main.go +++ b/cmd/mmc/main.go @@ -26,6 +26,7 @@ import ( "io" "io/fs" "log" + "net/url" "os" "os/exec" "os/signal" @@ -86,11 +87,15 @@ func main() { } defer DebugFd.Close() - login, password := netrc.Find(*entrypoint) + entrypointURL, err := url.Parse(*entrypoint) + if err != nil { + log.Fatalln(err) + } + login, password := netrc.Find(entrypointURL.Hostname()) if login == "" || password == "" { - log.Fatalln("no credentials found for:", *entrypoint) + log.Fatalln("no credentials found for:", entrypointURL.Hostname()) } - c := model.NewAPIv4Client("https://" + *entrypoint) + c := model.NewAPIv4Client(*entrypoint) c.Login(login, password) me, resp, err := c.GetMe("") if err != nil { @@ -439,7 +444,15 @@ func main() { }() needsShutdown := make(chan os.Signal) - wc, err := model.NewWebSocketClient4("wss://"+*entrypoint, c.AuthToken) + switch entrypointURL.Scheme { + case "http": + entrypointURL.Scheme = "ws" + case "https": + entrypointURL.Scheme = "wss" + default: + log.Println("unhandled scheme:", entrypointURL.Scheme) + } + wc, err := model.NewWebSocketClient4(entrypointURL.String(), c.AuthToken) if err != nil { log.Fatalln(err) } diff --git a/cmd/sb/main.go b/cmd/sb/main.go index 63f723f..9e987a9 100644 --- a/cmd/sb/main.go +++ b/cmd/sb/main.go @@ -19,12 +19,13 @@ package main import ( "flag" "log" + "net/url" "os" "time" "github.com/mattermost/mattermost-server/v6/model" - "go.cypherpunks.ru/recfile" "go.cypherpunks.ru/netrc" + "go.cypherpunks.ru/recfile" "go.stargrave.org/mmc" ) @@ -38,11 +39,15 @@ func main() { chId := flag.Arg(0) postId := flag.Arg(1) - login, password := netrc.Find(*entrypoint) + entrypointURL, err := url.Parse(*entrypoint) + if err != nil { + log.Fatalln(err) + } + login, password := netrc.Find(entrypointURL.Hostname()) if login == "" || password == "" { - log.Fatalln("no credentials found for:", *entrypoint) + log.Fatalln("no credentials found for:", entrypointURL.Hostname()) } - c := model.NewAPIv4Client("https://" + *entrypoint) + c := model.NewAPIv4Client(*entrypoint) c.Login(login, password) me, _, err := c.GetMe("") if err != nil { diff --git a/common.go b/common.go index 3e631db..f4d6a9d 100644 --- a/common.go +++ b/common.go @@ -105,7 +105,7 @@ func GetUsers(c *model.Client4, debugFd *os.File) (map[string]*model.User, error func GetEntrypoint() string { s := os.Getenv("MMC_ENTRYPOINT") if s == "" { - return "mm.invalid" + return "http://mm.invalid" } return s } -- 2.44.0