Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Anime-Manga committed Oct 1, 2023
1 parent a636433 commit e0fd88f
Showing 1 changed file with 18 additions and 50 deletions.
68 changes: 18 additions & 50 deletions src/references/Cesxhin.AnimeManga.Modules/Proxy/ProxyManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,35 @@

namespace Cesxhin.AnimeManga.Modules.Proxy
{
public static class ProxyManagement
public class ProxyManagement
{
private static List<string> GetList(string name)
{
var env = Environment.GetEnvironmentVariable(name);
List<string> listEnv = new();

if (!string.IsNullOrEmpty(env))
listEnv.AddRange(env.Split(','));

return listEnv;
}

private static JObject GetBlackList()
{
var blackList = Environment.GetEnvironmentVariable("BLACK_LIST_PROXY");
var jsonBlackList = new JObject();

if (blackList != null)
jsonBlackList = JObject.Parse(blackList);
private string blackList = "";
private List<string> listProxy = new();

return jsonBlackList;
}
public static void InitProxy()
public void InitProxy()
{
var enableProxy = Environment.GetEnvironmentVariable("PROXY_ENABLE") ?? "false";

if (enableProxy == "true")
{
var listProxy = System.IO.File.ReadAllText("proxy.txt");

Environment.SetEnvironmentVariable("LIST_PROXY", listProxy);
listProxy = System.IO.File.ReadAllText("proxy.txt").Split(",").ToList();
}
}

public static List<string> GetAllIP()
public List<string> GetAllIP()
{
return GetList("LIST_PROXY");
return listProxy;
}

public static string GetIp(string id)
public string GetIp()
{
var enableProxy = Environment.GetEnvironmentVariable("PROXY_ENABLE") ?? "false";

if(enableProxy == "true")
if(EnableProxy())
{
var listProxy = GetList("LIST_PROXY");
var blackProxyJson = GetBlackList();
var listBlackProxy = blackList.Split(",").ToList();

if (blackProxyJson.ContainsKey(id))
foreach (var blackProxy in listBlackProxy)
{
var listBlackProxy = ((string)blackProxyJson[id]).Split(",").ToList();

foreach (var blackProxy in listBlackProxy)
{
listProxy = listProxy.Where(e => e != blackProxy).ToList();
}
listProxy = listProxy.Where(e => e != blackProxy).ToList();
}

if (listProxy.Count > 0)
Expand All @@ -73,25 +45,21 @@ public static string GetIp(string id)
return null;
}

public static bool EnableProxy()
public bool EnableProxy()
{
var enableProxy = Environment.GetEnvironmentVariable("PROXY_ENABLE") ?? "false";
return enableProxy == "true";
}

public static void BlackListAdd(string ip, string id)
public void BlackListAdd(string ip)
{
var jsonBlackList = GetBlackList();
if (jsonBlackList.ContainsKey(id))
var list = blackList.Split(",").ToList();

if(list.Find(e => e == ip) == null)
{
var list = ((string)jsonBlackList[id]).Split(",").ToList();
list.Add(ip);
jsonBlackList[id] = string.Join(",", list);
blackList = string.Join(",", list);
}
else
jsonBlackList[id] = ip;

Environment.SetEnvironmentVariable("BLACK_LIST_PROXY", jsonBlackList.ToString());
}
}
}

0 comments on commit e0fd88f

Please sign in to comment.