最近處理的一個feature,
要求透過HTTPS送出一些資料給另一台server,
並且必須驗證對方的SSL憑證。
在Client端(Python)選用pycurl這個lib,
由於另一端的API也還在開發中,
所以就自己另外寫了一個dummy server,
使用self-signed certificate來測試配對,
並且提供相對應的API來模擬互動。
以下是小範圍測試的時候使用的Script,dummy server則是用node.js隨手兜的。
步驟概述如下:
1. 做一把ssl key,如果已經有了就不用做了
2. /usr/bin/openssl req -new -key ssl.key -out server.csr -config localhost.conf
其實我跟node.js不算熟,為何dummy server會用node.js?
剛好那天早上起床,朋友問我
「node.js可以做什麼?」
想著想著進辦公室剛好又看到旁邊同事桌上正好擺著node.js的書,
就爬了一下stackoverflow兜出了這個剛好夠用的工具。
幾年前第一次探索node.js,
試過用node.js搭配redis.io, mongodb做了個聊天室server,
另一頭則是html5+jQuery ui做出一些會在畫面上走來走去的「對畫框」
(對就真的這麼無聊)
實在是很不適應callback又callback的箭頭狀code啊....
(不過現在有promise可以解決這件事了我知道....)
這幾年node.js變成顯學當初應該多看兩眼的
要求透過HTTPS送出一些資料給另一台server,
並且必須驗證對方的SSL憑證。
在Client端(Python)選用pycurl這個lib,
由於另一端的API也還在開發中,
所以就自己另外寫了一個dummy server,
使用self-signed certificate來測試配對,
並且提供相對應的API來模擬互動。
以下是小範圍測試的時候使用的Script,dummy server則是用node.js隨手兜的。
步驟概述如下:
1. 做一把ssl key,如果已經有了就不用做了
2. /usr/bin/openssl req -new -key ssl.key -out server.csr -config localhost.conf
(localhost.conf是用來省略產生csr過程中一直回答問題的步驟,詳細內容請見
http://spin.atomicobject.com/2014/05/12/openssl-commands/ )
3. /usr/bin/openssl x509 -req -days 365 -in server.csr -signkey ssl.key -out server.crt
4. /usr/bin/node http_server.js
5. openssl s_client -connect localhost:443 -showcerts
6. 把-----BEGIN CERTIFICATE----- 到 -----END CERTIFICATE----- 之間那一段貼到rootca.pem裡
7. /usr/bin/python pycurl_verify_ssl_cert.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var https = require('https'); | |
var privateKey = fs.readFileSync('sslcert/ssl.key', 'utf8'); | |
var certificate = fs.readFileSync('sslcert/server.crt', 'utf8'); | |
var credentials = {key: privateKey, cert: certificate}; | |
var httpsServer = https.createServer(credentials, function (req, res) { | |
//console.log(req); | |
if (req.method == 'POST') { | |
if(req.url == '/api_1'){ | |
var body, res_json = { | |
"status": "0", | |
"message": "OK" | |
}; | |
req.on('data', function (data) { | |
body += data; | |
}); | |
req.on('end', function () { | |
console.log(body); | |
res.writeHead(200, {'Content-Type': 'application/json'}); | |
res.end(JSON.stringify(res_json)); | |
}); | |
} | |
else if(req.url == '/api_2'){ | |
var body = '', | |
inputCount = 0, | |
res_json = { | |
'status' : "0", | |
'message' : "OK", | |
'line_count' : 0 | |
}; | |
req.on('data', function (data) { | |
body += data; | |
}); | |
req.on('end', function () { | |
//console.log("Body: " + body); | |
fs.appendFile('received_lines.txt', body + '\n', function (err) { | |
if(err) | |
console.log(err) | |
}); | |
inputCount = (body.match(/\n/g)||[]).length +1; | |
res_json['line_count'] = inputCount; | |
console.log('received ' + inputCount + ' lines.'); | |
res.writeHead(200, {'Content-Type': 'application/json'}); | |
res.end(JSON.stringify(res_json)); | |
}); | |
} | |
} | |
}).listen(443); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import StringIO | |
import subprocess | |
import sys | |
import pycurl | |
status_code = 0 | |
res = StringIO.StringIO() | |
res_message = "" | |
c = pycurl.Curl() | |
c.setopt(pycurl.URL, "https://localhost:443/api_1") | |
c.setopt(pycurl.SSL_VERIFYPEER, 1) | |
c.setopt(pycurl.SSL_VERIFYHOST, 2) | |
c.setopt(pycurl.CAINFO, "rootca.pem") | |
#with proxy | |
#c.setopt(pycurl.PROXY, 'proxyserver') | |
#c.setopt(pycurl.PROXYUSERPWD,'username:password') | |
c.setopt(pycurl.HTTPHEADER, ['Content-Type: text/plain; charset=UTF-8']) | |
c.setopt(pycurl.POST, 1) | |
c.setopt(pycurl.POSTFIELDS, "post body.....") | |
c.perform() | |
status_code = c.getinfo(pycurl.HTTP_CODE) | |
res_message = res.getvalue() | |
print status_code | |
print res_message |
剛好那天早上起床,朋友問我
「node.js可以做什麼?」
想著想著進辦公室剛好又看到旁邊同事桌上正好擺著node.js的書,
就爬了一下stackoverflow兜出了這個剛好夠用的工具。
幾年前第一次探索node.js,
試過用node.js搭配redis.io, mongodb做了個聊天室server,
另一頭則是html5+jQuery ui做出一些會在畫面上走來走去的「對畫框」
(對就真的這麼無聊)
實在是很不適應callback又callback的箭頭狀code啊....
(不過現在有promise可以解決這件事了我知道....)
這幾年node.js變成顯學當初應該多看兩眼的
留言
張貼留言