00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "config.h"
00018 #include "ServerSettings.h"
00019 #include "TorSettings.h"
00020 #ifdef USE_MINIUPNPC
00021 #include "UPNPControl.h"
00022 #endif
00023
00024 #include "net.h"
00025 #include "stringutil.h"
00026
00027 #include <QHostInfo>
00028
00029
00030 #define VALID_NICKNAME_CHARS \
00031 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
00032
00033 #define MAX_NICKNAME_LEN 19
00034
00035
00036 #define SETTING_ENABLED "Enabled"
00037 #define SETTING_DIRMIRROR "DirectoryMirror"
00038 #define SETTING_NICKNAME "Nickname"
00039 #define SETTING_ORPORT "ORPort"
00040 #define SETTING_DIRPORT "DirPort"
00041 #define SETTING_CONTACT "ContactInfo"
00042 #define SETTING_EXITPOLICY "ExitPolicy"
00043 #define SETTING_BANDWIDTH_RATE "BandwidthRate"
00044 #define SETTING_BANDWIDTH_BURST "BandwidthBurst"
00045 #define SETTING_BRIDGE_RELAY "BridgeRelay"
00046 #define SETTING_ENABLE_UPNP "EnableUPnP"
00047 #define SETTING_RELAY_BANDWIDTH_RATE "RelayBandwidthRate"
00048 #define SETTING_RELAY_BANDWIDTH_BURST "RelayBandwidthBurst"
00049 #define SETTING_PUBLISH_SERVER_DESCRIPTOR "PublishServerDescriptor"
00050
00051
00052
00053
00054
00055
00056 ServerSettings::ServerSettings(TorControl *torControl)
00057 : AbstractTorSettings("Server", torControl)
00058 {
00059 setDefault(SETTING_ENABLED, false);
00060 setDefault(SETTING_DIRMIRROR, true);
00061 #if defined(Q_OS_WIN32)
00062 setDefault(SETTING_ORPORT, 443);
00063 #else
00064 setDefault(SETTING_ORPORT, 9001);
00065 #endif
00066 setDefault(SETTING_DIRPORT, 9030);
00067 setDefault(SETTING_NICKNAME, "Unnamed");
00068 setDefault(SETTING_CONTACT, "<you@example.com>");
00069 setDefault(SETTING_BANDWIDTH_RATE, 5242880);
00070 setDefault(SETTING_RELAY_BANDWIDTH_RATE, 5242880);
00071 setDefault(SETTING_BANDWIDTH_BURST, 10485760);
00072 setDefault(SETTING_RELAY_BANDWIDTH_BURST, 10485760);
00073 setDefault(SETTING_EXITPOLICY,
00074 ExitPolicy(ExitPolicy::Default).toString());
00075 setDefault(SETTING_ENABLE_UPNP, false);
00076 setDefault(SETTING_BRIDGE_RELAY, false);
00077 setDefault(SETTING_PUBLISH_SERVER_DESCRIPTOR, "1");
00078 }
00079
00080
00081
00082 QHash<QString, QString>
00083 ServerSettings::confValues()
00084 {
00085 QHash<QString, QString> conf;
00086 quint32 torVersion = torControl()->getTorVersion();
00087
00088
00089 conf.insert(SETTING_NICKNAME,
00090 (isServerEnabled() ? localValue(SETTING_NICKNAME).toString()
00091 : ""));
00092
00093 conf.insert(SETTING_ORPORT,
00094 (isServerEnabled() ? localValue(SETTING_ORPORT).toString()
00095 : "0"));
00096
00097 conf.insert(SETTING_DIRPORT,
00098 (isDirectoryMirror() ? localValue(SETTING_DIRPORT).toString()
00099 : "0"));
00100
00101 conf.insert(SETTING_EXITPOLICY,
00102 (isBridgeEnabled() ? "reject *:*"
00103 : localValue(SETTING_EXITPOLICY).toString()));
00104
00105
00106 conf.insert((torVersion >= 0x020001 ? SETTING_RELAY_BANDWIDTH_RATE
00107 : SETTING_BANDWIDTH_RATE),
00108 QString::number(localValue(SETTING_BANDWIDTH_RATE).toUInt()) + " bytes");
00109 conf.insert((torVersion >= 0x020001 ? SETTING_RELAY_BANDWIDTH_BURST
00110 : SETTING_BANDWIDTH_BURST),
00111 QString::number(localValue(SETTING_BANDWIDTH_BURST).toUInt()) + " bytes");
00112
00113
00114 QString contact =
00115 localValue(SETTING_CONTACT).toString().trimmed();
00116 QString defaultContact = defaultValue(SETTING_CONTACT).toString();
00117 if ((contact == defaultContact) ||
00118 (contact == scrub_email_addr(defaultContact))) {
00119
00120 contact = "";
00121 }
00122 conf.insert(SETTING_CONTACT, scrub_email_addr(contact));
00123
00124
00125 if (isBridgeEnabled()) {
00126 conf.insert(SETTING_BRIDGE_RELAY, "1");
00127 conf.insert(SETTING_PUBLISH_SERVER_DESCRIPTOR,
00128 publishServerDescriptor() ? "1" : "0");
00129 } else {
00130 conf.insert(SETTING_BRIDGE_RELAY, "0");
00131 conf.insert(SETTING_PUBLISH_SERVER_DESCRIPTOR, "1");
00132 }
00133 return conf;
00134 }
00135
00136
00137
00138
00139 bool
00140 ServerSettings::apply(QString *errmsg)
00141 {
00142 bool rc;
00143
00144 configurePortForwarding();
00145
00146 if (isServerEnabled()) {
00147 rc = torControl()->setConf(confValues(), errmsg);
00148 } else {
00149 QStringList resetKeys;
00150 quint32 torVersion = torControl()->getTorVersion();
00151 resetKeys << SETTING_ORPORT
00152 << SETTING_NICKNAME
00153 << SETTING_DIRPORT
00154 << SETTING_CONTACT
00155 << SETTING_EXITPOLICY
00156 << SETTING_BRIDGE_RELAY
00157 << SETTING_PUBLISH_SERVER_DESCRIPTOR;
00158 if (torVersion >= 0x020001) {
00159 resetKeys << SETTING_RELAY_BANDWIDTH_RATE
00160 << SETTING_RELAY_BANDWIDTH_BURST;
00161 } else {
00162 resetKeys << SETTING_BANDWIDTH_RATE
00163 << SETTING_BANDWIDTH_BURST;
00164 }
00165 rc = torControl()->resetConf(resetKeys, errmsg);
00166 }
00167 return rc;
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177 void
00178 ServerSettings::configurePortForwarding()
00179 {
00180 #ifdef USE_MINIUPNPC
00181 quint16 ORPort, DirPort;
00182
00183
00184 if (!isUpnpEnabled())
00185 return;
00186
00187 ORPort = getORPort();
00188 if (!isServerEnabled())
00189 ORPort = 0;
00190
00191 DirPort = getDirPort();
00192 if (!isServerEnabled() || !isDirectoryMirror())
00193 DirPort = 0;
00194
00195 UPNPControl *control = UPNPControl::instance();
00196 control->setDesiredState(DirPort, ORPort);
00197 #endif
00198 }
00199
00200 void
00201 ServerSettings::cleanupPortForwarding()
00202 {
00203 #ifdef USE_MINIUPNPC
00204 UPNPControl::cleanup();
00205 #endif
00206 }
00207
00208
00209
00210
00211 QVariant
00212 ServerSettings::torValue(const QString &key) const
00213 {
00214 if (torControl()->getTorVersion() >= 0x020001) {
00215 if (key == SETTING_BANDWIDTH_RATE)
00216 return AbstractTorSettings::torValue(SETTING_RELAY_BANDWIDTH_RATE);
00217 else if (key == SETTING_BANDWIDTH_BURST)
00218 return AbstractTorSettings::torValue(SETTING_RELAY_BANDWIDTH_BURST);
00219 }
00220 return AbstractTorSettings::torValue(key);
00221 }
00222
00223
00224
00225
00226 void
00227 ServerSettings::setServerEnabled(bool enable)
00228 {
00229 setValue(SETTING_ENABLED, enable);
00230 }
00231
00232
00233
00234
00235 bool
00236 ServerSettings::isServerEnabled()
00237 {
00238 QString orPort;
00239 if (torControl()->isConnected() && !changedSinceLastApply()) {
00240 if (torControl()->getConf(SETTING_ORPORT, orPort))
00241 return (orPort.toUInt() > 0);
00242 }
00243 return localValue(SETTING_ENABLED).toBool();
00244 }
00245
00246
00247
00248 void
00249 ServerSettings::setBridgeEnabled(bool enabled)
00250 {
00251 setValue(SETTING_BRIDGE_RELAY, enabled);
00252 }
00253
00254
00255 bool
00256 ServerSettings::isBridgeEnabled()
00257 {
00258 return value(SETTING_BRIDGE_RELAY).toBool() && isServerEnabled();
00259 }
00260
00261
00262 void
00263 ServerSettings::setORPort(quint16 orPort)
00264 {
00265 setValue(SETTING_ORPORT, orPort);
00266 }
00267
00268
00269 quint16
00270 ServerSettings::getORPort()
00271 {
00272 return (quint16)value(SETTING_ORPORT).toUInt();
00273 }
00274
00275
00276 void
00277 ServerSettings::setDirPort(quint16 dirPort)
00278 {
00279 setValue(SETTING_DIRPORT, dirPort);
00280 }
00281
00282
00283 quint16
00284 ServerSettings::getDirPort()
00285 {
00286 return (quint16)value(SETTING_DIRPORT).toUInt();
00287 }
00288
00289
00290 void
00291 ServerSettings::setNickname(QString nickname)
00292 {
00293 setValue(SETTING_NICKNAME, nickname);
00294 }
00295
00296
00297 QString
00298 ServerSettings::getNickname()
00299 {
00300 QString nickname = value(SETTING_NICKNAME).toString();
00301
00302 return ensure_valid_chars(nickname,
00303 VALID_NICKNAME_CHARS).left(MAX_NICKNAME_LEN);
00304 }
00305
00306
00307 void
00308 ServerSettings::setContactInfo(QString contact)
00309 {
00310 setValue(SETTING_CONTACT, contact);
00311 }
00312
00313
00314 QString
00315 ServerSettings::getContactInfo()
00316 {
00317 return value(SETTING_CONTACT).toString();
00318 }
00319
00320
00321 bool
00322 ServerSettings::isDirectoryMirror()
00323 {
00324 return localValue(SETTING_DIRMIRROR).toBool();
00325 }
00326
00327
00328 void
00329 ServerSettings::setDirectoryMirror(bool mirror)
00330 {
00331 setValue(SETTING_DIRMIRROR, mirror);
00332 }
00333
00334
00335 ExitPolicy
00336 ServerSettings::getExitPolicy()
00337 {
00338 return ExitPolicy(value(SETTING_EXITPOLICY).toString());
00339 }
00340
00341
00342 void
00343 ServerSettings::setExitPolicy(ExitPolicy &exitPolicy)
00344 {
00345 setValue(SETTING_EXITPOLICY, exitPolicy.toString());
00346 }
00347
00348
00349 quint32
00350 ServerSettings::getBandwidthAvgRate()
00351 {
00352 return value(SETTING_BANDWIDTH_RATE).toUInt();
00353 }
00354
00355
00356 void
00357 ServerSettings::setBandwidthAvgRate(quint32 rate)
00358 {
00359 setValue(SETTING_BANDWIDTH_RATE, rate);
00360 }
00361
00362
00363 quint32
00364 ServerSettings::getBandwidthBurstRate()
00365 {
00366 return value(SETTING_BANDWIDTH_BURST).toUInt();
00367 }
00368
00369
00370 void
00371 ServerSettings::setBandwidthBurstRate(quint32 rate)
00372 {
00373 setValue(SETTING_BANDWIDTH_BURST, rate);
00374 }
00375
00376
00377
00378
00379
00380 void
00381 ServerSettings::setPublishServerDescriptor(bool publish)
00382 {
00383 if (publish)
00384 setValue(SETTING_PUBLISH_SERVER_DESCRIPTOR, "1");
00385 else
00386 setValue(SETTING_PUBLISH_SERVER_DESCRIPTOR, "0");
00387 }
00388
00389
00390
00391 bool
00392 ServerSettings::publishServerDescriptor() const
00393 {
00394 return (value(SETTING_PUBLISH_SERVER_DESCRIPTOR).toString() != "0");
00395 }
00396
00397
00398 bool
00399 ServerSettings::isUpnpEnabled()
00400 {
00401 #if defined(USE_MINIUPNPC)
00402 return localValue(SETTING_ENABLE_UPNP).toBool();
00403 #else
00404 return false;
00405 #endif
00406 }
00407
00408
00409
00410 void
00411 ServerSettings::setUpnpEnabled(bool enabled)
00412 {
00413 #if defined(USE_MINIUPNPC)
00414 setValue(SETTING_ENABLE_UPNP, enabled);
00415 #endif
00416 }
00417