173 lines
4.8 KiB
C#
173 lines
4.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Web.Http;
|
|
using VNCServerList.Models;
|
|
using VNCServerList.Services;
|
|
|
|
namespace VNCServerList.Web.Controllers
|
|
{
|
|
[RoutePrefix("api/vncserver")]
|
|
public class VNCServerController : ApiController
|
|
{
|
|
private readonly DatabaseService _databaseService;
|
|
private readonly VNCService _vncService;
|
|
|
|
public VNCServerController()
|
|
{
|
|
_databaseService = new DatabaseService();
|
|
_vncService = new VNCService(_databaseService);
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("list")]
|
|
public IHttpActionResult GetServerList()
|
|
{
|
|
try
|
|
{
|
|
var servers = _databaseService.GetAllServers();
|
|
return Ok(servers);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return InternalServerError(ex);
|
|
}
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("get/{user}/{ip}")]
|
|
public IHttpActionResult GetServer(string user, string ip)
|
|
{
|
|
try
|
|
{
|
|
var server = _databaseService.GetServerByUserAndIP(user, ip);
|
|
if (server == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return Ok(server);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return InternalServerError(ex);
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("add")]
|
|
public IHttpActionResult AddServer([FromBody] VNCServer server)
|
|
{
|
|
try
|
|
{
|
|
if (server == null)
|
|
{
|
|
return BadRequest("서버 정보가 없습니다.");
|
|
}
|
|
|
|
bool success = _databaseService.AddServer(server);
|
|
if (success)
|
|
{
|
|
return Ok(new { Message = "서버가 성공적으로 추가되었습니다." });
|
|
}
|
|
else
|
|
{
|
|
return BadRequest("서버 추가에 실패했습니다.");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return InternalServerError(ex);
|
|
}
|
|
}
|
|
|
|
[HttpPut]
|
|
[Route("update")]
|
|
public IHttpActionResult UpdateServer([FromBody] VNCServer server)
|
|
{
|
|
try
|
|
{
|
|
if (server == null)
|
|
{
|
|
return BadRequest("서버 정보가 없습니다.");
|
|
}
|
|
|
|
bool success = _databaseService.UpdateServer(server);
|
|
if (success)
|
|
{
|
|
return Ok(new { Message = "서버가 성공적으로 업데이트되었습니다." });
|
|
}
|
|
else
|
|
{
|
|
return NotFound();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return InternalServerError(ex);
|
|
}
|
|
}
|
|
|
|
[HttpDelete]
|
|
[Route("delete/{user}/{ip}")]
|
|
public IHttpActionResult DeleteServer(string user, string ip)
|
|
{
|
|
try
|
|
{
|
|
bool success = _databaseService.DeleteServer(user, ip);
|
|
if (success)
|
|
{
|
|
return Ok(new { Message = "서버가 성공적으로 삭제되었습니다." });
|
|
}
|
|
else
|
|
{
|
|
return NotFound();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return InternalServerError(ex);
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("connect/{user}/{ip}")]
|
|
public IHttpActionResult ConnectToServer(string user, string ip)
|
|
{
|
|
try
|
|
{
|
|
bool success = _vncService.ConnectToServer(user, ip);
|
|
if (success)
|
|
{
|
|
return Ok(new { Message = "VNC 연결이 시작되었습니다." });
|
|
}
|
|
else
|
|
{
|
|
return BadRequest("VNC 연결에 실패했습니다.");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return InternalServerError(ex);
|
|
}
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("vnc-status")]
|
|
public IHttpActionResult GetVNCStatus()
|
|
{
|
|
try
|
|
{
|
|
bool isInstalled = _vncService.IsVNCViewerInstalled();
|
|
string path = _vncService.GetVNCViewerPath();
|
|
|
|
return Ok(new {
|
|
IsInstalled = isInstalled,
|
|
Path = path
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return InternalServerError(ex);
|
|
}
|
|
}
|
|
}
|
|
} |