示例#1
0
            public override IMessage Invoke(IMessage rawMsg)
            {
                var msg    = (IMethodCallMessage)rawMsg;
                var method = msg.MethodBase as MethodInfo;
                var args   = msg.Args;

                if (method == null || args == null)
                {
                    throw new InternalErrorException("The transparent proxy received an invalid message.");
                }

                var parameters = method.GetParameters();

                var hArgs = new List <HArg> {
                    new HArg("Method", mangledMethodName(method)),
                    new HArg("Arguments", new JsonList(args.Select((arg, i) =>
                                                                   parameters[i].IsOut ? null :
                                                                   arg is Delegate ? new JsonDict {
                        { ":delegate", true }
                    } :
                                                                   ClassifyJson.Serialize(parameters[i].ParameterType, arg))))
                };

                if (_objectId != -1)
                {
                    hArgs.Add(new HArg("ObjectID", _objectId));
                }
                var responseRaw = _client.Post(_url, hArgs.ToArray());
                var response    = ClassifyJson.Deserialize <CommunicatorResult>(responseRaw.DataJson);

                var responseRet = response as CommunicatorResultReturn;

                if (responseRet != null)
                {
                    var refOut = Enumerable.Range(0, parameters.Length)
                                 .Where(i => parameters[i].ParameterType.IsByRef)
                                 .Select(i => translate(parameters[i].ParameterType.GetElementType(), responseRet.RefOutArguments[i]))
                                 .ToArray();
                    return(new ReturnMessage(
                               translate(method.ReturnType, responseRet.ReturnValue),
                               refOut,
                               refOut.Length,
                               msg.LogicalCallContext,
                               msg));
                }

                throw new NotImplementedException();
            }
示例#2
0
 private void login(HClient http)
 {
     http.Cookies    = new CookieContainer();
     http.ReqReferer = Settings.BaseUrl + "/Main_Login.asp";
     http.Post(Settings.BaseUrl + "/login.cgi",
               new HArg("group_id", ""),
               new HArg("action_mode", ""),
               new HArg("action_script", ""),
               new HArg("action_wait", "5"),
               new HArg("current_page", "Main_Login.asp"),
               new HArg("next_page", "index.asp"),
               new HArg("login_authorization", Settings.LoginAuth)).Expect(HttpStatusCode.OK);
     http.Cookies.Add(new Uri(Settings.BaseUrl), new Cookie("asus_token", http.Cookies.GetCookies(new Uri(Settings.BaseUrl + "/login.cgi"))[0].Value));
     http.Cookies.Add(new Uri(Settings.BaseUrl), new Cookie("bw_rtab", "INTERNET"));
     http.Cookies.Add(new Uri(Settings.BaseUrl), new Cookie("traffic_warning_0", "2017.7:1"));
 }
示例#3
0
        public async void SyncDLCount(string item_id, string optime, string type, string value)
        {
            var dic = new Dictionary <string, string>();

            dic["sync_infos[][item_id]"] = item_id;
            dic["sync_infos[][optime]"]  = optime;
            dic["sync_infos[][type]"]    = type;
            dic["sync_infos[][value]"]   = value;

            client.RequestUri = $"https://theinfoapps.com/myfm/dlquota/sync/?idfa={idfa}";
            clientHeader.AddParam(dic);
            clientHeader.ContentType = "application/x-www-form-urlencoded";
            var result = await client.Post(clientCookie, clientHeader);

            obj = DynamicJson.Parse(result);
            if (!IsSyncDLCountSucceeded(obj))
            {
                throw new ArgumentException();
            }
        }
