106 lines
3.8 KiB
C#
106 lines
3.8 KiB
C#
using Renci.SshNet;
|
||
|
||
namespace Hk4eGm.Services;
|
||
|
||
public sealed record ServiceDef(string Key, string Label, string Process, string Kind = "java");
|
||
|
||
public sealed class SshService
|
||
{
|
||
public static readonly ServiceDef[] Services =
|
||
{
|
||
new("GameServer", "GameServer 游戏服", "GameServer"),
|
||
new("MuipServer", "MuipServer GM接口", "MuipServer"),
|
||
new("GateServer", "GateServer 网关", "GateServer"),
|
||
new("NodeServer", "NodeServer 节点", "NodeServer"),
|
||
new("DbGate", "DbGate 数据库网关", "DbGate"),
|
||
new("Dispatch", "Dispatch 调度", "Dispatch"),
|
||
new("Via", "Via 代理", "ViaGenshin", "via"),
|
||
new("Sdk", "SDK 登录服", "sdkserver", "sdk"),
|
||
new("MultiServer", "MultiServer 多服", "MultiServer"),
|
||
};
|
||
|
||
private readonly AppConfig _cfg;
|
||
private SshClient? _client;
|
||
private SftpClient? _sftp;
|
||
|
||
public Action<string>? Log { get; set; }
|
||
public SshService(AppConfig cfg) => _cfg = cfg;
|
||
|
||
public bool Connected => _client is { IsConnected: true };
|
||
|
||
public string Connect()
|
||
{
|
||
Disconnect();
|
||
var info = new ConnectionInfo(_cfg.Get("SSH", "IP地址", "127.0.0.1"),
|
||
_cfg.GetInt("SSH", "端口", 22),
|
||
_cfg.Get("SSH", "用户名", "root"),
|
||
new PasswordAuthenticationMethod(_cfg.Get("SSH", "用户名", "root"),
|
||
_cfg.Get("SSH", "密码", "")));
|
||
var client = new SshClient(info) { ConnectionInfo = { Timeout = TimeSpan.FromSeconds(8) } };
|
||
client.Connect();
|
||
_client = client;
|
||
return "SSH 连接成功";
|
||
}
|
||
|
||
public void Disconnect()
|
||
{
|
||
try { _client?.Disconnect(); } catch { }
|
||
_client?.Dispose();
|
||
_client = null;
|
||
try { _sftp?.Disconnect(); } catch { }
|
||
_sftp?.Dispose();
|
||
_sftp = null;
|
||
}
|
||
|
||
public string Run(string command, int timeoutMs = 25000)
|
||
{
|
||
if (!Connected) Connect();
|
||
using var cmd = _client!.CreateCommand($"bash -l -c \"{command.Replace("\"", "\\\"")}\"");
|
||
cmd.CommandTimeout = TimeSpan.FromMilliseconds(timeoutMs);
|
||
var output = cmd.Execute();
|
||
var error = cmd.Error;
|
||
return (output + error).Trim();
|
||
}
|
||
|
||
public string ServerDir => _cfg.Get("SSH", "服务目录", "/genshin/srv").TrimEnd('/');
|
||
|
||
public string? Pid(ServiceDef svc)
|
||
{
|
||
var output = Run($"pgrep -f {svc.Process}");
|
||
return output.Split('\n', StringSplitOptions.RemoveEmptyEntries)
|
||
.FirstOrDefault(x => x.Trim().All(char.IsDigit))?.Trim();
|
||
}
|
||
|
||
public string Resource(ServiceDef svc)
|
||
=> Run($"top -b -n 1 -E m | grep {svc.Process} | awk '{{print $6\"\\t\"$9}}' | head -n 1").Trim();
|
||
|
||
public string Start(ServiceDef svc, string bind = "0.0.0.0")
|
||
{
|
||
var dir = ServerDir;
|
||
var command = svc.Kind switch
|
||
{
|
||
"sdk" => $"screen -S sdkserver -X quit; screen -dmS sdkserver; screen -S sdkserver -X stuff 'cd {dir}/sdk && java -jar {dir}/sdk/sdk-server.jar\\n'",
|
||
"via" => $"cd {dir} ; nohup ./ViaGenshin >/dev/null 2>&1 &",
|
||
_ => $"cd {dir}/{svc.Key} ; nohup ./{svc.Key} -i {bind} >/dev/null 2>&1 &",
|
||
};
|
||
return Run(command);
|
||
}
|
||
|
||
public string Stop(ServiceDef svc, int waitSeconds = 12)
|
||
{
|
||
var pid = Pid(svc);
|
||
if (pid is null) return $"{svc.Label} 未运行";
|
||
Run($"kill -15 {pid}");
|
||
var deadline = DateTime.UtcNow.AddSeconds(waitSeconds);
|
||
while (DateTime.UtcNow < deadline)
|
||
{
|
||
if (Pid(svc) is null) return $"{svc.Label} 已关闭({pid})";
|
||
Thread.Sleep(800);
|
||
}
|
||
Run($"kill -9 {pid}");
|
||
return $"{svc.Label} 等待超时,已强制关闭({pid})";
|
||
}
|
||
|
||
public string ClearLogs() => Run($"rm -rf {ServerDir}/logs/*") is var o && o.Length > 0 ? o : "日志已清理";
|
||
}
|