You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Connections.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. #include <vector>
  21. #include <rfb_win32/Registry.h>
  22. #include <rfb_win32/Dialog.h>
  23. #include <rfb_win32/ModuleFileName.h>
  24. #include <rfb/Configuration.h>
  25. #include <rfb/Blacklist.h>
  26. #include <network/TcpSocket.h>
  27. static rfb::IntParameter http_port("HTTPPortNumber",
  28. "TCP/IP port on which the server will serve the Java applet VNC Viewer ", 5800);
  29. static rfb::IntParameter port_number("PortNumber",
  30. "TCP/IP port on which the server will accept connections", 5900);
  31. static rfb::StringParameter hosts("Hosts",
  32. "Filter describing which hosts are allowed access to this server", "+");
  33. static rfb::BoolParameter localHost("LocalHost",
  34. "Only accept connections from via the local loop-back network interface", false);
  35. namespace rfb {
  36. namespace win32 {
  37. class ConnHostDialog : public Dialog {
  38. public:
  39. ConnHostDialog() : Dialog(GetModuleHandle(0)) {}
  40. bool showDialog(const TCHAR* pat) {
  41. pattern.replaceBuf(tstrDup(pat));
  42. return Dialog::showDialog(MAKEINTRESOURCE(IDD_CONN_HOST));
  43. }
  44. void initDialog() {
  45. if (_tcslen(pattern.buf) == 0)
  46. pattern.replaceBuf(tstrDup("+"));
  47. if (pattern.buf[0] == _T('+'))
  48. setItemChecked(IDC_ALLOW, true);
  49. else if (pattern.buf[0] == _T('?'))
  50. setItemChecked(IDC_QUERY, true);
  51. else
  52. setItemChecked(IDC_DENY, true);
  53. setItemString(IDC_HOST_PATTERN, &pattern.buf[1]);
  54. pattern.replaceBuf(0);
  55. }
  56. bool onOk() {
  57. TCharArray host(getItemString(IDC_HOST_PATTERN));
  58. TCharArray newPat(_tcslen(host.buf)+2);
  59. if (isItemChecked(IDC_ALLOW))
  60. newPat.buf[0] = _T('+');
  61. else if (isItemChecked(IDC_QUERY))
  62. newPat.buf[0] = _T('?');
  63. else
  64. newPat.buf[0] = _T('-');
  65. newPat.buf[1] = 0;
  66. _tcscat(newPat.buf, host.buf);
  67. try {
  68. network::TcpFilter::Pattern pat(network::TcpFilter::parsePattern(CStr(newPat.buf)));
  69. pattern.replaceBuf(TCharArray(network::TcpFilter::patternToStr(pat)).takeBuf());
  70. } catch(rdr::Exception& e) {
  71. MsgBox(NULL, TStr(e.str()), MB_ICONEXCLAMATION | MB_OK);
  72. return false;
  73. }
  74. return true;
  75. }
  76. const TCHAR* getPattern() {return pattern.buf;}
  77. protected:
  78. TCharArray pattern;
  79. };
  80. class ConnectionsPage : public PropSheetPage {
  81. public:
  82. ConnectionsPage(const RegKey& rk)
  83. : PropSheetPage(GetModuleHandle(0), MAKEINTRESOURCE(IDD_CONNECTIONS)), regKey(rk) {}
  84. void initDialog() {
  85. vlog.debug("set IDC_PORT %d", (int)port_number);
  86. setItemInt(IDC_PORT, port_number ? port_number : 5900);
  87. setItemChecked(IDC_RFB_ENABLE, port_number != 0);
  88. setItemInt(IDC_IDLE_TIMEOUT, rfb::Server::idleTimeout);
  89. vlog.debug("set IDC_HTTP_PORT %d", (int)http_port);
  90. setItemInt(IDC_HTTP_PORT, http_port ? http_port : 5800);
  91. setItemChecked(IDC_HTTP_ENABLE, http_port != 0);
  92. enableItem(IDC_HTTP_PORT, http_port != 0);
  93. setItemChecked(IDC_LOCALHOST, localHost);
  94. HWND listBox = GetDlgItem(handle, IDC_HOSTS);
  95. while (SendMessage(listBox, LB_GETCOUNT, 0, 0))
  96. SendMessage(listBox, LB_DELETESTRING, 0, 0);
  97. CharArray tmp;
  98. tmp.buf = hosts.getData();
  99. while (tmp.buf) {
  100. CharArray first;
  101. strSplit(tmp.buf, ',', &first.buf, &tmp.buf);
  102. if (strlen(first.buf))
  103. SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)(const TCHAR*)TStr(first.buf));
  104. }
  105. onCommand(IDC_RFB_ENABLE, EN_CHANGE);
  106. }
  107. bool onCommand(int id, int cmd) {
  108. switch (id) {
  109. case IDC_HOSTS:
  110. {
  111. DWORD selected = SendMessage(GetDlgItem(handle, IDC_HOSTS), LB_GETCURSEL, 0, 0);
  112. DWORD count = SendMessage(GetDlgItem(handle, IDC_HOSTS), LB_GETCOUNT, 0, 0);
  113. bool enable = selected != (DWORD)LB_ERR;
  114. enableItem(IDC_HOST_REMOVE, enable);
  115. enableItem(IDC_HOST_UP, enable && (selected > 0));
  116. enableItem(IDC_HOST_DOWN, enable && (selected+1 < count));
  117. enableItem(IDC_HOST_EDIT, enable);
  118. setChanged(isChanged());
  119. }
  120. return true;
  121. case IDC_PORT:
  122. if (cmd == EN_CHANGE) {
  123. try {
  124. setItemInt(IDC_HTTP_PORT, rfbPortToHTTP(getItemInt(IDC_PORT)));
  125. } catch (...) {
  126. }
  127. }
  128. case IDC_HTTP_PORT:
  129. case IDC_IDLE_TIMEOUT:
  130. if (cmd == EN_CHANGE)
  131. setChanged(isChanged());
  132. return false;
  133. case IDC_HTTP_ENABLE:
  134. case IDC_RFB_ENABLE:
  135. case IDC_LOCALHOST:
  136. {
  137. // HTTP port
  138. enableItem(IDC_HTTP_PORT, isItemChecked(IDC_HTTP_ENABLE) && isItemChecked(IDC_RFB_ENABLE));
  139. enableItem(IDC_HTTP_ENABLE, isItemChecked(IDC_RFB_ENABLE));
  140. // RFB port
  141. enableItem(IDC_PORT, isItemChecked(IDC_RFB_ENABLE));
  142. // Hosts
  143. enableItem(IDC_LOCALHOST, isItemChecked(IDC_RFB_ENABLE));
  144. bool enableHosts = !isItemChecked(IDC_LOCALHOST) && isItemChecked(IDC_RFB_ENABLE);
  145. enableItem(IDC_HOSTS, enableHosts);
  146. enableItem(IDC_HOST_ADD, enableHosts);
  147. if (!enableHosts) {
  148. enableItem(IDC_HOST_REMOVE, enableHosts);
  149. enableItem(IDC_HOST_UP, enableHosts);
  150. enableItem(IDC_HOST_DOWN, enableHosts);
  151. enableItem(IDC_HOST_EDIT, enableHosts);
  152. } else {
  153. onCommand(IDC_HOSTS, EN_CHANGE);
  154. }
  155. setChanged(isChanged());
  156. return false;
  157. }
  158. case IDC_HOST_ADD:
  159. if (hostDialog.showDialog(_T("")))
  160. {
  161. const TCHAR* pattern = hostDialog.getPattern();
  162. if (pattern)
  163. SendMessage(GetDlgItem(handle, IDC_HOSTS), LB_ADDSTRING, 0, (LPARAM)pattern);
  164. }
  165. return true;
  166. case IDC_HOST_EDIT:
  167. {
  168. HWND listBox = GetDlgItem(handle, IDC_HOSTS);
  169. int item = SendMessage(listBox, LB_GETCURSEL, 0, 0);
  170. TCharArray pattern(SendMessage(listBox, LB_GETTEXTLEN, item, 0)+1);
  171. SendMessage(listBox, LB_GETTEXT, item, (LPARAM)pattern.buf);
  172. if (hostDialog.showDialog(pattern.buf)) {
  173. const TCHAR* newPat = hostDialog.getPattern();
  174. if (newPat) {
  175. item = SendMessage(listBox, LB_FINDSTRINGEXACT, item, (LPARAM)pattern.buf);
  176. if (item != LB_ERR) {
  177. SendMessage(listBox, LB_DELETESTRING, item, 0);
  178. SendMessage(listBox, LB_INSERTSTRING, item, (LPARAM)newPat);
  179. SendMessage(listBox, LB_SETCURSEL, item, 0);
  180. onCommand(IDC_HOSTS, EN_CHANGE);
  181. }
  182. }
  183. }
  184. }
  185. return true;
  186. case IDC_HOST_UP:
  187. {
  188. HWND listBox = GetDlgItem(handle, IDC_HOSTS);
  189. int item = SendMessage(listBox, LB_GETCURSEL, 0, 0);
  190. TCharArray pattern(SendMessage(listBox, LB_GETTEXTLEN, item, 0)+1);
  191. SendMessage(listBox, LB_GETTEXT, item, (LPARAM)pattern.buf);
  192. SendMessage(listBox, LB_DELETESTRING, item, 0);
  193. SendMessage(listBox, LB_INSERTSTRING, item-1, (LPARAM)pattern.buf);
  194. SendMessage(listBox, LB_SETCURSEL, item-1, 0);
  195. onCommand(IDC_HOSTS, EN_CHANGE);
  196. }
  197. return true;
  198. case IDC_HOST_DOWN:
  199. {
  200. HWND listBox = GetDlgItem(handle, IDC_HOSTS);
  201. int item = SendMessage(listBox, LB_GETCURSEL, 0, 0);
  202. TCharArray pattern(SendMessage(listBox, LB_GETTEXTLEN, item, 0)+1);
  203. SendMessage(listBox, LB_GETTEXT, item, (LPARAM)pattern.buf);
  204. SendMessage(listBox, LB_DELETESTRING, item, 0);
  205. SendMessage(listBox, LB_INSERTSTRING, item+1, (LPARAM)pattern.buf);
  206. SendMessage(listBox, LB_SETCURSEL, item+1, 0);
  207. onCommand(IDC_HOSTS, EN_CHANGE);
  208. }
  209. return true;
  210. case IDC_HOST_REMOVE:
  211. {
  212. HWND listBox = GetDlgItem(handle, IDC_HOSTS);
  213. int item = SendMessage(listBox, LB_GETCURSEL, 0, 0);
  214. SendMessage(listBox, LB_DELETESTRING, item, 0);
  215. onCommand(IDC_HOSTS, EN_CHANGE);
  216. }
  217. }
  218. return false;
  219. }
  220. bool onOk() {
  221. regKey.setInt(_T("PortNumber"), isItemChecked(IDC_RFB_ENABLE) ? getItemInt(IDC_PORT) : 0);
  222. regKey.setInt(_T("IdleTimeout"), getItemInt(IDC_IDLE_TIMEOUT));
  223. regKey.setInt(_T("HTTPPortNumber"), isItemChecked(IDC_HTTP_ENABLE) && isItemChecked(IDC_RFB_ENABLE)
  224. ? getItemInt(IDC_HTTP_PORT) : 0);
  225. regKey.setInt(_T("LocalHost"), isItemChecked(IDC_LOCALHOST));
  226. regKey.setString(_T("Hosts"), TCharArray(getHosts()).buf);
  227. return true;
  228. }
  229. bool isChanged() {
  230. try {
  231. CharArray new_hosts(getHosts());
  232. CharArray old_hosts(hosts.getData());
  233. return (strcmp(new_hosts.buf, old_hosts.buf) != 0) ||
  234. (localHost != isItemChecked(IDC_LOCALHOST)) ||
  235. (port_number != getItemInt(IDC_PORT)) ||
  236. (http_port != getItemInt(IDC_HTTP_PORT)) ||
  237. ((http_port!=0) != (isItemChecked(IDC_HTTP_ENABLE)!=0)) ||
  238. (rfb::Server::idleTimeout != getItemInt(IDC_IDLE_TIMEOUT));
  239. } catch (rdr::Exception&) {
  240. return false;
  241. }
  242. }
  243. char* getHosts() {
  244. int bufLen = 1, i;
  245. HWND listBox = GetDlgItem(handle, IDC_HOSTS);
  246. for (i=0; i<SendMessage(listBox, LB_GETCOUNT, 0, 0); i++)
  247. bufLen+=SendMessage(listBox, LB_GETTEXTLEN, i, 0)+1;
  248. TCharArray hosts_str(bufLen);
  249. hosts_str.buf[0] = 0;
  250. TCHAR* outPos = hosts_str.buf;
  251. for (i=0; i<SendMessage(listBox, LB_GETCOUNT, 0, 0); i++) {
  252. outPos += SendMessage(listBox, LB_GETTEXT, i, (LPARAM)outPos);
  253. outPos[0] = ',';
  254. outPos[1] = 0;
  255. outPos++;
  256. }
  257. return strDup(hosts_str.buf);
  258. }
  259. int rfbPortToHTTP(int rfbPort) {
  260. int offset = -100;
  261. if (http_port)
  262. offset = http_port - port_number;
  263. int httpPort = rfbPort + offset;
  264. if (httpPort <= 0)
  265. httpPort = rfbPort;
  266. return httpPort;
  267. }
  268. protected:
  269. RegKey regKey;
  270. ConnHostDialog hostDialog;
  271. };
  272. };
  273. };
  274. #endif