diff --git a/attachments.go b/attachments.go index 12a4750..0b1b603 100644 --- a/attachments.go +++ b/attachments.go @@ -11,7 +11,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/http" "strconv" @@ -70,7 +69,7 @@ func uploadAttachment(r io.Reader, ct string) (*att, error) { keys := make([]byte, 64) randBytes(keys) - b, err := ioutil.ReadAll(r) + b, err := io.ReadAll(r) if err != nil { return nil, err } @@ -101,7 +100,7 @@ func uploadVoiceNote(r io.Reader, ct string) (*att, error) { keys := make([]byte, 64) randBytes(keys) - b, err := ioutil.ReadAll(r) + b, err := io.ReadAll(r) if err != nil { return nil, err } @@ -141,7 +140,7 @@ func handleSingleAttachment(a *textsecure.AttachmentPointer) (*Attachment, error } defer r.Close() - b, err := ioutil.ReadAll(r) + b, err := io.ReadAll(r) if err != nil { return nil, err } @@ -172,7 +171,7 @@ func handleProfileAvatar(profileAvatar *signalservice.ContactDetails_Avatar, key } defer r.Close() - b, err := ioutil.ReadAll(r) + b, err := io.ReadAll(r) if err != nil { return nil, err } diff --git a/cmd/textsecure/main.go b/cmd/textsecure/main.go index f8a519c..d578067 100644 --- a/cmd/textsecure/main.go +++ b/cmd/textsecure/main.go @@ -332,7 +332,7 @@ func GroupsHandler(w http.ResponseWriter, r *http.Request) { filepath.Walk(".storage/groups", func(path string, info os.FileInfo, e error) error { if info.Mode().IsRegular() { - b, err := ioutil.ReadFile(path) + b, err := os.ReadFile(path) if err != nil { return err } @@ -414,7 +414,7 @@ func JSONHandler(w http.ResponseWriter, r *http.Request) { if messageField == "" { messageField = "message" } - body, err := ioutil.ReadAll(r.Body) + body, err := io.ReadAll(r.Body) if err != nil { log.Error("Error: ", err.Error()) w.WriteHeader(http.StatusInternalServerError) diff --git a/config.go b/config.go index fbd39d0..4b7cbae 100644 --- a/config.go +++ b/config.go @@ -5,6 +5,7 @@ package textsecure import ( "io/ioutil" + "os" "github.com/go-yaml/yaml" "github.com/signal-golang/textsecure/config" @@ -36,7 +37,7 @@ func checkUUID(cfg *config.Config) *config.Config { // ReadConfig reads a YAML config file func ReadConfig(fileName string) (*config.Config, error) { - b, err := ioutil.ReadFile(fileName) + b, err := os.ReadFile(fileName) if err != nil { return nil, err } diff --git a/contacts/contacts.go b/contacts/contacts.go index 441581e..950cb10 100644 --- a/contacts/contacts.go +++ b/contacts/contacts.go @@ -4,8 +4,8 @@ package contacts import ( - "fmt" "io/ioutil" + "os" signalservice "github.com/signal-golang/textsecure/protobuf" log "github.com/sirupsen/logrus" @@ -67,7 +67,7 @@ var filePath string // ReadContacts loads the contacts yaml file and pareses it func ReadContacts(fileName string) ([]Contact, error) { log.Debug("[textsecure] read contacts from ", fileName) - b, err := ioutil.ReadFile(fileName) + b, err := os.ReadFile(fileName) filePath = fileName contactsYaml := &yamlContacts{} if err != nil { @@ -127,7 +127,7 @@ func updateContact(c *signalservice.ContactDetails) error { // r = att.R // buf.ReadFrom(r) // } - // avatar, _ := ioutil.ReadAll(buf) + // avatar, _ := io.ReadAll(buf) Contacts[c.GetUuid()] = Contact{ Tel: c.GetNumber(), @@ -167,7 +167,14 @@ func UpdateProfileKey(src string, profileKey []byte) error { Contacts[src] = contact return WriteContactsToPath() } - return fmt.Errorf("Contact to update not found %s", src) + // create new contact + Contacts[src] = Contact{ + UUID: src, + Name: src, + ProfileKey: profileKey, + } + return WriteContactsToPath() + } func GetContact(uuid string) Contact { diff --git a/groups.go b/groups.go index 8ca8015..9ab8055 100644 --- a/groups.go +++ b/groups.go @@ -66,7 +66,7 @@ func saveGroup(hexid string) error { // loadGroup loads a group's state from a file. func loadGroup(path string) error { _, hexid := filepath.Split(path) - b, err := ioutil.ReadFile(path) + b, err := os.ReadFile(path) if err != nil { return err } diff --git a/groupsv2/credentials.go b/groupsv2/credentials.go index 3323636..ed64565 100644 --- a/groupsv2/credentials.go +++ b/groupsv2/credentials.go @@ -3,7 +3,7 @@ package groupsv2 import ( "encoding/json" "fmt" - "io/ioutil" + "io" "time" signalservice "github.com/signal-golang/textsecure/protobuf" @@ -88,7 +88,7 @@ func GetGroupExternalCredential(credential *GroupCredential) (*signalservice.Gro if resp.IsError() { return nil, resp } - data, err := ioutil.ReadAll(resp.Body) + data, err := io.ReadAll(resp.Body) if err != nil { return nil, err } diff --git a/groupsv2/groupsv2.go b/groupsv2/groupsv2.go index 358d443..5a60aca 100644 --- a/groupsv2/groupsv2.go +++ b/groupsv2/groupsv2.go @@ -344,7 +344,7 @@ func saveGroupV2(hexid string) error { // loadGroup loads a group's state from a file. func loadGroupV2(hexid string) (*GroupV2, error) { - b, err := ioutil.ReadFile(idToPath(hexid)) + b, err := os.ReadFile(idToPath(hexid)) if err != nil { return nil, err } diff --git a/handler.go b/handler.go index fb07c7d..c931b58 100644 --- a/handler.go +++ b/handler.go @@ -2,7 +2,6 @@ package textsecure import ( "github.com/golang/protobuf/proto" - "github.com/signal-golang/textsecure/config" "github.com/signal-golang/textsecure/groupsv2" signalservice "github.com/signal-golang/textsecure/protobuf" log "github.com/sirupsen/logrus" @@ -20,7 +19,7 @@ func handleMessage(srcE164 string, srcUUID string, timestamp uint64, b []byte) e if dm := content.GetDataMessage(); dm != nil { return handleDataMessage(srcE164, srcUUID, timestamp, dm) - } else if sm := content.GetSyncMessage(); sm != nil && config.ConfigFile.Tel == srcE164 { + } else if sm := content.GetSyncMessage(); sm != nil { return handleSyncMessage(srcE164, srcUUID, timestamp, sm) } else if cm := content.GetCallMessage(); cm != nil { return handleCallMessage(srcE164, srcUUID, timestamp, cm) diff --git a/profiles/avatars.go b/profiles/avatars.go index 6bd457a..448a5ea 100644 --- a/profiles/avatars.go +++ b/profiles/avatars.go @@ -37,8 +37,21 @@ func GetRemoteAvatar(avatarURL string) (io.ReadCloser, error) { return resp.Body, nil } - -func getLocalAvatar(uuid string) (io.ReadCloser, error) { +func GetLocalAvatarPath(uuid string) (string, error) { + log.Debugln("[textsecure] get local avatar for ", uuid) + if uuid == "" { + return "", fmt.Errorf("empty uuid") + } + if avatarsPath == "" { + err := setupAvatarsPath() + if err != nil { + return "", err + } + } + avatarFile := filepath.Join(avatarsPath, uuid) + return avatarFile, nil +} +func GetLocalAvatar(uuid string) (io.ReadCloser, error) { log.Debugln("[textsecure] get local avatar for ", uuid) if uuid == "" { return nil, fmt.Errorf("empty uuid") @@ -101,7 +114,6 @@ func setupAvatarsPath() error { if err := os.MkdirAll(avatarsPath, 0700); err != nil { return err } - log.Debugln("[textsecure] setup avatars path ", avatarsPath) return nil } diff --git a/profiles/profile.go b/profiles/profile.go index 60cda1c..a5dcef2 100644 --- a/profiles/profile.go +++ b/profiles/profile.go @@ -194,6 +194,10 @@ func GetProfile(UUID string, profileKey []byte) (*Profile, error) { } func GetProfileAndCredential(UUID string, profileKey []byte) (*Profile, error) { + if len(profileKey) == 0 { + return nil, errors.New("profileKey is empty") + } + log.Infoln("[textsecure] GetProfileAndCredential for " + UUID) uuid, err := uuidUtil.FromString(UUID) if err != nil { @@ -229,7 +233,6 @@ func GetProfileAndCredential(UUID string, profileKey []byte) (*Profile, error) { if err != nil { return nil, err } - // fmt.Printf("%s\n", string(bytes)) err = json.Unmarshal(bytes, profile) if err != nil { log.Debugln("[textsecure] GetProfileAndCredential json unmarshall", err) @@ -241,6 +244,11 @@ func GetProfileAndCredential(UUID string, profileKey []byte) (*Profile, error) { return nil, err } } + log.Debugf("[textsecure] GetProfileAndCredential profile %+v", profile) + if profile.Credential == nil { + log.Debugf("[textsecure] GetProfileAndCredential profile %+v", profile) + return nil, errors.New("profile credential is empty") + } response := zkgroup.ProfileKeyCredentialResponse(profile.Credential) if err != nil { log.Debugln("[textsecure] GetProfileAndCredential", err) @@ -261,6 +269,9 @@ func GetProfileAndCredential(UUID string, profileKey []byte) (*Profile, error) { } func decryptProfile(profileKey []byte, profile *Profile) error { + if len(profileKey) == 0 { + return fmt.Errorf("[textsecure] decryptProfile: no profile key") + } log.Println("[textsecure] decryptProfile") if profile.Name != "" { name, err := decryptString(profileKey, profile.Name) @@ -361,7 +372,7 @@ func GetProfileE164(tel string) (contacts.Contact, error) { // GetProfileUUID get a profile by a phone number func GetProfileUUID(uuid string) (*Profile, error) { - log.Debug("[textsecure] GetProfileUUID", uuid) + log.Debugln("[textsecure] GetProfileUUID", uuid) c := contacts.Contacts[uuid] profile := &Profile{} var err error diff --git a/rootCa/rootca.go b/rootCa/rootca.go index 38dac63..cf3380e 100644 --- a/rootCa/rootca.go +++ b/rootCa/rootca.go @@ -2,7 +2,7 @@ package rootCa import ( "crypto/x509" - "io/ioutil" + "os" "github.com/signal-golang/textsecure/utils" @@ -70,7 +70,7 @@ var DirectoryCA *x509.CertPool func SetupCA(rootca string) { pem := []byte(rootPEM) if rootca != "" && utils.Exists(rootca) { - b, err := ioutil.ReadFile(rootca) + b, err := os.ReadFile(rootca) if err != nil { log.Error(err) return diff --git a/server.go b/server.go index 81b516e..50f62ca 100644 --- a/server.go +++ b/server.go @@ -11,7 +11,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "mime/quotedprintable" "regexp" "strconv" @@ -377,6 +377,12 @@ func GetProfileByUUID(uuid string) (*profiles.Profile, error) { } return profile, nil } +func GetAvatar(uuid string) (io.ReadCloser, error) { + return profiles.GetLocalAvatar(uuid) +} +func GetAvatarPath(uuid string) (string, error) { + return profiles.GetLocalAvatarPath(uuid) +} func GetProfileAndCredential(uuid string, profileKey []byte) (*profiles.Profile, error) { if uuid == "" || len(profileKey) == 0 { @@ -678,7 +684,7 @@ func GetRegisteredContacts() ([]contacts.Contact, error) { UUID := idToHexUUID(responseData[ind*uuidlength : (ind+1)*uuidlength]) index := findIndexByE147(phone, localContacts) if strings.Count(localContacts[index].Name, "=") > 2 { - decodedName, err := ioutil.ReadAll(quotedprintable.NewReader(strings.NewReader(localContacts[index].Name))) + decodedName, err := io.ReadAll(quotedprintable.NewReader(strings.NewReader(localContacts[index].Name))) if err != nil { log.Debug("[textsecure] GetRegisteredContacts update name from quoted printable error:", err) } else { diff --git a/store.go b/store.go index cda9333..b87b88b 100644 --- a/store.go +++ b/store.go @@ -78,7 +78,7 @@ func newStore(password, path string) (*store, error) { return nil, err } } else { - salt, err = ioutil.ReadFile(saltFile) + salt, err = os.ReadFile(saltFile) if err != nil { return nil, err } @@ -161,7 +161,7 @@ func (s *store) decrypt(ciphertext []byte) ([]byte, error) { } func (s *store) readFile(path string) ([]byte, error) { - b, err := ioutil.ReadFile(path) + b, err := os.ReadFile(path) if err != nil { return nil, err } diff --git a/sync.go b/sync.go index f59f0ea..7245202 100644 --- a/sync.go +++ b/sync.go @@ -17,41 +17,24 @@ import ( // handleSyncMessage handles an incoming SyncMessage. func handleSyncMessage(src string, srcUUID string, timestamp uint64, sm *signalservice.SyncMessage) error { log.Debugf("[textsecure] SyncMessage recieved at %d", timestamp) - - if sm.GetContacts() != nil { + if sm.GetSent() != nil { + log.Debugln("[textsecure] SyncMessage getSent") + return handleSyncSent(sm.GetSent(), timestamp) + } else if sm.GetContacts() != nil { log.Debugln("[textsecure] SyncMessage unhandled contacts") return nil } else if sm.GetGroups() != nil { log.Debugln("[textsecure] SyncMessage groups") return nil + } else if sm.GetRequest() != nil { + log.Debugln("[textsecure] SyncMessage getRequest") + return handleSyncRequest(sm.GetRequest()) } else if sm.GetRead() != nil { log.Debugln("[textsecure] SyncMessage getRead") return handleSyncRead(sm.GetRead()) - } else if sm.GetViewed() != nil { - log.Debugln("[textsecure] SyncMessage unhandled getViewed") - return nil - } else if sm.GetViewOnceOpen() != nil { - log.Debugln("[textsecure] SyncMessage unhandled GetViewOnceOpen") - return nil - } else if sm.GetViewOnceOpen() != nil { - log.Debugln("[textsecure] SyncMessage unhandled GetBlockedList") - return nil } else if sm.GetBlocked() != nil { - log.Debugln("[textsecure] SyncMessage blocked") - return nil - } else if sm.GetConfiguration() != nil { - log.Debugln("[textsecure] SyncMessage unahndled configuration") + log.Debugln("[textsecure] SyncMessage unhandled getBlocked") return nil - } else if sm.GetSent() != nil { - log.Debugln("[textsecure] SyncMessage getSent") - return handleSyncSent(sm.GetSent(), timestamp) - } else if sm.GetStickerPackOperation() != nil { - log.Debugln("[textsecure] SyncMessage unhandled GetStickerPackOperation") - return nil - } else if sm.GetRequest() != nil { - log.Debugln("[textsecure] SyncMessage getRequest") - return handleSyncRequest(sm.GetRequest()) - } else if sm.GetVerified() != nil { log.Debugln("[textsecure] SyncMessage verified") unidentifiedAccess, err := unidentifiedAccess.GetAccessForSync(config.ConfigFile.ProfileKey, config.ConfigFile.Certificate) @@ -59,13 +42,38 @@ func handleSyncMessage(src string, srcUUID string, timestamp uint64, sm *signals return err } return sendVerifiedMessage(sm.GetVerified(), unidentifiedAccess) - + } else if sm.GetConfiguration() != nil { + log.Debugln("[textsecure] SyncMessage unahndled configuration") + return nil } else if sm.GetPadding() != nil { - log.Debugln("[textsecure] SyncMessage padding") + log.Debugln("[textsecure] SyncMessage unhandled padding") + return nil + } else if sm.GetStickerPackOperation() != nil { + log.Debugln("[textsecure] SyncMessage unhandled GetStickerPackOperation") + return nil + } else if sm.GetViewOnceOpen() != nil { + log.Debugln("[textsecure] SyncMessage unhandled GetViewOnceOpen") return nil - } else if sm.GetFetchLatest() != nil { - log.Debugln("[textsecure] SyncMessage GetFetchLatest") + log.Debugln("[textsecure] SyncMessage unhandled GetFetchLatest") + return nil + } else if sm.GetKeys() != nil { + log.Debugln("[textsecure] SyncMessage unhandled GetKeys") + return nil + } else if sm.GetMessageRequestResponse() != nil { + log.Debugln("[textsecure] SyncMessage unhandled GetMessageRequestResponse") + return nil + } else if sm.GetOutgoingPayment() != nil { + log.Debugln("[textsecure] SyncMessage unhandled GetOutgoing payment") + return nil + } else if sm.GetViewed() != nil { + log.Debugln("[textsecure] SyncMessage unhandled getViewed") + return nil + } else if sm.GetPniIdentity() != nil { + log.Debug("[textsecure] SyncMessage unhandled getPniIdentity") + return nil + } else if sm.GetPniChangeNumber() != nil { + log.Debugln("[textsecure] SyncMessage unhandled getPniChangeNumber") return nil } else { log.Errorf("[textsecure] SyncMessage contains no known sync types") diff --git a/textsecure.go b/textsecure.go index fb1c421..b8c48c2 100644 --- a/textsecure.go +++ b/textsecure.go @@ -356,7 +356,8 @@ func Setup(c *Client) error { } } } - if len(config.ConfigFile.ProfileKeyCredential) == 0 || true { + if len(config.ConfigFile.ProfileKeyCredential) == 0 { + log.Infoln("[textsecure] Generating profile key credential") profiles.UpdateProfile(config.ConfigFile.ProfileKey, config.ConfigFile.UUID, config.ConfigFile.Name) profile, err := profiles.GetProfileAndCredential(config.ConfigFile.UUID, config.ConfigFile.ProfileKey) if err != nil { @@ -365,6 +366,8 @@ func Setup(c *Client) error { config.ConfigFile.ProfileKeyCredential = []byte(profile.Credential) saveConfig(config.ConfigFile) + } else { + log.Infoln("[textsecure] Using existing profile key credential", len(config.ConfigFile.ProfileKeyCredential)) } return err