Discord dispatch 出現 “thread ‘main’ panicked at ‘called `Option::unwrap()` on a `None` value’, src/libcore/option.rs:347:21” 解法

心得

站長最近需要用到 Discord dispatch 因此使用 Docker 跑 Ubuntu 18.04 寫了以下的腳本

Ubuntu 18.04 與 Discord dispatch Dockerfile

FROM ubuntu:18.04

RUN  apt-get install -y wget \
  && rm -rf /var/lib/apt/lists/* \
  && wget -O /bin/dispatch https://dl-dispatch.discordapp.net/download/linux \
  && chmod +x /bin/dispatch

主要是下載 Discord dispatch 並且把它設定成可以直接呼叫的指令,不過使用後遇到以下錯誤

dispatch 指令錯誤

dispatch v1.0.0-rc3-d42fde1
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/libcore/option.rs:347:21
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

這時候在參數加上 RUST_BACKTRACE=1 看了一下噴出來的訊息

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/libcore/option.rs:347:21
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at ./cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.29/src/backtrace/libunwind.rs:88
   1: backtrace::backtrace::trace_unsynchronized
             at ./cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.29/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:47
   3: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:36
   4: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:198
   5: std::panicking::default_hook
             at src/libstd/panicking.rs:212
   6: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:475
   7: std::panicking::continue_panic_fmt
             at src/libstd/panicking.rs:382
   8: rust_begin_unwind
             at src/libstd/panicking.rs:309
   9: core::panicking::panic_fmt
             at src/libcore/panicking.rs:85
  10: core::panicking::panic
             at src/libcore/panicking.rs:49
  11: openssl::ssl::connector::ctx
  12: openssl::ssl::connector::SslConnectorBuilder::new
  13: native_tls::TlsConnector::builder
  14: reqwest::async_impl::client::ClientBuilder::new
  15: reqwest::client::ClientBuilder::new
  16: dispatch::credentials::make_client
  17: dispatch::credentials::create
  18: dispatch_cli::cmd::login::exec
  19: dispatch_cli::cmd::exec
  20: dispatch::main
  21: std::rt::lang_start::{{closure}}
  22: std::rt::lang_start_internal::{{closure}}
             at src/libstd/rt.rs:49
  23: std::panicking::try::do_call
             at src/libstd/panicking.rs:294
  24: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:85
  25: std::panicking::try
             at src/libstd/panicking.rs:273
  26: std::panic::catch_unwind
             at src/libstd/panic.rs:388
  27: std::rt::lang_start_internal
             at src/libstd/rt.rs:48
  28: main
  29: __libc_start_main
  30: _start
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

爬了一下文翻到了 issueissue,簡單來說問題出在目前系統的 OpenSSL 版本是 1.1.1,沒辦法使用需要降版到 1.1.0,這時候我就去看了一下系統的 OpenSSL 版本

openssl version -a
OpenSSL 1.1.1  11 Sep 2018
built on: Tue Nov 12 16:58:35 2019 UTC

看起來真的只能降版了,所以我就把原本的 Dockerfile 做一點更新讓它可以使用 1.1.0

編譯 OpenSSL 1.1.0

apt install -y build-essential checkinstall zlib1g-dev curl #安裝必要套件
curl https://www.openssl.org/source/openssl-1.1.0l.tar.gz -o openssl-1.1.0l.tar.gz #下載 openssl
sha256sum openssl-1.1.0l.tar.gz | grep 74a2f756c64fd7386a29184dc0344f4831192d61dc2481a93a4c5dd727f41148 #檢查 checksum
tar -xf openssl-1.1.0l.tar.gz #解壓縮 openssl
./config --prefix=/usr/local/ssl --openssldir=/usr/local/ssl shared zlib && \
  make && \
  make test && \
  make install #編譯與跑測試
echo "/usr/local/ssl/lib" > openssl-${VERSION}.conf && \
  ldconfig -v #設定索引路徑

以上對於 Dockerfile 的步驟做一點解釋

結論

此篇主要簡單介紹 OpenSSL 編譯與找出 Discord dispatch 的問題,如果只是需要使用 Discord dispatch 的朋友可以直接使用我的 docker hub 下載回去使用就可以了喔!

docker run -ti clarencetw/discord-dispatch:latest

將 dispatch 設定檔放入對應位置,記得修改 application_idtoken

root:~# mkdir /root/.dispatch
root:~# echo "{\"BotCredentials\":{\"application_id\":\"111111111111111111\",\"token\":\"111111111111111111\"}}" > /root/.dispatch/credentials.json

測試 dispatch login

root:~# dispatch login
dispatch v1.0.0-rc3-d42fde1
[2020-00-00][00:00:00][INFO] Already logged in

如果有問題歡迎在下面留言!

參考資料