forked from flxn/tor-relay-configurator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TorConfig.php
134 lines (109 loc) · 4.65 KB
/
TorConfig.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
<?php
class TorConfig
{
private $config = array();
function __construct($variables)
{
if ($variables['node-type'] != 'bridge' && (!$variables['orport'] || !$variables['relayname'] || !$variables['contactinfo'])) {
$missing = array();
if(!$variables['orport']) $missing[] = 'ORPort';
if(!$variables['relayname']) $missing[] = 'Relay Name';
if(!$variables['contactinfo']) $missing[] = 'Contact Info';
throw new Exception('Required fields missing (' . implode(', ', $missing) . ')');
}
if ($variables['orport'] != "auto" && !is_numeric($variables['orport'])) {
throw new Exception('ORPort must be numeric.');
}
if ($variables['dirport'] && !is_numeric($variables['dirport'])) {
throw new Exception('DirPort must be numeric.');
}
$re = '/^[a-zA-Z0-9]{1,19}$/';
if ($variables['node-type'] != 'bridge' && preg_match($re, $variables['relayname']) !== 1) {
throw new Exception('Relay names must be 1-19 characters long and may only contain numbers and letters.');
}
// Escape variables
foreach ($variables as $var) {
$var = escapeshellarg($var);
}
// Apply slight e-mail obfuscation
$variables['contactinfo'] = str_replace('@', '(at)', $variables['contactinfo']);
$variables['contactinfo'] = str_replace('.', '(dot)', $variables['contactinfo']);
if(isset($variables['trc-track'])) {
$variables['contactinfo'] .= ' [tor-relay.co]';
}
// Minimum default config for non-bridge nodes
$baseConfig = array(
"SocksPort" => 0,
"RunAsDaemon" => 1,
"ORPort" => $variables['orport'],
"Nickname" => $variables['relayname'],
"ContactInfo" => $variables['contactinfo']
);
if ($variables['node-type'] == 'bridge') {
// Special config for bridge nodes
$baseConfig = array(
"ORPort" => $variables['orport'],
"SocksPort" => 0,
"BridgeRelay" => 1,
"ServerTransportPlugin" => "obfs4 exec /usr/bin/obfs4proxy",
"ServerTransportListenAddr" => "obfs4 0.0.0.0:".$variables['obfs4port'],
"ExtOrPort" => "auto"
);
if (isset($variables['ipv6'])) {
$baseConfig["ServerTransportListenAddr"] = array($baseConfig["ServerTransportListenAddr"], "obfs4 [::]:".$variables["obfs4port"]);
}
}
$baseConfig["Log"] = "notice file /var/log/tor/notices.log";
if (isset($variables['ipv6'])) {
$baseConfig["ORPort"] = array($baseConfig["ORPort"], "[INSERT_IPV6_ADDRESS]:".$baseConfig["ORPort"]);
}
if ($variables['dirport']) {
$baseConfig["DirPort"] = $variables['dirport'];
}
if ($variables['node-type'] == 'exit') {
$baseConfig["DirPortFrontPage"] = "/etc/tor/tor-exit-notice.html";
$baseConfig["ExitPolicy"] = explode("\n", file_get_contents('misc/exitpolicy.txt'));
$baseConfig["IPv6Exit"] = isset($variables["ipv6"]) ? 1 : 0;
} else {
$baseConfig["ExitPolicy"] = (isset($variables["ipv6"]) ? "reject6 *:*, " : "")."reject *:*";
}
if ($variables['bandwidth-rate']) {
$baseConfig["RelayBandwidthRate"] = $variables['bandwidth-rate'].' MBits';
$baseConfig["RelayBandwidthBurst"] = ($variables['bandwidth-burst'] ? $variables['bandwidth-burst'] : $variables['bandwidth-rate']).' MBits';
}
if ($variables['traffic-limit']) {
$limit = $variables['traffic-limit'];
if ($variables['traffic-unit'] == 'TB') {
$limit *= 1000;
}
$baseConfig["AccountingStart"] = "month 1 00:00";
$baseConfig["AccountingMax"] = floor($limit / 2).' GB';
}
if (isset($variables['enable-nyx'])) {
$baseConfig["DisableDebuggerAttachment"] = 0;
$baseConfig["ControlPort"] = 9051;
$baseConfig["CookieAuthentication"] = 1;
}
$this -> config = $baseConfig;
}
public function getTorrc()
{
$torrcString = '';
foreach ($this -> config as $key => $value) {
if(!is_array($value) && trim($value) == "") {
continue;
}
if(is_array($value)) {
foreach ($value as $val) {
if(trim($val) == "") {
continue;
}
$torrcString .= $key . ' ' . $val . "\n";
}
} else {
$torrcString .= $key . ' ' . $value . "\n";
}
}
return $torrcString;
}
}