add update config with api
This commit is contained in:
@@ -83,7 +83,17 @@ namespace clashN.Base
|
||||
var byteContent = new ByteArrayContent(buffer);
|
||||
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
||||
|
||||
var result = await httpClient.PutAsync(url, byteContent);
|
||||
await httpClient.PutAsync(url, byteContent);
|
||||
}
|
||||
|
||||
public async Task PatchAsync(string url, Dictionary<string, string> headers)
|
||||
{
|
||||
var myContent = Utils.ToJson(headers);
|
||||
var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
|
||||
var byteContent = new ByteArrayContent(buffer);
|
||||
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
||||
|
||||
await httpClient.PatchAsync(url, byteContent);
|
||||
}
|
||||
|
||||
public async Task DownloadFileAsync(HttpClient client, string url, string fileName, IProgress<double> progress, CancellationToken token)
|
||||
|
||||
@@ -99,7 +99,6 @@ namespace clashN.Forms
|
||||
OnProgramStarted("shown", true);
|
||||
|
||||
_ = LoadCore();
|
||||
SetRuleMode(config.ruleMode, false);
|
||||
|
||||
proxiesControl.Init(config, UpdateTaskHandler);
|
||||
}
|
||||
@@ -456,7 +455,7 @@ namespace clashN.Forms
|
||||
statistics?.SaveToFile();
|
||||
|
||||
ChangePACButtonStatus(config.sysProxyType);
|
||||
//SetRuleMode(config.ruleMode, false);
|
||||
SetRuleMode(config.ruleMode);
|
||||
|
||||
SwitchUI(true);
|
||||
proxiesControl.ProxiesReload();
|
||||
@@ -1172,24 +1171,24 @@ namespace clashN.Forms
|
||||
|
||||
private void menuModeRule_Click(object sender, EventArgs e)
|
||||
{
|
||||
SetRuleMode(ERuleMode.Rule, true);
|
||||
SetRuleMode(ERuleMode.Rule);
|
||||
}
|
||||
|
||||
private void menuModeGlobal_Click(object sender, EventArgs e)
|
||||
{
|
||||
SetRuleMode(ERuleMode.Global, true);
|
||||
SetRuleMode(ERuleMode.Global);
|
||||
}
|
||||
|
||||
private void menuModeDirect_Click(object sender, EventArgs e)
|
||||
{
|
||||
SetRuleMode(ERuleMode.Direct, true);
|
||||
SetRuleMode(ERuleMode.Direct);
|
||||
}
|
||||
|
||||
private void menuModeKeep_Click(object sender, EventArgs e)
|
||||
{
|
||||
SetRuleMode(ERuleMode.Unchanged, true);
|
||||
SetRuleMode(ERuleMode.Unchanged);
|
||||
}
|
||||
private void SetRuleMode(ERuleMode mode, bool reloadCore)
|
||||
private void SetRuleMode(ERuleMode mode)
|
||||
{
|
||||
for (int k = 0; k < menuRuleMode.DropDownItems.Count; k++)
|
||||
{
|
||||
@@ -1198,18 +1197,15 @@ namespace clashN.Forms
|
||||
}
|
||||
mainMsgControl.SetToolSslInfo("routing", mode.ToString());
|
||||
|
||||
//if (config.ruleMode == mode)
|
||||
//{
|
||||
// return;
|
||||
//}
|
||||
|
||||
if (reloadCore)
|
||||
{
|
||||
AppendText(false, $"{config.ruleMode.ToString()}->{mode.ToString()}");
|
||||
config.ruleMode = mode;
|
||||
|
||||
Global.reloadCore = true;
|
||||
_ = LoadCore();
|
||||
ConfigHandler.SaveConfig(ref config, false);
|
||||
|
||||
if (mode != ERuleMode.Unchanged)
|
||||
{
|
||||
Dictionary<string, string> headers = new Dictionary<string, string>();
|
||||
headers.Add("mode", config.ruleMode.ToString().ToLower());
|
||||
MainFormHandler.Instance.ClashConfigUpdate(headers);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -254,12 +254,15 @@ namespace clashN.Forms
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var name = lvProxies.Items[index].Tag.ToString();
|
||||
if (Utils.IsNullOrEmpty(name))
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (proxies == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
proxies.TryGetValue(name, out ProxiesItem proxy);
|
||||
if (proxy == null || proxy.all == null)
|
||||
@@ -406,10 +409,7 @@ namespace clashN.Forms
|
||||
return;
|
||||
}
|
||||
|
||||
var url = $"{Global.httpProtocol}{Global.Loopback}:{_config.APIPort}/proxies/{name}";
|
||||
Dictionary<string, string> headers = new Dictionary<string, string>();
|
||||
headers.Add("name", nameNode);
|
||||
_ = HttpClientHelper.GetInstance().PutAsync(url, headers);
|
||||
MainFormHandler.Instance.ClashSetActiveProxy(name, nameNode);
|
||||
|
||||
selectedProxy.now = nameNode;
|
||||
RefreshDetail(GetLvSelectedIndex());
|
||||
|
||||
@@ -332,5 +332,30 @@ namespace clashN.Handler
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async void ClashSetActiveProxy(string name, string nameNode)
|
||||
{
|
||||
var url = $"{Global.httpProtocol}{Global.Loopback}:{LazyConfig.Instance.GetConfig().APIPort}/proxies/{name}";
|
||||
Dictionary<string, string> headers = new Dictionary<string, string>();
|
||||
headers.Add("name", nameNode);
|
||||
await HttpClientHelper.GetInstance().PutAsync(url, headers);
|
||||
|
||||
}
|
||||
|
||||
public void ClashConfigUpdate(Dictionary<string, string> headers)
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
var proxies = LazyConfig.Instance.GetProxies();
|
||||
if (proxies == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var urlBase = $"{Global.httpProtocol}{Global.Loopback}:{LazyConfig.Instance.GetConfig().APIPort}/configs";
|
||||
|
||||
await HttpClientHelper.GetInstance().PatchAsync(urlBase, headers);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user