示例#4
0
        private void thread()
        {
            var http = new HClient();

            login(http);
            var    ptPrev        = _history.LastOrDefault();
            double avgRx         = 0;
            double avgTx         = 0;
            double avgRxFast     = 0;
            double avgTxFast     = 0;
            var    recentHistory = new Queue <(double txRate, double rxRate)>();

            while (true)
            {
                var pt = new RouterHistoryPoint();

                try
                {
                    http.ReqReferer = Settings.BaseUrl + "/Main_TrafficMonitor_realtime.asp";
                    var resp = http.Post(Settings.BaseUrl + "/update.cgi",
                                         new HArg("output", "netdev"),
                                         new HArg("_http_id", "TIDe855a6487043d70a")).Expect(HttpStatusCode.OK);
                    pt.Timestamp = DateTime.UtcNow;

                    var match = Regex.Match(resp.DataString, @"'INTERNET':{rx:0x(?<rx>.*?),tx:0x(?<tx>.*?)}");
                    if (!match.Success)
                    {
                        throw new Exception();
                    }

                    pt.TxTotal = Convert.ToInt64(match.Groups["tx"].Value, 16);
                    pt.RxTotal = Convert.ToInt64(match.Groups["rx"].Value, 16);
                }
                catch
                {
                    Thread.Sleep(TimeSpan.FromSeconds(60));
                    try { login(http); continue; }
                    catch { }
                }

                _history.Enqueue(pt);
                while (_history.Peek().Timestamp < DateTime.UtcNow.AddHours(-24))
                {
                    _history.Dequeue();
                }

                if (ptPrev != null)
                {
                    while (pt.TxTotal < ptPrev.TxTotal)
                    {
                        pt.TxTotal += uint.MaxValue;
                    }
                    while (pt.RxTotal < ptPrev.RxTotal)
                    {
                        pt.RxTotal += uint.MaxValue;
                    }
                    var txDiff   = pt.TxTotal - ptPrev.TxTotal;
                    var rxDiff   = pt.RxTotal - ptPrev.RxTotal;
                    var timeDiff = (pt.Timestamp - ptPrev.Timestamp).TotalSeconds;

                    using (var db = Db.Open())
                        db.Insert(new TbRouterHistoryEntry {
                            Timestamp = pt.Timestamp.ToDbDateTime(), TxTotal = pt.TxTotal, RxTotal = pt.RxTotal
                        });

                    var rxRate = rxDiff / timeDiff;
                    var txRate = txDiff / timeDiff;

                    recentHistory.Enqueue((txRate: txRate, rxRate: rxRate));
                    while (recentHistory.Count > 24)
                    {
                        recentHistory.Dequeue();
                    }

                    avgRx     = avgRx * Settings.AverageDecay + rxRate * (1 - Settings.AverageDecay);
                    avgTx     = avgTx * Settings.AverageDecay + txRate * (1 - Settings.AverageDecay);
                    avgRxFast = avgRxFast * Settings.AverageDecayFast + rxRate * (1 - Settings.AverageDecayFast);
                    avgTxFast = avgTxFast * Settings.AverageDecayFast + txRate * (1 - Settings.AverageDecayFast);
                    if (Math.Min(avgRx, avgRxFast) / Math.Max(avgRx, avgRxFast) < 0.5)
                    {
                        avgRx = avgRxFast = rxRate;
                    }
                    if (Math.Min(avgTx, avgTxFast) / Math.Max(avgTx, avgTxFast) < 0.5)
                    {
                        avgTx = avgTxFast = txRate;
                    }

                    var dto = new RouterDto();
                    dto.ValidUntilUtc = DateTime.UtcNow + TimeSpan.FromSeconds(10);

                    dto.RxLast          = (int)Math.Round(rxRate);
                    dto.TxLast          = (int)Math.Round(txRate);
                    dto.RxAverageRecent = (int)Math.Round(avgRx);
                    dto.TxAverageRecent = (int)Math.Round(avgTx);
                    dto.HistoryRecent   = recentHistory.Select(h => new RouterHistoryPointDto {
                        TxRate = h.txRate, RxRate = h.rxRate
                    }).ToArray();
                    dto.HistoryHourly = Enumerable.Range(1, 24).Select(h =>
                    {
                        var from     = new DateTime(pt.Timestamp.Year, pt.Timestamp.Month, pt.Timestamp.Day, pt.Timestamp.Hour, 0, 0, DateTimeKind.Utc).AddHours(-24 + h);
                        var to       = from.AddHours(1);
                        var earliest = _history.Where(p => p.Timestamp >= from && p.Timestamp < to).MinElementOrDefault(p => p.Timestamp);
                        var latest   = _history.Where(p => p.Timestamp >= from && p.Timestamp < to).MaxElementOrDefault(p => p.Timestamp);
                        RouterHistoryPointDto result = null;
                        if (earliest != null && earliest.Timestamp != latest.Timestamp)
                        {
                            result = new RouterHistoryPointDto
                            {
                                TxRate = (latest.TxTotal - earliest.TxTotal) / (latest.Timestamp - earliest.Timestamp).TotalSeconds,
                                RxRate = (latest.RxTotal - earliest.RxTotal) / (latest.Timestamp - earliest.Timestamp).TotalSeconds,
                            }
                        }
                        ;
                        if (result == null || result.TxRate < 0 || result.RxRate < 0)
                        {
                            return(null);
                        }
                        else
                        {
                            return(result);
                        }
                    }).ToArray();

                    SendUpdate(dto);
                }

                ptPrev = pt;

                Thread.Sleep(TimeSpan.FromSeconds(Settings.QueryInterval));
            }
        }