ServiceSettings.cpp

Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If you
00004 **  did not receive the LICENSE file with this file, you may obtain it from the
00005 **  Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.vidalia-project.net/. No part of Vidalia, including this file,
00007 **  may be copied, modified, propagated, or distributed except according to the
00008 **  terms described in the LICENSE file.
00009 */
00010 
00011 #include "ServiceSettings.h"
00012 #include "TorSettings.h"
00013 
00014 #include "stringutil.h"
00015 
00016 /* Service Settings */
00017 #define SETTING_SERVICE_VIRTUAL_PORT "Service/VirtualPort"
00018 #define SETTING_SERVICE_ADDRESS "Service/ServiceAddress"
00019 #define SETTING_SERVICE_PHYSICAL_ADDRESS "Service/ServicePhysicalAddress"
00020 #define SETTING_SERVICE_ENABLED "Service/Enabled"
00021 #define SETTING_TOR_SERVICES "Service/Services"
00022 
00023 /** Constructor.
00024  * \param torControl a TorControl object used to read and apply the Service
00025  * configuration settings.
00026  */
00027 ServiceSettings::ServiceSettings(TorControl *torControl)
00028 {
00029   _torControl = torControl;
00030   setDefault(SETTING_SERVICE_VIRTUAL_PORT , 0);
00031   setDefault(SETTING_SERVICE_PHYSICAL_ADDRESS, "127.0.0.1:0");
00032   setDefault(SETTING_SERVICE_ENABLED, "true");
00033 }
00034 
00035 /** Set ServiceList to serialise it */
00036 void
00037 ServiceSettings::setServices(ServiceList service)
00038 {
00039   QStringList serviceList;
00040   if(service.services().size() > 0) {
00041     QList<Service> services = service.services();
00042     foreach (Service tempService, services) {
00043       serviceList << tempService.toString();
00044     }
00045   }
00046   setValue(SETTING_TOR_SERVICES, serviceList);
00047 }
00048 
00049 /** Get  serialised ServiceList */
00050 ServiceList
00051 ServiceSettings::getServices()
00052 {
00053   QString address,virtualPort,physAddrPort,serviceDir,enabledS,additionalData;
00054   bool enabled = false;
00055   QStringList stringList;
00056   ServiceList services;
00057 
00058   stringList = value(SETTING_TOR_SERVICES).toStringList();
00059   foreach (QString s, stringList) {
00060     QStringList skippedList = s.split("#");
00061     address = skippedList.first();
00062     skippedList.removeFirst();
00063     virtualPort = skippedList.first();
00064     skippedList.removeFirst();
00065     physAddrPort = skippedList.first();
00066     skippedList.removeFirst();
00067     serviceDir = skippedList.first();
00068     skippedList.removeFirst();
00069     enabledS = skippedList.first();
00070     skippedList.removeFirst();
00071     additionalData = skippedList.first();
00072     if(enabledS.compare("x1") == 0) {
00073       enabled = true;
00074     }
00075     Service service(address, virtualPort, physAddrPort, serviceDir, enabled);
00076     service.setAdditionalServiceOptions(additionalData);
00077     services.addService(service);
00078   }
00079   return services;
00080 }
00081 
00082 /** Returns the virtual port for a specific service*/
00083 QString
00084 ServiceSettings::getVirtualPort()
00085 {
00086   QString port = value(SETTING_SERVICE_VIRTUAL_PORT).toString();
00087   return port;
00088 }
00089 
00090 /** Set the virtual port for a specific service*/
00091 void
00092 ServiceSettings::setVirtualPort(QString servicePort)
00093 {
00094   setValue(SETTING_SERVICE_VIRTUAL_PORT, servicePort);
00095 }
00096 
00097 /** Returns the .onion - service address for a specific service */
00098 QString
00099 ServiceSettings::getServiceAddress()
00100 {
00101   QString addr = value(SETTING_SERVICE_ADDRESS).toString();
00102   return addr;
00103 }
00104 
00105 /** Set the .onion - service address or hostname for a specific service */
00106 void
00107 ServiceSettings::setServiceAddress(QString addr)
00108 {
00109   setValue(SETTING_SERVICE_ADDRESS, addr);
00110 }
00111 
00112 /** Returns the physical address for a specific service */
00113 QString
00114 ServiceSettings::getPhysicalAddressPort()
00115 {
00116   QString addr = value(SETTING_SERVICE_PHYSICAL_ADDRESS).toString();
00117   return addr;
00118 }
00119 
00120 /** Set the physical address or hostname for a specific service */
00121 void
00122 ServiceSettings::setPhysicalAddressPort(QString addr)
00123 {
00124   setValue(SETTING_SERVICE_PHYSICAL_ADDRESS, addr);
00125 }
00126 
00127 /** Returns if the Service is enabled */
00128 bool
00129 ServiceSettings::isEnabled()
00130 {
00131   return value(SETTING_SERVICE_ENABLED).toBool();
00132 }
00133 
00134 /** Set the service enabled */
00135 void
00136 ServiceSettings::setEnabled(bool boolean)
00137 {
00138   setValue(SETTING_SERVICE_ENABLED, boolean);
00139 }
00140 
00141 /** Get all service directories from Tor */
00142 QString
00143 ServiceSettings::getHiddenServiceDirectories()
00144 {
00145   /*XXX: Domenik: Why does this always try to getconf hiddenserviceoptions
00146    * even if the socket is not connected? */
00147   QString value =  _torControl->getHiddenServiceConf("hiddenserviceoptions");
00148   return value;
00149 }
00150 
00151 /** Set all services the user wants to start and send it to the
00152  * Tor Controller*/
00153 void
00154 ServiceSettings::applyServices(QString value, QString *errmsg)
00155 {
00156   _torControl->setConf(value, errmsg);
00157   _torControl->saveConf(errmsg);
00158 }
00159 
00160 /** Unpublish all HiddenServices */
00161 void
00162 ServiceSettings::unpublishAllServices(QString *errmsg)
00163 {
00164   _torControl->resetConf("HiddenServiceDir", errmsg);
00165   _torControl->saveConf(errmsg);
00166 }
00167 

Generated on Mon Aug 30 19:14:02 2010 for Vidalia by  doxygen 1.5.9