public bool ParseBase64WithHead(string textBase64) { //考虑ssd2://等情况,head必须放在本函数内处理 textBase64 = textBase64.Replace("ssd://", "") .Replace('-', '+') .Replace('_', '/'); var mod4 = textBase64.Length % 4; if (mod4 > 0) { textBase64 += new string('=', 4 - mod4); } try { var jsonBuffer = Convert.FromBase64String(textBase64); var jsonText = Encoding.UTF8.GetString(jsonBuffer); var jsonObject = JObject.Parse(jsonText); airport = jsonObject["airport"].ToString(); port = jsonObject["port"].ToObject <int>(); encryption = jsonObject["encryption"].ToString(); password = jsonObject["password"].ToString(); var subscriptionUrl = jsonObject.GetValue("url"); if (subscriptionUrl != null) { url = subscriptionUrl.ToString(); } var subscriptionTrafficUsed = jsonObject.GetValue("traffic_used"); if (subscriptionTrafficUsed != null) { traffic_used = subscriptionTrafficUsed.ToObject <double>(); } var subscriptionTrafficTotal = jsonObject.GetValue("traffic_total"); if (subscriptionTrafficTotal != null) { traffic_total = subscriptionTrafficTotal.ToObject <double>(); } var subscriptionExpiry = jsonObject.GetValue("expiry"); if (subscriptionExpiry != null) { expiry = subscriptionExpiry.ToObject <DateTime>(); } var subscriptionPluginOptions = jsonObject.GetValue("plugin_options"); if (subscriptionPluginOptions != null) { plugin_options = subscriptionPluginOptions.ToString(); } var subscriptionPluginArguments = jsonObject.GetValue("plugin_arguments"); if (subscriptionPluginArguments != null) { plugin_arguments = subscriptionPluginArguments.ToString(); } var subscriptionPlugin = jsonObject.GetValue("plugin"); if (subscriptionPlugin != null) { plugin = subscriptionPlugin.ToString(); } configuration.configs.RemoveAll(it => it.subscription_url == url); var subscriptionServers = JArray.Parse(jsonObject["servers"].ToString()); foreach (var subscriptionServer in subscriptionServers) { var newServer = new Server(); newServer.SetSubscription(this); var jsonServerObject = JObject.Parse(subscriptionServer.ToString()); newServer.id = jsonServerObject["id"].ToObject <int>(); newServer.server = jsonServerObject["server"].ToString(); var serverPort = jsonServerObject.GetValue("port"); if (serverPort != null) { newServer.server_port = serverPort.ToObject <int>(); } else { newServer.server_port = port; } var serverEncryption = jsonServerObject.GetValue("encryption"); if (serverEncryption != null) { newServer.method = serverEncryption.ToString(); } else { newServer.method = encryption; } var serverPassword = jsonServerObject.GetValue("password"); if (serverPassword != null) { newServer.password = serverPassword.ToString(); } else { newServer.password = password; } var serverPluginOptions = jsonServerObject.GetValue("plugin_options"); if (serverPluginOptions != null) { newServer.plugin_opts = serverPluginOptions.ToString(); } else { newServer.plugin_opts = plugin_options; } var serverPluginArguments = jsonServerObject.GetValue("plugin_arguments"); if (serverPluginArguments != null) { newServer.plugin_args = serverPluginArguments.ToString(); } else { newServer.plugin_args = plugin_arguments; } var serverPlugin = jsonServerObject.GetValue("plugin"); if (serverPlugin != null) { newServer.plugin = serverPlugin.ToString(); } else { newServer.plugin = plugin; } var serverRatio = jsonServerObject.GetValue("ratio"); if (serverRatio != null) { newServer.ratio = serverRatio.ToObject <double>(); } var serverRemarks = jsonServerObject.GetValue("remarks"); if (serverRemarks != null) { newServer.remarks = serverRemarks.ToString(); } configuration.configs.Add(newServer); } } catch (Exception) { return(false); } return(true); }
public Subscription ParseBase64WithHead(string textBase64, string bindUrl = null) { textBase64 = textBase64.Replace("ssd://", "") .Replace('-', '+') .Replace('_', '/'); var mod4 = textBase64.Length % 4; if (mod4 > 0) { textBase64 += new string('=', 4 - mod4); } var jsonBuffer = Convert.FromBase64String(textBase64); var jsonText = Encoding.UTF8.GetString(jsonBuffer); //todo 重新解析 var jsonObject = JObject.Parse(jsonText); var subscriptionUrl = jsonObject.GetValue("url"); var newSubscription = FindSubscription(subscriptionUrl == null ? bindUrl : subscriptionUrl.ToString()); newSubscription.airport = jsonObject["airport"].ToString(); newSubscription.port = jsonObject["port"].ToObject <int>(); newSubscription.encryption = jsonObject["encryption"].ToString(); newSubscription.password = jsonObject["password"].ToString(); var subscriptionTrafficUsed = jsonObject.GetValue("traffic_used"); if (subscriptionTrafficUsed != null) { newSubscription.traffic_used = subscriptionTrafficUsed.ToObject <double>(); } var subscriptionTrafficTotal = jsonObject.GetValue("traffic_total"); if (subscriptionTrafficTotal != null) { newSubscription.traffic_total = subscriptionTrafficTotal.ToObject <double>(); } var subscriptionExpiry = jsonObject.GetValue("expiry"); if (subscriptionExpiry != null) { newSubscription.expiry = subscriptionExpiry.ToObject <DateTime>(); } var subscriptionPluginOptions = jsonObject.GetValue("plugin_options"); if (subscriptionPluginOptions != null) { newSubscription.plugin_options = subscriptionPluginOptions.ToString(); } var subscriptionPluginArguments = jsonObject.GetValue("plugin_arguments"); if (subscriptionPluginArguments != null) { newSubscription.plugin_arguments = subscriptionPluginArguments.ToString(); } var subscriptionPlugin = jsonObject.GetValue("plugin"); if (subscriptionPlugin != null) { newSubscription.plugin = subscriptionPlugin.ToString(); } configs.RemoveAll(it => it.subscription_url == newSubscription.url); var subscriptionServers = JArray.Parse(jsonObject["servers"].ToString()); foreach (var subscriptionServer in subscriptionServers) { var newServer = new Server(); newServer.SetSubscription(newSubscription); var jsonServerObject = JObject.Parse(subscriptionServer.ToString()); newServer.id = jsonServerObject["id"].ToObject <int>(); newServer.server = jsonServerObject["server"].ToString(); var serverPort = jsonServerObject.GetValue("port"); if (serverPort != null) { newServer.server_port = serverPort.ToObject <int>(); } else { newServer.server_port = newSubscription.port; } var serverEncryption = jsonServerObject.GetValue("encryption"); if (serverEncryption != null) { newServer.method = serverEncryption.ToString(); } else { newServer.method = newSubscription.encryption; } var serverPassword = jsonServerObject.GetValue("password"); if (serverPassword != null) { newServer.password = serverPassword.ToString(); } else { newServer.password = newSubscription.password; } var serverPluginOptions = jsonServerObject.GetValue("plugin_options"); if (serverPluginOptions != null) { newServer.plugin_opts = serverPluginOptions.ToString(); } else { newServer.plugin_opts = newSubscription.plugin_options; } var serverPluginArguments = jsonServerObject.GetValue("plugin_arguments"); if (serverPluginArguments != null) { newServer.plugin_args = serverPluginArguments.ToString(); } else { newServer.plugin_args = newSubscription.plugin_arguments; } var serverPlugin = jsonServerObject.GetValue("plugin"); if (serverPlugin != null) { newServer.plugin = serverPlugin.ToString(); } else { newServer.plugin = newSubscription.plugin; } var serverRatio = jsonServerObject.GetValue("ratio"); if (serverRatio != null) { newServer.ratio = serverRatio.ToObject <double>(); } var serverRemarks = jsonServerObject.GetValue("remarks"); if (serverRemarks != null) { newServer.remarks = serverRemarks.ToString(); } configs.Add(newServer); } newSubscription.configuration = this; return(newSubscription); }