コード例 #1
0
        async Task SendResponseAsync(IHttpRequestResponse request, HttpSender httpSender)
        {
            try
            {
                if (request.RequestType == RequestType.TCP)
                {
                    string path = request.Path;
                    string responseContentType = ContentHTML;
                    string responseBody        = $"<html>\r\n<body>Unknown Path:\r\n{path}</body>\r\n</html>";


                    if (path.StartsWith("/echo"))
                    {
                        responseContentType = ContentHTML;
                        responseBody        = EP_echo(request);
                    }
                    else if (path.StartsWith("/history"))
                    {
                        responseContentType = ContentHTML;
                        responseBody        = EP_domainHistory(request);
                    }
                    else if (path.StartsWith("/check"))
                    {
                        responseContentType = ContentJSON;
                        responseBody        = EP_checkDomain(request);
                    }
                    else if (path.StartsWith("/reason"))
                    {
                        responseContentType = ContentHTML;
                        responseBody        = EP_show_reason(request);
                    }

                    var response = new HttpResponse
                    {
                        StatusCode     = (int)HttpStatusCode.OK,
                        ResponseReason = HttpStatusCode.OK.ToString(),
                        Headers        = new Dictionary <string, string>
                        {
                            { "Date", DateTime.UtcNow.ToString("r") },
                            { "Content-Type", responseContentType },
                            { "Connection", "close" }
                        },
                        Body = new MemoryStream(Encoding.UTF8.GetBytes(responseBody))
                    };

                    AndroidBridge.d(TAG, "[RESP-SEND] \"" + request.Path + "\" IP: " + ipString(request.RemoteIpEndPoint));

                    await httpSender.SendTcpResponseAsync(request, response).ConfigureAwait(false);

                    request.TcpClient.Close(); // Force close!

                    AndroidBridge.d(TAG, "[END] \"" + request.Path + "\" IP: " + ipString(request.RemoteIpEndPoint));
                }
            }
            catch (Exception ex)
            {
                AndroidBridge.e(TAG, ex.ToString());
            }
        }
コード例 #2
0
 public void Stop()
 {
     try
     {
         AndroidBridge.d(TAG, "Closing server");
         cancellationTokenSource?.Cancel();
         myServerInstance?.Dispose();
         myListener?.Stop();
     }
     catch (Exception ex)
     {
         AndroidBridge.e(TAG, ex.ToString());
     }
 }
コード例 #3
0
        public void Start(int port)
        {
            try
            {
                myListener = new TcpListener(IPAddress.Loopback, port)
                {
                    ExclusiveAddressUse = false
                };

                var httpSender = new HttpSender();

                cancellationTokenSource = new CancellationTokenSource();

                myServerInstance = myListener.ToHttpListenerObservable(cancellationTokenSource.Token)
                                   .Do(r =>
                {
                    AndroidBridge.d(TAG, "[START] \"" + r.Path + "\" IP: " + ipString(r.RemoteIpEndPoint));
                })
                                   // Send reply to browser
                                   .Select(r => Observable.FromAsync(() => SendResponseAsync(r, httpSender)))
                                   .Concat()
                                   .Subscribe(r =>
                {
                    //Console.WriteLine("Reply sent.");
                },
                                              ex =>
                {
                    AndroidBridge.e(TAG, ex.ToString());
                },
                                              () =>
                {
                    AndroidBridge.d(TAG, "Server completed");
                });
            }
            catch (Exception ex)
            {
                AndroidBridge.e(TAG, ex.ToString());
            }
        }
コード例 #4
0
        public void StartFlow()
        {
            try
            {
                // Reload policies:
                FilteringObjects.httpPolicy.reloadPolicy(File.ReadAllText(Filenames.HTTP_POLICY.getAppPrivate()));
                FilteringObjects.timePolicy.reloadPolicy(File.ReadAllText(Filenames.TIME_POLICY.getAppPrivate()));

                if (!AndroidBridge.isForegroundServiceUp())
                {
                    AndroidBridge.OnForgroundServiceStart = () =>
                    {
                        AndroidBridge.d(TAG, "Starting filtering flow");
                        myHTTPServer.StartHttpServer();
                        StartPeriodicTasks();
                    };

                    AndroidBridge.OnForgroundServiceStop = () =>
                    {
                        AndroidBridge.d(TAG, "Stopping filtering flow");
                        StopPeriodicTasks();
                        myHTTPServer.StopHTTPServer();
                    };

                    AndroidBridge.StartForgroundService();
                }
                else
                {
                    AndroidBridge.ToastIt("Service already up!");
                }
            }
            catch (Exception ex)
            {
                AndroidBridge.e(TAG, ex);
            }
        }
コード例 #5
0
        private static string ForceLockDate(DateTime date)
        {
            string result = "";

            try
            {
                string unlockPath = Filenames.LOCK_DATE.getAppPrivate();

                AndroidBridge.d(TAG, "(*) Locking until '" + date.ToString() + "'");
                if (File.Exists(unlockPath))
                {
                    File.Delete(unlockPath);
                }
                File.WriteAllText(unlockPath, date.ToString());

                result = "Sucess! Locked to " + date.ToString();
            }
            catch (Exception ex)
            {
                AndroidBridge.e(TAG, ex);
                result = ex.ToString();
            }
            return(result);
        }
コード例 #6
0
        public static void WifiCheckerCallback(List <string> wifiLists, TimeSpan?timeSinceLastScan, Exception callbackEx)
        {
            string reason         = "init";
            bool   shouldLogDebug = false;

            if (callbackEx != null)
            {
                // Dont even check, bad zone:
                FilteringObjects.isInWifiBlockZone = true;

                reason         = "callback with exepction.";
                shouldLogDebug = true;
            }
            else
            {
                TimeSpan actualTime = (timeSinceLastScan ?? TimeSpan.FromDays(1));
                if (actualTime > FilteringServiceFlow.WIFI_PERIOD_BLOCKED)
                {
                    // Dont even check, bad zone:
                    FilteringObjects.isInWifiBlockZone = true;

                    reason         = "results are old, wifi off? (Time: " + actualTime + ")";
                    shouldLogDebug = true;
                }
                else
                {
                    bool inBadZoneResult = true;
                    try
                    {
                        if (!File.Exists(Filenames.WIFI_POLICY.getAppPrivate()))
                        {
                            File.WriteAllText(Filenames.WIFI_POLICY.getAppPrivate(), "");
                        }

                        IEnumerable <string> currentRules = File.ReadAllLines(Filenames.WIFI_POLICY.getAppPrivate());
                        List <string>        newRules     = new List <string>();

                        //TODO Reason what wifi blocked.

                        inBadZoneResult = CheckBlacklistedWifiStandard.WifiHelper.fastBlockZoneCheck(wifiLists, currentRules, out newRules,
                                                                                                     (log) => { /* AndroidBridge.d(TAG, "[CheckBlacklistedWifi] " + log);*/ }, out reason
                                                                                                     );

                        File.WriteAllLines(Filenames.WIFI_POLICY.getAppPrivate(), newRules);
                    }
                    catch (Exception ex2)
                    {
                        AndroidBridge.e(TAG, ex2);
                        inBadZoneResult = false; // allow in case of exceptions...

                        reason = "Exception processing Wifi Algo";
                    }

                    if (!showReason && FilteringObjects.isInWifiBlockZone != inBadZoneResult)
                    {
                        AndroidBridge.ToastIt("Changing blackzone to: " + inBadZoneResult);
                    }

                    FilteringObjects.isInWifiBlockZone = inBadZoneResult;
                }
            }

            reason = "[Blockzone? " + FilteringObjects.isInWifiBlockZone + "] " + reason;

            if (showReason)
            {
                AndroidBridge.ToastIt(reason);
                showReason = false; // Reset it until user mark it again.
            }

            if (shouldLogDebug)
            {
                if (callbackEx != null)
                {
                    reason += "\nCallback Exception:\n" + callbackEx.ToString();
                }
                AndroidBridge.d(TAG, reason);
            }
        }