cgi – matoken's blog https://matoken.org/blog Is there no plan B? Sun, 10 Jul 2022 13:35:19 +0000 ja hourly 1 https://wordpress.org/?v=6.9 https://matoken.org/blog/wp-content/uploads/2025/03/cropped-1865f695c4eecc844385acef2f078255036adccd42c254580ea3844543ab56d9-32x32.jpeg cgi – matoken's blog https://matoken.org/blog 32 32 Lighttpdで全ページ503を返す https://matoken.org/blog/2022/06/28/lighttpd-returns-all-pages-503/ https://matoken.org/blog/2022/06/28/lighttpd-returns-all-pages-503/#comments Mon, 27 Jun 2022 15:15:56 +0000 http://matoken.org/blog/?p=3683

先日自宅サーバが起動しなくなってしまいました.10年以上ほぼ起動しっぱなしでしたししょうがないかなという感じも.この端末はNASやUnbound,apt-cacher-ngなど自宅向けのサービスの他にも外部向けのWebServerとしても動作させていました.
すぐに復旧するのは難しそうなのでとりあえずシングルボードコンピュータでWebServerを起動してHTTPステータスコードの503を返すようにしてみました.

利用したシングルボードコンピュータ
Raspberry Pi 3 model B (SoC arm v7 4core / Memory 1GB / OS Raspberry Pi OS bullseye armhf)
設定したいこと
2つのドメインのhttp/httpsへのアクセスにHTTPステータスコード503を返したい

前準備

すでに micro-httpd が動作しています.このアプリでやりたいことができるのかよくわからないのでこれは停止してLighttpdに変更します.

micro-httpdの停止と自動起動無効化
$ sudo systemctl stop micro-httpd
$ sudo systemctl disable micro-httpd
Note
もう使わないのであれば apt purge micro-httpd でいいと思います.

Lighttpdのインストール

Raspberry Pi OSパッケージのLighttpdを導入します.

Lighttpdのインストール
$ sudo apt install lighttpd

SSL証明書の入手

認証局には以前も利用していたLet’s Encryptを利用します.

専用の証明書管理ツールのcertbotを導入します.これもディストリビューションパッケージから導入します.

$ sudo apt install certbot

早速証明書を取得します.

$ sudo certbot certonly -d sub1.example.org (1)
Saving debug log to /var/log/letsencrypt/letsencrypt.log
How would you like to authenticate with the ACME CA?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: Spin up a temporary webserver (standalone)
2: Place files in webroot directory (webroot)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2 (2)
Plugins selected: Authenticator webroot, Installer None
Requesting a certificate for sub1.example.com
Performing the following challenges:
http-01 challenge for sub1.example.org
Input the webroot for sub1.example.org: (Enter 'c' to cancel): /var/www/html (3)
  :
  1. sub1.example.org というドメインの証明書を取得
  2. 認証にLighttpdのhttpdを利用
  3. Lighttpdのドキュメントルートを指定

同様にもう一つのドメインも設定します.

Lighttpdのhttps設定

lighttpd-enable-mod コマンドでLighttpdのsslモジュールとrewriteモジュールを有効にします.

$ sudo lighttpd-enable-mod rewrite ssl
Note
lighttpd-enable-mod は引数無しで実行すると対話形式になります.モジュールを無効にする場合は lighttpd-disable-mod コマンドが利用できます.

Lighttpdでのhttpsの設定

Lighttpdでhttpsでアクセスできるようにします.

lighttpd/conf-enabled/10-ssl.conf を編集します.

ssl.pemfile の行を以下のように書き換えてLet’s Encryptの証明書を使うようにします.

ssl.pemfile = "/etc/letsencrypt/live/sub1.example.org/fullchain.pem"
ssl.privkey = "/etc/letsencrypt/live/sub1.example.org/privkey.pem"

その下に2つ目ののドメインを設定します.

$HTTP["host"] =~ "sub2.example.org"{
        ssl.pemfile = "/etc/letsencrypt/live/sub2.example.org/fullchain.pem"
        ssl.privkey = "/etc/letsencrypt/live/sub2.example.org/privkey.pem"
}

Lighttpdを再読込して動作を確認します.

$ sudo systemctl lighttpd restart
$ w3m https://sub1.example.org/
$ w3m https://sub2.example.org/

HTTPステータスコード503を返すようにする

HTTPステータスコード503を返すだけのcgi scriptを作成していつもそれを返すようにします.

cgiを有効に

lighttpd-enable-mod コマンドでLighttpdのcgiモジュールを有効にします.

$ sudo lighttpd-enable-mod cgi

/etc/lighttpd/conf-enabled/10-cgi.conf を以下のように修正します.

$ grep -v ^# lighttpd/conf-enabled/10-cgi.conf | uniq

