Files
netch/Netch/Models/Server.cs
Connection Refused c235713c1b Remove V2Ray support
2020-02-09 22:23:55 +08:00

174 lines
4.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace Netch.Models
{
public class Server
{
/// <summary>
/// 备注
/// </summary>
public string Remark;
/// <summary>
/// 组
/// </summary>
public string Group = "None";
/// <summary>
/// 代理类型HTTP、HTTPS、Socks5、SS、SSR
/// </summary>
public string Type;
/// <summary>
/// 倍率
/// </summary>
public double Rate = 1.0;
/// <summary>
/// 地址
/// </summary>
public string Hostname;
/// <summary>
/// 端口
/// </summary>
public int Port;
/// <summary>
/// 账号HTTP、HTTPS、Socks5
/// </summary>
public string Username;
/// <summary>
/// 密码HTTP、HTTPS、Socks5、SS、SSR
/// </summary>
public string Password;
/// <summary>
/// 加密方式SS、SSR
/// </summary>
public string EncryptMethod;
/// <summary>
/// 插件SS
/// </summary>
public string Plugin;
/// <summary>
/// 插件参数SS
/// </summary>
public string PluginOption;
/// <summary>
/// 协议SSR
/// </summary>
public string Protocol;
/// <summary>
/// 协议参数SSR
/// </summary>
public string ProtocolParam;
/// <summary>
/// 混淆SSR
/// </summary>
public string OBFS;
/// <summary>
/// 混淆参数SSR
/// </summary>
public string OBFSParam;
/// <summary>
/// 延迟
/// </summary>
public int Delay = -1;
/// <summary>
/// 获取备注
/// </summary>
/// <returns>备注</returns>
public override string ToString()
{
if (string.IsNullOrWhiteSpace(Remark))
{
Remark = $"{Hostname}:{Port}";
}
switch (Type)
{
case "Socks5":
return $"[S5] {Remark}";
case "SS":
return $"[SS] {Remark}";
case "SSR":
return $"[SR] {Remark}";
default:
return "WTF";
}
}
/// <summary>
/// 测试延迟
/// </summary>
/// <returns>延迟</returns>
public int Test()
{
try
{
var destination = Utils.DNS.Lookup(Hostname);
if (destination == null)
{
return Delay = -2;
}
var list = new Task<int>[3];
for (var i = 0; i < 3; i++)
{
list[i] = Task.Run(() =>
{
try
{
using (var client = new Socket(SocketType.Stream, ProtocolType.Tcp))
{
var watch = new Stopwatch();
watch.Start();
var task = client.BeginConnect(new IPEndPoint(destination, Port), result =>
{
watch.Stop();
}, 0);
if (task.AsyncWaitHandle.WaitOne(1000))
{
return (int)watch.ElapsedMilliseconds;
}
return 1000;
}
}
catch (Exception)
{
return -4;
}
});
}
Task.WaitAll(list);
var min = Math.Min(list[0].Result, list[1].Result);
min = Math.Min(min, list[2].Result);
return Delay = min;
}
catch (Exception)
{
return Delay = -4;
}
}
}
}