htx_swap_ws.rs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. use std::io::Read;
  2. use std::str::from_utf8;
  3. use std::sync::Arc;
  4. use std::sync::atomic::AtomicBool;
  5. use std::time::Duration;
  6. use chrono::Utc;
  7. use flate2::bufread::GzDecoder;
  8. use futures_channel::mpsc::{UnboundedReceiver, UnboundedSender};
  9. use once_cell::sync::Lazy;
  10. use ring::hmac;
  11. use serde_json::{json, Value};
  12. use tokio::sync::Mutex;
  13. use tokio::task;
  14. use tokio_tungstenite::tungstenite::{Error, Message};
  15. use tracing::{error, info, trace};
  16. use crate::response_base::ResponseData;
  17. use crate::socket_tool::AbstractWsMode;
  18. pub(crate) static LOGIN_DATA: Lazy<Mutex<(bool, bool)>> = Lazy::new(|| {
  19. println!("初始化...");
  20. // 0: 需要登录, 1:是否已经登录
  21. Mutex::new((false, false))
  22. });
  23. pub enum HtxSwapWsType {
  24. Public,
  25. Private,
  26. }
  27. //订阅频道
  28. #[derive(Clone)]
  29. pub enum HtxSwapSubscribeType {
  30. // 深度
  31. PuFuturesDepth,
  32. // // 公开成交
  33. // PuFuturesDeals,
  34. // 订单
  35. PrFuturesOrders,
  36. // 仓位
  37. PrFuturesPositions,
  38. // 余额
  39. PrFuturesBalances,
  40. }
  41. //账号信息
  42. #[derive(Clone)]
  43. #[allow(dead_code)]
  44. pub struct HtxSwapLogin {
  45. pub api_key: String,
  46. pub secret: String,
  47. }
  48. #[derive(Clone)]
  49. pub struct HtxSwapWs {
  50. //类型
  51. label: String,
  52. //地址
  53. address_url: String,
  54. //账号信息
  55. login_param: Option<HtxSwapLogin>,
  56. //币对
  57. symbol_s: Vec<String>,
  58. //订阅
  59. subscribe_types: Vec<HtxSwapSubscribeType>,
  60. //心跳间隔
  61. _heartbeat_time: u64,
  62. }
  63. impl HtxSwapWs {
  64. /*******************************************************************************************************/
  65. /*****************************************实例化一个对象****************************************************/
  66. /*******************************************************************************************************/
  67. pub fn new(login_param: Option<HtxSwapLogin>, ws_type: HtxSwapWsType) -> HtxSwapWs {
  68. return HtxSwapWs::new_label("default-HtxSwapWs".to_string(), login_param, ws_type);
  69. }
  70. pub fn new_label(label: String, login_param: Option<HtxSwapLogin>, ws_type: HtxSwapWsType) -> HtxSwapWs
  71. {
  72. /*******公共频道-私有频道数据组装*/
  73. let address_url = match ws_type {
  74. HtxSwapWsType::Public => {
  75. let url = "wss://api.hbdm.vn/linear-swap-ws".to_string();
  76. info!("走普通通道(不支持colo通道):{}", url);
  77. url
  78. }
  79. HtxSwapWsType::Private => {
  80. let url = "wss://api.hbdm.vn/linear-swap-notification".to_string();
  81. info!("走普通通道(不支持colo通道):{}", url);
  82. url
  83. }
  84. };
  85. HtxSwapWs {
  86. label,
  87. address_url,
  88. login_param,
  89. symbol_s: vec![],
  90. subscribe_types: vec![],
  91. _heartbeat_time: 1000 * 10,
  92. }
  93. }
  94. /*******************************************************************************************************/
  95. /*****************************************订阅函数********************************************************/
  96. /*******************************************************************************************************/
  97. //手动添加订阅信息
  98. pub fn set_subscribe(&mut self, subscribe_types: Vec<HtxSwapSubscribeType>) {
  99. self.subscribe_types.extend(subscribe_types);
  100. }
  101. //手动添加币对
  102. pub fn set_symbols(&mut self, mut b_array: Vec<String>) {
  103. for symbol in b_array.iter_mut() {
  104. // 大写
  105. *symbol = symbol.to_uppercase();
  106. // 字符串替换
  107. *symbol = symbol.replace("_", "-");
  108. }
  109. self.symbol_s = b_array;
  110. }
  111. //频道是否需要登录
  112. fn contains_pr(&self) -> bool {
  113. for t in self.subscribe_types.clone() {
  114. if match t {
  115. HtxSwapSubscribeType::PuFuturesDepth => false,
  116. // HtxSwapSubscribeType::PuFuturesDeals => false,
  117. //
  118. HtxSwapSubscribeType::PrFuturesOrders => true,
  119. HtxSwapSubscribeType::PrFuturesPositions => true,
  120. HtxSwapSubscribeType::PrFuturesBalances => true,
  121. } {
  122. return true;
  123. }
  124. }
  125. false
  126. }
  127. /*******************************************************************************************************/
  128. /*****************************************工具函数********************************************************/
  129. /*******************************************************************************************************/
  130. //订阅枚举解析
  131. pub fn enum_to_string(symbol: String, subscribe_type: HtxSwapSubscribeType, _login_param: Option<HtxSwapLogin>) -> Value {
  132. // let access_key;
  133. // let secret_key;
  134. // match login_param {
  135. // None => {
  136. // access_key = "".to_string();
  137. // secret_key = "".to_string();
  138. // }
  139. // Some(param) => {
  140. // access_key = param.api_key.clone();
  141. // secret_key = param.secret.clone();
  142. // }
  143. // }
  144. // let cid = "";
  145. match subscribe_type {
  146. HtxSwapSubscribeType::PuFuturesDepth => {
  147. json!({
  148. "sub":format!("market.{}.depth.step0", symbol.to_uppercase()),
  149. "id": "id5"
  150. })
  151. }
  152. HtxSwapSubscribeType::PrFuturesOrders => {
  153. json!({
  154. "op":"sub",
  155. "topic":format!("orders_cross.{}", symbol.to_lowercase())
  156. })
  157. }
  158. HtxSwapSubscribeType::PrFuturesPositions => {
  159. json!({
  160. "op":"sub",
  161. "topic":format!("positions_cross.{}", symbol.to_uppercase())
  162. })
  163. }
  164. HtxSwapSubscribeType::PrFuturesBalances => {
  165. json!({
  166. "op":"sub",
  167. "topic":format!("accounts_cross.USDT")
  168. })
  169. }
  170. }
  171. }
  172. //订阅信息生成
  173. pub fn get_subscription(&self) -> Vec<Value> {
  174. let mut args = vec![];
  175. // 只获取第一个
  176. // let symbol = self.symbol_s.get(0).unwrap().replace("_", "-");
  177. for symbol in &self.symbol_s {
  178. for subscribe_type in &self.subscribe_types {
  179. let ty_str = Self::enum_to_string(symbol.clone(),
  180. subscribe_type.clone(),
  181. self.login_param.clone(),
  182. );
  183. args.push(ty_str);
  184. }
  185. }
  186. args
  187. }
  188. /*******************************************************************************************************/
  189. /*****************************************socket基本*****************************************************/
  190. /*******************************************************************************************************/
  191. //链接
  192. pub async fn ws_connect_async<F, Future>(&mut self,
  193. is_shutdown_arc: Arc<AtomicBool>,
  194. handle_function: F,
  195. write_tx_am: &Arc<Mutex<UnboundedSender<Message>>>,
  196. write_to_socket_rx: UnboundedReceiver<Message>) -> Result<(), Error>
  197. where
  198. F: Fn(ResponseData) -> Future + Clone + Send + 'static + Sync,
  199. Future: std::future::Future<Output=()> + Send + 'static, // 确保 Fut 是一个 Future,且输出类型为 ()
  200. {
  201. let login_is = self.contains_pr();
  202. let login_param = self.login_param.clone();
  203. let subscription = self.get_subscription();
  204. let address_url = self.address_url.clone();
  205. let label = self.label.clone();
  206. // let heartbeat_time = self.heartbeat_time.clone();
  207. //心跳-- 方法内部线程启动
  208. // let write_tx_clone1 = Arc::clone(write_tx_am);
  209. let write_tx_clone2 = Arc::clone(write_tx_am);
  210. // tokio::spawn(async move {
  211. // trace!("线程-异步心跳-开始");
  212. // let ping_str = json!({
  213. // "method": "server.ping",
  214. // "params": {},
  215. // "id": 1
  216. // });
  217. // AbstractWsMode::ping_or_pong(write_tx_clone1, HeartbeatType::Custom(ping_str.to_string()), heartbeat_time).await;
  218. // trace!("线程-异步心跳-结束");
  219. // });
  220. //设置订阅
  221. let mut subscribe_array = vec![];
  222. for s in subscription {
  223. subscribe_array.push(s.to_string());
  224. }
  225. //链接
  226. let t2 = tokio::spawn(async move {
  227. let write_to_socket_rx_arc = Arc::new(Mutex::new(write_to_socket_rx));
  228. info!("启动连接");
  229. loop {
  230. info!("htx_usdt_swap socket 连接中……");
  231. // 需要登录
  232. if login_is {
  233. let mut login_data = LOGIN_DATA.lock().await;
  234. let login_param_real = login_param.clone().unwrap();
  235. login_data.0 = true;
  236. let utc_now = Utc::now();
  237. let timestamp = utc_now.format("%Y-%m-%dT%H:%M:%S").to_string();
  238. let timestamp_str = percent_encoding::utf8_percent_encode(timestamp.clone().as_str(), crate::htx_swap_rest::FRAGMENT).to_string();
  239. let access_key = login_param_real.api_key.clone();
  240. let secret_key = login_param_real.secret.clone();
  241. let param_str = format!("AccessKeyId={}&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp={}", access_key, timestamp_str);
  242. let signature = {
  243. let message = format!("GET\napi.hbdm.vn\n/linear-swap-notification\n{}", param_str);
  244. trace!("组装数据:\n{}", message);
  245. let hmac_key = hmac::Key::new(hmac::HMAC_SHA256, secret_key.as_bytes());
  246. let result = hmac::sign(&hmac_key, &message.as_bytes());
  247. let sign = base64::encode(result);
  248. sign
  249. };
  250. let login_param = json!({
  251. "op": "auth",
  252. "type": "api",
  253. "AccessKeyId": access_key,
  254. "SignatureMethod": "HmacSHA256",
  255. "SignatureVersion": "2",
  256. "Timestamp": timestamp,
  257. "Signature": signature,
  258. });
  259. let login_str = login_param.to_string();
  260. info!("发起ws登录: {}", login_str);
  261. let write_tx_c = Arc::clone(&write_tx_clone2);
  262. AbstractWsMode::send_subscribe(write_tx_c, Message::Text(login_str)).await;
  263. }
  264. AbstractWsMode::ws_connect_async(is_shutdown_arc.clone(), handle_function.clone(), address_url.clone(),
  265. login_is, label.clone(), subscribe_array.clone(), write_to_socket_rx_arc.clone(),
  266. Self::message_text_sync, Self::message_ping, Self::message_pong, Self::message_binary_sync,
  267. ).await;
  268. let mut login_data = LOGIN_DATA.lock().await;
  269. // 断联后 设置为没有登录
  270. login_data.1 = false;
  271. info!("htx_usdt_swap socket 断连,1s以后重连……");
  272. error!("htx_usdt_swap socket 断连,1s以后重连……");
  273. tokio::time::sleep(Duration::from_secs(1)).await;
  274. }
  275. });
  276. tokio::try_join!(t2).unwrap();
  277. trace!("线程-心跳与链接-结束");
  278. Ok(())
  279. }
  280. /*******************************************************************************************************/
  281. /*****************************************数据解析*****************************************************/
  282. /*******************************************************************************************************/
  283. //数据解析-Text
  284. pub async fn message_text(text: String) -> Option<ResponseData> {
  285. let response_data = Self::ok_text(text).await;
  286. Option::from(response_data)
  287. }
  288. pub fn message_text_sync(text: String) -> Option<ResponseData> {
  289. // 使用 tokio::task::block_in_place 来等待异步函数的结果
  290. task::block_in_place(|| {
  291. tokio::runtime::Handle::current().block_on(Self::message_text(text))
  292. })
  293. }
  294. //数据解析-ping
  295. pub fn message_ping(_pi: Vec<u8>) -> Option<ResponseData> {
  296. return Option::from(ResponseData::new("".to_string(), -300, "success".to_string(), Value::Null));
  297. }
  298. //数据解析-pong
  299. pub fn message_pong(_po: Vec<u8>) -> Option<ResponseData> {
  300. return Option::from(ResponseData::new("".to_string(), -301, "success".to_string(), Value::Null));
  301. }
  302. //数据解析-二进制
  303. pub async fn message_binary(binary: Vec<u8>) -> Option<ResponseData> {
  304. //二进制WebSocket消息
  305. let message_str = Self::parse_zip_data(binary);
  306. let response_data = Self::ok_text(message_str).await;
  307. Option::from(response_data)
  308. }
  309. pub fn message_binary_sync(binary: Vec<u8>) -> Option<ResponseData> {
  310. // 使用 tokio::task::block_in_place 来等待异步函数的结果
  311. task::block_in_place(|| {
  312. tokio::runtime::Handle::current().block_on(Self::message_binary(binary))
  313. })
  314. }
  315. //数据解析
  316. pub async fn ok_text(text: String) -> ResponseData
  317. {
  318. trace!("原始数据:{}", text);
  319. let mut res_data = ResponseData::new("".to_string(), 200, "success".to_string(), Value::Null);
  320. let json_value: Value = serde_json::from_str(&text).unwrap();
  321. /*公共:响应*/
  322. //心跳包
  323. let ping = json_value["ping"].as_i64();
  324. match ping {
  325. Some(ping_ts) => {
  326. let pong = json!({
  327. "pong": ping_ts
  328. });
  329. return ResponseData::new("".to_string(), -302, "success".to_string(), pong);
  330. }
  331. None => {}
  332. }
  333. //推送数据
  334. let status = json_value["status"].as_str();
  335. match status {
  336. Some(v) => {
  337. match v {
  338. "ok" => {
  339. res_data.channel = format!("{}", v);
  340. res_data.code = -201;
  341. res_data.data = json_value["data"].clone();
  342. }
  343. "error" => {
  344. res_data.code = 400;
  345. res_data.message = format!("{}", json_value["err-msg"].as_str().unwrap());
  346. res_data.channel = format!("{}", json_value["id"].as_str().unwrap());
  347. }
  348. &_ => {}
  349. }
  350. return res_data;
  351. }
  352. None => {}
  353. }
  354. let ch = json_value["ch"].as_str();
  355. match ch {
  356. Some(channel) => {
  357. res_data.channel = channel.parse().unwrap();
  358. res_data.code = 200;
  359. res_data.data = json_value["tick"].clone();
  360. return res_data;
  361. }
  362. None => {}
  363. }
  364. /*私有:响应*/
  365. let op = json_value["op"].as_str().unwrap();
  366. match op {
  367. "auth" => {
  368. let op = json_value["err-code"].as_i64().unwrap();
  369. res_data.channel = "auth".to_string();
  370. if op == 0 {
  371. res_data.code = -200;
  372. res_data.message = "登录成功".to_string();
  373. res_data.data = json_value["data"].clone();
  374. } else {
  375. res_data.code = 400;
  376. res_data.message = format!("登录失败:{}", json_value["err-msg"].as_str().unwrap());
  377. }
  378. return res_data;
  379. }
  380. "ping" => {
  381. let ts = json_value["ts"].as_str().unwrap();
  382. let pong = json!({
  383. "op": "pong",
  384. "ts": ts
  385. });
  386. return ResponseData::new("".to_string(), -302, "success".to_string(), pong);
  387. }
  388. "sub" => {
  389. res_data.channel = json_value["topic"].as_str().unwrap().to_string();
  390. res_data.code = -201;
  391. res_data.message = "订阅成功".to_string();
  392. return res_data;
  393. }
  394. "notify" => {
  395. res_data.channel = json_value["topic"].as_str().unwrap().to_string();
  396. res_data.code = 200;
  397. res_data.message = "推送数据".to_string();
  398. if json_value.get("data").is_some() {
  399. res_data.data = json_value["data"].clone();
  400. } else {
  401. res_data.data = json_value.clone();
  402. }
  403. return res_data;
  404. }
  405. _ => {}
  406. }
  407. res_data.code = 400;
  408. res_data.message = format!("未知响应内容");
  409. res_data.data = text.parse().unwrap();
  410. trace!("--------------------------------");
  411. res_data
  412. }
  413. fn parse_zip_data(p0: Vec<u8>) -> String {
  414. // 创建一个GzDecoder的实例,将压缩数据作为输入
  415. let mut decoder = GzDecoder::new(&p0[..]);
  416. // 创建一个缓冲区来存放解压缩后的数据
  417. let mut decompressed_data = Vec::new();
  418. // 读取解压缩的数据到缓冲区中
  419. decoder.read_to_end(&mut decompressed_data).expect("解压缩失败");
  420. let result = from_utf8(&decompressed_data)
  421. .expect("解压缩后的数据不是有效的UTF-8");
  422. // info!("解压缩数据 {:?}", result);
  423. result.to_string()
  424. }
  425. }