ConoHaWING+Node.jsでSSLローカルサーバー1
前回、Nodeでローカルサーバーを構築しました。

今回は、SSLを使ってセキュアなローカルサーバーにして見たいと思います。
mkcertをインストール
ConoHaWINGでは、Opensslがインストールされていますが、少し簡単に出来る「mkcert」を使います。
FiloSottile/mkcert
①mkcertよりダウンロード
「mkcert-v~~amd64」⇨「mkcert」にリネーム
FTPかファイルマネージャーで「/home/username/bin」内にアップロード
②または、
SSHでConoHaにログインして、wgetでダウンロード
「/home/username/bin」内に移動+リネーム
ssh a11223344@host111.conoha.ne.jp -p 8022 -i C:\ssh\key.pem wget https://github.com/FiloSottile/mkcert/releases/download/v1.4.3/mkcert-v1.4.3-linux-amd64 mv mkcert-v1.4.3-linux-amd64 /home/username/bin/mkcert
証明書と鍵の作成
証明書と鍵を作る時に指定するホスト名は、「localhost」「127.0.0.1」の他にSSHの時にも使っているホスト名「host111.conoha.ne.jp(各自違う)」やIPアドレス「111.22.333.44(各自違う)」を指定出来ます。IPアドレスの確認は、DNSの設定で「A @または、A WWW」に記載されているアドレスが使えます。
また、複数指定出来ます。今回は4つ指定して(勿論1つで良い)出力ファイルの名前も指定しています。
「yourdomain」「host111.conoha.ne.jp」「111.22.333.44」は各自変更
cd public_html/yourdomain mkcert -key-file key.pem -cert-file cert.pem localhost 127.0.0.1 host111.conoha.ne.jp 111.22.333.44
mkcert -help
Usage of mkcert:$ mkcert -install
Install the local CA in the system trust store.$ mkcert example.org
Generate “example.org.pem” and “example.org-key.pem”.$ mkcert example.com myapp.dev localhost 127.0.0.1 ::1
Generate “example.com+4.pem” and “example.com+4-key.pem”.$ mkcert “*.example.it”
Generate “_wildcard.example.it.pem” and “_wildcard.example.it-key.pem”.$ mkcert -uninstall
Uninstall the local CA (but do not delete it).Advanced options:-cert-file FILE, -key-file FILE, -p12-file FILE
Customize the output paths.-client
Generate a certificate for client authentication.-ecdsa
Generate a certificate with an ECDSA key.-pkcs12
Generate a “.p12” PKCS #12 file, also know as a “.pfx” file,
containing certificate and key for legacy applications.-csr CSR
Generate a certificate based on the supplied CSR. Conflicts with
all other flags and arguments except -install and -cert-file.-CAROOT
Print the CA certificate and key storage location.$CAROOT (environment variable)
Set the CA certificate and key storage location. (This allows
maintaining multiple local CAs in parallel.)$TRUST_STORES (environment variable)
A comma-separated list of trust stores to install the local
root CA into. Options are: “system”, “java” and “nss” (includes
Firefox). Autodetected by default.
「-install」は、ConoHaではエラーが出るので不要です。
mkcert -install Warning: "sudo" is not available, and mkcert is not running as root. The (un)install operation might fail. ERROR: failed to execute "tee": exit status 1 tee: /etc/pki/ca-trust/source/anchors/mkcert_development_CA_181634411653955166408909910633601461665.pem: Permission denied
セキュアサーバーを建てる
/public_html/yourdomain/server2.js
const https = require('https'); const fs = require('fs'); const hostname = 'localhost'; const port = 8443; const server = https.createServer({ key: fs.readFileSync("./key.pem"), cert: fs.readFileSync("./cert.pem"), }, (req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/html'); res.end('<html><head><title>CGI Test</title></head><body><h1>Node.js わーるど2!</h1></body></html>'); }); server.listen(port, hostname, () => { console.log(`Server running at https://${hostname}:${port}/`); });
クライアント(フロントCGI)PHP作成
/public_html/yourdomain/client2.php
<?php $url = "https://localhost:8443/"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_CAINFO, './cert.pem'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $res = curl_exec($ch); curl_close($ch);
CURLOPT_SSL_VERIFYPEERは、trueを指定。
CURLOPT_CAINFOは.pem以外でも使え「key」は必要ないようです。
ConoHaWING+Node.jsでSSLローカルサーバー2
npmでもmkcertが有るのでこちらも使って見ます。
npmでmkcertをインストール2
npm install -g mkcert
証明書と鍵の作成2
「yourdomain」は各自変更
–domains 以外は、デフォルト設定のままでもOKです。
cd public_html/yourdomain
mkcert create-ca
mkcert create-cert --domains "localhost,127.0.0.1"
「ca.key」「ca.crt」「cert.key」「cert.crt」が出来ます。
mkcert create-ca --help Usage: create-ca [options] Options: --organization [value] Organization name (default: "Test CA") --country-code [value] Country code (default: "US") --state [value] State name (default: "California") --locality [value] Locality address (default: "San Francisco") --validity [days] Validity in days (default: 365) --key [file] Output key (default: "ca.key") --cert [file] Output certificate (default: "ca.crt") -h, --help output usage information
mkcert create-cert --help Usage: create-cert [options] Options: --ca-key [file] CA private key (default: "ca.key") --ca-cert [file] CA certificate (default: "ca.crt") --validity [days] Validity in days (default: 365) --key [file] Output key (default: "cert.key") --cert [file] Output certificate (default: "cert.crt") --domains [values] Comma separated list of domains/ip addresses (default: "localhost,127.0.0.1") -h, --help output usage information
セキュアサーバーを建てる2
/public_html/yourdomain/server3.js
const https = require('https'); const fs = require('fs'); const hostname = '127.0.0.1'; const port = 8443; const server = https.createServer({ key: fs.readFileSync("./cert.key"), cert: fs.readFileSync("./cert.crt"), }, (req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/html'); res.end('<html><head><title>CGI Test</title></head><body><h1>Node.js わーるど2!</h1></body></html>'); }); server.listen(port, hostname, () => { console.log(`Server running at https://${hostname}:${port}/`); });
クライアント(フロントCGI)node.js作成
/public_html/yourdomain/client.jss
a11223344は各ユーザー名になります。
1行目(シェバン)はnodeをインストールした場所になります。nodeのインストールはこちらをご覧下さい。
process.envは環境変数です。
#!/home/a11223344/bin/node/v16.14.2/bin/node const https = require('https'); const fs = require('fs'); const env = process.env; const options = { hostname: 'localhost', port: 8443, path: '/', method: 'GET', ca: fs.readFileSync('./ca.crt') }; console.log('Content-type: text/html; charset=utf-8\n'); try{ const req = https.request(options, (res) => { console.log('<h1>Node.js https</h1>'); res.on('data', (d) => { process.stdout.write(d); }); res.on('end', () => { console.log("test"); }); }); req.on('error', (e) => { console.error(e); }); req.end(); }catch(e){ console.log('<h1>エラー</h1>'); } console.log(env.REQUEST_URI); console.log('<h1>テスト</h1>');
クライアント(フロントCGI)PHP作成2
/public_html/yourdomain/client3.php
<?php $url = "https://127.0.0.1:8443/"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_CAINFO, './ca.crt'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $res = curl_exec($ch); curl_close($ch);