1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# gemini
**[Gemini protocol](https://gemini.circumlunar.space) server & client.**
[](https://www.npmjs.com/package/@derhuerst/gemini)
[](https://travis-ci.org/derhuerst/gemini)


[](https://gitter.im/derhuerst)
[](https://patreon.com/derhuerst)
## Installation
```shell
npm install @derhuerst/gemini
```
## Usage
### Server
The following code assumes that you have a valid SSL certificate & key.
```js
const {createServer, DEFAULT_PORT} = require('@derhuerst/gemini')
const onError = (err) => {
console.error(err)
process.exit(1)
}
const server = createGeminiServer({
cert: …, // certificate (+ chain)
key: …, // private key
passphrase: …, // passphrase, if the key is encrypted
}, (req, res) => {
if (req.path === '/foo') {
if (!req.clientFingerprint) {
return res.requestTransientClientCert('/foo is secret!')
}
res.write('foo')
res.end('!')
} else if (req.path === '/bar') {
res.redirect('/foo')
} else {
res.gone()
}
})
server.listen(DEFAULT_PORT)
server.on('error', console.error)
```
### Client
```js
const request = require('@derhuerst/gemini/client')
request('/bar', (err, res) => {
if (err) {
console.error(err)
process.exit(1)
}
console.log(res.statusCode, res.statusMessage)
if (res.meta) console.log(res.meta)
res.pipe(process.stdout)
})
```
#### [TOFU](https://en.wikipedia.org/wiki/Trust_on_first_use)-style client certificates
> Interactive clients for human users MUST inform users that such a session has been requested and require the user to approve generation of such a certificate. Transient certificates MUST NOT be generated automatically.
– [Gemini spec](https://gemini.circumlunar.space/docs/spec-spec.txt), section 1.4.3
If is up to you how to implement that approval process. As an example, we're going to build a simple CLI prompt:
```js
const {createInterface} = require('readline')
const letUserConfirmClientCertUsage = ({host, reason}, cb) => {
const prompt = createInterface({
input: process.stdin,
output: process.stdout,
history: 0,
})
prompt.question(`Send client cert to ${host}? Server says: "${reason}". y/n > `, (confirmed) => {
prompt.close()
cb(confirmed === 'y' || confirmed === 'Y')
})
}
request('/foo', {
// opt into client certificates
useClientCerts: true,
letUserConfirmClientCertUsage,
}, cb)
```
## API
```js
const createServer = require('@derhuerst/gemini/server')
createServer(opt = {}, onRequest)
```
`opt` extends the following defaults:
```js
{
// SSL certificate & key
cert: null, key: null, passphrase: null,
// additional options to be passed into `tls.createServer`
tlsOpt: {},
}
```
---
```js
const request = require('@derhuerst/gemini/client')
request(pathOrUrl, opt = {}, cb)
```
`opt` extends the following defaults:
```js
{
// follow redirects automatically
followRedirects: false,
// client certificates
useClientCerts: false,
letUserConfirmClientCertUsage: null,
clientCertStore: defaultClientCertStore,
// additional options to be passed into `tls.connect`
tlsOpt: {},
}
```
---
```js
const connect = require('@derhuerst/gemini/connect')
connect(opt = {}, cb)
```
`opt` extends the following defaults:
```js
{
hostname: '127.0.0.1',
port: 1965,
// client certificate
cert: null, key: null, passphrase: null,
// additional options to be passed into `tls.connect`
tlsOpt: {},
}
```
## Contributing
If you have a question or need support using `gemini`, please double-check your code and setup first. If you think you have found a bug or want to propose a feature, use [the issues page](https://github.com/derhuerst/gemini/issues).
|