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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. network::TcpFilter::Pattern pat(network::TcpFilter::parsePattern(CStr(newPat.buf)));
  68. pattern.replaceBuf(TCharArray(network::TcpFilter::patternToStr(pat)).takeBuf());
  69. return true;
  70. }
  71. const TCHAR* getPattern() {return pattern.buf;}
  72. protected:
  73. TCharArray pattern;
  74. };
  75. class ConnectionsPage : public PropSheetPage {
  76. public:
  77. ConnectionsPage(const RegKey& rk)
  78. : PropSheetPage(GetModuleHandle(0), MAKEINTRESOURCE(IDD_CONNECTIONS)), regKey(rk) {}
  79. void initDialog() {
  80. vlog.debug("set IDC_PORT %d", (int)port_number);
  81. setItemInt(IDC_PORT, port_number ? port_number : 5900);
  82. setItemChecked(IDC_RFB_ENABLE, port_number != 0);
  83. setItemInt(IDC_IDLE_TIMEOUT, rfb::Server::idleTimeout);
  84. vlog.debug("set IDC_HTTP_PORT %d", (int)http_port);
  85. setItemInt(IDC_HTTP_PORT, http_port ? http_port : 5800);
  86. setItemChecked(IDC_HTTP_ENABLE, http_port != 0);
  87. enableItem(IDC_HTTP_PORT, http_port != 0);
  88. setItemChecked(IDC_LOCALHOST, localHost);
  89. HWND listBox = GetDlgItem(handle, IDC_HOSTS);
  90. while (SendMessage(listBox, LB_GETCOUNT, 0, 0))
  91. SendMessage(listBox, LB_DELETESTRING, 0, 0);
  92. CharArray tmp;
  93. tmp.buf = hosts.getData();
  94. while (tmp.buf) {
  95. CharArray first;
  96. strSplit(tmp.buf, ',', &first.buf, &tmp.buf);
  97. if (strlen(first.buf))
  98. SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)(const TCHAR*)TStr(first.buf));
  99. }
  100. onCommand(IDC_RFB_ENABLE, EN_CHANGE);
  101. }
  102. bool onCommand(int id, int cmd) {
  103. switch (id) {
  104. case IDC_HOSTS:
  105. {
  106. DWORD selected = SendMessage(GetDlgItem(handle, IDC_HOSTS), LB_GETCURSEL, 0, 0);
  107. int count = SendMessage(GetDlgItem(handle, IDC_HOSTS), LB_GETCOUNT, 0, 0);
  108. bool enable = selected != LB_ERR;
  109. enableItem(IDC_HOST_REMOVE, enable);
  110. enableItem(IDC_HOST_UP, enable && (selected > 0));
  111. enableItem(IDC_HOST_DOWN, enable && (selected < count-1));
  112. enableItem(IDC_HOST_EDIT, enable);
  113. setChanged(isChanged());
  114. }
  115. return true;
  116. case IDC_PORT:
  117. if (cmd == EN_CHANGE) {
  118. try {
  119. setItemInt(IDC_HTTP_PORT, rfbPortToHTTP(getItemInt(IDC_PORT)));
  120. } catch (...) {
  121. }
  122. }
  123. case IDC_HTTP_PORT:
  124. case IDC_IDLE_TIMEOUT:
  125. if (cmd == EN_CHANGE)
  126. setChanged(isChanged());
  127. return false;
  128. case IDC_HTTP_ENABLE:
  129. case IDC_RFB_ENABLE:
  130. case IDC_LOCALHOST:
  131. {
  132. // HTTP port
  133. enableItem(IDC_HTTP_PORT, isItemChecked(IDC_HTTP_ENABLE) && isItemChecked(IDC_RFB_ENABLE));
  134. enableItem(IDC_HTTP_ENABLE, isItemChecked(IDC_RFB_ENABLE));
  135. // RFB port
  136. enableItem(IDC_PORT, isItemChecked(IDC_RFB_ENABLE));
  137. // Hosts
  138. enableItem(IDC_LOCALHOST, isItemChecked(IDC_RFB_ENABLE));
  139. bool enableHosts = !isItemChecked(IDC_LOCALHOST) && isItemChecked(IDC_RFB_ENABLE);
  140. enableItem(IDC_HOSTS, enableHosts);
  141. enableItem(IDC_HOST_ADD, enableHosts);
  142. if (!enableHosts) {
  143. enableItem(IDC_HOST_REMOVE, enableHosts);
  144. enableItem(IDC_HOST_UP, enableHosts);
  145. enableItem(IDC_HOST_DOWN, enableHosts);
  146. enableItem(IDC_HOST_EDIT, enableHosts);
  147. } else {
  148. onCommand(IDC_HOSTS, EN_CHANGE);
  149. }
  150. setChanged(isChanged());
  151. return false;
  152. }
  153. case IDC_HOST_ADD:
  154. if (hostDialog.showDialog(_T("")))
  155. {
  156. const TCHAR* pattern = hostDialog.getPattern();
  157. if (pattern)
  158. SendMessage(GetDlgItem(handle, IDC_HOSTS), LB_ADDSTRING, 0, (LPARAM)pattern);
  159. }
  160. return true;
  161. case IDC_HOST_EDIT:
  162. {
  163. HWND listBox = GetDlgItem(handle, IDC_HOSTS);
  164. int item = SendMessage(listBox, LB_GETCURSEL, 0, 0);
  165. TCharArray pattern(SendMessage(listBox, LB_GETTEXTLEN, item, 0)+1);
  166. SendMessage(listBox, LB_GETTEXT, item, (LPARAM)pattern.buf);
  167. if (hostDialog.showDialog(pattern.buf)) {
  168. const TCHAR* newPat = hostDialog.getPattern();
  169. if (newPat) {
  170. item = SendMessage(listBox, LB_FINDSTRINGEXACT, item, (LPARAM)pattern.buf);
  171. if (item != LB_ERR) {
  172. SendMessage(listBox, LB_DELETESTRING, item, 0);
  173. SendMessage(listBox, LB_INSERTSTRING, item, (LPARAM)newPat);
  174. SendMessage(listBox, LB_SETCURSEL, item, 0);
  175. onCommand(IDC_HOSTS, EN_CHANGE);
  176. }
  177. }
  178. }
  179. }
  180. return true;
  181. case IDC_HOST_UP:
  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. SendMessage(listBox, LB_DELETESTRING, item, 0);
  188. SendMessage(listBox, LB_INSERTSTRING, item-1, (LPARAM)pattern.buf);
  189. SendMessage(listBox, LB_SETCURSEL, item-1, 0);
  190. onCommand(IDC_HOSTS, EN_CHANGE);
  191. }
  192. return true;
  193. case IDC_HOST_DOWN:
  194. {
  195. HWND listBox = GetDlgItem(handle, IDC_HOSTS);
  196. int item = SendMessage(listBox, LB_GETCURSEL, 0, 0);
  197. TCharArray pattern(SendMessage(listBox, LB_GETTEXTLEN, item, 0)+1);
  198. SendMessage(listBox, LB_GETTEXT, item, (LPARAM)pattern.buf);
  199. SendMessage(listBox, LB_DELETESTRING, item, 0);
  200. SendMessage(listBox, LB_INSERTSTRING, item+1, (LPARAM)pattern.buf);
  201. SendMessage(listBox, LB_SETCURSEL, item+1, 0);
  202. onCommand(IDC_HOSTS, EN_CHANGE);
  203. }
  204. return true;
  205. case IDC_HOST_REMOVE:
  206. {
  207. HWND listBox = GetDlgItem(handle, IDC_HOSTS);
  208. int item = SendMessage(listBox, LB_GETCURSEL, 0, 0);
  209. SendMessage(listBox, LB_DELETESTRING, item, 0);
  210. onCommand(IDC_HOSTS, EN_CHANGE);
  211. }
  212. }
  213. return false;
  214. }
  215. bool onOk() {
  216. regKey.setInt(_T("PortNumber"), isItemChecked(IDC_RFB_ENABLE) ? getItemInt(IDC_PORT) : 0);
  217. regKey.setInt(_T("IdleTimeout"), getItemInt(IDC_IDLE_TIMEOUT));
  218. regKey.setInt(_T("HTTPPortNumber"), isItemChecked(IDC_HTTP_ENABLE) && isItemChecked(IDC_RFB_ENABLE)
  219. ? getItemInt(IDC_HTTP_PORT) : 0);
  220. regKey.setInt(_T("LocalHost"), isItemChecked(IDC_LOCALHOST));
  221. regKey.setString(_T("Hosts"), TCharArray(getHosts()).buf);
  222. return true;
  223. }
  224. bool isChanged() {
  225. try {
  226. CharArray new_hosts = getHosts();
  227. CharArray old_hosts = hosts.getData();
  228. return (strcmp(new_hosts.buf, old_hosts.buf) != 0) ||
  229. (localHost != isItemChecked(IDC_LOCALHOST)) ||
  230. (port_number != getItemInt(IDC_PORT)) ||
  231. (http_port != getItemInt(IDC_HTTP_PORT)) ||
  232. ((http_port!=0) != (isItemChecked(IDC_HTTP_ENABLE)!=0)) ||
  233. (rfb::Server::idleTimeout != getItemInt(IDC_IDLE_TIMEOUT));
  234. } catch (rdr::Exception) {
  235. return false;
  236. }
  237. }
  238. char* getHosts() {
  239. int bufLen = 1, i;
  240. HWND listBox = GetDlgItem(handle, IDC_HOSTS);
  241. for (i=0; i<SendMessage(listBox, LB_GETCOUNT, 0, 0); i++)
  242. bufLen+=SendMessage(listBox, LB_GETTEXTLEN, i, 0)+1;
  243. TCharArray hosts_str(bufLen);
  244. hosts_str.buf[0] = 0;
  245. TCHAR* outPos = hosts_str.buf;
  246. for (i=0; i<SendMessage(listBox, LB_GETCOUNT, 0, 0); i++) {
  247. outPos += SendMessage(listBox, LB_GETTEXT, i, (LPARAM)outPos);
  248. outPos[0] = ',';
  249. outPos[1] = 0;
  250. outPos++;
  251. }
  252. return strDup(hosts_str.buf);
  253. }
  254. int rfbPortToHTTP(int rfbPort) {
  255. int offset = -100;
  256. if (http_port)
  257. offset = http_port - port_number;
  258. int httpPort = rfbPort + offset;
  259. if (httpPort <= 0)
  260. httpPort = rfbPort;
  261. return httpPort;
  262. }
  263. protected:
  264. RegKey regKey;
  265. ConnHostDialog hostDialog;
  266. };
  267. };
  268. };
  269. #endif