Select Git revision
RDPClient.cs
RDPClient.cs 8.71 KiB
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Windows.Forms;
using AxMSTSCLib;
using MSTSCLib;
using Newtonsoft.Json;
namespace RDPClient
{
public partial class RDPClient : Form
{
List<ExistingVM> existingVMJson;
string token;
string email;
string jsonstring;
public RDPClient(String token, String email)
{
this.token = token;
this.email = email;
InitializeComponent();
jsonstring = CallApi("http://192.168.37.140:8090/existing/"+email+"/user", token);
existingVMJson = JsonConvert.DeserializeObject<List<ExistingVM>>(jsonstring);
if (existingVMJson != null)
{
for (int i = 0; i < existingVMJson.Count; i++)
{
VMListView.Items.Add(existingVMJson[i].VmName);
}
}
else
{
VMListView.Items.Add("할당된 VM이 없습니다.");
}
}
public void client_OnConnecting(object sender, EventArgs e)
{
AxMsRdpClient9NotSafeForScripting client = sender as AxMsRdpClient9NotSafeForScripting;
}
private void Connect_button_Click(object sender, EventArgs e)
{
string server;
int port = 3389;
string userID;
string password;
if (existingVMJson != null)
{
if (VMListView.SelectedIndices.Count > 0)
{
int selectedIndex = VMListView.SelectedIndices[0];
server = existingVMJson[selectedIndex].PublicIPAddress;
userID = existingVMJson[selectedIndex].AdminName;
password = existingVMJson[selectedIndex].AdminPassword;
axMsRdpClient9NotSafeForScripting1.Server = server;
axMsRdpClient9NotSafeForScripting1.AdvancedSettings9.RDPPort = port;
axMsRdpClient9NotSafeForScripting1.UserName = userID;
axMsRdpClient9NotSafeForScripting1.Domain = "";
axMsRdpClient9NotSafeForScripting1.AdvancedSettings9.ClearTextPassword = password;
axMsRdpClient9NotSafeForScripting1.AdvancedSettings9.EnableCredSspSupport = true;
axMsRdpClient9NotSafeForScripting1.AdvancedSettings2.SmartSizing = true;
axMsRdpClient9NotSafeForScripting1.Dock = DockStyle.Fill;
IMsRdpClientNonScriptable4 ocx = axMsRdpClient9NotSafeForScripting1.GetOcx() as IMsRdpClientNonScriptable4;
ocx.AllowCredentialSaving = false;
ocx.PromptForCredentials = false;
ocx.PromptForCredsOnClient = false;
this.WindowState = FormWindowState.Maximized;
RDPContainer.Dock = DockStyle.Fill;
RDPContainer.Visible = true;
axMsRdpClient9NotSafeForScripting1.Dock = DockStyle.Fill;
axMsRdpClient9NotSafeForScripting1.Size = RDPContainer.Size;
axMsRdpClient9NotSafeForScripting1.Visible = true;
axMsRdpClient9NotSafeForScripting1.Connect();
}
}
}
private void close_button_Click_1(object sender, EventArgs e)
{
Application.Exit();
}
private void RDPClient_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
static string CallApi(string url, string token)
{
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
using (var client = new HttpClient())
{
if (!string.IsNullOrWhiteSpace(token))
{
//var t = JsonConvert.DeserializeObject<Token>(token);
//client.DefaultRequestHeaders.Clear();
//client.DefaultRequestHeaders.Add("Authorization",token);
}
var response = client.GetAsync(url + "?token=" + token).Result;
return response.Content.ReadAsStringAsync().Result;
}
}
static string CallPutApi(string url, string token, string jsonString)
{
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
using (var client = new HttpClient())
{
if (!string.IsNullOrWhiteSpace(token))
{
//var t = JsonConvert.DeserializeObject<Token>(token);
//client.DefaultRequestHeaders.Clear();
//client.DefaultRequestHeaders.Add("Authorization",token);
}
var httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
var response = client.PutAsync(url + "?token=" + token, httpContent).Result;
return response.Content.ReadAsStringAsync().Result;
}
}
private void VMListView_SelectedIndexChanged(object sender, EventArgs e)
{
if (existingVMJson != null)
{
if (VMListView.SelectedIndices.Count > 0)
{
int selectedIndex = VMListView.SelectedIndices[0];
/*Standard_B1ls(1 vcpu, 0.5 GiB)
Standard_B1s(1 vcpu, 1 GiB)
Standard_DS1_v2(1 vcpu, 3.5 GiB)
Standard_D2s_v3(2 vcpu, 8 GiB)*/
if (existingVMJson[selectedIndex].Size == "Standard_B1ls")
{
Memory_label.Text = "0.5 GiB";
CPU_label.Text = "1개";
}
else if (existingVMJson[selectedIndex].Size == "Standard_B1s")
{
Memory_label.Text = "1 GiB";
CPU_label.Text = "1개";
}
else if (existingVMJson[selectedIndex].Size == "Standard_DS1_v2")
{
Memory_label.Text = "3.5 GiB";
CPU_label.Text = "1개";
}
else if (existingVMJson[selectedIndex].Size == "Standard_D2s_v3")
{
Memory_label.Text = "8 GiB";
CPU_label.Text = "2개";
}
OS_label.Text = existingVMJson[selectedIndex].OsType;
Status_label.Text = existingVMJson[selectedIndex].Status;
if (Status_label.Text == "stopped")
{
Connect_button.Enabled = false;
start_stop_btn.Text = "시작";
}
else if(Status_label.Text == "running")
{
Connect_button.Enabled = true;
start_stop_btn.Text = "중지";
}
}
}
}
private void axMsRdpClient9NotSafeForScripting1_OnDisconnected(object sender, IMsTscAxEvents_OnDisconnectedEvent e)
{
this.Hide();
RDPClient client = new RDPClient(this.token, this.email);
client.Show();
}
private void start_stop_btn_Click(object sender, EventArgs e)
{
int selectedIndex = VMListView.SelectedIndices[0];
if (Status_label.Text == "running")
{
Status_label.Text = "중지중...";
CallPutApi("http://192.168.37.140:8090/existing/stop/user/" + existingVMJson[selectedIndex].ResourceGroupName+"/"+ existingVMJson[selectedIndex].VmName, token,jsonstring);
start_stop_btn.Text = "시작";
Connect_button.Enabled = false;
}
else if (Status_label.Text == "stopped")
{
Status_label.Text = "시작중...";
CallPutApi("http://192.168.37.140:8090/existing/start/user/" + existingVMJson[selectedIndex].ResourceGroupName + "/" + existingVMJson[selectedIndex].VmName, token,jsonstring);
start_stop_btn.Text = "중지";
Connect_button.Enabled = true;
}
jsonstring = CallApi("http://192.168.37.140:8090/existing/" + email + "/user", token);
existingVMJson = JsonConvert.DeserializeObject<List<ExistingVM>>(jsonstring);
Status_label.Text = existingVMJson[selectedIndex].Status;
}
}
}