|
1 |
| -using Coe.WebSocketWrapper; |
2 |
| -using Google.Protobuf; |
3 |
| -using libsignal.util; |
4 |
| -using libsignalservice.push; |
5 |
| -using libsignalservice.util; |
6 |
| -using Microsoft.Extensions.Logging; |
7 |
| -using System; |
8 |
| -using System.Collections.Concurrent; |
9 |
| -using System.IO; |
10 |
| -using System.Text; |
11 |
| -using System.Threading; |
12 |
| -using System.Threading.Tasks; |
13 |
| - |
14 |
| -namespace libsignalservice.websocket |
15 |
| -{ |
16 |
| - internal class SignalWebSocketConnection |
17 |
| - { |
18 |
| - private readonly ILogger Logger = LibsignalLogging.CreateLogger<SignalWebSocketConnection>(); |
19 |
| - private static readonly Object obj = new Object(); |
20 |
| - |
21 |
| - private readonly BlockingCollection<WebSocketRequestMessage> IncomingRequests = new BlockingCollection<WebSocketRequestMessage>(new ConcurrentQueue<WebSocketRequestMessage>()); |
22 |
| - private readonly ConcurrentDictionary<ulong, Tuple<CountdownEvent, uint, string>> OutgoingRequests = new ConcurrentDictionary<ulong, Tuple<CountdownEvent, uint, string>>(); |
23 |
| - |
24 |
| - private readonly string WsUri; |
25 |
| - private readonly CredentialsProvider CredentialsProvider; |
26 |
| - private readonly string UserAgent; |
27 |
| - private WebSocketWrapper WebSocket; |
28 |
| - private CancellationToken Token; |
29 |
| - |
30 |
| - internal SignalWebSocketConnection(CancellationToken token, string httpUri, CredentialsProvider credentialsProvider, string userAgent) |
31 |
| - { |
32 |
| - Token = token; |
33 |
| - CredentialsProvider = credentialsProvider; |
34 |
| - UserAgent = userAgent; |
35 |
| - if (credentialsProvider.GetDeviceId() == SignalServiceAddress.DEFAULT_DEVICE_ID) |
36 |
| - { |
37 |
| - WsUri = httpUri.Replace("https://", "wss://") |
38 |
| - .Replace("http://", "ws://") + $"/v1/websocket/?login={credentialsProvider.GetUser()}&password={credentialsProvider.GetPassword()}"; |
39 |
| - } |
40 |
| - else |
41 |
| - { |
42 |
| - WsUri = httpUri.Replace("https://", "wss://") |
43 |
| - .Replace("http://", "ws://") + $"/v1/websocket/?login={credentialsProvider.GetUser()}.{credentialsProvider.GetDeviceId()}&password={credentialsProvider.GetPassword()}"; |
44 |
| - } |
45 |
| - UserAgent = userAgent; |
46 |
| - WebSocket = new WebSocketWrapper(WsUri, token); |
47 |
| - WebSocket.OnConnect(Connection_OnOpened); |
48 |
| - WebSocket.OnMessage(Connection_OnMessage); |
49 |
| - } |
50 |
| - |
51 |
| - public void Connect() |
52 |
| - { |
53 |
| - WebSocket.Connect(); |
54 |
| - } |
55 |
| - |
56 |
| - private void Connection_OnOpened() |
57 |
| - { |
58 |
| - } |
59 |
| - |
60 |
| - private void Connection_OnMessage(byte[] obj) |
61 |
| - { |
62 |
| - var msg = WebSocketMessage.Parser.ParseFrom(obj); |
63 |
| - if (msg.Type == WebSocketMessage.Types.Type.Request) |
64 |
| - { |
65 |
| - Logger.LogTrace("Adding request to IncomingRequests"); |
66 |
| - IncomingRequests.Add(msg.Request); |
67 |
| - } |
68 |
| - else if (msg.Type == WebSocketMessage.Types.Type.Response) |
69 |
| - { |
70 |
| - Logger.LogTrace("Adding response {0} ({1} {2})", msg.Response.Id, msg.Response.Status, Encoding.UTF8.GetString(msg.Response.Body.ToByteArray())); |
71 |
| - var t = new Tuple<CountdownEvent, uint, string>(null, msg.Response.Status, Encoding.UTF8.GetString(msg.Response.Body.ToByteArray())); |
72 |
| - Tuple<CountdownEvent, uint, string> savedRequest; |
73 |
| - OutgoingRequests.TryGetValue(msg.Response.Id, out savedRequest); |
74 |
| - OutgoingRequests.AddOrUpdate(msg.Response.Id, t, (k, v) => t); |
75 |
| - savedRequest.Item1.Signal(); |
76 |
| - } |
77 |
| - } |
78 |
| - |
79 |
| - public void Disconnect() |
80 |
| - { |
81 |
| - Logger.LogWarning("Disconnect is not supported yet"); |
82 |
| - throw new NotImplementedException(); |
83 |
| - } |
84 |
| - |
85 |
| - |
86 |
| - |
87 |
| - /// <summary> |
88 |
| - /// Gets the next WebSocketRequestMessage from the websocket. |
89 |
| - /// If there are no received messages in the buffer, this method will block until there are, or this connection's token is cancelled. |
90 |
| - /// </summary> |
91 |
| - /// <remarks> |
92 |
| - /// keks |
93 |
| - /// </remarks> |
94 |
| - /// <returns>A WebSocketRequestMessage read from the websocket's pipe</returns> |
95 |
| - public WebSocketRequestMessage ReadRequestBlocking() |
96 |
| - { |
97 |
| - return IncomingRequests.Take(Token); |
98 |
| - } |
99 |
| - |
100 |
| - /// <summary> |
101 |
| - /// Sends a WebSocketRequestMessage to the Signal server. The returned task will block for a maximum of 10 seconds. |
102 |
| - /// </summary> |
103 |
| - /// <param name="request"></param> |
104 |
| - /// <returns>Returns a task that returns a server response or throws an exception.</returns> |
105 |
| - internal async Task<Tuple<uint, string>> SendRequest(WebSocketRequestMessage request) |
106 |
| - { |
107 |
| - Tuple<CountdownEvent, uint, string> t = new Tuple<CountdownEvent, uint, string>(new CountdownEvent(1), 0, null); |
108 |
| - WebSocketMessage message = new WebSocketMessage |
109 |
| - { |
110 |
| - Type = WebSocketMessage.Types.Type.Request, |
111 |
| - Request = request |
112 |
| - }; |
113 |
| - OutgoingRequests.AddOrUpdate(request.Id, t, (k, v) => t); |
114 |
| - WebSocket.OutgoingQueue.Add(message.ToByteArray()); |
115 |
| - return await Task.Run(() => |
116 |
| - { |
117 |
| - if (t.Item1.Wait(10 * 1000, Token)) |
118 |
| - { |
119 |
| - var handledTuple = OutgoingRequests[request.Id]; |
120 |
| - return new Tuple<uint, string>(handledTuple.Item2, handledTuple.Item3); |
121 |
| - } |
122 |
| - throw new IOException("wait for confirmation timeout"); |
123 |
| - }); |
124 |
| - } |
125 |
| - |
126 |
| - /// <summary> |
127 |
| - /// Sends a WebSocketResponseMessage to the Signal server. This method does not block until the message is actually sent. |
128 |
| - /// </summary> |
129 |
| - /// <param name="response"></param> |
130 |
| - public void SendResponse(WebSocketResponseMessage response) |
131 |
| - { |
132 |
| - WebSocketMessage message = new WebSocketMessage |
133 |
| - { |
134 |
| - Type = WebSocketMessage.Types.Type.Response, |
135 |
| - Response = response |
136 |
| - }; |
137 |
| - WebSocket.OutgoingQueue.Add(message.ToByteArray()); |
138 |
| - } |
139 |
| - |
140 |
| - private void SendKeepAlive(CancellationToken token, object state) |
141 |
| - { |
142 |
| - WebSocketMessage message = new WebSocketMessage |
143 |
| - { |
144 |
| - Type = WebSocketMessage.Types.Type.Request, |
145 |
| - Request = new WebSocketRequestMessage |
146 |
| - { |
147 |
| - Id = KeyHelper.getTime(), |
148 |
| - Path = "/v1/keepalive", |
149 |
| - Verb = "GET" |
150 |
| - }, |
151 |
| - }; |
152 |
| - WebSocket.OutgoingQueue.Add(message.ToByteArray()); |
153 |
| - } |
154 |
| - } |
155 |
| -} |
| 1 | +using Coe.WebSocketWrapper; |
| 2 | +using Google.Protobuf; |
| 3 | +using libsignal.util; |
| 4 | +using libsignalservice.push; |
| 5 | +using libsignalservice.util; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | +using System; |
| 8 | +using System.Collections.Concurrent; |
| 9 | +using System.IO; |
| 10 | +using System.Text; |
| 11 | +using System.Threading; |
| 12 | +using System.Threading.Tasks; |
| 13 | + |
| 14 | +namespace libsignalservice.websocket |
| 15 | +{ |
| 16 | + internal class SignalWebSocketConnection |
| 17 | + { |
| 18 | + private readonly ILogger Logger = LibsignalLogging.CreateLogger<SignalWebSocketConnection>(); |
| 19 | + private static readonly Object obj = new Object(); |
| 20 | + |
| 21 | + private readonly BlockingCollection<WebSocketRequestMessage> IncomingRequests = new BlockingCollection<WebSocketRequestMessage>(new ConcurrentQueue<WebSocketRequestMessage>()); |
| 22 | + private readonly ConcurrentDictionary<ulong, Tuple<CountdownEvent, uint, string>> OutgoingRequests = new ConcurrentDictionary<ulong, Tuple<CountdownEvent, uint, string>>(); |
| 23 | + |
| 24 | + private readonly string WsUri; |
| 25 | + private readonly CredentialsProvider CredentialsProvider; |
| 26 | + private readonly string UserAgent; |
| 27 | + private WebSocketWrapper WebSocket; |
| 28 | + private readonly CancellationToken Token; |
| 29 | + private readonly ConnectivityListener Listener; |
| 30 | + |
| 31 | + internal SignalWebSocketConnection(CancellationToken token, string httpUri, CredentialsProvider credentialsProvider, string userAgent, ConnectivityListener listener) |
| 32 | + { |
| 33 | + Token = token; |
| 34 | + CredentialsProvider = credentialsProvider; |
| 35 | + UserAgent = userAgent; |
| 36 | + Listener = listener; |
| 37 | + if (credentialsProvider.GetDeviceId() == SignalServiceAddress.DEFAULT_DEVICE_ID) |
| 38 | + { |
| 39 | + WsUri = httpUri.Replace("https://", "wss://") |
| 40 | + .Replace("http://", "ws://") + $"/v1/websocket/?login={credentialsProvider.GetUser()}&password={credentialsProvider.GetPassword()}"; |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + WsUri = httpUri.Replace("https://", "wss://") |
| 45 | + .Replace("http://", "ws://") + $"/v1/websocket/?login={credentialsProvider.GetUser()}.{credentialsProvider.GetDeviceId()}&password={credentialsProvider.GetPassword()}"; |
| 46 | + } |
| 47 | + UserAgent = userAgent; |
| 48 | + WebSocket = new WebSocketWrapper(WsUri, token); |
| 49 | + WebSocket.OnConnect(Connection_OnOpened); |
| 50 | + WebSocket.OnMessage(Connection_OnMessage); |
| 51 | + } |
| 52 | + |
| 53 | + public void Connect() |
| 54 | + { |
| 55 | + Listener?.OnConnecting(); |
| 56 | + WebSocket.Connect(); |
| 57 | + } |
| 58 | + |
| 59 | + private void Connection_OnOpened() |
| 60 | + { |
| 61 | + Listener?.OnConnecting(); |
| 62 | + } |
| 63 | + |
| 64 | + private void Connection_OnMessage(byte[] obj) |
| 65 | + { |
| 66 | + var msg = WebSocketMessage.Parser.ParseFrom(obj); |
| 67 | + if (msg.Type == WebSocketMessage.Types.Type.Request) |
| 68 | + { |
| 69 | + Logger.LogTrace("Adding request to IncomingRequests"); |
| 70 | + IncomingRequests.Add(msg.Request); |
| 71 | + } |
| 72 | + else if (msg.Type == WebSocketMessage.Types.Type.Response) |
| 73 | + { |
| 74 | + Logger.LogTrace("Adding response {0} ({1} {2})", msg.Response.Id, msg.Response.Status, Encoding.UTF8.GetString(msg.Response.Body.ToByteArray())); |
| 75 | + var t = new Tuple<CountdownEvent, uint, string>(null, msg.Response.Status, Encoding.UTF8.GetString(msg.Response.Body.ToByteArray())); |
| 76 | + Tuple<CountdownEvent, uint, string> savedRequest; |
| 77 | + OutgoingRequests.TryGetValue(msg.Response.Id, out savedRequest); |
| 78 | + OutgoingRequests.AddOrUpdate(msg.Response.Id, t, (k, v) => t); |
| 79 | + savedRequest.Item1.Signal(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public void Disconnect() |
| 84 | + { |
| 85 | + Logger.LogWarning("Disconnect is not supported yet"); |
| 86 | + throw new NotImplementedException(); |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Gets the next WebSocketRequestMessage from the websocket. |
| 93 | + /// If there are no received messages in the buffer, this method will block until there are, or this connection's token is cancelled. |
| 94 | + /// </summary> |
| 95 | + /// <remarks> |
| 96 | + /// keks |
| 97 | + /// </remarks> |
| 98 | + /// <returns>A WebSocketRequestMessage read from the websocket's pipe</returns> |
| 99 | + public WebSocketRequestMessage ReadRequestBlocking() |
| 100 | + { |
| 101 | + return IncomingRequests.Take(Token); |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Sends a WebSocketRequestMessage to the Signal server. The returned task will block for a maximum of 10 seconds. |
| 106 | + /// </summary> |
| 107 | + /// <param name="request"></param> |
| 108 | + /// <returns>Returns a task that returns a server response or throws an exception.</returns> |
| 109 | + internal async Task<Tuple<uint, string>> SendRequest(WebSocketRequestMessage request) |
| 110 | + { |
| 111 | + Tuple<CountdownEvent, uint, string> t = new Tuple<CountdownEvent, uint, string>(new CountdownEvent(1), 0, null); |
| 112 | + WebSocketMessage message = new WebSocketMessage |
| 113 | + { |
| 114 | + Type = WebSocketMessage.Types.Type.Request, |
| 115 | + Request = request |
| 116 | + }; |
| 117 | + OutgoingRequests.AddOrUpdate(request.Id, t, (k, v) => t); |
| 118 | + WebSocket.OutgoingQueue.Add(message.ToByteArray()); |
| 119 | + return await Task.Run(() => |
| 120 | + { |
| 121 | + if (t.Item1.Wait(10 * 1000, Token)) |
| 122 | + { |
| 123 | + var handledTuple = OutgoingRequests[request.Id]; |
| 124 | + return new Tuple<uint, string>(handledTuple.Item2, handledTuple.Item3); |
| 125 | + } |
| 126 | + throw new IOException("wait for confirmation timeout"); |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Sends a WebSocketResponseMessage to the Signal server. This method does not block until the message is actually sent. |
| 132 | + /// </summary> |
| 133 | + /// <param name="response"></param> |
| 134 | + public void SendResponse(WebSocketResponseMessage response) |
| 135 | + { |
| 136 | + WebSocketMessage message = new WebSocketMessage |
| 137 | + { |
| 138 | + Type = WebSocketMessage.Types.Type.Response, |
| 139 | + Response = response |
| 140 | + }; |
| 141 | + WebSocket.OutgoingQueue.Add(message.ToByteArray()); |
| 142 | + } |
| 143 | + |
| 144 | + private void SendKeepAlive(CancellationToken token, object state) |
| 145 | + { |
| 146 | + WebSocketMessage message = new WebSocketMessage |
| 147 | + { |
| 148 | + Type = WebSocketMessage.Types.Type.Request, |
| 149 | + Request = new WebSocketRequestMessage |
| 150 | + { |
| 151 | + Id = KeyHelper.getTime(), |
| 152 | + Path = "/v1/keepalive", |
| 153 | + Verb = "GET" |
| 154 | + }, |
| 155 | + }; |
| 156 | + WebSocket.OutgoingQueue.Add(message.ToByteArray()); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments