1
0
mirror of https://github.com/Tygs/0bin.git synced 2023-08-10 21:13:00 +03:00

Add https support for cli tool

This commit is contained in:
Yann Bordenave 2015-04-28 17:55:41 +02:00
parent e5586121d1
commit f193dd9403

View File

@ -13,10 +13,11 @@ program
'Path to zerobin configuration file (default: ~/.zerobinpasterc).\n'\ 'Path to zerobin configuration file (default: ~/.zerobinpasterc).\n'\
+ ' Should be json-file with the same keys as can be specified on the command line.\n'\ + ' Should be json-file with the same keys as can be specified on the command line.\n'\
+ ' Example contents: {"url": "http://some-0bin.com"}', '~/.zerobinpasterc') + ' Example contents: {"url": "http://some-0bin.com"}', '~/.zerobinpasterc')
.option('-n, --nocheck', 'do not check SSL certs.')
.parse(process.argv); .parse(process.argv);
[http, url, qs, fs, path] = ['http', 'url', 'querystring', 'fs', 'path'].map(require) [http, https, url, qs, fs, path] = ['http', 'https', 'url', 'querystring', 'fs', 'path'].map(require)
# Parse config file, if any # Parse config file, if any
@ -74,8 +75,14 @@ paste_file = (content, name) ->
if not program.url.match(/^https?:\/\//) if not program.url.match(/^https?:\/\//)
program.url = 'http://' + program.url.replace(/^\/+/, '') program.url = 'http://' + program.url.replace(/^\/+/, '')
proto = http
if program.url.match(/^https:\/\//)
proto = https
req_opts = url.parse(program.url) req_opts = url.parse(program.url)
req_opts.method = 'POST' req_opts.method = 'POST'
req_opts.headers = req_opts.headers =
'Content-Type': 'application/x-www-form-urlencoded' 'Content-Type': 'application/x-www-form-urlencoded'
'Content-Length': content.length 'Content-Length': content.length
@ -84,7 +91,10 @@ paste_file = (content, name) ->
.replace(/\/paste\/create\/?$/, '').replace(/\/+$/, '') .replace(/\/paste\/create\/?$/, '').replace(/\/+$/, '')
req_opts.path = req_url_base + '/paste/create' req_opts.path = req_url_base + '/paste/create'
req = http.request req_opts, (res) -> if program.nocheck
req_opts.rejectUnauthorized = false
req = proto.request req_opts, (res) ->
req_reply = '' req_reply = ''
res.setEncoding('utf8') res.setEncoding('utf8')
res.on 'data', (chunk) -> req_reply += chunk res.on 'data', (chunk) -> req_reply += chunk
@ -102,7 +112,7 @@ paste_file = (content, name) ->
req.write(content) req.write(content)
req.end() req.end()
req.on 'error', (e) -> console.error(e)
# Seed sjcl prng from /dev/(u)random # Seed sjcl prng from /dev/(u)random
do (bytes=64) -> do (bytes=64) ->