server.modules += ( "mod_cgi" )

cgi.assign      = (
        ".cgi" => "/usr/bin/perl",
)

Lighttpdを再読込して動作を確認します.

$ sudo systemctl lighttpd restart
$ echo "#!/usr/bin/perl
print 'Content-Type: text/plain\n\nhello world.'" > /var/www/html/hello.cgi
$ sudo chown www-data.www-data /var/www/html/hello.cgi
$ sudo chmod 700 /var/www/html/hello.cgi
$ w3m https://sub1.example.org/hello.cgi
$ sudo rm /var/www/html/hello.cgi

503を返すcgiを用意

HTTPステータスコード503を返す /var/www/html/503.cgi を用意します.

#!/usr/bin/perl

use strict;
use warnings;

print "Status: 503 Service Unavailable\n";
print "Content-Type: text/html\n\n";
print <<END_OF_HTML;
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
  <meta charset="utf-8" />
  <meta name="generator" content="pandoc" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
  <title>home.matoken.org</title>
  <style>
    code{white-space: pre-wrap;}
    span.smallcaps{font-variant: small-caps;}
    span.underline{text-decoration: underline;}
    div.column{display: inline-block; vertical-align: top; width: 50%;}
    div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
    ul.task-list{list-style: none;}
  </style>
</head>
<body>
<p>Server is down.</p>
</body>
</html>
END_OF_HTML

権限を設定して動作確認します.

$ sudo chown www-data.www-data /var/www/html/503.cgi
$ sudo chmod 700 /var/www/html/503.cgi
$ w3m -dump_head https://sub1.example.org/503.cgi | grep ^HTTP
HTTP/1.0 503 Service Unavailable
$ w3m https://sub1.example.org/503.cgi

Lighttpdですべてのアクセスに503を返すようにする

/etc/lighttpd/lighttpd.conf ファイルに以下を追記します.これですべてのURLが503.cgiになるはずです.

$HTTP["url"] != "/503.cgi$" {
   url.rewrite = ( "" => "/503.cgi" )
}

Lighttpdを再読込して動作を確認します.

$ sudo systemctl lighttpd restart
$ w3m https://sub1.example.org/hoge
$ w3m https://sub2.example.org/fuga
$ w3m http://sub1.example.org/hoge
$ w3m http://sub2.example.org/fuga
環境
$ dpkg-query -W lighttpd certbot w3m perl
certbot 1.12.0-2
lighttpd        1.4.59-1+deb11u1
perl    5.32.1-4+deb11u2
w3m     0.5.3+git20210102-6
$ lsb_release -dr
Description:    Raspbian GNU/Linux 11 (bullseye)
Release:        11
$ cat /proc/device-tree/model && echo
Raspberry Pi 3 Model B Rev 1.2
]]>
https://matoken.org/blog/2022/06/28/lighttpd-returns-all-pages-503/feed/ 2
LibreOffice Impress の HTML WebCast 書き出しで遠隔プレゼン機能を試す https://matoken.org/blog/2020/06/03/try-remote-presentation-with-html-webcast-export-in-libreoffice-impress/ https://matoken.org/blog/2020/06/03/try-remote-presentation-with-html-webcast-export-in-libreoffice-impress/#respond Wed, 03 Jun 2020 14:29:26 +0000 http://matoken.org/blog/?p=2771
add 2020-06-05

該当しそうなBug

オンラインでプレゼンテーションをするとき一般的には画面共有すると思いますが,スライドファイルを前もって配布しておいてお互いそれを見ながらプレゼンも出来ます.
その間くらいの機能でスライドのページめくりをプレゼンターと視聴者で同期するサービスが欲しいなと思っています.
以下のようなサービスがあるようですが自分が何度か試した限りでは「ログイン/新規登録」が出来ず使えていません.

LibreOffice Impress のHTML 書き出し機能にWebCast 機能を見つけたので試してみました.
しかし紹介しておいてなんですがこの機能は長らくメンテされていないようなのであまり使わないほうがいいかもしれません.(Perl cgi環境も用意しにくいだろうし)

ヘルプもざっくりとした説明しかありません.

HTML 形式で保存するプレゼンテーションを開きます。

ファイル → エクスポート を行ないます。

ファイル書式 を HTML ドキュメント (LibreOffice Impress) (.html;.htm) に設定します。

ファイル名 を入力して、エクスポート をクリックします。

HTML エクスポート ウィザードの指示に従います。

てことでメモがてら.

LibreOffice Impress で書き出したいスライドを開きます.

「ファイル」→「エクスポート」でエクスポートウィドウが開きます.

LibreOffice WebCast01

書き出し先のディレクトリ(複数のファイルが書き出されるので新しいディレクトリを用意したほうが良い)を指定し,任意のファイル名を指定します.
「ファイル形式」に「HTML ドキュメント (Impress) (.html;.htm)」を選択して「エクスポート」ボタンを押します.

