Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame^] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | #ifndef WINVNCCONF_CONNECTIONS |
| 19 | #define WINVNCCONF_CONNECTIONS |
| 20 | |
| 21 | #include <vector> |
| 22 | |
| 23 | #include <rfb_win32/Registry.h> |
| 24 | #include <rfb_win32/Dialog.h> |
| 25 | #include <rfb_win32/ModuleFileName.h> |
| 26 | #include <rfb/Configuration.h> |
| 27 | #include <rfb/Blacklist.h> |
| 28 | #include <network/TcpSocket.h> |
| 29 | |
| 30 | static rfb::IntParameter http_port("HTTPPortNumber", |
| 31 | "TCP/IP port on which the server will serve the Java applet VNC Viewer ", 5800); |
| 32 | static rfb::IntParameter port_number("PortNumber", |
| 33 | "TCP/IP port on which the server will accept connections", 5900); |
| 34 | static rfb::StringParameter hosts("Hosts", |
| 35 | "Filter describing which hosts are allowed access to this server", "+"); |
| 36 | static rfb::BoolParameter localHost("LocalHost", |
| 37 | "Only accept connections from via the local loop-back network interface", false); |
| 38 | |
| 39 | namespace rfb { |
| 40 | |
| 41 | namespace win32 { |
| 42 | |
| 43 | class ConnHostDialog : public Dialog { |
| 44 | public: |
| 45 | ConnHostDialog() : Dialog(GetModuleHandle(0)) {} |
| 46 | bool showDialog(const TCHAR* pat) { |
| 47 | pattern.replaceBuf(tstrDup(pat)); |
| 48 | return Dialog::showDialog(MAKEINTRESOURCE(IDD_CONN_HOST)); |
| 49 | } |
| 50 | void initDialog() { |
| 51 | if (_tcslen(pattern.buf) == 0) |
| 52 | pattern.replaceBuf(tstrDup("+")); |
| 53 | |
| 54 | if (pattern.buf[0] == _T('+')) |
| 55 | setItemChecked(IDC_ALLOW, true); |
| 56 | else if (pattern.buf[0] == _T('?')) |
| 57 | setItemChecked(IDC_QUERY, true); |
| 58 | else |
| 59 | setItemChecked(IDC_DENY, true); |
| 60 | |
| 61 | setItemString(IDC_HOST_PATTERN, &pattern.buf[1]); |
| 62 | pattern.replaceBuf(0); |
| 63 | } |
| 64 | bool onOk() { |
| 65 | TCharArray host = getItemString(IDC_HOST_PATTERN); |
| 66 | TCharArray newPat(_tcslen(host.buf)+2); |
| 67 | if (isItemChecked(IDC_ALLOW)) |
| 68 | newPat.buf[0] = _T('+'); |
| 69 | else if (isItemChecked(IDC_QUERY)) |
| 70 | newPat.buf[0] = _T('?'); |
| 71 | else |
| 72 | newPat.buf[0] = _T('-'); |
| 73 | newPat.buf[1] = 0; |
| 74 | _tcscat(newPat.buf, host.buf); |
| 75 | |
| 76 | network::TcpFilter::Pattern pat(network::TcpFilter::parsePattern(CStr(newPat.buf))); |
| 77 | pattern.replaceBuf(TCharArray(network::TcpFilter::patternToStr(pat)).takeBuf()); |
| 78 | return true; |
| 79 | } |
| 80 | const TCHAR* getPattern() {return pattern.buf;} |
| 81 | protected: |
| 82 | TCharArray pattern; |
| 83 | }; |
| 84 | |
| 85 | class ConnectionsPage : public PropSheetPage { |
| 86 | public: |
| 87 | ConnectionsPage(const RegKey& rk) |
| 88 | : PropSheetPage(GetModuleHandle(0), MAKEINTRESOURCE(IDD_CONNECTIONS)), regKey(rk) {} |
| 89 | void initDialog() { |
| 90 | vlog.debug("set IDC_PORT %d", (int)port_number); |
| 91 | setItemInt(IDC_PORT, port_number ? port_number : 5900); |
| 92 | setItemChecked(IDC_RFB_ENABLE, port_number != 0); |
| 93 | setItemInt(IDC_IDLE_TIMEOUT, rfb::Server::idleTimeout); |
| 94 | vlog.debug("set IDC_HTTP_PORT %d", (int)http_port); |
| 95 | setItemInt(IDC_HTTP_PORT, http_port ? http_port : 5800); |
| 96 | setItemChecked(IDC_HTTP_ENABLE, http_port != 0); |
| 97 | enableItem(IDC_HTTP_PORT, http_port != 0); |
| 98 | setItemChecked(IDC_LOCALHOST, localHost); |
| 99 | |
| 100 | HWND listBox = GetDlgItem(handle, IDC_HOSTS); |
| 101 | while (SendMessage(listBox, LB_GETCOUNT, 0, 0)) |
| 102 | SendMessage(listBox, LB_DELETESTRING, 0, 0); |
| 103 | |
| 104 | CharArray tmp; |
| 105 | tmp.buf = hosts.getData(); |
| 106 | while (tmp.buf) { |
| 107 | CharArray first; |
| 108 | strSplit(tmp.buf, ',', &first.buf, &tmp.buf); |
| 109 | if (strlen(first.buf)) |
| 110 | SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)(const TCHAR*)TStr(first.buf)); |
| 111 | } |
| 112 | |
| 113 | onCommand(IDC_RFB_ENABLE, EN_CHANGE); |
| 114 | } |
| 115 | bool onCommand(int id, int cmd) { |
| 116 | switch (id) { |
| 117 | case IDC_HOSTS: |
| 118 | { |
| 119 | DWORD selected = SendMessage(GetDlgItem(handle, IDC_HOSTS), LB_GETCURSEL, 0, 0); |
| 120 | int count = SendMessage(GetDlgItem(handle, IDC_HOSTS), LB_GETCOUNT, 0, 0); |
| 121 | bool enable = selected != LB_ERR; |
| 122 | enableItem(IDC_HOST_REMOVE, enable); |
| 123 | enableItem(IDC_HOST_UP, enable && (selected > 0)); |
| 124 | enableItem(IDC_HOST_DOWN, enable && (selected < count-1)); |
| 125 | enableItem(IDC_HOST_EDIT, enable); |
| 126 | setChanged(isChanged()); |
| 127 | } |
| 128 | return true; |
| 129 | |
| 130 | case IDC_PORT: |
| 131 | if (cmd == EN_CHANGE) { |
| 132 | try { |
| 133 | setItemInt(IDC_HTTP_PORT, rfbPortToHTTP(getItemInt(IDC_PORT))); |
| 134 | } catch (...) { |
| 135 | } |
| 136 | } |
| 137 | case IDC_HTTP_PORT: |
| 138 | case IDC_IDLE_TIMEOUT: |
| 139 | if (cmd == EN_CHANGE) |
| 140 | setChanged(isChanged()); |
| 141 | return false; |
| 142 | |
| 143 | case IDC_HTTP_ENABLE: |
| 144 | case IDC_RFB_ENABLE: |
| 145 | case IDC_LOCALHOST: |
| 146 | { |
| 147 | // HTTP port |
| 148 | enableItem(IDC_HTTP_PORT, isItemChecked(IDC_HTTP_ENABLE) && isItemChecked(IDC_RFB_ENABLE)); |
| 149 | enableItem(IDC_HTTP_ENABLE, isItemChecked(IDC_RFB_ENABLE)); |
| 150 | |
| 151 | // RFB port |
| 152 | enableItem(IDC_PORT, isItemChecked(IDC_RFB_ENABLE)); |
| 153 | |
| 154 | // Hosts |
| 155 | enableItem(IDC_LOCALHOST, isItemChecked(IDC_RFB_ENABLE)); |
| 156 | |
| 157 | bool enableHosts = !isItemChecked(IDC_LOCALHOST) && isItemChecked(IDC_RFB_ENABLE); |
| 158 | enableItem(IDC_HOSTS, enableHosts); |
| 159 | enableItem(IDC_HOST_ADD, enableHosts); |
| 160 | if (!enableHosts) { |
| 161 | enableItem(IDC_HOST_REMOVE, enableHosts); |
| 162 | enableItem(IDC_HOST_UP, enableHosts); |
| 163 | enableItem(IDC_HOST_DOWN, enableHosts); |
| 164 | enableItem(IDC_HOST_EDIT, enableHosts); |
| 165 | } else { |
| 166 | onCommand(IDC_HOSTS, EN_CHANGE); |
| 167 | } |
| 168 | setChanged(isChanged()); |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | case IDC_HOST_ADD: |
| 173 | if (hostDialog.showDialog(_T(""))) |
| 174 | { |
| 175 | const TCHAR* pattern = hostDialog.getPattern(); |
| 176 | if (pattern) |
| 177 | SendMessage(GetDlgItem(handle, IDC_HOSTS), LB_ADDSTRING, 0, (LPARAM)pattern); |
| 178 | } |
| 179 | return true; |
| 180 | |
| 181 | case IDC_HOST_EDIT: |
| 182 | { |
| 183 | HWND listBox = GetDlgItem(handle, IDC_HOSTS); |
| 184 | int item = SendMessage(listBox, LB_GETCURSEL, 0, 0); |
| 185 | TCharArray pattern(SendMessage(listBox, LB_GETTEXTLEN, item, 0)+1); |
| 186 | SendMessage(listBox, LB_GETTEXT, item, (LPARAM)pattern.buf); |
| 187 | |
| 188 | if (hostDialog.showDialog(pattern.buf)) { |
| 189 | const TCHAR* newPat = hostDialog.getPattern(); |
| 190 | if (newPat) { |
| 191 | item = SendMessage(listBox, LB_FINDSTRINGEXACT, item, (LPARAM)pattern.buf); |
| 192 | if (item != LB_ERR) { |
| 193 | SendMessage(listBox, LB_DELETESTRING, item, 0); |
| 194 | SendMessage(listBox, LB_INSERTSTRING, item, (LPARAM)newPat); |
| 195 | SendMessage(listBox, LB_SETCURSEL, item, 0); |
| 196 | onCommand(IDC_HOSTS, EN_CHANGE); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | return true; |
| 202 | |
| 203 | case IDC_HOST_UP: |
| 204 | { |
| 205 | HWND listBox = GetDlgItem(handle, IDC_HOSTS); |
| 206 | int item = SendMessage(listBox, LB_GETCURSEL, 0, 0); |
| 207 | TCharArray pattern(SendMessage(listBox, LB_GETTEXTLEN, item, 0)+1); |
| 208 | SendMessage(listBox, LB_GETTEXT, item, (LPARAM)pattern.buf); |
| 209 | SendMessage(listBox, LB_DELETESTRING, item, 0); |
| 210 | SendMessage(listBox, LB_INSERTSTRING, item-1, (LPARAM)pattern.buf); |
| 211 | SendMessage(listBox, LB_SETCURSEL, item-1, 0); |
| 212 | onCommand(IDC_HOSTS, EN_CHANGE); |
| 213 | } |
| 214 | return true; |
| 215 | |
| 216 | case IDC_HOST_DOWN: |
| 217 | { |
| 218 | HWND listBox = GetDlgItem(handle, IDC_HOSTS); |
| 219 | int item = SendMessage(listBox, LB_GETCURSEL, 0, 0); |
| 220 | TCharArray pattern(SendMessage(listBox, LB_GETTEXTLEN, item, 0)+1); |
| 221 | SendMessage(listBox, LB_GETTEXT, item, (LPARAM)pattern.buf); |
| 222 | SendMessage(listBox, LB_DELETESTRING, item, 0); |
| 223 | SendMessage(listBox, LB_INSERTSTRING, item+1, (LPARAM)pattern.buf); |
| 224 | SendMessage(listBox, LB_SETCURSEL, item+1, 0); |
| 225 | onCommand(IDC_HOSTS, EN_CHANGE); |
| 226 | } |
| 227 | return true; |
| 228 | |
| 229 | case IDC_HOST_REMOVE: |
| 230 | { |
| 231 | HWND listBox = GetDlgItem(handle, IDC_HOSTS); |
| 232 | int item = SendMessage(listBox, LB_GETCURSEL, 0, 0); |
| 233 | SendMessage(listBox, LB_DELETESTRING, item, 0); |
| 234 | onCommand(IDC_HOSTS, EN_CHANGE); |
| 235 | } |
| 236 | |
| 237 | } |
| 238 | return false; |
| 239 | } |
| 240 | bool onOk() { |
| 241 | regKey.setInt(_T("PortNumber"), isItemChecked(IDC_RFB_ENABLE) ? getItemInt(IDC_PORT) : 0); |
| 242 | regKey.setInt(_T("IdleTimeout"), getItemInt(IDC_IDLE_TIMEOUT)); |
| 243 | regKey.setInt(_T("HTTPPortNumber"), isItemChecked(IDC_HTTP_ENABLE) && isItemChecked(IDC_RFB_ENABLE) |
| 244 | ? getItemInt(IDC_HTTP_PORT) : 0); |
| 245 | regKey.setInt(_T("LocalHost"), isItemChecked(IDC_LOCALHOST)); |
| 246 | regKey.setString(_T("Hosts"), TCharArray(getHosts()).buf); |
| 247 | return true; |
| 248 | } |
| 249 | bool isChanged() { |
| 250 | try { |
| 251 | CharArray new_hosts = getHosts(); |
| 252 | CharArray old_hosts = hosts.getData(); |
| 253 | return (strcmp(new_hosts.buf, old_hosts.buf) != 0) || |
| 254 | (localHost != isItemChecked(IDC_LOCALHOST)) || |
| 255 | (port_number != getItemInt(IDC_PORT)) || |
| 256 | (http_port != getItemInt(IDC_HTTP_PORT)) || |
| 257 | ((http_port!=0) != (isItemChecked(IDC_HTTP_ENABLE)!=0)) || |
| 258 | (rfb::Server::idleTimeout != getItemInt(IDC_IDLE_TIMEOUT)); |
| 259 | } catch (rdr::Exception) { |
| 260 | return false; |
| 261 | } |
| 262 | } |
| 263 | char* getHosts() { |
| 264 | int bufLen = 1, i; |
| 265 | HWND listBox = GetDlgItem(handle, IDC_HOSTS); |
| 266 | for (i=0; i<SendMessage(listBox, LB_GETCOUNT, 0, 0); i++) |
| 267 | bufLen+=SendMessage(listBox, LB_GETTEXTLEN, i, 0)+1; |
| 268 | TCharArray hosts_str(bufLen); |
| 269 | hosts_str.buf[0] = 0; |
| 270 | TCHAR* outPos = hosts_str.buf; |
| 271 | for (i=0; i<SendMessage(listBox, LB_GETCOUNT, 0, 0); i++) { |
| 272 | outPos += SendMessage(listBox, LB_GETTEXT, i, (LPARAM)outPos); |
| 273 | outPos[0] = ','; |
| 274 | outPos[1] = 0; |
| 275 | outPos++; |
| 276 | } |
| 277 | return strDup(hosts_str.buf); |
| 278 | } |
| 279 | int rfbPortToHTTP(int rfbPort) { |
| 280 | int offset = -100; |
| 281 | if (http_port) |
| 282 | offset = http_port - port_number; |
| 283 | int httpPort = rfbPort + offset; |
| 284 | if (httpPort <= 0) |
| 285 | httpPort = rfbPort; |
| 286 | return httpPort; |
| 287 | } |
| 288 | |
| 289 | protected: |
| 290 | RegKey regKey; |
| 291 | ConnHostDialog hostDialog; |
| 292 | }; |
| 293 | |
| 294 | }; |
| 295 | |
| 296 | }; |
| 297 | |
| 298 | #endif |