Skip to content

Commit d7eba67

Browse files
authored
adds https support (#35)
* adds https support * ensures options is set * updates readme and adds example for https * fixes lint * documents git ssl override
1 parent f8aba26 commit d7eba67

File tree

6 files changed

+92
-11
lines changed

6 files changed

+92
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Unreleased
2+
3+
- adds https support
4+
15
# 0.4.1 (12/04/2017)
26

37
- fixes type to be the same as the event names

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ npm install node-git-server
2121
# Usage
2222

2323
```javascript
24+
const path = require('path');
2425
const Server = require('node-git-server');
26+
2527
const repos = new Server(path.resolve(__dirname, 'tmp'), {
2628
autoCreate: true,
2729
authenticate: (type, repo, user, next) => {
@@ -38,14 +40,12 @@ const repos = new Server(path.resolve(__dirname, 'tmp'), {
3840
const port = process.env.PORT || 7005;
3941

4042
repos.on('push', (push) => {
41-
console.log('push ' + push.repo + '/' + push.commit
42-
+ ' (' + push.branch + ')'
43-
);
43+
console.log(`push ${push.repo}/${push.commit} (${push.branch})`);
4444
push.accept();
4545
});
4646

4747
repos.on('fetch', (fetch) => {
48-
console.log('fetch ' + fetch.commit);
48+
console.log(`fetch ${fetch.commit}`);
4949
fetch.accept();
5050
});
5151

@@ -73,6 +73,22 @@ To http://localhost:7005/beep
7373
* [new branch] master -> master
7474
```
7575

76+
## Example
77+
78+
Running the following command will start up a simple http server:
79+
80+
```
81+
node example/index.js
82+
```
83+
84+
If you want to try using https run the following
85+
86+
```
87+
node example/index.js --https
88+
```
89+
90+
> When running https with self-signed certs there are two ways to override the git-clients behavior using `git config http.sslVerify false` or `git config --global http.sslCAInfo /path/to/cert.pem`
91+
7692
For more information please visit the [docs](http://www.gabrielcsapo.com/node-git-server/code/index.html)
7793

7894
# Philosophy

example/certificate.pem

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIB0TCCAToCCQCvzaaNTZEFTDANBgkqhkiG9w0BAQUFADAtMQswCQYDVQQGEwJV
3+
UzELMAkGA1UECAwCQ0ExETAPBgNVBAcMCFNhbiBKb3NlMB4XDTE3MTIwNzE5MTI0
4+
NloXDTE4MDEwNjE5MTI0NlowLTELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNBMREw
5+
DwYDVQQHDAhTYW4gSm9zZTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAplVm
6+
UrhFbgb+g7gHDjfdTZUwqNnJ6AajytmqmzV7ceTeXKtKfNAN7i2DyFxf/IwQWoxs
7+
0g7MOE8UP19GCInK0I5T9svvbtSVEfxrtGLMBY7qnojN7Q+DIjdvM7f/m5W8oNkQ
8+
qb3EzkBKX6A5HobxKqTdu40IBLBkxa0faEmU84MCAwEAATANBgkqhkiG9w0BAQUF
9+
AAOBgQAzN5VrWE0bFT/UZgUCHrR6h+EQRfGCybAq6MmS1hEoHkLyLPIjokcRWxFe
10+
eufVyxesJaUiO6f3W34J2NoZK4NsEF931t/n12bB8Ku8H1tokNsyWqJns2Whj+bR
11+
012osVh6ghdOvKD0yrnv6oLjMU51A8UKuxh51xoMThU0vBwvDg==
12+
-----END CERTIFICATE-----

example/index.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
// You Can Use The Commands Below To Generate A Self Signed Certificate For Use With This Tutorial
2+
// These Commands Require That You have 'openssl' installed on your system
3+
// openssl genrsa -out privatekey.pem 1024
4+
// openssl req -new -key privatekey.pem -out certrequest.csr
5+
// openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem
6+
7+
let type = 'http';
8+
9+
process.argv.slice(2).forEach((arg) => {
10+
switch(arg) {
11+
case 'https':
12+
case '--https':
13+
type = 'https';
14+
break;
15+
}
16+
});
17+
18+
const fs = require('fs');
119
const path = require('path');
220

321
const Server = require('../');
@@ -28,14 +46,18 @@ repos.on('push', (push) => {
2846
});
2947

3048
repos.on('fetch', (fetch) => {
31-
console.log('username', fetch.username); // eslint-disable-line
32-
console.log('fetch ' + fetch.repo + '/' + fetch.commit); // eslint-disable-line
49+
console.log(`username ${fetch.username}`); // eslint-disable-line
50+
console.log(`fetch ${fetch.repo}/${fetch.commit}`); // eslint-disable-line
3351
fetch.accept();
3452
});
3553

36-
repos.listen(port, (error) => {
54+
repos.listen(port, {
55+
type,
56+
key: fs.readFileSync('./privatekey.pem'),
57+
cert: fs.readFileSync('./certificate.pem')
58+
}, (error) => {
3759
if(error) return console.error(`failed to start git-server because of error ${error}`); // eslint-disable-line
38-
console.log(`node-git-server running at http://localhost:${port}`); // eslint-disable-line
60+
console.log(`node-git-server running at ${type}://localhost:${port}`); // eslint-disable-line
3961
repos.list((err, result) => {
4062
if (!result) {
4163
console.log("No repositories available..."); // eslint-disable-line

example/privatekey.pem

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-----BEGIN RSA PRIVATE KEY-----
2+
MIICWwIBAAKBgQCmVWZSuEVuBv6DuAcON91NlTCo2cnoBqPK2aqbNXtx5N5cq0p8
3+
0A3uLYPIXF/8jBBajGzSDsw4TxQ/X0YIicrQjlP2y+9u1JUR/Gu0YswFjuqeiM3t
4+
D4MiN28zt/+blbyg2RCpvcTOQEpfoDkehvEqpN27jQgEsGTFrR9oSZTzgwIDAQAB
5+
AoGAb3gr6qOzY9ksF/nsQIsPtD6XLZFGzkgk3Hyi6QEeiWVn35KriJmlvEikWFIP
6+
wZ/cFdKl2uAv3EyitRWUSYSOdcD+tri253WkwpKr8qEq3MKdEsQGZlPiO2MJpWsa
7+
4vsy1bleUxqbB2TYIIXdjgD8TpTCh8sc8pVxEWEuEThxIIECQQDY9TSZkZHqsqpC
8+
bK/VUpluj3coZJcczk64ZAdkWYlXfNpvgdT3ViPjpdEYeabzTP0xI7OHkx6fVLiS
9+
87Y6EwWfAkEAxEQKhgj+saudBzl2EUQW5qntdJh3Fr+OLErSHb4M5E/nWvxW6il0
10+
XWRE3b7KrikThPkwUtXPTBNGiZ7D1dvfnQJAHMLk5jbWETb+OzANX0pD7NQ4B7LO
11+
FZOD/A3GrRbxjhePHZkokmFpAJTK02PNLhPWvNzuv9pRBO5GSbTlQ22iIQJAQiEy
12+
8oqhVrgeRsrjr1mj5cCn07tzlOSiQOZM+dyJd3w81flkR64EGVupoJWisSACBbH4
13+
yFBmcpmkEMa/8ZUOOQJAfAqyF5aCRh4MbPT7pCGSf0gckc3p6qISb1Zodh7GLSdb
14+
f/VvQzhRG3MKnSl+ZQ+GQT+F+FeZl0/ZPjVRYG/Avw==
15+
-----END RSA PRIVATE KEY-----

lib/git.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fs = require('fs');
22
const path = require('path');
33
const http = require('http');
4+
const https = require('https');
45
const url = require('url');
56
const qs = require('querystring');
67
const httpDuplex = require('./http-duplex');
@@ -427,13 +428,24 @@ class Git extends EventEmitter {
427428
* @method listen
428429
* @memberof Git
429430
* @param {Number} port - the port to start the server on
431+
* @param {Object=} options - the options to add extended functionality to the server
432+
* @param {String=} options.type - this is either https or http (the default is http)
433+
* @param {Buffer|String=} options.key - the key file for the https server
434+
* @param {Buffer|String=} options.cert - the cert file for the https server
430435
* @param {Function} callback - the function to call when server is started or error has occured
431436
*/
432-
listen(port, callback) {
433-
var self = this;
434-
this.server = http.createServer(function(req, res) {
437+
listen(port, options, callback) {
438+
const self = this;
439+
if(typeof options == 'function' || !options) {
440+
callback = options;
441+
options = { type: 'http' };
442+
}
443+
const createServer = options.type == 'http' ? http.createServer : https.createServer.bind(this, options);
444+
445+
this.server = createServer(function(req, res) {
435446
self.handle(req, res);
436447
});
448+
437449
this.server.listen(port, callback);
438450
}
439451
/**

0 commit comments

Comments
 (0)