-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
76 lines (64 loc) · 1.96 KB
/
server.js
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
const REPOS_PATH = 'tmp/repos';
const pushover = require('pushover');
const repos = pushover(REPOS_PATH);
const http = require('http');
const exec = require('child_process').exec;
const fs = require('fs');
const path = require('path');
var key_filename = '/root/.ssh/id_rsa';
var locked = {};
function lock(key, real_action, continue_action) {
function remove_lock(result) {
if(!locked[key]) return;
locked[key].map(function(cb){try{cb(result)}catch(e){console.error(e)}});
delete locked[key];
}
if(!locked[key]) {
setTimeout(function() { remove_lock(null); }, 10000);
locked[key] = [continue_action];
real_action(remove_lock)
} else {
locked[key].push(continue_action);
}
}
if (fs.existsSync(key_filename)) {
console.log('The file with a private key already exists.');
} else {
var dirname = path.dirname(key_filename);
if (!fs.existsSync(dirname)) {
exec('mkdir -p ' + dirname +
'; echo "$PRIVATE_KEY" > ' + key_filename +
'; (echo "Host *"; echo "StrictHostKeyChecking no") > ' + dirname + '/config' +
'; chmod 600 ' + key_filename +
'; chmod 400 ' + dirname + '/config' +
'; chmod 700 ' + dirname
);
}
}
repos.on('info', function(info) {
lock(
info.repo,
function(cb) {
exec(info.exists
? 'cd ' + info.cwd + '; git fetch --all -p'
: 'git clone --mirror ' + info.repo + ' ' + info.cwd,
cb)
},
function(err) {
if (!err) {
info.accept();
} else {
info.reject();
}
}
)
});
var server = http.createServer(function (req, res) {
if(req.url.match(/\.git$/)) {
res.statusCode = 200;
res.end('ok');
} else {
repos.handle(req, res);
}
});
server.listen(process.env.PORT || 8080);