using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace Netch.Models
{
public class Server
{
///
/// 备注
///
public string Remark;
///
/// 组
///
public string Group = "None";
///
/// 代理类型(HTTP、HTTPS、Socks5、SS、SSR、VMess)
///
public string Type;
///
/// 倍率
///
public double Rate = 1.0;
///
/// 地址
///
public string Hostname;
///
/// 端口
///
public int Port;
///
/// 账号(HTTP、HTTPS、Socks5)
///
public string Username;
///
/// 密码(HTTP、HTTPS、Socks5、SS、SSR)
///
public string Password;
///
/// 用户 ID(VMess)
///
public string UserID = string.Empty;
///
/// 额外 ID(VMess)
///
public int AlterID = 0;
///
/// 加密方式(SS、SSR、VMess)
///
public string EncryptMethod;
///
/// 插件(SS)
///
public string Plugin;
///
/// 插件参数(SS)
///
public string PluginOption;
///
/// 协议(SSR)
///
public string Protocol;
///
/// 协议参数(SSR)
///
public string ProtocolParam;
///
/// 混淆(SSR)
///
public string OBFS;
///
/// 混淆参数(SSR)
///
public string OBFSParam;
///
/// 传输协议(VMess)
///
public string TransferProtocol = "tcp";
///
/// 伪装类型(VMess)
///
public string FakeType = string.Empty;
///
/// 伪装域名(VMess:HTTP、WebSocket、HTTP/2)
///
public string Host = string.Empty;
///
/// 传输路径(VMess:WebSocket、HTTP/2)
///
public string Path = string.Empty;
///
/// QUIC 加密方式(VMess)
///
public string QUICSecure = "none";
///
/// QUIC 加密密钥(VMess)
///
public string QUICSecret = string.Empty;
///
/// TLS 底层传输安全(VMess)
///
public bool TLSSecure = false;
///
/// Mux 多路复用(VMess)
///
public bool UseMux = false;
///
/// 延迟
///
public int Delay = -1;
///
/// 获取备注
///
/// 备注
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}";
case "VMess":
return $"[V2] {Remark}";
default:
return "WTF";
}
}
///
/// 测试延迟
///
/// 延迟
public int Test()
{
try
{
var destination = Utils.DNS.Lookup(Hostname);
if (destination == null)
{
return Delay = -2;
}
var list = new Task[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;
}
}
}
}