コード例 #1
0
        public static void UpdateUser(string uuid, RayPortUser rayPortUser)
        {
            RayConfigRepository repo = new RayConfigRepository();
            var rayPort = repo.GetRayPorts()
                          .FirstOrDefault(
                o => o.Settings
                .Clients
                ?.Contains(rayPortUser, RayPortUserEqualityComparer.Default) ?? false);
            var rayPortUsers = rayPort.Settings.Clients;
            int index        = rayPortUsers.IndexOf(
                rayPortUsers.FirstOrDefault(r => r.Uuid == uuid));
            int port = rayPort.Port;

            JObject rayConfigObj = repo.GetRayConfigJObject();

            JArray clientsObj = rayConfigObj.SelectToken($"inbounds[?(@port == {port})].settings.clients") as JArray;

            if (!(rayConfigObj
                  .SelectToken($"inbounds[?(@port == {port})].settings.clients[?(@id == '{uuid}')]") is JObject clientObjToUpdate))
            {
                return;
            }

            clientsObj.Remove(clientObjToUpdate);
            clientsObj.Insert(index, JObject.FromObject(rayPortUser, RayConfigJsonSetting.JsonSerializer));

            WriteJsonToFile(rayConfigObj);
        }
コード例 #2
0
        public static RayPort GetRayPort(RayPortUser rayPortUser)
        {
            var     repo     = new RayConfigRepository();
            var     rayPorts = repo.GetRayPorts();
            RayPort rayPort  = null;

            foreach (var r in rayPorts)
            {
                if (r.Settings?.Clients?.Any(obj => obj.Uuid == rayPortUser.Uuid) ?? false)
                {
                    rayPort = r;
                    break;
                }
            }
            return(rayPort);
        }
コード例 #3
0
        public IList <RayPortUser> GetRayPortsUsers()
        {
            var repo = new RayConfigRepository();

            return(repo.GetRayPorts().SelectMany(r => r.Settings?.Clients ?? new List <RayPortUser>()).ToList());
        }
コード例 #4
0
        /// <summary>
        /// 配置流量统计功能
        /// </summary>

        /*
         * "stats": {},
         *  "api": {
         *      "tag": "api",
         *      "services": [
         *          "StatsService"
         *      ]
         *  },
         *  "policy": {
         *      "levels": {
         *          "0": {
         *              "statsUserUplink": true,
         *              "statsUserDownlink": true
         *          }
         *      },
         *      "system": {
         *          "statsInboundUplink": true,
         *          "statsInboundDownlink": true
         *      }
         *  },
         */
        public static void ConfigTrafficStatistic()
        {
            JObject rootJObj  = RayConfig.RayConfigJObject;
            bool    hasChange = false;

            if (!rootJObj.ContainsKey("stats"))
            {
                rootJObj.Add("stats", JObject.Parse("{}"));
                hasChange = true;
            }
            if (!rootJObj.ContainsKey("api") ||
                (!(rootJObj.SelectToken("api.services") as JArray)?.Children().Contains("StatsService") ?? false))
            {
                var api = new
                {
                    tag      = "api",
                    services = new string[] { "StatsService", "LoggerService", "HandlerService" },
                };
                rootJObj.Add("api", JObject.FromObject(api));
                hasChange = true;
            }

            if (!rootJObj.ContainsKey("policy"))
            {
                var policy = new
                {
                    levels = new Dictionary <string, object>
                    {
                        {
                            "1", new Dictionary <string, bool>
                            {
                                { "statsUserUplink", true },
                                { "statsUserDownlink", true },
                            }
                        },
                    },
                    system = new Dictionary <string, bool>
                    {
                        { "statsInboundUplink", true },
                        { "statsInboundDownlink", true },
                    }
                };
                rootJObj.Add("policy", JObject.FromObject(policy));
                hasChange = true;
            }

            if (!(rootJObj.SelectToken("routing.rules[?(@inboundTag[0] == 'api')]") is JObject))
            {
                JArray rules = (rootJObj.SelectToken("routing.rules") as JArray) ?? new JArray();
                var    rule  = new
                {
                    inboundTag  = new string[] { "api" },
                    outboundTag = "api",
                    type        = "field"
                };
                rules.Insert(0, JObject.FromObject(rule));
                hasChange = true;
            }

            if (null == (rootJObj.SelectToken("routing") as JObject).Property("strategy"))
            {
                (rootJObj.SelectToken("routing") as JObject).Add("strategy", "rules");
                hasChange = true;
            }

            RayConfigRepository repo     = new RayConfigRepository();
            RayPort             statPort = new RayPort
            {
                Listen   = "127.0.0.1",
                Port     = 13888,
                Protocol = "dokodemo-door",
                Settings = new RayPortSettings {
                    Address = "127.0.0.1"
                },
                Tag = "api",
            };

            repo.AddPort(statPort);

            if (hasChange)
            {
                WriteJsonToFile(rootJObj);
            }
        }