Ejemplo n.º 1
0
 private static void CompileJumpTarget(KeyValuePair<int, ProxyRule>[] rules, ProxyRule rule)
 {
     if (rule.Action == MatchAction.GotoName) {
         rule.Action = MatchAction.Goto;
         rule._jumpTarget = ((rules.Select((e, i) => new { Index = i, Item = e }).FirstOrDefault(x => x.Item.Value.Name == rule.ActionString)?.Index) ?? rules.Length - 1);
     } else if (rule.Action == MatchAction.Goto) {
         int number;
         if (!int.TryParse(rule.ActionString, out number)) number = int.MaxValue;
         rule._jumpTarget = rules.Select((e, i) => new { Index = i, Item = e }).First(x => x.Item.Key >= number).Index;
     }
 }
Ejemplo n.º 2
0
 public static ExecutionResult ExecuteRules(ProxyRule[] rules, string method, Uri uri)
 {
     var result = ExecutionResult.Default;
     for (int i = 0; i < rules.Length; i++) {
         var rule = rules[i];
         string match;
         switch(rule.MatchIn) {
             case MatchPortion.FullUrl:
                 match = uri.AbsoluteUri;
                 break;
             case MatchPortion.Host:
                 match = uri.Host;
                 break;
             case MatchPortion.Port:
                 match = uri.Port.ToString();
                 break;
             case MatchPortion.HostAndPort:
                 match = string.Format(uri.HostNameType == UriHostNameType.IPv6 ? "[{0}]:{1}" : "{0}:{1}", uri.Host, uri.Port);
                 break;
             case MatchPortion.Method:
                 match = method;
                 break;
             case MatchPortion.Path:
                 match = uri.AbsolutePath;
                 break;
             case MatchPortion.Query:
                 match = uri.Query;
                 break;
             case MatchPortion.PathAndQuery:
                 match = uri.PathAndQuery;
                 break;
             default:
                 continue;
         }
         if (!rule.Negative ^ rule.Matcher.Match(match)) continue;
         switch(rule.Action) {
             case MatchAction.Proxy:
                 result.Proxy = rule.ActionString;
                 goto case MatchAction.SystemProxy;
             case MatchAction.Block:
             case MatchAction.SystemProxy:
                 result.Action = rule.Action;
                 return result;
             case MatchAction.SetProxyAuth:
                 result.ProxyAuth = rule.ActionString;
                 break;
             case MatchAction.Goto:
                 i = rule._jumpTarget;
                 break;
         }
     }
     return result;
 }
Ejemplo n.º 3
0
        private static void CompileJumpTarget(KeyValuePair <int, ProxyRule>[] rules, ProxyRule rule)
        {
            if (rule.Action == MatchAction.GotoName)
            {
                rule.Action = MatchAction.Goto;

                rule._jumpTarget = ((rules.Select((e, i) => new { Index = i, Item = e }).FirstOrDefault(x => x.Item.Value.Name == rule.ActionString)?.Index) ?? rules.Length - 1);
            }
            else if (rule.Action == MatchAction.Goto)
            {
                int number;
                if (!int.TryParse(rule.ActionString, out number))
                {
                    number = int.MaxValue;
                }
                rule._jumpTarget = rules.Select((e, i) => new { Index = i, Item = e }).First(x => x.Item.Key >= number).Index;
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Fiddler からのリクエスト発行時にプロキシを挟む設定を行います。
        /// </summary>
        /// <param name="requestingSession">通信を行おうとしているセッション。</param>
        private void Fiddler_BeforeRequest(Fiddler.Session requestingSession)
        {
            if (requestingSession.hostname == "kancolleviewer.local")
            {
                requestingSession.utilCreateResponseAndBypassServer();
                var path       = requestingSession.PathAndQuery;
                var queryIndex = path.IndexOf('?');
                if (queryIndex >= 0)
                {
                    path = path.Substring(0, queryIndex);
                }

                Action <Fiddler.Session> handler;
                if (localRequestHandlers.TryGetValue(path, out handler))
                {
                    requestingSession.oResponse.headers.HTTPResponseCode   = 200;
                    requestingSession.oResponse.headers.HTTPResponseStatus = "200 OK";
                    handler?.Invoke(requestingSession);
                }
                else
                {
                    requestingSession.oResponse.headers.HTTPResponseCode   = 410;
                    requestingSession.oResponse.headers.HTTPResponseStatus = "410 Gone";
                }
                return;
            }

            var settings = this.UpstreamProxySettings;

            if (settings == null)
            {
                return;
            }

            var compiled = settings.CompiledRules;

            if (compiled == null)
            {
                settings.CompiledRules = compiled = ProxyRule.CompileRule(settings.Rules);
            }
            var result = ProxyRule.ExecuteRules(compiled, requestingSession.RequestMethod, new Uri(requestingSession.fullUrl));

            if (result.Action == ProxyRule.MatchAction.Block)
            {
                requestingSession.utilCreateResponseAndBypassServer();
                requestingSession.oResponse.headers.HTTPResponseCode   = 450;
                requestingSession.oResponse.headers.HTTPResponseStatus = "450 Blocked As Requested";
                return;
            }

            if (KanColleClient.Current.Settings.DisallowSortieWithHeavyDamage)
            {
                if (KanColleClient.Current.Homeport.Organization.Fleets.Any(x => x.Value.IsInSortie && x.Value.State.Situation.HasFlag(Models.FleetSituation.HeavilyDamaged)))
                {
                    if (requestingSession.PathAndQuery.Length > 7 &&
                        requestingSession.PathAndQuery.Substring(0, 7) == "/kcsapi" &&
                        (requestingSession.PathAndQuery.Contains("battle") || requestingSession.PathAndQuery.Contains("sortie")) &&
                        !requestingSession.PathAndQuery.Contains("practice") &&
                        !requestingSession.PathAndQuery.Contains("result"))
                    {
                        requestingSession.utilCreateResponseAndBypassServer();
                        requestingSession.oResponse.headers.HTTPResponseCode   = 450;
                        requestingSession.oResponse.headers.HTTPResponseStatus = "450 Blocked As Requested";
                        return;
                    }
                }
            }

            if (result.Action == ProxyRule.MatchAction.Proxy && result.Proxy != null)
            {
                requestingSession["X-OverrideGateway"] = result.Proxy;
                if (result.ProxyAuth != null && !requestingSession.RequestHeaders.Exists("Proxy-Authorization"))
                {
                    requestingSession["X-OverrideGateway"] = result.Proxy;
                    requestingSession.RequestHeaders.Add("Proxy-Authorization", result.ProxyAuth);
                }
            }

            requestingSession.bBufferResponse = false;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Fiddler からのリクエスト発行時にプロキシを挟む設定を行います。
        /// </summary>
        /// <param name="requestingSession">通信を行おうとしているセッション。</param>
        private void Fiddler_BeforeRequest(Session requestingSession)
        {
            if (requestingSession.hostname == "kancolleviewer.local")
            {
                requestingSession.utilCreateResponseAndBypassServer();
                var path       = requestingSession.PathAndQuery;
                var queryIndex = path.IndexOf('?');
                if (queryIndex >= 0)
                {
                    path = path.Substring(0, queryIndex);
                }

                Action <Session> handler;
                if (localRequestHandlers.TryGetValue(path, out handler))
                {
                    requestingSession.oResponse.headers.HTTPResponseCode   = 200;
                    requestingSession.oResponse.headers.HTTPResponseStatus = "200 OK";
                    handler?.Invoke(requestingSession);
                }
                else
                {
                    requestingSession.oResponse.headers.HTTPResponseCode   = 410;
                    requestingSession.oResponse.headers.HTTPResponseStatus = "410 Gone";
                }
                return;
            }

            var settings = this.UpstreamProxySettings;

            if (settings == null)
            {
                return;
            }

            var compiled = settings.CompiledRules;

            if (compiled == null)
            {
                settings.CompiledRules = compiled = ProxyRule.CompileRule(settings.Rules);
            }
            var result = ProxyRule.ExecuteRules(compiled, requestingSession.RequestMethod, new Uri(requestingSession.fullUrl));

            if (result.Action == ProxyRule.MatchAction.Block)
            {
                requestingSession.utilCreateResponseAndBypassServer();
                requestingSession.oResponse.headers.HTTPResponseCode   = 403;
                requestingSession.oResponse.headers.HTTPResponseStatus = "403 Forbidden";
                return;
            }

            if (result.Action == ProxyRule.MatchAction.Proxy && result.Proxy != null)
            {
                requestingSession["X-OverrideGateway"] = result.Proxy;
                if (result.ProxyAuth != null && !requestingSession.RequestHeaders.Exists("Proxy-Authorization"))
                {
                    requestingSession["X-OverrideGateway"] = result.Proxy;
                    requestingSession.RequestHeaders.Add("Proxy-Authorization", result.ProxyAuth);
                }
            }

            requestingSession.bBufferResponse = false;
        }