LoginSignup
49
54

More than 5 years have passed since last update.

オレオレ証明書とhttpsサーバーを3分で立ち上げる5つのステップ

Last updated at Posted at 2016-01-08

opensslとnodeがあればなんのOSでもいけるかと思います。
(Amazon Linux ,CnetOSなら下記のようにyum で)

其の一 KEY作成

乱数でキーを作る 
※random state e is e is 65537 (0x10001)とかエラーみたいのがでてもできてたらいける

openssl genrsa 2048 > mysslserver.key

其の二 CSR 作成

場所、組織名など登録 (Tokyo , Shibuya , Conpany Name , Company Dept としました)

openssl req -new -key mysslserver.key -subj "/C=JP/ST=Tokyo-to/L=Shibuya/O=Company Name/OU=IT dept./CN=Company Dept CA" > mysslserver.csr

其の三 CRT作成

日数を指定(3650日 約10年)

openssl x509 -days 3650 -req -signkey mysslserver.key < mysslserver.csr > mysslserver.crt

其の四 pfx作成、キーと証明書をパックする

openssl pkcs12 -export -inkey mysslserver.key -in mysslserver.crt > mysslserver.pfx

其の五 nodeの組み込みだけでHTTPS

ファイルを下記のように用意したらサーバーを起動する

sudo yum install -y node
node https.js &

https.js

https = require('https');
fs = require('fs');
host='127.0.0.1';
port=8443;
https.createServer({pfx: fs.readFileSync('mysslserver.pfx')}, function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(port, host);
console.log('Server running at https://' + host + ':' + port);

と、ここまでで終了

指定したポートでhttpsをたたいてみると(ブラウザでもよいです)

curl -k https://127.0.0.1:8443/
Hello World

あ、はい、こんにちわ

curl は kをつけたらオレオレでもOK
If you'd like to turn off curl's verification of the certificate, use
the -k (or --insecure) option.

hello world 以外は自分でアプリケーション書いてください。

其の六 静的サイトでいいならこちら

npm install node-static
node https-static.js &

https-static.js
https = require('https');
statics = require('node-static');
fs = require('fs');
st =  new(statics.Server)('../htdocs/');
port=8443;

https.createServer({pfx: fs.readFileSync('mysslserver.pfx')}, function (req, res) {
  st.serve(req,res);
}).listen(port);

あくまで、エラーもなにもないので、あったらページがみれるんで。

其の七 HTTPS -> HTTP に串する

も少し、サーバーぽいのがすでにたってるからバインドしたいって場合は

python -m SimpleHTTPServer &

これでhttp://127.0.0.1:8000/が立ち上がるので
(いや、普通にapacheでもいいです。。)

yum install httpd
service httpd start

てゆうか、もう、だったらnodeじゃなくていいから。。
apacheでhttpsしたらいいから。。

HTTPS -> HTTP にリダイレクトしてWEBページみれる

https://127.0.0.1:8443/
 http://127.0.0.1:8000/

npm install http-proxy
node https-proxy.js &

https-proxy.js
httpProxy = require('http-proxy');
fs = require('fs');
httpProxy.createServer({
  ssl: {
    key: fs.readFileSync('mysslserver.key', 'utf8'),
    cert: fs.readFileSync('mysslserver.crt', 'utf8')
  },
   target: {
    host: '127.0.0.1',
    port: '8000'
  }
}).listen(8443);

なにって、いまそこにいるってだけで、作業しているフォルダでサーバーをたちあげて、ちょっと確認して、ほい、みたいなときにいいでしょ!

49
54
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
49
54