|
| 1 | +using System.Text.Json.Nodes; |
| 2 | +using ServiceLib; |
| 3 | +using ServiceLib.Enums; |
| 4 | +using ServiceLib.Models; |
| 5 | +using ServiceLib.Services.CoreConfig; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace ServiceLib.Tests; |
| 9 | + |
| 10 | +public class CoreConfigV2rayServiceTests |
| 11 | +{ |
| 12 | + private const string SendThrough = "198.51.100.10"; |
| 13 | + |
| 14 | + [Fact] |
| 15 | + public void GenerateClientConfigContent_OnlyAppliesSendThroughToRemoteProxyOutbounds() |
| 16 | + { |
| 17 | + var node = CreateProxyNode("proxy-1", "198.51.100.1", 443); |
| 18 | + var service = new CoreConfigV2rayService(CreateContext(node)); |
| 19 | + |
| 20 | + var result = service.GenerateClientConfigContent(); |
| 21 | + |
| 22 | + Assert.True(result.Success); |
| 23 | + |
| 24 | + var outbounds = GetOutbounds(result.Data?.ToString()); |
| 25 | + var proxyOutbound = outbounds.Single(outbound => outbound["tag"]!.GetValue<string>() == Global.ProxyTag); |
| 26 | + var directOutbound = outbounds.Single(outbound => outbound["tag"]!.GetValue<string>() == Global.DirectTag); |
| 27 | + var blockOutbound = outbounds.Single(outbound => outbound["tag"]!.GetValue<string>() == Global.BlockTag); |
| 28 | + |
| 29 | + Assert.Equal(SendThrough, proxyOutbound["sendThrough"]?.GetValue<string>()); |
| 30 | + Assert.Null(directOutbound["sendThrough"]); |
| 31 | + Assert.Null(blockOutbound["sendThrough"]); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void GenerateClientConfigContent_OnlyAppliesSendThroughToChainExitOutbounds() |
| 36 | + { |
| 37 | + var exitNode = CreateProxyNode("exit", "198.51.100.2", 443); |
| 38 | + var entryNode = CreateProxyNode("entry", "198.51.100.3", 443); |
| 39 | + var chainNode = CreateChainNode("chain", exitNode, entryNode); |
| 40 | + |
| 41 | + var service = new CoreConfigV2rayService(CreateContext( |
| 42 | + chainNode, |
| 43 | + allProxiesMap: new Dictionary<string, ProfileItem> |
| 44 | + { |
| 45 | + [exitNode.IndexId] = exitNode, |
| 46 | + [entryNode.IndexId] = entryNode, |
| 47 | + })); |
| 48 | + |
| 49 | + var result = service.GenerateClientConfigContent(); |
| 50 | + |
| 51 | + Assert.True(result.Success); |
| 52 | + |
| 53 | + var outbounds = GetOutbounds(result.Data?.ToString()) |
| 54 | + .Where(outbound => outbound["protocol"]?.GetValue<string>() is not ("freedom" or "blackhole" or "dns")) |
| 55 | + .ToList(); |
| 56 | + |
| 57 | + var sendThroughOutbounds = outbounds |
| 58 | + .Where(outbound => outbound["sendThrough"]?.GetValue<string>() == SendThrough) |
| 59 | + .ToList(); |
| 60 | + var chainedOutbounds = outbounds |
| 61 | + .Where(outbound => outbound["streamSettings"]?["sockopt"]?["dialerProxy"] is not null) |
| 62 | + .ToList(); |
| 63 | + |
| 64 | + Assert.Single(sendThroughOutbounds); |
| 65 | + Assert.All(chainedOutbounds, outbound => Assert.Null(outbound["sendThrough"])); |
| 66 | + } |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public void GenerateClientConfigContent_DoesNotApplySendThroughToTunRelayLoopbackOutbound() |
| 70 | + { |
| 71 | + var node = CreateProxyNode("proxy-1", "198.51.100.4", 443); |
| 72 | + var config = CreateConfig(); |
| 73 | + config.TunModeItem.EnableLegacyProtect = false; |
| 74 | + |
| 75 | + var service = new CoreConfigV2rayService(CreateContext( |
| 76 | + node, |
| 77 | + config, |
| 78 | + isTunEnabled: true, |
| 79 | + tunProtectSsPort: 10811, |
| 80 | + proxyRelaySsPort: 10812)); |
| 81 | + |
| 82 | + var result = service.GenerateClientConfigContent(); |
| 83 | + |
| 84 | + Assert.True(result.Success); |
| 85 | + |
| 86 | + var outbounds = GetOutbounds(result.Data?.ToString()); |
| 87 | + Assert.DoesNotContain(outbounds, outbound => outbound["sendThrough"]?.GetValue<string>() == SendThrough); |
| 88 | + } |
| 89 | + |
| 90 | + private static CoreConfigContext CreateContext( |
| 91 | + ProfileItem node, |
| 92 | + Config? config = null, |
| 93 | + Dictionary<string, ProfileItem>? allProxiesMap = null, |
| 94 | + bool isTunEnabled = false, |
| 95 | + int tunProtectSsPort = 0, |
| 96 | + int proxyRelaySsPort = 0) |
| 97 | + { |
| 98 | + return new CoreConfigContext |
| 99 | + { |
| 100 | + Node = node, |
| 101 | + RunCoreType = ECoreType.Xray, |
| 102 | + AppConfig = config ?? CreateConfig(), |
| 103 | + AllProxiesMap = allProxiesMap ?? new(), |
| 104 | + SimpleDnsItem = new SimpleDNSItem(), |
| 105 | + IsTunEnabled = isTunEnabled, |
| 106 | + TunProtectSsPort = tunProtectSsPort, |
| 107 | + ProxyRelaySsPort = proxyRelaySsPort, |
| 108 | + }; |
| 109 | + } |
| 110 | + |
| 111 | + private static Config CreateConfig() |
| 112 | + { |
| 113 | + return new Config |
| 114 | + { |
| 115 | + IndexId = string.Empty, |
| 116 | + SubIndexId = string.Empty, |
| 117 | + CoreBasicItem = new() |
| 118 | + { |
| 119 | + LogEnabled = false, |
| 120 | + Loglevel = "warning", |
| 121 | + MuxEnabled = false, |
| 122 | + DefAllowInsecure = false, |
| 123 | + DefFingerprint = Global.Fingerprints.First(), |
| 124 | + DefUserAgent = string.Empty, |
| 125 | + SendThrough = SendThrough, |
| 126 | + EnableFragment = false, |
| 127 | + EnableCacheFile4Sbox = true, |
| 128 | + }, |
| 129 | + TunModeItem = new() |
| 130 | + { |
| 131 | + EnableTun = false, |
| 132 | + AutoRoute = true, |
| 133 | + StrictRoute = true, |
| 134 | + Stack = string.Empty, |
| 135 | + Mtu = 9000, |
| 136 | + EnableIPv6Address = false, |
| 137 | + IcmpRouting = Global.TunIcmpRoutingPolicies.First(), |
| 138 | + EnableLegacyProtect = false, |
| 139 | + }, |
| 140 | + KcpItem = new(), |
| 141 | + GrpcItem = new(), |
| 142 | + RoutingBasicItem = new() |
| 143 | + { |
| 144 | + DomainStrategy = Global.DomainStrategies.First(), |
| 145 | + DomainStrategy4Singbox = Global.DomainStrategies4Sbox.First(), |
| 146 | + RoutingIndexId = string.Empty, |
| 147 | + }, |
| 148 | + GuiItem = new(), |
| 149 | + MsgUIItem = new(), |
| 150 | + UiItem = new() |
| 151 | + { |
| 152 | + CurrentLanguage = "en", |
| 153 | + CurrentFontFamily = string.Empty, |
| 154 | + MainColumnItem = [], |
| 155 | + WindowSizeItem = [], |
| 156 | + }, |
| 157 | + ConstItem = new(), |
| 158 | + SpeedTestItem = new(), |
| 159 | + Mux4RayItem = new() |
| 160 | + { |
| 161 | + Concurrency = 8, |
| 162 | + XudpConcurrency = 8, |
| 163 | + XudpProxyUDP443 = "reject", |
| 164 | + }, |
| 165 | + Mux4SboxItem = new() |
| 166 | + { |
| 167 | + Protocol = string.Empty, |
| 168 | + }, |
| 169 | + HysteriaItem = new(), |
| 170 | + ClashUIItem = new() |
| 171 | + { |
| 172 | + ConnectionsColumnItem = [], |
| 173 | + }, |
| 174 | + SystemProxyItem = new(), |
| 175 | + WebDavItem = new(), |
| 176 | + CheckUpdateItem = new(), |
| 177 | + Fragment4RayItem = null, |
| 178 | + Inbound = [new InItem |
| 179 | + { |
| 180 | + Protocol = EInboundProtocol.socks.ToString(), |
| 181 | + LocalPort = 10808, |
| 182 | + UdpEnabled = true, |
| 183 | + SniffingEnabled = true, |
| 184 | + RouteOnly = false, |
| 185 | + }], |
| 186 | + GlobalHotkeys = [], |
| 187 | + CoreTypeItem = [], |
| 188 | + SimpleDNSItem = new(), |
| 189 | + }; |
| 190 | + } |
| 191 | + |
| 192 | + private static ProfileItem CreateProxyNode(string indexId, string address, int port) |
| 193 | + { |
| 194 | + return new ProfileItem |
| 195 | + { |
| 196 | + IndexId = indexId, |
| 197 | + Remarks = indexId, |
| 198 | + ConfigType = EConfigType.SOCKS, |
| 199 | + CoreType = ECoreType.Xray, |
| 200 | + Address = address, |
| 201 | + Port = port, |
| 202 | + }; |
| 203 | + } |
| 204 | + |
| 205 | + private static ProfileItem CreateChainNode(string indexId, params ProfileItem[] nodes) |
| 206 | + { |
| 207 | + var chainNode = new ProfileItem |
| 208 | + { |
| 209 | + IndexId = indexId, |
| 210 | + Remarks = indexId, |
| 211 | + ConfigType = EConfigType.ProxyChain, |
| 212 | + CoreType = ECoreType.Xray, |
| 213 | + }; |
| 214 | + chainNode.SetProtocolExtra(new ProtocolExtraItem |
| 215 | + { |
| 216 | + ChildItems = string.Join(',', nodes.Select(node => node.IndexId)), |
| 217 | + }); |
| 218 | + return chainNode; |
| 219 | + } |
| 220 | + |
| 221 | + private static List<JsonObject> GetOutbounds(string? json) |
| 222 | + { |
| 223 | + var root = JsonNode.Parse(json ?? throw new InvalidOperationException("Config JSON is missing"))?.AsObject() |
| 224 | + ?? throw new InvalidOperationException("Failed to parse config JSON"); |
| 225 | + return root["outbounds"]?.AsArray().Select(node => node!.AsObject()).ToList() |
| 226 | + ?? throw new InvalidOperationException("Config JSON does not contain outbounds"); |
| 227 | + } |
| 228 | +} |
0 commit comments