LibreOffice WebCast02

「デザインの割り当て」は初回は「新規デザイン」しか選べません.「次へ」ボタンを押します.

LibreOffice WebCast03

「発表方法」に「WebCast」を選択し,右側の「WebCast」では「Perl」を選択します(ASPは未検証).テキストボックスは規定値のままで構いません.「次へ」ボタンを押します.

LibreOffice WebCast04

「名前を付けて画像を保存」(原文は Save Image As なので訳が間違っていそう)で画像形式を,「モニターの解像度」でスライドの解像度を指定します.「作成」ボタンで書き出されます.
書き出し時にHTMLデザインの保存ウィンドウが表示されますがキャンセルも出来ます.

LibreOffice WebCast05

書き出したディレクトリを見ると,スライドの画像といくつかの.htmlファイルや.plなども出力されています.これらのファイルをcgiの動くsiteにコピーします.
今はcgiの動く場所がとても限られているのでこれが一番難しいかもしれないですね.

$ ls -w 80 ./slide_dir
common.pl    img15.png  img24.png  img33.png  img42.png  img51.png  img7.png
currpic.txt  img16.png  img25.png  img34.png  img43.png  img52.png  img8.png
editpic.pl   img17.png  img26.png  img35.png  img44.png  img53.png  img9.png
img0.png     img18.png  img27.png  img36.png  img45.png  img54.png  index.html
img1.png     img19.png  img28.png  img37.png  img46.png  img55.png  picture.txt
img10.png    img2.png   img29.png  img38.png  img47.png  img56.png  poll.pl
img11.png    img20.png  img3.png   img39.png  img48.png  img57.png  savepic.pl
img12.png    img21.png  img30.png  img4.png   img49.png  img58.png  show.pl
img13.png    img22.png  img31.png  img40.png  img5.png   img59.png  slide.html
img14.png    img23.png  img32.png  img41.png  img50.png  img6.png   webcast.pl

今回は自宅の適当なApache httpdの動いている環境にコピーしました.

$ scp -r ./slide_dir user@host:~/public_html/cgi-bin/

この環境は .cgi しかcgiとして動かないので,.htaccess を作成して .pl もcgiとして動くようにしました.

$ echo 'AddHandler cgi-script .pl' > ./.htaccess
$ cat ./.htaccess
AddHandler cgi-script .pl

次に *.pl ファイルに実行権を付与します.httpdのユーザが実行できる権限にします.

$ chmod o+x ./*.pl

次にページ管理ファイルの currpic.txt に読み書き権を付与します.httpdのユーザが読み書きできる権限にします.

$ chmod o+rw ./currpic.txt

これは恐らくサーバの環境依存でやらなくても動く環境のほうが多いと思うので一旦スキップして動かなかったら設定してください.editpic.pl, editpic.pl, savepic.pl, show.pl ファイルの require "common.pl"; 行を require "./common.pl"; に書き換えます.

- require "common.pl";
+ require "./common.pl";

この状態でウェブブラウザで視聴者は index.html を,プレゼンターは slide.html (html書き出し時のファイル名)を開けばOKなはずです.

以下今回試したサンプルです.多分すぐに消します.

仕組みとしてはプレゼンターがページめくり操作をすると,ページ番号の保存されている currpic.txt ファイルにページ番号を格納.
視聴者側のブラウザでは1秒毎に cgi を呼び出していて,呼び出された cgi 側では現在のページ番号が保存されている currpic.txt の内容から該当のページを表示する.といったことをしているようです.
そのためプレゼンターが操作してから実際に視聴者のページが更新されるまで数秒掛かる感じです.

中を見ると解りますがライブラリも同梱の小さな独自ライブラリ1つだけで行数も少なくとてもプリミティブです.これは誰も使わず埋もれて何年も放置されていそうです.
出来ることなら最近のギジュとで書き直されてほしいけどこれまでの放置っぷりから難しそうですね.

とりあえずはWebに公開して口頭でページめくりを指示するのが現実的そうです.
(それか普通に画面共有)

LibreOffice環境
$ dpkg-query -W libreoffice
libreoffice     1:6.4.4-1
$ lsb_release -dr
Description:    Debian GNU/Linux bullseye/sid
Release:        unstable
$ uname -m
x86_64
WebServer環境
$ dpkg-query -W apache2 perl
apache2 2.4.41-4ubuntu3
perl    5.30.0-9build1
$ lsb_release -dr
Description:    Ubuntu 20.04 LTS
Release:        20.04
$ uname -m
x86_64

]]>
https://matoken.org/blog/2020/06/03/try-remote-presentation-with-html-webcast-export-in-libreoffice-impress/feed/ 0