func (uc *UpdateClient) Delete(repoURL string, name string) error { repo, err := client.NewUCRepo(repoURL) if err != nil { return err } return repo.Delete(name) }
// List always tells the latest apps. func (uc *UpdateClient) List(repoURL string) ([]string, error) { repo, err := client.NewUCRepo(repoURL) if err != nil { return nil, err } return repo.List() }
// Update the meta data, meta sign, public key func (uc *UpdateClient) Update(repoURL string) error { repo, err := client.NewUCRepo(repoURL) if err != nil { return err } needUpdate := false remoteMetaBytes, err := repo.GetMeta() if err != nil { return err } localMetaFile := filepath.Join(uc.getCacheDir(), repo.NRString(), defaultMeta) localMetaBytes, err := ioutil.ReadFile(localMetaFile) if err != nil { needUpdate = true } else { var remoteMeta utils.Meta var localMeta utils.Meta json.Unmarshal(remoteMetaBytes, &remoteMeta) json.Unmarshal(localMetaBytes, &localMeta) needUpdate = localMeta.Before(remoteMeta) } if !needUpdate { fmt.Println("Already updated to the latest version.") return nil } uc.save(repo, defaultMeta, remoteMetaBytes) signBytes, _ := repo.GetMetaSign() pubBytes, _ := repo.GetPublicKey() err = utils.SHA256Verify(pubBytes, remoteMetaBytes, signBytes) if err != nil { fmt.Println("Fail to verify meta by public key") return err } metaURL, err := uc.save(repo, defaultMeta, remoteMetaBytes) if err != nil { return err } signURL, err := uc.save(repo, defaultMetaSign, signBytes) if err != nil { os.Remove(metaURL) return err } _, err = uc.save(repo, defaultPublicKey, pubBytes) if err != nil { os.Remove(metaURL) os.Remove(signURL) return err } return nil }
func (uc *UpdateClient) GetFile(repoURL string, name string) (string, error) { repo, err := client.NewUCRepo(repoURL) if err != nil { return "", err } fileBytes, err := repo.GetFile(name) if err != nil { return "", err } fileURL, err := uc.save(repo, name, fileBytes) if err != nil { return "", err } err = uc.Update(repoURL) if err != nil { fmt.Println("Fail to verify if downloaded file is valid") return fileURL, err } var meta utils.Meta metaBytes, _ := ioutil.ReadFile(filepath.Join(uc.cacheDir, repo.NRString(), defaultMeta)) fileHash := fmt.Sprintf("%x", sha1.Sum(fileBytes)) json.Unmarshal(metaBytes, &meta) for _, m := range meta.Items { if m.Name != name { continue } if m.Hash == fileHash { fmt.Println("Congratulations! The file is valid!") if m.Method != utils.EncryptNone { fmt.Printf("%s is encrypted, please using your private key to decrypt it. For security reason, we don't provide API to get private key, so if the private key is generated by server, contact system admin to get it!", fileURL) } return fileURL, nil } err := errors.New("the file is invalid, maybe security issue") fmt.Println(err) return fileURL, err } return fileURL, errors.New("Cannot find the file in meta data, maybe remote system error") }
} } else { encrypt = utils.EncryptNone } if argsLen < 3 || argsLen > 4 { err := errors.New("wrong syntax: 'repoURL' 'fileURL' 'prefix' 'encryptMethod(default to none)'. prefix in appv1 means 'os/arch'") fmt.Println(err) return err } repoURL := context.Args().Get(0) fileURL := context.Args().Get(1) prefix := context.Args().Get(2) repo, err := client.NewUCRepo(repoURL) if err != nil { fmt.Println(err) return err } content, err := ioutil.ReadFile(fileURL) if err != nil { fmt.Println(err) return err } err = repo.Put(prefix+"/"+filepath.Base(fileURL), content, encrypt) if err != nil { fmt.Println(err) return err