summaryrefslogtreecommitdiffstats
path: root/connect.js
diff options
context:
space:
mode:
authorJannis R <mail@jannisr.de>2020-05-02 15:41:32 +0200
committerJannis R <mail@jannisr.de>2020-05-02 16:36:09 +0200
commit58d2e4c5e17b3306e35e7a8e28cce5cc4eb74f26 (patch)
tree44fd76efa940862b38442fa8926d6a0092268487 /connect.js
parent1b5af6f4bdfca939e780ae70bd6e1819425824f7 (diff)
basic client
Diffstat (limited to 'connect.js')
-rw-r--r--connect.js50
1 files changed, 50 insertions, 0 deletions
diff --git a/connect.js b/connect.js
new file mode 100644
index 0000000..35e2297
--- /dev/null
+++ b/connect.js
@@ -0,0 +1,50 @@
+'use strict'
+
+const {connect: connectTls} = require('tls')
+const {
+ DEFAULT_PORT,
+ ALPN_ID,
+ MIN_TLS_VERSION,
+} = require('./lib/util')
+
+const connectToGeminiServer = (opt, cb) => {
+ if (typeof opt === 'function') {
+ cb = opt
+ opt = {}
+ }
+ const {
+ host,
+ port,
+ tlsOpt,
+ } = {
+ host: '127.0.0.1',
+ port: DEFAULT_PORT,
+ tlsOpt: {},
+ // todo: TOFU via isTrustedCertificate()
+ ...opt,
+ }
+
+ const socket = connectTls({
+ ALPNProtocols: [ALPN_ID],
+ minVersion: MIN_TLS_VERSION,
+ host, port,
+ // todo: cert, key, passphrase
+ ...tlsOpt,
+ })
+
+ let cbCalled = false
+ socket.once('error', (err) => {
+ if (cbCalled) return;
+ cbCalled = true
+ cb(err)
+ })
+ socket.once('secureConnect', () => {
+ if (cbCalled) return;
+ cbCalled = true
+ cb(null, socket)
+ })
+
+ return socket
+}
+
+module.exports = connectToGeminiServer