| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/web_socket_proxy.h" | 5 #include "chrome/browser/chromeos/web_socket_proxy.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 #include <stdlib.h> | 8 #include <stdlib.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 | 10 |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 content::NotificationService::current()->Notify( | 170 content::NotificationService::current()->Notify( |
| 171 chrome::NOTIFICATION_WEB_SOCKET_PROXY_STARTED, | 171 chrome::NOTIFICATION_WEB_SOCKET_PROXY_STARTED, |
| 172 content::NotificationService::AllSources(), content::Details<int>(&port)); | 172 content::NotificationService::AllSources(), content::Details<int>(&port)); |
| 173 } | 173 } |
| 174 | 174 |
| 175 class Conn; | 175 class Conn; |
| 176 | 176 |
| 177 // Websocket to TCP proxy server. | 177 // Websocket to TCP proxy server. |
| 178 class Serv { | 178 class Serv { |
| 179 public: | 179 public: |
| 180 explicit Serv(const std::vector<std::string>& allowed_origins); | 180 Serv(); |
| 181 ~Serv(); | 181 ~Serv(); |
| 182 | 182 |
| 183 // Do not call it twice. | 183 // Do not call it twice. |
| 184 void Run(); | 184 void Run(); |
| 185 | 185 |
| 186 // Terminates running server (should be called on a different thread). | 186 // Terminates running server (should be called on a different thread). |
| 187 void Shutdown(); | 187 void Shutdown(); |
| 188 // Called on network change. Reinitializes DNS resolution service. | 188 // Called on network change. Reinitializes DNS resolution service. |
| 189 void OnNetworkChange(); | 189 void OnNetworkChange(); |
| 190 | 190 |
| 191 void ZapConn(Conn*); | 191 void ZapConn(Conn*); |
| 192 void MarkConnImportance(Conn*, bool important); | 192 void MarkConnImportance(Conn*, bool important); |
| 193 Conn* GetFreshConn(); | 193 Conn* GetFreshConn(); |
| 194 bool IsConnSane(Conn*); | 194 bool IsConnSane(Conn*); |
| 195 bool IsOriginAllowed(const std::string& origin); | |
| 196 void CloseAll(); | 195 void CloseAll(); |
| 197 | 196 |
| 198 static void OnConnect(int listening_sock, short event, void*); | 197 static void OnConnect(int listening_sock, short event, void*); |
| 199 static void OnControlRequest(int fd, short event, void*); | 198 static void OnControlRequest(int fd, short event, void*); |
| 200 | 199 |
| 201 struct event_base* evbase() { return evbase_; } | 200 struct event_base* evbase() { return evbase_; } |
| 202 | 201 |
| 203 // Checked against value of Origin field specified | |
| 204 // in a client websocket handshake. | |
| 205 std::vector<std::string> allowed_origins_; | |
| 206 | |
| 207 // Libevent base. | 202 // Libevent base. |
| 208 struct event_base* evbase_; | 203 struct event_base* evbase_; |
| 209 | 204 |
| 210 // Socket to listen incoming websocket connections. | 205 // Socket to listen incoming websocket connections. |
| 211 int listening_sock_; | 206 int listening_sock_; |
| 212 | 207 |
| 213 // TODO(dilmah): remove this extra socket as soon as obsolete | 208 // TODO(dilmah): remove this extra socket as soon as obsolete |
| 214 // getPassportForTCP function is removed from webSocketProxyPrivate API. | 209 // getPassportForTCP function is removed from webSocketProxyPrivate API. |
| 215 // Additional socket to listen incoming connections on fixed port 10101. | 210 // Additional socket to listen incoming connections on fixed port 10101. |
| 216 int extra_listening_sock_; | 211 int extra_listening_sock_; |
| (...skipping 579 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 796 bool is_read_pipe_blocked_; | 791 bool is_read_pipe_blocked_; |
| 797 bool is_write_pipe_blocked_; | 792 bool is_write_pipe_blocked_; |
| 798 base::WeakPtrFactory<SSLChan> weak_factory_; | 793 base::WeakPtrFactory<SSLChan> weak_factory_; |
| 799 MessageLoopForIO::FileDescriptorWatcher read_pipe_controller_; | 794 MessageLoopForIO::FileDescriptorWatcher read_pipe_controller_; |
| 800 MessageLoopForIO::FileDescriptorWatcher write_pipe_controller_; | 795 MessageLoopForIO::FileDescriptorWatcher write_pipe_controller_; |
| 801 | 796 |
| 802 friend class base::DeleteHelper<SSLChan>; | 797 friend class base::DeleteHelper<SSLChan>; |
| 803 DISALLOW_COPY_AND_ASSIGN(SSLChan); | 798 DISALLOW_COPY_AND_ASSIGN(SSLChan); |
| 804 }; | 799 }; |
| 805 | 800 |
| 806 Serv::Serv(const std::vector<std::string>& allowed_origins) | 801 Serv::Serv() |
| 807 : allowed_origins_(allowed_origins), | 802 : evbase_(NULL), |
| 808 evbase_(NULL), | |
| 809 listening_sock_(-1), | 803 listening_sock_(-1), |
| 810 extra_listening_sock_(-1), | 804 extra_listening_sock_(-1), |
| 811 shutdown_requested_(false) { | 805 shutdown_requested_(false) { |
| 812 std::sort(allowed_origins_.begin(), allowed_origins_.end()); | |
| 813 control_descriptor_[0] = -1; | 806 control_descriptor_[0] = -1; |
| 814 control_descriptor_[1] = -1; | 807 control_descriptor_[1] = -1; |
| 815 } | 808 } |
| 816 | 809 |
| 817 Serv::~Serv() { | 810 Serv::~Serv() { |
| 818 CloseAll(); | 811 CloseAll(); |
| 819 } | 812 } |
| 820 | 813 |
| 821 void Serv::Run() { | 814 void Serv::Run() { |
| 822 if (evbase_ || shutdown_requested_) | 815 if (evbase_ || shutdown_requested_) |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1045 Conn* cs = new Conn(this); | 1038 Conn* cs = new Conn(this); |
| 1046 conn_pool_.push_front(cs); | 1039 conn_pool_.push_front(cs); |
| 1047 rev_map_[cs] = conn_pool_.begin(); | 1040 rev_map_[cs] = conn_pool_.begin(); |
| 1048 return cs; | 1041 return cs; |
| 1049 } | 1042 } |
| 1050 | 1043 |
| 1051 bool Serv::IsConnSane(Conn* cs) { | 1044 bool Serv::IsConnSane(Conn* cs) { |
| 1052 return rev_map_.find(cs) != rev_map_.end(); | 1045 return rev_map_.find(cs) != rev_map_.end(); |
| 1053 } | 1046 } |
| 1054 | 1047 |
| 1055 bool Serv::IsOriginAllowed(const std::string& origin) { | |
| 1056 return allowed_origins_.empty() || std::binary_search( | |
| 1057 allowed_origins_.begin(), allowed_origins_.end(), origin); | |
| 1058 } | |
| 1059 | |
| 1060 // static | 1048 // static |
| 1061 void Serv::OnConnect(int listening_sock, short event, void* ctx) { | 1049 void Serv::OnConnect(int listening_sock, short event, void* ctx) { |
| 1062 Serv* self = static_cast<Serv*>(ctx); | 1050 Serv* self = static_cast<Serv*>(ctx); |
| 1063 Conn* cs = self->GetFreshConn(); | 1051 Conn* cs = self->GetFreshConn(); |
| 1064 int sock = accept(listening_sock, NULL, NULL); | 1052 int sock = accept(listening_sock, NULL, NULL); |
| 1065 if (sock < 0 || !SetNonBlock(sock)) { | 1053 if (sock < 0 || !SetNonBlock(sock)) { |
| 1066 // Read readiness was triggered on listening socket | 1054 // Read readiness was triggered on listening socket |
| 1067 // yet we failed to accept a connection; definitely weird. | 1055 // yet we failed to accept a connection; definitely weird. |
| 1068 NOTREACHED(); | 1056 NOTREACHED(); |
| 1069 self->ZapConn(cs); | 1057 self->ZapConn(cs); |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1258 return STATUS_ABORT; | 1246 return STATUS_ABORT; |
| 1259 } | 1247 } |
| 1260 if (header_fields_["sec-websocket-version"] != "8" && | 1248 if (header_fields_["sec-websocket-version"] != "8" && |
| 1261 header_fields_["sec-websocket-version"] != "13") { | 1249 header_fields_["sec-websocket-version"] != "13") { |
| 1262 return STATUS_ABORT; | 1250 return STATUS_ABORT; |
| 1263 } | 1251 } |
| 1264 // Normalize origin (e.g. leading slash). | 1252 // Normalize origin (e.g. leading slash). |
| 1265 GURL origin = GURL(GetOrigin()).GetOrigin(); | 1253 GURL origin = GURL(GetOrigin()).GetOrigin(); |
| 1266 if (!origin.is_valid()) | 1254 if (!origin.is_valid()) |
| 1267 return STATUS_ABORT; | 1255 return STATUS_ABORT; |
| 1268 if (!master_->IsOriginAllowed(origin.spec())) | |
| 1269 return STATUS_ABORT; | |
| 1270 | 1256 |
| 1271 if (!requested_parameters_.empty()) { | 1257 if (!requested_parameters_.empty()) { |
| 1272 destname_ = requested_parameters_["hostname"]; | 1258 destname_ = requested_parameters_["hostname"]; |
| 1273 int port; | 1259 int port; |
| 1274 if (!base::StringToInt(requested_parameters_["port"], &port) || | 1260 if (!base::StringToInt(requested_parameters_["port"], &port) || |
| 1275 port < 0 || port >= 1 << 16) { | 1261 port < 0 || port >= 1 << 16) { |
| 1276 return STATUS_ABORT; | 1262 return STATUS_ABORT; |
| 1277 } | 1263 } |
| 1278 destport_ = port; | 1264 destport_ = port; |
| 1279 destaddr_ = requested_parameters_["addr"]; | 1265 destaddr_ = requested_parameters_["addr"]; |
| (...skipping 611 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1891 | 1877 |
| 1892 // static | 1878 // static |
| 1893 Conn::EventKey Conn::last_evkey_ = 0; | 1879 Conn::EventKey Conn::last_evkey_ = 0; |
| 1894 | 1880 |
| 1895 // static | 1881 // static |
| 1896 base::LazyInstance<Conn::EventKeyMap>::Leaky | 1882 base::LazyInstance<Conn::EventKeyMap>::Leaky |
| 1897 Conn::evkey_map_ = LAZY_INSTANCE_INITIALIZER; | 1883 Conn::evkey_map_ = LAZY_INSTANCE_INITIALIZER; |
| 1898 | 1884 |
| 1899 } // namespace | 1885 } // namespace |
| 1900 | 1886 |
| 1901 WebSocketProxy::WebSocketProxy(const std::vector<std::string>& allowed_origins) | 1887 WebSocketProxy::WebSocketProxy() : impl_(new Serv()) { |
| 1902 : impl_(new Serv(allowed_origins)) { | |
| 1903 } | 1888 } |
| 1904 | 1889 |
| 1905 WebSocketProxy::~WebSocketProxy() { | 1890 WebSocketProxy::~WebSocketProxy() { |
| 1906 delete static_cast<Serv*>(impl_); | 1891 delete static_cast<Serv*>(impl_); |
| 1907 impl_ = NULL; | 1892 impl_ = NULL; |
| 1908 } | 1893 } |
| 1909 | 1894 |
| 1910 void WebSocketProxy::Run() { | 1895 void WebSocketProxy::Run() { |
| 1911 static_cast<Serv*>(impl_)->Run(); | 1896 static_cast<Serv*>(impl_)->Run(); |
| 1912 } | 1897 } |
| 1913 | 1898 |
| 1914 void WebSocketProxy::Shutdown() { | 1899 void WebSocketProxy::Shutdown() { |
| 1915 static_cast<Serv*>(impl_)->Shutdown(); | 1900 static_cast<Serv*>(impl_)->Shutdown(); |
| 1916 } | 1901 } |
| 1917 | 1902 |
| 1918 void WebSocketProxy::OnNetworkChange() { | 1903 void WebSocketProxy::OnNetworkChange() { |
| 1919 static_cast<Serv*>(impl_)->OnNetworkChange(); | 1904 static_cast<Serv*>(impl_)->OnNetworkChange(); |
| 1920 } | 1905 } |
| 1921 | 1906 |
| 1922 } // namespace chromeos | 1907 } // namespace chromeos |
| OLD | NEW |