1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef COMMUNICATIONNETSTACK_WEBSOCKET_SERVER_EXEC_H 17 #define COMMUNICATIONNETSTACK_WEBSOCKET_SERVER_EXEC_H 18 19 #include "server_start_context.h" 20 #include "list_all_connections_context.h" 21 #include "server_send_context.h" 22 #include "server_close_context.h" 23 #include "server_stop_context.h" 24 25 namespace OHOS::NetStack::Websocket { 26 27 struct ClientInfo { 28 int32_t cnt; 29 uint64_t lastConnectionTime; 30 }; 31 32 struct WebSocketMessage { 33 std::string data; 34 WebSocketConnection connection; 35 }; 36 37 class WebSocketServerExec final { 38 public: 39 /* async work execute */ 40 static bool ExecServerStart(ServerStartContext *context); 41 42 static bool ExecListAllConnections(ListAllConnectionsContext *context); 43 44 static bool ExecServerClose(ServerCloseContext *context); 45 46 static bool ExecServerSend(ServerSendContext *context); 47 48 static bool ExecServerStop(ServerStopContext *context); 49 50 static int lwsServerCallback(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 51 52 /* async work callback */ 53 static napi_value ServerStartCallback(ServerStartContext *context); 54 55 static napi_value ListAllConnectionsCallback(ListAllConnectionsContext *context); 56 57 static napi_value ServerCloseCallback(ServerCloseContext *context); 58 59 static napi_value ServerSendCallback(ServerSendContext *context); 60 61 static napi_value ServerStopCallback(ServerStopContext *context); 62 63 private: 64 static int HttpDummy(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 65 66 static int RaiseServerError(EventManager *manager); 67 68 static int LwsCallbackEstablished(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 69 70 static int LwsCallbackFilterProtocolConnection(lws *wsi, lws_callback_reasons reason, 71 void *user, void *in, size_t len); 72 73 static int LwsCallbackReceive(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 74 75 static int LwsCallbackServerWriteable(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 76 77 static int LwsCallbackWsPeerInitiatedCloseServer(lws *wsi, lws_callback_reasons reason, 78 void *user, void *in, size_t len); 79 80 static int LwsCallbackClosed(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 81 82 static int LwsCallbackWsiDestroyServer(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 83 84 static int LwsCallbackProtocolDestroyServer(lws *wsi, lws_callback_reasons reason, 85 void *user, void *in, size_t len); 86 87 static void OnConnect(lws *wsi, EventManager *manager); 88 89 static void OnServerClose(lws *wsi, EventManager *manager, lws_close_status closeStatus, 90 const std::string &closeReason); 91 92 static void OnServerMessage(lws *wsi, EventManager *manager, void *data, size_t length, 93 bool isBinary, bool isFinal); 94 95 static void OnServerError(EventManager *manager, int32_t code); 96 97 static void HandleServerRcvMessage(lws *wsi, EventManager *manager, void *data, 98 size_t length, bool isBinary, bool isFinal); 99 100 static void SetWebsocketMessage(lws *wsi, EventManager *manager, const std::string &msg, void *dataMsg); 101 102 static bool IsOverMaxClientConns(EventManager *manager, const std::string ip); 103 104 static bool IsOverMaxConcurrentClientsCnt(EventManager *manager, const std::vector<WebSocketConnection> connections, 105 const std::string ip); 106 107 static bool IsOverMaxCntForOneClient(EventManager *manager, const std::vector<WebSocketConnection> connections, 108 const std::string ip); 109 110 static bool IsAllowConnection(const std::string &clientId); 111 112 static bool IsIpInBanList(const std::string &id); 113 114 static bool IsHighFreqConnection(const std::string &id); 115 116 static void AddBanList(const std::string &id); 117 118 static void UpdataClientList(const std::string &id); 119 120 static lws *GetClientWsi(const std::string clientId); 121 122 static uint64_t GetCurrentSecond(); 123 124 static void CloseAllConnection(const std::shared_ptr<UserData> &userData); 125 126 static void FillServerContextInfo(ServerStartContext *context, std::shared_ptr<EventManager> &manager, 127 lws_context_creation_info &info); 128 129 static bool FillServerCertPath(ServerStartContext *context, lws_context_creation_info &info); 130 131 static void StartService(lws_context_creation_info &info, std::shared_ptr<EventManager> &manager); 132 133 static void AddConnections(const std::string &Id, lws *wsi, std::shared_ptr<UserData> &userData, 134 WebSocketConnection &conn); 135 136 static void RemoveConnections(const std::string &Id, UserData &userData); 137 138 static bool GetPeerConnMsg(lws *wsi, EventManager *manager, std::string &clientId, WebSocketConnection &conn); 139 140 static std::vector<WebSocketConnection> GetConnections(); 141 }; 142 } // namespace OHOS::NetStack::Websocket 143 #endif /* COMMUNICATIONNETSTACK_WEBSOCKET_EXEC_H */