| | 141 | } |
| | 142 | |
| | 143 | /** Adds the exit policy contained in item to the exitPolicy */ |
| | 144 | void |
| | 145 | ServerPage::savePolicy(QTreeWidgetItem *item, ExitPolicy &exitPolicy) |
| | 146 | { |
| | 147 | /* Build policy string */ |
| | 148 | QString policyString = item->text(COL_ACTION) + " "; |
| | 149 | policyString += item->text(COL_ADDRESS) + ":" + item->text(COL_PORT); |
| | 150 | |
| | 151 | /* Add policy to ServerSettings */ |
| | 152 | exitPolicy.addPolicy(Policy(policyString)); |
| | 153 | } |
| | 154 | |
| | 155 | /** Moves or appends the correct default exit rule to the policy list */ |
| | 156 | void |
| | 157 | ServerPage::setDefaultRule() |
| | 158 | { |
| | 159 | bool override = ui.chkExitOverride->isChecked(); |
| | 160 | Policy::SpecialPolicy action; |
| | 161 | QString actionString; |
| | 162 | QList<QTreeWidgetItem *> list; |
| | 163 | bool found = false; |
| | 164 | |
| | 165 | if (override) { |
| | 166 | action = Policy::RejectAll; |
| | 167 | actionString = "reject"; |
| | 168 | } else { |
| | 169 | action = Policy::AcceptAll; |
| | 170 | actionString = "accept"; |
| | 171 | } |
| | 172 | |
| | 173 | /* Search for the policy, if exists: move to bottom else: append it */ |
| | 174 | /* Remove any of the opposite default exit policy */ |
| | 175 | list = ui.lstExitPolicies->findItems("0.0.0.0", Qt::MatchExactly, COL_ADDRESS); |
| | 176 | |
| | 177 | foreach (QTreeWidgetItem *item, list) { |
| | 178 | if (item->text(COL_PORT) == "*") { |
| | 179 | int index = ui.lstExitPolicies->indexOfTopLevelItem(item); |
| | 180 | |
| | 181 | /* Found target so move to bottom of list */ |
| | 182 | if (item->text(COL_ACTION) == actionString) { |
| | 183 | ui.lstExitPolicies->addTopLevelItem(ui.lstExitPolicies-> |
| | 184 | takeTopLevelItem(index)); |
| | 185 | found = true; |
| | 186 | |
| | 187 | /* Found the opposite so remove */ |
| | 188 | } else { |
| | 189 | ui.lstExitPolicies->takeTopLevelItem(index); |
| | 190 | } |
| | 191 | } |
| | 192 | } |
| | 193 | |
| | 194 | /* Search failed so just append the necessary policy */ |
| | 195 | if (!found) addPolicyItem(Policy(action)); |
| 129 | | QTreeWidgetItem *newPolicy = new QTreeWidgetItem(); |
| 130 | | |
| 131 | | /* Add new policy's action */ |
| 132 | | newPolicy->setText(COL_ACTION, ui.cmboExitAction->currentText()); |
| 133 | | newPolicy->setTextAlignment(COL_ACTION, Qt::AlignCenter); |
| 134 | | |
| 135 | | /* Add new policy's ip address */ |
| 136 | | QString address = ui.lineExitAddress->text(); |
| 137 | | if (!ui.lineExitMask->text().isEmpty()) { |
| 138 | | address += ":" + ui.lineExitMask->text(); |
| 139 | | } |
| 140 | | newPolicy->setText(COL_ADDRESS, address); |
| 141 | | newPolicy->setTextAlignment(COL_ADDRESS, Qt::AlignCenter); |
| 142 | | |
| 143 | | /* Add new policy's port or port range */ |
| 144 | | QString portRange = ui.lineExitFromPort->text(); |
| 145 | | if (!portRange.isEmpty()) { |
| 146 | | QString toPort = ui.lineExitToPort->text(); |
| 147 | | if (!toPort.isEmpty() && portRange != "*") { |
| 148 | | portRange += "-" + ui.lineExitToPort->text(); |
| 149 | | } |
| 150 | | } |
| 151 | | newPolicy->setText(COL_PORT, portRange); |
| 152 | | newPolicy->setTextAlignment(COL_PORT, Qt::AlignCenter); |
| 153 | | |
| 154 | | /* Add new policy to list */ |
| 155 | | ui.lstExitPolicies->addTopLevelItem(newPolicy); |
| | 207 | /* Add the policy to the list */ |
| | 208 | addPolicyItem(ui.cmboExitAction->currentText(), ui.lineExitAddress->text(), |
| | 209 | ui.lineExitMask->text(), ui.lineExitFromPort->text(), |
| | 210 | ui.lineExitToPort->text()); |
| | 219 | /* Adds a new QTreeWidget item to the exit policy list */ |
| | 220 | void |
| | 221 | ServerPage::addPolicyItem(Policy policy) |
| | 222 | { |
| | 223 | /* Convert to strings */ |
| | 224 | QString action = (policy.action() == Policy::Accept ? "accept" : "reject"); |
| | 225 | QHostAddress address = policy.address(); |
| | 226 | QString addr = (address.isNull() ? "*" : address.toString()); |
| | 227 | QString mask = (policy.mask() ? QString::number(policy.mask()) : ""); |
| | 228 | int port = policy.fromPort(); |
| | 229 | QString fromPort = (port ? QString::number(port) : ""); |
| | 230 | port = policy.toPort(); |
| | 231 | QString toPort = (port ? QString::number(port) : ""); |
| | 232 | |
| | 233 | /* Add to list */ |
| | 234 | addPolicyItem(action, addr, mask, fromPort, toPort); |
| | 235 | } |
| | 236 | |
| | 237 | /* Adds a new QTreeWidget item to the exit policy list */ |
| | 238 | void |
| | 239 | ServerPage::addPolicyItem(QString action, QString address, QString mask, |
| | 240 | QString fromPort, QString toPort) |
| | 241 | { |
| | 242 | QTreeWidgetItem *newPolicy = new QTreeWidgetItem(); |
| | 243 | |
| | 244 | newPolicy->setText(COL_ACTION, action); |
| | 245 | newPolicy->setTextAlignment(COL_ACTION, Qt::AlignCenter); |
| | 246 | |
| | 247 | if (!mask.isEmpty()) { |
| | 248 | address += "/" + mask; |
| | 249 | } |
| | 250 | newPolicy->setText(COL_ADDRESS, address); |
| | 251 | newPolicy->setTextAlignment(COL_ADDRESS, Qt::AlignCenter); |
| | 252 | |
| | 253 | if (!fromPort.isEmpty()) { |
| | 254 | if (!toPort.isEmpty() && fromPort != "*") { |
| | 255 | fromPort += "-" + toPort; |
| | 256 | } |
| | 257 | } else { |
| | 258 | fromPort = "*"; |
| | 259 | } |
| | 260 | |
| | 261 | newPolicy->setText(COL_PORT, fromPort); |
| | 262 | newPolicy->setTextAlignment(COL_PORT, Qt::AlignCenter); |
| | 263 | |
| | 264 | ui.lstExitPolicies->addTopLevelItem(newPolicy); |
| | 265 | } |
| | 266 | |