public WebSocketConnection(String httpUri, TrustStore trustStore, CredentialsProvider credentialsProvider, string userAgent)
 {
     this.trustStore = trustStore;
      this.credentialsProvider = credentialsProvider;
     this.wsUri = httpUri.Replace("https://", "wss://")
                                       .Replace("http://", "ws://") + $"/v1/websocket/?login={credentialsProvider.GetUser()}&password={credentialsProvider.GetPassword()}";
     this.userAgent = userAgent;
 }
        private HttpResponseMessage getConnection(string urlFragment, string method, string body)
        {
            try
            {
                SignalConnectionInformation connectionInformation = getRandom(signalConnectionInformation);
                string       url        = connectionInformation.getUrl();
                May <string> hostHeader = connectionInformation.getHostHeader();
                Uri          uri        = new Uri(string.Format("{0}{1}", url, urlFragment));
                Debug.WriteLine("{0}: Uri {1}", TAG, uri);
                HttpClient connection = new HttpClient();

                var headers = connection.DefaultRequestHeaders;

                if (credentialsProvider.GetPassword() != null)
                {
                    string authHeader = getAuthorizationHeader();
                    Debug.WriteLine(String.Format("Authorization: {0}", authHeader), TAG);
                    headers.Add("Authorization", authHeader);
                }

                if (userAgent != null)
                {
                    headers.Add("X-Signal-Agent", userAgent);
                }

                if (hostHeader.HasValue)
                {
                    headers.Host = hostHeader.ForceGetValue();
                }

                StringContent content;
                if (body != null)
                {
                    content = new StringContent(body, Encoding.UTF8, "application/json");
                    Debug.WriteLine(body);
                }
                else
                {
                    content = new StringContent("");
                }
                switch (method)
                {
                case "POST":
                    return(connection.PostAsync(uri, content).Result);

                case "PUT":
                    return(connection.PutAsync(uri, content).Result);

                case "DELETE":
                    return(connection.DeleteAsync(uri).Result);

                case "GET":
                    return(connection.GetAsync(uri).Result);

                default:
                    throw new Exception("Unknown method: " + method);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("getConnection() failed:", TAG);
                Debug.WriteLine(e.Message);
                Debug.WriteLine(e.StackTrace);
                throw new PushNetworkException(e);
            }
        }
        private async Task <HttpResponseMessage> getConnection(String urlFragment, String method, String body)
        {
            try
            {
                /*SSLContext context = SSLContext.getInstance("TLS");
                 * context.init(null, trustManagers, null);*/

                Uri url = new Uri(String.Format("{0}{1}", serviceUrl, urlFragment));
                //Log.w(TAG, "Push service URL: " + serviceUrl);
                //Log.w(TAG, "Opening URL: " + url);
                var filter = new HttpBaseProtocolFilter();

                if (HttpClientCertificatePolicyState.Policy == HttpClientCertificatePolicy.DevelopmentMode)
                {
                    filter.IgnorableServerCertificateErrors.Add(Windows.Security.Cryptography.Certificates.ChainValidationResult.Expired);
                    filter.IgnorableServerCertificateErrors.Add(Windows.Security.Cryptography.Certificates.ChainValidationResult.Untrusted);
                    filter.IgnorableServerCertificateErrors.Add(Windows.Security.Cryptography.Certificates.ChainValidationResult.InvalidName);
                }

                //HttpURLConnection connection = (HttpURLConnection)url.openConnection();

                HttpClient connection = new HttpClient(filter);

                /*if (ENFORCE_SSL)
                 * {
                 *  ((HttpsURLConnection)connection).setSSLSocketFactory(context.getSocketFactory());
                 *  ((HttpsURLConnection)connection).setHostnameVerifier(new StrictHostnameVerifier());
                 * }*/

                var headers = connection.DefaultRequestHeaders;

                //connection.setRequestMethod(method);
                //headers.Add("Content-Type", "application/json");

                if (credentialsProvider.GetPassword() != null)
                {
                    headers.Add("Authorization", getAuthorizationHeader());
                }

                if (userAgent != null)
                {
                    headers.Add("X-Signal-Agent", userAgent);
                }

                /*if (body != null)
                 * {
                 *  connection.setDoOutput(true);
                 * }*/

                //connection.connect();

                HttpStringContent content;
                if (body != null)
                {
                    content = new HttpStringContent(body, Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");
                    Debug.WriteLine(body);
                }
                else
                {
                    content = new HttpStringContent("");
                }
                switch (method)
                {
                case "POST":
                    return(await connection.PostAsync(url, content));

                case "PUT":
                    return(await connection.PutAsync(url, content));

                case "DELETE":
                    return(await connection.DeleteAsync(url));

                case "GET":
                    return(await connection.GetAsync(url));

                default:
                    throw new Exception("Unknown method: " + method);
                }
            }
            catch (UriFormatException e)
            {
                throw new Exception(string.Format("Uri {0} {1} is wrong", serviceUrl, urlFragment));
            }
            catch (Exception e)
            {
                Debug.WriteLine(string.Format("Other exception {0}{1} is wrong", serviceUrl, urlFragment));
                throw new PushNetworkException(e);
            }
        }
예제 #4
0
 public WebSocketConnection(string httpUri, TrustStore trustStore, CredentialsProvider credentialsProvider, string userAgent)
 {
     this.trustStore          = trustStore;
     this.credentialsProvider = credentialsProvider;
     this.userAgent           = userAgent;
     this.attempts            = 0;
     this.connected           = false;
     this.wsUri = httpUri.Replace("https://", "wss://")
                  .Replace("http://", "ws://") + $"/v1/websocket/?login={credentialsProvider.GetUser()}&password={credentialsProvider.GetPassword()}";
     this.userAgent = userAgent;
 }
예제 #5
0
 public SignalWebSocketConnection(CancellationToken token, string httpUri, CredentialsProvider credentialsProvider, string userAgent)
 {
     Token = token;
     CredentialsProvider = credentialsProvider;
     UserAgent           = userAgent;
     if (credentialsProvider.GetDeviceId() == SignalServiceAddress.DEFAULT_DEVICE_ID)
     {
         WsUri = httpUri.Replace("https://", "wss://")
                 .Replace("http://", "ws://") + $"/v1/websocket/?login={credentialsProvider.GetUser()}&password={credentialsProvider.GetPassword()}";
     }
     else
     {
         WsUri = httpUri.Replace("https://", "wss://")
                 .Replace("http://", "ws://") + $"/v1/websocket/?login={credentialsProvider.GetUser()}.{credentialsProvider.GetDeviceId()}&password={credentialsProvider.GetPassword()}";
     }
     UserAgent = userAgent;
     WebSocket = new WebSocketWrapper(WsUri, token);
     WebSocket.OnConnect(Connection_OnOpened);
     WebSocket.OnMessage(Connection_OnMessage);
 }