Skip to content

Add hash to redirected page #9

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Open

Conversation

tyru
Copy link
Member

@tyru tyru commented Jun 30, 2018

Fixes #8

#8 を実装してみました。
以下3点について相談させてください。

  1. 一度にアクセスしないよう時間をバラけさせるか、もしくはダウンロードキューに入れて最大接続数を制限とかした方がいいかもしれませんが、実装はしてません。実装した方が良いでしょうか?
  2. とりあえずよく見る SHA1 を表示するようにしましたが、MD5, SHA256 等のがいい、もしくは他に追加した方が良いものはあるでしょうか?
  3. _data/redirects.ymlgithub_release が書かれてない過去や安定版のリリースについては手動で調べて追記する必要があります。
    • とりあえず全体的にこの PR の方針で良いか相談後に OK だったら追加します。

ちなみにスクリプトを実行 + jekyll serve 後はこんな感じになります。

スクリーンショット

default

実行後の差分

diff --git a/koron/vim-kaoriya/latest/win32.html b/koron/vim-kaoriya/latest/win32.html
index a13198d..520f345 100644
--- a/koron/vim-kaoriya/latest/win32.html
+++ b/koron/vim-kaoriya/latest/win32.html
@@ -2,5 +2,7 @@
 title: Latest Vim +kaoriya binary for Windows 32bit
 redirect_to:
   - https://github.com/koron/vim-kaoriya/releases/download/v8.1.0005-20180520/vim81-kaoriya-win32-8.1.0005-20180520.zip
+hash:
+  sha1: 9f44f8bb9867024b5ac0250602b89d7181cd95e1
 layout: redirect
 ---
diff --git a/koron/vim-kaoriya/latest/win64.html b/koron/vim-kaoriya/latest/win64.html
index 1965722..c19a3d2 100644
--- a/koron/vim-kaoriya/latest/win64.html
+++ b/koron/vim-kaoriya/latest/win64.html
@@ -2,5 +2,7 @@
 title: Latest Vim +kaoriya binary for Windows 64bit
 redirect_to:
   - https://github.com/koron/vim-kaoriya/releases/download/v8.1.0005-20180520/vim81-kaoriya-win64-8.1.0005-20180520.zip
+hash:
+  sha1: cf891e14beec3d258b4ac6b776e1b5436b716206
 layout: redirect
 ---
diff --git a/splhack/macvim-kaoriya/latest.html b/splhack/macvim-kaoriya/latest.html
index b34d686..079e1be 100644
--- a/splhack/macvim-kaoriya/latest.html
+++ b/splhack/macvim-kaoriya/latest.html
@@ -2,5 +2,7 @@
 title: Latest MacVim +kaoriya binary for MacOSX
 redirect_to:
   - https://github.com/splhack/macvim-kaoriya/releases/download/20180324/MacVim-KaoriYa-20180324.dmg
+hash:
+  sha1: 7545971b597b73b5258a8d1df0428715024a7afc
 layout: redirect
 ---
diff --git a/vim/vim-win32-installer/latest/x64.html b/vim/vim-win32-installer/latest/x64.html
index 3f77676..be1a377 100644
--- a/vim/vim-win32-installer/latest/x64.html
+++ b/vim/vim-win32-installer/latest/x64.html
@@ -2,5 +2,7 @@
 title: Latest Vim official binary for Windows x64
 redirect_to:
   - https://github.com/vim/vim-win32-installer/releases/download/v8.1.0127/gvim_8.1.0127_x64.zip
+hash:
+  sha1: 8271276f2fd9d8746b45482433902d5ee0053367
 layout: redirect
 ---
diff --git a/vim/vim-win32-installer/latest/x86.html b/vim/vim-win32-installer/latest/x86.html
index 0a66435..9839e42 100644
--- a/vim/vim-win32-installer/latest/x86.html
+++ b/vim/vim-win32-installer/latest/x86.html
@@ -2,5 +2,7 @@
 title: Latest Vim official binary for Windows x86
 redirect_to:
   - https://github.com/vim/vim-win32-installer/releases/download/v8.1.0127/gvim_8.1.0127_x86.zip
+hash:
+  sha1: a1ce478fbf63f7f3c97322adb993b5ade199d790
 layout: redirect
 ---

@tyru
Copy link
Member Author

tyru commented Jun 30, 2018

(当初 GitHub Releases API にハッシュ値が含まれてるかと淡い期待を抱きましたが、流石にそれはなかったので仕方なく都度 fetch して調べるようにしました)

@tyru
Copy link
Member Author

tyru commented Jul 1, 2018

vital.vim で vim-jp/redirects に fetch してるスクリプトがありますが、http やオレオレ証明書だと Invoke-WebRequest でエラーが起きてしまうため、新しい HTML で問題が起きないかは確認できませんでした… (すみません)
リンクは追加していないので .Links の値は変わらないはずですが、誰か解決できる人試してほしいです…

試したスクリプト
$Env:VIM_URL = 'https://localhost/koron/vim-kaoriya/vim74/oldest/win64/'
$Env:APPVEYOR_BUILD_FOLDER = '.'
$Env:APPVEYOR_BUILD_FOLDER = '.'

# 以下 vital.vim で実際に使用しているスクリプト
$zip = $Env:APPVEYOR_BUILD_FOLDER + '\vim.zip'
$vim = $Env:APPVEYOR_BUILD_FOLDER + '\vim\'
$redirect = Invoke-WebRequest -URI $Env:VIM_URL
(New-Object Net.WebClient).DownloadFile($redirect.Links[0].href, $zip)
[Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') > $null
[System.IO.Compression.ZipFile]::ExtractToDirectory($zip, $vim)
$Env:THEMIS_VIM = $vim + (Get-ChildItem $vim).Name + '\vim.exe'
結果
PS C:\Users\tyru> $Env:VIM_URL = 'https://localhost/koron/vim-kaoriya/vim74/oldest/win64/'
PS C:\Users\tyru> $Env:APPVEYOR_BUILD_FOLDER = '.'
PS C:\Users\tyru> $Env:APPVEYOR_BUILD_FOLDER = '.'
PS C:\Users\tyru>
PS C:\Users\tyru> $zip = $Env:APPVEYOR_BUILD_FOLDER + '\vim.zip'
PS C:\Users\tyru> $vim = $Env:APPVEYOR_BUILD_FOLDER + '\vim\'
PS C:\Users\tyru> $redirect = Invoke-WebRequest -URI $Env:VIM_URL
Invoke-WebRequest : 接続が切断されました: SSL/TLS のセキュリティで保護されているチャネルに対する信頼関係を確立できませ
んでした。
発生場所 行:1 文字:13
+ $redirect = Invoke-WebRequest -URI $Env:VIM_URL
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest]、WebExce
    ption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

PS C:\Users\tyru> (New-Object Net.WebClient).DownloadFile($redirect.Links[0].href, $zip)
null 配列にインデックスを付けることはできません。
発生場所 行:1 文字:1
+ (New-Object Net.WebClient).DownloadFile($redirect.Links[0].href, $zip ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) []、RuntimeException
    + FullyQualifiedErrorId : NullArray

PS C:\Users\tyru> [Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') > $null
PS C:\Users\tyru> [System.IO.Compression.ZipFile]::ExtractToDirectory($zip, $vim)
"2" 個の引数を指定して "ExtractToDirectory" を呼び出し中に例外が発生しました: "ファイル 'C:\Users\tyru\vim.zip' が見つ
かりませんでした。"
発生場所 行:1 文字:1
+ [System.IO.Compression.ZipFile]::ExtractToDirectory($zip, $vim)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FileNotFoundException

PS C:\Users\tyru> $Env:THEMIS_VIM = $vim + (Get-ChildItem $vim).Name + '\vim.exe'
Get-ChildItem : パス 'C:\Users\tyru\vim\' が存在しないため検出できません。
発生場所 行:1 文字:27
+ $Env:THEMIS_VIM = $vim + (Get-ChildItem $vim).Name + '\vim.exe'
+                           ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\tyru\vim\:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

@koron
Copy link
Member

koron commented Jul 1, 2018

  1. いまのままだとスクリプトが走るたびに毎回全部DLすることになるので、やりたくないです。

    くらいあれば許容できます。

  2. ftp とかでよく見かけるのは複数記載してあるやつですね。

  3. あとで決めれば良いでしょう。

あとソースコード的なレビュー追加しておきます。

@@ -77,10 +84,14 @@ func fetchRedirect(d redirect) (*github.Asset, error) {
return nil, nil
}

func updateRedirect(d redirect, a *github.Asset) error {
func updateRedirect(d redirect, a *github.Asset, bin io.ReadCloser) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

io.ReadCloser を渡すのではなく、この関数のなかでDLしたほうが良い。

Asset の中身だけを見て Redirect を更新するかどうかを決定し、更新時にのみ本体をDLする
という理想形に近づけておくため。

})
if err != nil {
return nil
}
return nil
}

func calcSha1Hash(r io.ReadCloser) (string, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この用途なら引数は io.Reader で十分。
ただし複数の hash アルゴリズムに渡すことを考慮するなら単に []byte で良い。
ガチなら io.TeeReader を挟むのだが…

if err != nil {
log.Printf("update failed for %s: %s", d.Path, err)
return
}
}

func fetchReleaseBinary(a *github.Asset) (io.ReadCloser, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

関数名は downloadAsset(*github.Asset) もしくは fetchURL(string) のが良いだろう。

if err != nil {
log.Printf("update failed for %s: %s", d.Path, err)
return
}
}

func fetchReleaseBinary(a *github.Asset) (io.ReadCloser, error) {
resp, err := http.DefaultClient.Get(a.DownloadURL)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

特にクライアントをいじる気がないなら http.Get() を使う。

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants