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.4KB

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