-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.php
139 lines (117 loc) · 5.46 KB
/
index.php
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
<?php
require 'vendor/autoload.php';
require 'utils.php';
require 'TorConfig.php';
date_default_timezone_set('UTC');
Flight::set('serverCount', '-');
Flight::set('combinedUptime', '-');
Flight::set('combinedBandwidth', '-');
Flight::set('nodeslist', array());
if(file_exists('misc/stats.json')) {
$stats = json_decode(file_get_contents('misc/stats.json'), true);
Flight::set('nodeslist', $stats["nodes"]);
Flight::set('serverCount', intval(count($stats["nodes"])));
Flight::set('combinedUptime', round($stats["uptime"]/60/60) . 'h');
Flight::set('combinedBandwidth', round(intval($stats["bandwidth"])*8/1000/1000) . 'Mb/s');
Flight::set('bandwidthChartJson', file_get_contents('misc/bandwidth_graph.json'));
Flight::set('nodesChartJson', file_get_contents('misc/nodes_graph.json'));
Flight::set('currentCommit', trim(exec('git log --pretty="%h" -n1 HEAD')));
}
Flight::route('GET /', function () {
Flight::render('main', array('errors' => ''), 'main_content');
Flight::render('layout', array(
'title' => 'Tor Relay Configurator',
'serverCount' => Flight::get('serverCount'),
'combinedUptime' => Flight::get('combinedUptime'),
'combinedBandwidth' => Flight::get('combinedBandwidth'),
'currentCommit' => Flight::get('currentCommit'),
'nodes' => Flight::get('nodeslist'),
'bandwidthChartData' => Flight::get('bandwidthChartJson'),
'nodesChartData' => Flight::get('nodesChartJson')
));
});
Flight::route('POST /', function () {
$req = Flight::request();
try {
$torConfig = new TorConfig($req->data);
$torrc = $torConfig->getTorrc();
$generatedScriptHash = md5(implode(',', $req->data->getData()));
$scriptFile = $generatedScriptHash.'.sh';
// Load script template and replace template variables
$template = file_get_contents('misc/install.template');
$template = str_replace('%RELEASE%', escapeshellcmd($req->data['os']), $template);
$template = str_replace('%TORRC%', $torrc, $template);
$template = str_replace('%NYX%', isset($req->data['enable-nyx']) ? 'true' : 'false', $template);
$template = str_replace('%EXIT%', ($req->data['node-type'] == 'exit') ? 'true' : 'false', $template);
$template = str_replace('%BRIDGE%', ($req->data['node-type'] == 'bridge') ? 'true' : 'false', $template);
$template = str_replace('%CHECKIPV6%', isset($req->data['ipv6']) ? 'true' : 'false', $template);
$template = str_replace('%ENABLE_AUTO_UPDATE%', isset($req->data['unattended-upgrades']) ? 'true' : 'false', $template);
$template = str_replace('%OBFS4PORT_LT_1024%', (isset($req->data['obfs4port']) && intval($req->data['obfs4port']) < 1024) ? 'true' : 'false', $template);
// Write script to file
if (!file_exists('userconfigs/')) {
mkdir('userconfigs/', 0777, true);
}
file_put_contents('userconfigs/'.$scriptFile, $template);
file_put_contents('userconfigs/'.$generatedScriptHash.'.torrc', $torrc);
Flight::redirect("/setup/" . $generatedScriptHash);
} catch (Exception $e) {
Flight::render('main', array('errors' => $e->getMessage()), 'main_content');
}
Flight::render('layout', array(
'title' => 'Tor Relay Configurator | Installation',
'serverCount' => Flight::get('serverCount'),
'combinedUptime' => Flight::get('combinedUptime'),
'combinedBandwidth' => Flight::get('combinedBandwidth'),
'currentCommit' => Flight::get('currentCommit'),
'nodes' => Flight::get('nodeslist'),
'bandwidthChartData' => Flight::get('bandwidthChartJson'),
'nodesChartData' => Flight::get('nodesChartJson')
));
});
Flight::route('GET /setup/@hash', function ($hash) {
$validateInputRegex = '/^[\w]+$/m';
if (preg_match($validateInputRegex, $hash) == 0 || !file_exists('userconfigs/' . $hash . '.torrc')) {
// invalid format
Flight::redirect('/');
return;
}
$torrc = file_get_contents('userconfigs/' . $hash . '.torrc');
Flight::render('installation', array(
'torrc' => $torrc,
'configFile' => $hash . '.sh',
), 'main_content');
Flight::render('layout', array(
'title' => 'Tor Relay Configurator | Installation',
'serverCount' => Flight::get('serverCount'),
'combinedUptime' => Flight::get('combinedUptime'),
'combinedBandwidth' => Flight::get('combinedBandwidth'),
'currentCommit' => Flight::get('currentCommit'),
'nodes' => Flight::get('nodeslist'),
'bandwidthChartData' => Flight::get('bandwidthChartJson'),
'nodesChartData' => Flight::get('nodesChartJson')
));
});
Flight::route('GET /nf/@hash.sh', function ($hash) {
// Filter out any non-alphanumeric characters
$hash = preg_replace('/[^a-zA-Z0-9]/', '', $hash);
header('Content-Type: application/x-sh');
try {
echo file_get_contents("userconfigs/$hash.sh");
} catch (Exception $e) {
echo 'echo "There was an error fetching your generated script! Please return to tor-relay.co and try again."';
}
});
Flight::route('GET /install-sudo-on-debian', function () {
Flight::render('install-sudo', array(), 'main_content');
Flight::render('layout', array(
'title' => 'Tor Relay Configurator',
'serverCount' => Flight::get('serverCount'),
'combinedUptime' => Flight::get('combinedUptime'),
'combinedBandwidth' => Flight::get('combinedBandwidth'),
'currentCommit' => Flight::get('currentCommit'),
'nodes' => Flight::get('nodeslist'),
'bandwidthChartData' => Flight::get('bandwidthChartJson'),
'nodesChartData' => Flight::get('nodesChartJson')
));
});
Flight::start();