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 9.6KB

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