From 5f4b5509d679ee60d429bbfeb965ceec625f046e Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Mon, 29 Apr 2024 15:42:58 +0300 Subject: [PATCH] internal package --- cmd/ch-leave/main.go | 2 +- cmd/mmc/main.go | 2 +- cmd/mmc/post.go | 2 +- cmd/rd/main.go | 2 +- cmd/sb/main.go | 2 +- internal/cert.go | 43 ++++++++++++++++++++ lock.go => internal/lock.go | 0 common.go => internal/post.go | 74 ----------------------------------- internal/users.go | 49 +++++++++++++++++++++++ internal/var.go | 47 ++++++++++++++++++++++ 10 files changed, 144 insertions(+), 79 deletions(-) create mode 100644 internal/cert.go rename lock.go => internal/lock.go (100%) rename common.go => internal/post.go (57%) create mode 100644 internal/users.go create mode 100644 internal/var.go diff --git a/cmd/ch-leave/main.go b/cmd/ch-leave/main.go index d9e3385..e182983 100644 --- a/cmd/ch-leave/main.go +++ b/cmd/ch-leave/main.go @@ -23,7 +23,7 @@ import ( "github.com/mattermost/mattermost-server/v6/model" "go.cypherpunks.ru/netrc" - "go.stargrave.org/mmc" + "go.stargrave.org/mmc/internal" ) func main() { diff --git a/cmd/mmc/main.go b/cmd/mmc/main.go index 069f0e4..3e89009 100644 --- a/cmd/mmc/main.go +++ b/cmd/mmc/main.go @@ -43,7 +43,7 @@ import ( "github.com/gorilla/websocket" "github.com/mattermost/mattermost-server/v6/model" "go.cypherpunks.ru/netrc" - "go.stargrave.org/mmc" + "go.stargrave.org/mmc/internal" ) var ( diff --git a/cmd/mmc/post.go b/cmd/mmc/post.go index 6506303..b18a486 100644 --- a/cmd/mmc/post.go +++ b/cmd/mmc/post.go @@ -27,7 +27,7 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/mattermost/mattermost-server/v6/model" "go.cypherpunks.ru/recfile" - "go.stargrave.org/mmc" + "go.stargrave.org/mmc/internal" ) const CmdFile = "/FILE " diff --git a/cmd/rd/main.go b/cmd/rd/main.go index 74f613a..535332f 100644 --- a/cmd/rd/main.go +++ b/cmd/rd/main.go @@ -29,7 +29,7 @@ import ( "github.com/fsnotify/fsnotify" "github.com/mattermost/mattermost-server/v6/model" "go.cypherpunks.ru/recfile" - "go.stargrave.org/mmc" + "go.stargrave.org/mmc/internal" ) var Threads = make(map[string]string) diff --git a/cmd/sb/main.go b/cmd/sb/main.go index 9e987a9..b1901c9 100644 --- a/cmd/sb/main.go +++ b/cmd/sb/main.go @@ -26,7 +26,7 @@ import ( "github.com/mattermost/mattermost-server/v6/model" "go.cypherpunks.ru/netrc" "go.cypherpunks.ru/recfile" - "go.stargrave.org/mmc" + "go.stargrave.org/mmc/internal" ) func main() { diff --git a/internal/cert.go b/internal/cert.go new file mode 100644 index 0000000..aa2f39b --- /dev/null +++ b/internal/cert.go @@ -0,0 +1,43 @@ +// mmc -- Mattermost client +// Copyright (C) 2023-2024 Sergey Matveev +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package mmc + +import ( + "crypto/sha256" + "crypto/x509" + "encoding/hex" + "errors" +) + +func NewVerifyPeerCertificate(hashExpected string) func( + rawCerts [][]byte, verifiedChains [][]*x509.Certificate, +) error { + return func( + rawCerts [][]byte, verifiedChains [][]*x509.Certificate, + ) error { + cer, err := x509.ParseCertificate(rawCerts[0]) + if err != nil { + return err + } + spki := cer.RawSubjectPublicKeyInfo + hsh := sha256.Sum256(spki) + if hashExpected != hex.EncodeToString(hsh[:]) { + return errors.New("server certificate's SPKI hash mismatch") + } + return nil + } +} diff --git a/lock.go b/internal/lock.go similarity index 100% rename from lock.go rename to internal/lock.go diff --git a/common.go b/internal/post.go similarity index 57% rename from common.go rename to internal/post.go index ce84af6..106a065 100644 --- a/common.go +++ b/internal/post.go @@ -17,28 +17,13 @@ package mmc import ( - "crypto/sha256" - "crypto/x509" - "encoding/hex" - "errors" - "os" "strings" "time" - "github.com/davecgh/go-spew/spew" "github.com/mattermost/mattermost-server/v6/model" "go.cypherpunks.ru/recfile" ) -const ( - PerPage = 100 - OutRec = "out.rec" - OutRecLock = "out.rec.lock" - Last = "last" -) - -var SleepTime = 250 * time.Millisecond - type Post struct { P *model.Post E string @@ -81,62 +66,3 @@ func PostToRec(w *recfile.Writer, users map[string]*model.User, post Post) error _, err = w.WriteFieldMultiline("Text", strings.Split(post.P.Message, "\n")) return err } - -func GetUsers(c *model.Client4, debugFd *os.File) (map[string]*model.User, error) { - users := make(map[string]*model.User) - for n := 0; ; n++ { - time.Sleep(SleepTime) - page, resp, err := c.GetUsers(n, PerPage, "") - if err != nil { - if debugFd != nil { - spew.Fdump(debugFd, resp) - } - return nil, err - } - if debugFd != nil { - spew.Fdump(debugFd, page) - } - for _, u := range page { - users[u.Id] = u - } - if len(page) < PerPage { - break - } - } - return users, nil -} - -func GetEntrypoint() string { - s := os.Getenv("MMC_ENTRYPOINT") - if s == "" { - return "http://mm.invalid" - } - return s -} - -func GetSPKIHash() string { - s := os.Getenv("MMC_SPKI") - if s == "" { - return "deadbeef" - } - return s -} - -func NewVerifyPeerCertificate(hashExpected string) func( - rawCerts [][]byte, verifiedChains [][]*x509.Certificate, -) error { - return func( - rawCerts [][]byte, verifiedChains [][]*x509.Certificate, - ) error { - cer, err := x509.ParseCertificate(rawCerts[0]) - if err != nil { - return err - } - spki := cer.RawSubjectPublicKeyInfo - hsh := sha256.Sum256(spki) - if hashExpected != hex.EncodeToString(hsh[:]) { - return errors.New("server certificate's SPKI hash mismatch") - } - return nil - } -} diff --git a/internal/users.go b/internal/users.go new file mode 100644 index 0000000..83802e7 --- /dev/null +++ b/internal/users.go @@ -0,0 +1,49 @@ +// mmc -- Mattermost client +// Copyright (C) 2023-2024 Sergey Matveev +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package mmc + +import ( + "os" + "time" + + "github.com/davecgh/go-spew/spew" + "github.com/mattermost/mattermost-server/v6/model" +) + +func GetUsers(c *model.Client4, debugFd *os.File) (map[string]*model.User, error) { + users := make(map[string]*model.User) + for n := 0; ; n++ { + time.Sleep(SleepTime) + page, resp, err := c.GetUsers(n, PerPage, "") + if err != nil { + if debugFd != nil { + spew.Fdump(debugFd, resp) + } + return nil, err + } + if debugFd != nil { + spew.Fdump(debugFd, page) + } + for _, u := range page { + users[u.Id] = u + } + if len(page) < PerPage { + break + } + } + return users, nil +} diff --git a/internal/var.go b/internal/var.go new file mode 100644 index 0000000..ece260c --- /dev/null +++ b/internal/var.go @@ -0,0 +1,47 @@ +// mmc -- Mattermost client +// Copyright (C) 2023-2024 Sergey Matveev +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package mmc + +import ( + "os" + "time" +) + +const ( + PerPage = 100 + OutRec = "out.rec" + OutRecLock = "out.rec.lock" + Last = "last" +) + +var SleepTime = 250 * time.Millisecond + +func GetEntrypoint() string { + s := os.Getenv("MMC_ENTRYPOINT") + if s == "" { + return "http://mm.invalid" + } + return s +} + +func GetSPKIHash() string { + s := os.Getenv("MMC_SPKI") + if s == "" { + return "deadbeef" + } + return s +} -- 2.44.0