๊ธฐ์
๋๋ฃ๊ฐ ์์ ์น ์๋น์ค๋ฅผ ๋ง๋ค์๊ณ ์ ์ํ๋ฉด์ ๋ชจ๋ ๊ฒ์ด ์์๋์์ต๋๋ค. ๋ถ์ฏ๊น๊ณผ ๊ฐ์ ๊ฒ์ด์ง๋ง IT ํ ์์์์ํ ๊ฒ์
๋๋ค. ๊ธฐ๋ฅ์ ์์ ํ ๊ฐ๋จํฉ๋๋ค. ๋ฑ๋กํ๊ณ ํ๋กํ์ ์์ฑํ๊ณ ๋๋ด์๋ฅผ ์ฐพ๊ณ ์ฐ๊ฒฐ์ ํ์ฅํ๊ณ ์๋ก์ด ์ง์ธ์ ๋ง๋๋ ์ฃผ์ ์ง์ ์ผ๋ก ์ด๋ํฉ๋๋ค.
์ฌ๊ธฐ์์ ๋๋ ๋ ์์ ์ ๋ํด ์กฐ๊ธ ์ด์ผ๊ธฐํ๊ณ ์ด์ผ๊ธฐํด์ผํ๋ฏ๋ก ์์ผ๋ก ๋ด๊ฐ ์ ๊ทธ๋ฌํ ๊ฐ๋ฐ ๋จ๊ณ๋ฅผ ๋ฐ์๋์ง ๋ ๋ช ํํด์ง ๊ฒ์ ๋๋ค.
ํ์ฌ ์ ๋ ๊ฒ์ ์คํ๋์ค์์ ํ ํฌ๋์ปฌ ์ํฐ์คํธ์ ์ง์ฑ ์ ๋งก๊ณ ์์ผ๋ฉฐ C# ํ๋ก๊ทธ๋๋ฐ ๊ฒฝํ์ Unity์ฉ ์คํฌ๋ฆฝํธ ๋ฐ ์ ํธ๋ฆฌํฐ ์์ฑ๊ณผ ๋๋ถ์ด ์๋๋ก์ด๋ ์ฅ์น๋ก ์ ์์ค ์์ ์ ์ํ ํ๋ฌ๊ทธ์ธ ์์ฑ์๋ง ๊ธฐ๋ฐ์ ๋๊ณ ์์ต๋๋ค. ์ด ์ธ์ ๋ฐ์์๋ ์์ง ๊ทธ๋ฐ ๊ธฐํ๋ฅผ ์ ํํด์ ์ฐพ์๋ณธ ์ ์ด ์์ต๋๋ค.
ํํธ 1. ํ๋ ์ ํ๋กํ ํ์ดํ
์ด ์๋น์ค๊ฐ ๋ฌด์์ธ์ง ๊ฒฐ์ ํ ํ ๊ตฌํ ์ต์
์ ์ฐพ๊ธฐ ์์ํ์ต๋๋ค. ๊ฐ์ฅ ์ฌ์ด ๋ฐฉ๋ฒ์ ์ผ์ข
์ ๊ธฐ์ฑ ์๋ฃจ์
์ ์ฐพ๋ ๊ฒ์
๋๋ค. ์ง๊ตฌ๋ณธ์ ์ฌ๋นผ๋ฏธ์ฒ๋ผ ์ฐ๋ฆฌ์ ์ญํ์ ๋์ด๋ด์ด ๋์ค์ ๋น๋์ ์ํด ๋ชจ๋ ๊ฒ์ ๋ฐฐ์นํ ์ ์์ต๋๋ค.
๊ทธ๋ฌ๋ ์ด๊ฒ์ ํฅ๋ฏธ๋กญ์ง ์๊ณ ์ด๊ฒ์์ ์ด๋ค ๋์ ๊ณผ ์๋ฏธ๋ ๋ณด์ง ๋ชปํ๊ธฐ ๋๋ฌธ์ ์น ๊ธฐ์ ๊ณผ ์ํธ ์์ฉ ๋ฐฉ๋ฒ์ ์ฐ๊ตฌํ๊ธฐ ์์ํ์ต๋๋ค.
์ฐ๊ตฌ๋ C# .Net์์ ๊ธฐ์ฌ์ ๋ฌธ์๋ฅผ ๋ณด๋ ๊ฒ์ผ๋ก ์์๋์์ต๋๋ค. ์ฌ๊ธฐ์์ ์์ ์ ์ํํ๋ ๋ค์ํ ๋ฐฉ๋ฒ์ ์ฐพ์์ต๋๋ค. ASP.Net ๋๋ Azure ์๋น์ค์ ๊ฐ์ ๋ณธ๊ฒฉ์ ์ธ ์๋ฃจ์ ์์ TcpHttp ์ฐ๊ฒฐ๊ณผ์ ์ง์ ์ ์ธ ์ํธ ์์ฉ์ ์ด๋ฅด๊ธฐ๊น์ง ๋คํธ์ํฌ์ ์ํธ ์์ฉํ๊ธฐ ์ํ ๋ง์ ๋ฉ์ปค๋์ฆ์ด ์์ต๋๋ค.
ASP๋ก ์ฒซ ์๋๋ฅผ ํ ํ ์ฆ์ ์ทจ์ํ๋๋ฐ ์ ํฌ ์๋น์ค์ ๋๋ฌด ์ด๋ ค์ด ๊ฒฐ์ ์ด์๋ค๊ณ ์๊ฐํฉ๋๋ค. ์ด ํ๋ซํผ ๊ธฐ๋ฅ์ XNUMX/XNUMX๋ ์ฌ์ฉํ์ง ์์ ๊ฒ์ด๋ฏ๋ก ๊ฒ์์ ๊ณ์ํ์ต๋๋ค. TCP์ Http ํด๋ผ์ด์ธํธ-์๋ฒ ์ฌ์ด์์ ์ ํ์ด ์ด๋ฃจ์ด์ก์ต๋๋ค. ์ฌ๊ธฐ Habrรฉ์์ ๋ค์๊ณผ ๊ฐ์ ๊ธฐ์ฌ๋ฅผ ์ ํ์ต๋๋ค.
์๋ฒ์ ์ฒซ ๋ฒ์งธ ๋ฒ์ ์๋ ์ฐ๊ฒฐ ์ฒ๋ฆฌ, ์ ์ ์น ํ์ด์ง ์ฝํ ์ธ ์ ๊ณต ๋ฐ ์ฌ์ฉ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค๊ฐ ํฌํจ๋์์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ฐ์ ์ฌ์ดํธ ์์ ์ ์ํ ๊ธฐ๋ฅ์ ๊ตฌ์ถํ๊ธฐ๋ก ๊ฒฐ์ ํ์ต๋๋ค. ๋์ค์ ์ฌ๊ธฐ์์ Android ๋ฐ iOS์์ ์ ํ๋ฆฌ์ผ์ด์ ์ฒ๋ฆฌ๋ฅผ ์ฐ๊ฒฐํ ์ ์์ต๋๋ค.
์ฌ๊ธฐ์ ๋ช ๊ฐ์ง ์ฝ๋๊ฐ ์์ต๋๋ค.
๋ฌดํ ๋ฃจํ์์ ํด๋ผ์ด์ธํธ๋ฅผ ์๋ฝํ๋ ๊ธฐ๋ณธ ์ค๋ ๋:
using System;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace ClearServer
{
class Server
{
TcpListener Listener;
public Server(int Port)
{
Listener = new TcpListener(IPAddress.Any, Port);
Listener.Start();
while (true)
{
TcpClient Client = Listener.AcceptTcpClient();
Thread Thread = new Thread(new ParameterizedThreadStart(ClientThread));
Thread.Start(Client);
}
}
static void ClientThread(Object StateInfo)
{
new Client((TcpClient)StateInfo);
}
~Server()
{
if (Listener != null)
{
Listener.Stop();
}
}
static void Main(string[] args)
{
DatabaseWorker sqlBase = DatabaseWorker.GetInstance;
new Server(80);
}
}
}
ํด๋ผ์ด์ธํธ ์ฒ๋ฆฌ๊ธฐ ์์ฒด:
using System; using System.IO; using System.Net.Sockets; using System.Text; using System.Text.RegularExpressions; namespace ClearServer { class Client { public Client(TcpClient Client) { string Message = ""; byte[] Buffer = new byte[1024]; int Count; while ((Count = Client.GetStream().Read(Buffer, 0, Buffer.Length)) > 0) { Message += Encoding.UTF8.GetString(Buffer, 0, Count); if (Message.IndexOf("rnrn") >= 0 || Message.Length > 4096) { Console.WriteLine(Message); break; } } Match ReqMatch = Regex.Match(Message, @"^w+s+([^s?]+)[^s]*s+HTTP/.*|"); if (ReqMatch == Match.Empty) { ErrorWorker.SendError(Client, 400); return; } string RequestUri = ReqMatch.Groups[1].Value; RequestUri = Uri.UnescapeDataString(RequestUri); if (RequestUri.IndexOf("..") >= 0) { ErrorWorker.SendError(Client, 400); return; } if (RequestUri.EndsWith("/")) { RequestUri += "index.html"; } string FilePath =
quot;D:/Web/TestSite{RequestUri}";
if (!File.Exists(FilePath))
{
ErrorWorker.SendError(Client, 404);
return;
}string Extension = RequestUri.Substring(RequestUri.LastIndexOf('.'));
string ContentType = "";
switch (Extension)
{
case ".htm":
case ".html":
ContentType = "text/html";
break;
case ".css":
ContentType = "text/css";
break;
case ".js":
ContentType = "text/javascript";
break;
case ".jpg":
ContentType = "image/jpeg";
break;
case ".jpeg":
case ".png":
case ".gif":
ContentType =quot;image/{Extension.Substring(1)}";
break;
default:
if (Extension.Length > 1)
{
ContentType =quot;application/{Extension.Substring(1)}";
}
else
{
ContentType = "application/unknown";
}
break;
}FileStream FS;
try
{
FS = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
}
catch (Exception)
{
ErrorWorker.SendError(Client, 500);
return;
}string Headers =
quot;HTTP/1.1 200 OKnContent-Type: {ContentType}nContent-Length: {FS.Length}nn";
byte[] HeadersBuffer = Encoding.ASCII.GetBytes(Headers);
Client.GetStream().Write(HeadersBuffer, 0, HeadersBuffer.Length);while (FS.Position < FS.Length)
{
Count = FS.Read(Buffer, 0, Buffer.Length);
Client.GetStream().Write(Buffer, 0, Count);
}
FS.Close();
Client.Close();
}
}
}
๊ทธ๋ฆฌ๊ณ ๋ก์ปฌ SQL์ ๊ตฌ์ถ๋ ์ต์ด์ ๋ฐ์ดํฐ๋ฒ ์ด์ค:using System; using System.Data.Linq; namespace ClearServer { class DatabaseWorker { private static DatabaseWorker instance; public static DatabaseWorker GetInstance { get { if (instance == null) instance = new DatabaseWorker(); return instance; } } private DatabaseWorker() { string connectionStr = databasePath; using (DataContext db = new DataContext(connectionStr)) { Table<User> users = db.GetTable<User>(); foreach (var item in users) { Console.WriteLine(
quot;{item.login} {item.password}");
}
}
}
}
}
๋ณด์๋ค์ํผ ์ด ๋ฒ์ ์ ๊ธฐ์ฌ์ ๋ฒ์ ๊ณผ ์ฝ๊ฐ ๋ค๋ฆ ๋๋ค. ์ค์ ๋ก ์ปดํจํฐ์ ํด๋์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ํ์ด์ง๋ฅผ ๋ก๋ํ๋ ๊ฒ๋ง ์ฌ๊ธฐ์ ์ถ๊ฐ๋์์ต๋๋ค(๊ทธ๋ฐ๋ฐ ์๋ชป๋ ์ฐ๊ฒฐ ์ํคํ ์ฒ๋ก ์ธํด ์ด ๋ฒ์ ์์๋ ์๋ํ์ง ์์์ต๋๋ค).์ 2 ์ฅ
์๋ฒ๋ฅผ ํ ์คํธํ ํ ์ด๊ฒ์ด ํ๋ฅญํ ์๋ฃจ์ ์ด๋ผ๋ ๊ฒฐ๋ก ์ ๋๋ฌํ์ต๋๋ค(์คํฌ์ผ๋ฌ: ์๋์), ์ฐ๋ฆฌ ์๋น์ค๋ฅผ ์ํด ํ๋ก์ ํธ๊ฐ ๋ ผ๋ฆฌ๋ฅผ ์ป๊ธฐ ์์ํ์ต๋๋ค.
๋จ๊ณ์ ์ผ๋ก ์๋ก์ด ๋ชจ๋์ด ๋ํ๋๊ธฐ ์์ํ๊ณ ์๋ฒ์ ๊ธฐ๋ฅ์ด ์ฑ์ฅํ์ต๋๋ค. ์๋ฒ์ ํ ์คํธ ๋๋ฉ์ธ๊ณผ SSL ์ฐ๊ฒฐ ์ํธํ๊ฐ ์์ต๋๋ค.์๋ฒ์ ๋ ผ๋ฆฌ์ ํด๋ผ์ด์ธํธ ์ฒ๋ฆฌ๋ฅผ ์ค๋ช ํ๋ ์กฐ๊ธ ๋ ๋ง์ ์ฝ๋
์ธ์ฆ์ ์ฌ์ฉ์ ํฌํจํ์ฌ ์๋ฒ์ ์ ๋ฐ์ดํธ๋ ๋ฒ์ ์ ๋๋ค.using System; using System.Net; using System.Net.Sockets; using System.Reflection; using System.Security; using System.Security.Cryptography.X509Certificates; using System.Security.Permissions; using System.Security.Policy; using System.Threading; namespace ClearServer { sealed class Server { readonly bool ServerRunning = true; readonly TcpListener sslListner; public static X509Certificate serverCertificate = null; Server() { serverCertificate = X509Certificate.CreateFromSignedFile(@"C:sslitinder.online.crt"); sslListner = new TcpListener(IPAddress.Any, 443); sslListner.Start(); Console.WriteLine("Starting server.." + serverCertificate.Subject + "n" + Assembly.GetExecutingAssembly().Location); while (ServerRunning) { TcpClient SslClient = sslListner.AcceptTcpClient(); Thread SslThread = new Thread(new ParameterizedThreadStart(ClientThread)); SslThread.Start(SslClient); } } static void ClientThread(Object StateInfo) { new Client((TcpClient)StateInfo); } ~Server() { if (sslListner != null) { sslListner.Stop(); } } public static void Main(string[] args) { if (AppDomain.CurrentDomain.IsDefaultAppDomain()) { Console.WriteLine("Switching another domain"); new AppDomainSetup { ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase }; var current = AppDomain.CurrentDomain; var strongNames = new StrongName[0]; var domain = AppDomain.CreateDomain( "ClearServer", null, current.SetupInformation, new PermissionSet(PermissionState.Unrestricted), strongNames); domain.ExecuteAssembly(Assembly.GetExecutingAssembly().Location); } new Server(); } } }
SSL์ ํตํ ๊ถํ ๋ถ์ฌ๊ฐ ์๋ ์๋ก์ด ํด๋ผ์ด์ธํธ ์ฒ๋ฆฌ๊ธฐ:
using ClearServer.Core.Requester; using System; using System.Net.Security; using System.Net.Sockets; namespace ClearServer { public class Client { public Client(TcpClient Client) { SslStream SSlClientStream = new SslStream(Client.GetStream(), false); try { SSlClientStream.AuthenticateAsServer(Server.serverCertificate, clientCertificateRequired: false, checkCertificateRevocation: true); } catch (Exception e) { Console.WriteLine( "---------------------------------------------------------------------n" +
quot;|{DateTime.Now:g}n|------------n|{Client.Client.RemoteEndPoint}n|------------n|Exception: {e.Message}n|------------n|Authentication failed - closing the connection.n" +
"---------------------------------------------------------------------n");
SSlClientStream.Close();
Client.Close();
}
new RequestContext(SSlClientStream, Client);
}
}
}
ํ์ง๋ง ์๋ฒ๋ TCP ์ฐ๊ฒฐ๋ก๋ง ๋์ํ๊ธฐ ๋๋ฌธ์ ์์ฒญ ์ปจํ ์คํธ๋ฅผ ์ธ์ํ ์ ์๋ ๋ชจ๋์ ๋ง๋ค์ด์ผ ํฉ๋๋ค. ํด๋ผ์ด์ธํธ์๊ฒ ํ์ํ ๋ต๋ณ์ ์ ๊ณตํ๊ธฐ ์ํด ํด๋ผ์ด์ธํธ์ ์์ฒญ์ ๋ด๊ฐ ์ํธ ์์ฉํ ์ ์๋ ๋ณ๋์ ๋ถ๋ถ์ผ๋ก ๋๋ ํ์๊ฐ ์ฌ๊ธฐ์ ์ ํฉํ๋ค๊ณ ๊ฒฐ์ ํ์ต๋๋ค.ํ์
using ClearServer.Core.UserController; using ReServer.Core.Classes; using System; using System.Collections.Generic; using System.Linq; using System.Net.Security; using System.Net.Sockets; using System.Text; using System.Text.RegularExpressions; namespace ClearServer.Core.Requester { public class RequestContext { public string Message = ""; private readonly byte[] buffer = new byte[1024]; public string RequestMethod; public string RequestUrl; public User RequestProfile; public User CurrentUser = null; public List<RequestValues> HeadersValues; public List<RequestValues> FormValues; private TcpClient TcpClient; private event Action<SslStream, RequestContext> OnRead = RequestHandler.OnHandle; DatabaseWorker databaseWorker = new DatabaseWorker(); public RequestContext(SslStream ClientStream, TcpClient Client) { this.TcpClient = Client; try { ClientStream.BeginRead(buffer, 0, buffer.Length, ClientRead, ClientStream); } catch { return; } } private void ClientRead(IAsyncResult ar) { SslStream ClientStream = (SslStream)ar.AsyncState; if (ar.IsCompleted) { Message = Encoding.UTF8.GetString(buffer); Message = Uri.UnescapeDataString(Message); Console.WriteLine(
quot;n{DateTime.Now:g} Client IP:{TcpClient.Client.RemoteEndPoint}n{Message}");
RequestParse();
HeadersValues = HeaderValues();
FormValues = ContentValues();
UserParse();
ProfileParse();
OnRead?.Invoke(ClientStream, this);
}
}private void RequestParse()
{
Match methodParse = Regex.Match(Message, @"(^w+)s+([^s?]+)[^s]*s+HTTP/.*|");
RequestMethod = methodParse.Groups[1].Value.Trim();
RequestUrl = methodParse.Groups[2].Value.Trim();
}
private void UserParse()
{
string cookie;
try
{
if (HeadersValues.Any(x => x.Name.Contains("Cookie")))
{
cookie = HeadersValues.FirstOrDefault(x => x.Name.Contains("Cookie")).Value;
try
{
CurrentUser = databaseWorker.CookieValidate(cookie);
}
catch { }
}
}
catch { }}
private List<RequestValues> HeaderValues()
{
var values = new List<RequestValues>();
var parse = Regex.Matches(Message, @"(.*?): (.*?)n");
foreach (Match match in parse)
{
values.Add(new RequestValues()
{
Name = match.Groups[1].Value.Trim(),
Value = match.Groups[2].Value.Trim()
});
}
return values;
}
private void ProfileParse()
{
if (RequestUrl.Contains("@"))
{
RequestProfile = databaseWorker.FindUser(RequestUrl.Substring(2));
RequestUrl = "/profile";
}
}
private List<RequestValues> ContentValues()
{
var values = new List<RequestValues>();
var output = Message.Trim('n').Split().Last();
var parse = Regex.Matches(output, @"([^&].*?)=([^&]*b)");
foreach (Match match in parse)
{
values.Add(new RequestValues()
{
Name = match.Groups[1].Value.Trim(),
Value = match.Groups[2].Value.Trim().Replace('+', ' ')
});
}
return values;
}
}
}
๊ทธ ๋ณธ์ง์ ์ ๊ท ํํ์์ ์ฌ์ฉํ์ฌ ์์ฒญ์ ์ฌ๋ฌ ๋ถ๋ถ์ผ๋ก ๋๋๋ค๋ ์ฌ์ค์ ์์ต๋๋ค. ํด๋ผ์ด์ธํธ๋ก๋ถํฐ ๋ฉ์์ง๋ฅผ ๋ฐ๊ณ ๋ฉ์๋์ ์์ฒญ URL์ด ํฌํจ๋ ์ฒซ ๋ฒ์งธ ์ค์ ์ ํํฉ๋๋ค. ๊ทธ๋ฐ ๋ค์ HeaderName = Content ํ์์ ๋ฐฐ์ด๋ก ๊ตฌ๋ํ๋ ํค๋๋ฅผ ์ฝ๊ณ ์ ์ฌํ ๋ฐฐ์ด๋ก ๊ตฌ๋ํ๋ ์๋ฐ๋๋ ์ฝํ ์ธ (์: ์ฟผ๋ฆฌ ๋ฌธ์์ด)๊ฐ ์๋ ๊ฒฝ์ฐ ์ฐพ๊ธฐ๋ ํฉ๋๋ค. ๋ํ ํ์๋ ํ์ฌ ํด๋ผ์ด์ธํธ๊ฐ ๊ถํ์ด ์๋์ง ํ์ธํ๊ณ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํฉ๋๋ค. ์น์ธ๋ ํด๋ผ์ด์ธํธ์ ๋ชจ๋ ์์ฒญ์๋ ์ฟ ํค์ ์ ์ฅ๋๋ ์น์ธ ํด์๊ฐ ํฌํจ๋์ด ์์ต๋๋ค. ๋๋ถ์ ๋ ๊ฐ์ง ์ ํ์ ํด๋ผ์ด์ธํธ์ ๋ํ ์ถ๊ฐ ์์ ๋ก์ง์ ๋ถ๋ฆฌํ๊ณ ์ฌ๋ฐ๋ฅธ ๋ต๋ณ์ ์ ๊ณตํ ์ ์์ต๋๋ค.์, "site.com/@UserName"๊ณผ ๊ฐ์ ์์ฒญ์ ๋์ ์ผ๋ก ์์ฑ๋ ์ฌ์ฉ์ ํ์ด์ง๋ก ๋ณํํ๋ ๋ณ๋์ ๋ชจ๋๋ก ์ด๋ํด์ผ ํ๋ ์๊ณ ๋ฉ์ง ๊ธฐ๋ฅ์ ๋๋ค. ์์ฒญ์ ์ฒ๋ฆฌํ ํ ๋ค์ ๋ชจ๋์ด ์๋ํฉ๋๋ค.
Chapter 3. ํธ๋ค๋ฐ ์ค์น, ์ฒด์ธ ์คํ
ํ์๊ฐ ์๋ฃ๋์๋ง์ ํธ๋ค๋ฌ๊ฐ ์๋ํ์ฌ ์๋ฒ์ ์ถ๊ฐ ์ง์นจ์ ์ ๊ณตํ๊ณ ์ ์ด๋ฅผ ๋ ๋ถ๋ถ์ผ๋ก ๋๋๋๋ค.
๋จ์ ํธ๋ค๋ฌ
using ClearServer.Core.UserController; using System.Net.Security; namespace ClearServer.Core.Requester { public class RequestHandler { public static void OnHandle(SslStream ClientStream, RequestContext context) { if (context.CurrentUser != null) { new AuthUserController(ClientStream, context); } else { new NonAuthUserController(ClientStream, context); }; } } }
์ค์ ๋ก ์์ฒญ ์ฒ๋ฆฌ๊ฐ ์์๋ ํ ์ฌ์ฉ์ ์ธ์ฆ์ ๋ํ ํ์ธ์ด ํ ๋ฒ๋ง ์์ต๋๋ค.
ํด๋ผ์ด์ธํธ ์ปจํธ๋กค๋ฌ
์ฌ์ฉ์๊ฐ ๊ถํ์ด ์๋ ๊ฒฝ์ฐ ๊ธฐ๋ฅ์ ์ฌ์ฉ์ ํ๋กํ ํ์ ๋ฐ ๊ถํ ๋ฑ๋ก ์ฐฝ์๋ง ๊ธฐ๋ฐํฉ๋๋ค. ์ธ์ฆ๋ ์ฌ์ฉ์์ ๋ํ ์ฝ๋๋ ๊ฑฐ์ ๋์ผํ๊ฒ ๋ณด์ด๋ฏ๋ก ๋ณต์ ํ ์ด์ ๊ฐ ์์ต๋๋ค.์น์ธ๋์ง ์์ ์ฌ์ฉ์
using ClearServer.Core.Requester; using System.IO; using System.Net.Security; namespace ClearServer.Core.UserController { internal class NonAuthUserController { private readonly SslStream ClientStream; private readonly RequestContext Context; private readonly WriteController WriteController; private readonly AuthorizationController AuthorizationController; private readonly string ViewPath = "C:/Users/drdre/source/repos/ClearServer/View"; public NonAuthUserController(SslStream clientStream, RequestContext context) { this.ClientStream = clientStream; this.Context = context; this.WriteController = new WriteController(clientStream); this.AuthorizationController = new AuthorizationController(clientStream, context); ResourceLoad(); } void ResourceLoad() { string[] blockextension = new string[] {"cshtml", "html", "htm"}; bool block = false; foreach (var item in blockextension) { if (Context.RequestUrl.Contains(item)) { block = true; break; } } string FilePath = ""; string Header = ""; var RazorController = new RazorController(Context, ClientStream); switch (Context.RequestMethod) { case "GET": switch (Context.RequestUrl) { case "/": FilePath = ViewPath + "/loginForm.html"; Header =
quot;HTTP/1.1 200 OKnContent-Type: text/html";
WriteController.DefaultWriter(Header, FilePath);
break;
case "/profile":
RazorController.ProfileLoader(ViewPath);
break;
default:
//ะฒ ะดะฐะฝะฝะพะผ ะฑะปะพะบะต ะบะพะดะฐ ะฟัะพะธัั ะพะดะธั ะพััะตัะตะฝะธะต ะทะฐะฟัะพัะพะฒ ะบ ัะตัะฒะตัั ะฟะพ ะฟััะผะพะผั ะฐะดัะตัั ัััะฐะฝะธัั ะฒะธะดะฐ site.com/page.html
if (!File.Exists(ViewPath + Context.RequestUrl) | block)
{
RazorController.ErrorLoader(404);}
else if (Path.HasExtension(Context.RequestUrl) && File.Exists(ViewPath + Context.RequestUrl))
{
Header = WriteController.ContentType(Context.RequestUrl);
FilePath = ViewPath + Context.RequestUrl;
WriteController.DefaultWriter(Header, FilePath);
}
break;
}
break;case "POST":
AuthorizationController.MethodRecognizer();
break;}
}
}
}
๋ฌผ๋ก ์ฌ์ฉ์๋ ํ์ด์ง์ ์ผ๋ถ ์ฝํ ์ธ ๋ฅผ ๋ฐ์์ผ ํ๋ฏ๋ก ์๋ต์ ์ํด ๋ฆฌ์์ค ์์ฒญ์ ์๋ตํ๋ ๋ค์ ๋ชจ๋์ด ์์ต๋๋ค.์๊ฐ ์ปจํธ๋กค๋ฌ
using System; using System.IO; using System.Net.Security; using System.Text; namespace ClearServer.Core.UserController { public class WriteController { SslStream ClientStream; public WriteController(SslStream ClientStream) { this.ClientStream = ClientStream; } public void DefaultWriter(string Header, string FilePath) { FileStream fileStream; try { fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); Header =
quot;{Header}nContent-Length: {fileStream.Length}nn";
ClientStream.Write(Encoding.UTF8.GetBytes(Header));
byte[] response = new byte[fileStream.Length];
fileStream.BeginRead(response, 0, response.Length, OnFileRead, response);
}
catch { }
}public string ContentType(string Uri)
{
string extension = Path.GetExtension(Uri);
string Header = "HTTP/1.1 200 OKnContent-Type:";
switch (extension)
{
case ".html":
case ".htm":
returnquot;{Header} text/html";
case ".css":
returnquot;{Header} text/css";
case ".js":
returnquot;{Header} text/javascript";
case ".jpg":
case ".jpeg":
case ".png":
case ".gif":
returnquot;{Header} image/{extension}";
default:
if (extension.Length > 1)
{
returnquot;{Header} application/" + extension.Substring(1);
}
else
{
returnquot;{Header} application/unknown";
}
}
}public void OnFileRead(IAsyncResult ar)
{
if (ar.IsCompleted)
{
var file = (byte[])ar.AsyncState;
ClientStream.BeginWrite(file, 0, file.Length, OnClientSend, null);
}
}
public void OnClientSend(IAsyncResult ar)
{
if (ar.IsCompleted)
{
ClientStream.Close();
}
}
}
๊ทธ๋ฌ๋ ์ฌ์ฉ์์๊ฒ ์์ ์ ํ๋กํ๊ณผ ๋ค๋ฅธ ์ฌ์ฉ์์ ํ๋กํ์ ๋ณด์ฌ์ฃผ๊ธฐ ์ํด RazorEngine ๋๋ ๊ทธ ์ผ๋ถ๋ฅผ ์ฌ์ฉํ๊ธฐ๋ก ๊ฒฐ์ ํ์ต๋๋ค. ๋ํ ์๋ชป๋ ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ ์ ์ ํ ์ค๋ฅ ์ฝ๋๋ฅผ ๋ฐํํ๋ ๊ฒ๋ ํฌํจ๋ฉ๋๋ค.๋ ์ด์ ์ปจํธ๋กค๋ฌ
using ClearServer.Core.Requester; using RazorEngine; using RazorEngine.Templating; using System; using System.IO; using System.Net; using System.Net.Security; namespace ClearServer.Core.UserController { internal class RazorController { private RequestContext Context; private SslStream ClientStream; dynamic PageContent; public RazorController(RequestContext context, SslStream clientStream) { this.Context = context; this.ClientStream = clientStream; } public void ProfileLoader(string ViewPath) { string Filepath = ViewPath + "/profile.cshtml"; if (Context.RequestProfile != null) { if (Context.CurrentUser != null && Context.RequestProfile.login == Context.CurrentUser.login) { try { PageContent = new { isAuth = true, Name = Context.CurrentUser.name, Login = Context.CurrentUser.login, Skills = Context.CurrentUser.skills }; ClientSend(Filepath, Context.CurrentUser.login); } catch (Exception e) { Console.WriteLine(e); } } else { try { PageContent = new { isAuth = false, Name = Context.RequestProfile.name, Login = Context.RequestProfile.login, Skills = Context.RequestProfile.skills }; ClientSend(Filepath, "PublicProfile:"+ Context.RequestProfile.login); } catch (Exception e) { Console.WriteLine(e); } } } else { ErrorLoader(404); } } public void ErrorLoader(int Code) { try { PageContent = new { ErrorCode = Code, Message = ((HttpStatusCode)Code).ToString() }; string ErrorPage = "C:/Users/drdre/source/repos/ClearServer/View/Errors/ErrorPage.cshtml"; ClientSend(ErrorPage, Code.ToString()); } catch { } } private void ClientSend(string FilePath, string Key) { var template = File.ReadAllText(FilePath); var result = Engine.Razor.RunCompile(template, Key, null, (object)PageContent); byte[] buffer = System.Text.Encoding.UTF8.GetBytes(result); ClientStream.BeginWrite(buffer, 0, buffer.Length, OnClientSend, ClientStream); } private void OnClientSend(IAsyncResult ar) { if (ar.IsCompleted) { ClientStream.Close(); } } } }
๋ฌผ๋ก ๊ถํ์ด ์๋ ์ฌ์ฉ์์ ํ์ธ์ด ์๋ํ๋ ค๋ฉด ๊ถํ์ด ํ์ํฉ๋๋ค. ์ธ์ฆ ๋ชจ๋์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ํธ ์์ฉํฉ๋๋ค. ์ฌ์ดํธ์ ์์์์ ์์ ๋ ๋ฐ์ดํฐ๋ ์ปจํ ์คํธ์์ ๊ตฌ๋ฌธ ๋ถ์๋๊ณ ์ฌ์ฉ์๋ ์ ์ฅ๋๋ฉฐ ์ฟ ํค๋ฅผ ์์ ํ๊ณ ๊ทธ ๋๊ฐ๋ก ์๋น์ค์ ์ก์ธ์คํฉ๋๋ค.
์ธ์ฆ ๋ชจ๋
using ClearServer.Core.Cookies; using ClearServer.Core.Requester; using ClearServer.Core.Security; using System; using System.Linq; using System.Net.Security; using System.Text; namespace ClearServer.Core.UserController { internal class AuthorizationController { private SslStream ClientStream; private RequestContext Context; private UserCookies cookies; private WriteController WriteController; DatabaseWorker DatabaseWorker; RazorController RazorController; PasswordHasher PasswordHasher; public AuthorizationController(SslStream clientStream, RequestContext context) { ClientStream = clientStream; Context = context; DatabaseWorker = new DatabaseWorker(); WriteController = new WriteController(ClientStream); RazorController = new RazorController(context, clientStream); PasswordHasher = new PasswordHasher(); } internal void MethodRecognizer() { if (Context.FormValues.Count == 2 && Context.FormValues.Any(x => x.Name == "password")) Authorize(); else if (Context.FormValues.Count == 3 && Context.FormValues.Any(x => x.Name == "regPass")) Registration(); else { RazorController.ErrorLoader(401); } } private void Authorize() { var values = Context.FormValues; var user = new User() { login = values[0].Value, password = PasswordHasher.PasswordHash(values[1].Value) }; user = DatabaseWorker.UserAuth(user); if (user != null) { cookies = new UserCookies(user.login, user.password); user.cookie = cookies.AuthCookie; DatabaseWorker.UserUpdate(user); var response = Encoding.UTF8.GetBytes(
quot;HTTP/1.1 301 Moved PermanentlynLocation: /@{user.login}nSet-Cookie: {cookies.AuthCookie}; Expires={DateTime.Now.AddDays(2):R}; Secure; HttpOnlynn");
ClientStream.BeginWrite(response, 0, response.Length, WriteController.OnClientSend, null);}
else
{
RazorController.ErrorLoader(401);}
}private void Registration()
{
var values = Context.FormValues;
var user = new User()
{
name = values[0].Value,
login = values[1].Value,
password = PasswordHasher.PasswordHash(values[2].Value),
};
cookies = new UserCookies(user.login, user.password);
user.cookie = cookies.AuthCookie;
if (DatabaseWorker.LoginValidate(user.login))
{
Console.WriteLine("User ready");
Console.WriteLine(quot;{user.password} {user.password.Trim().Length}");
DatabaseWorker.UserRegister(user);
var response = Encoding.UTF8.GetBytes(
quot;HTTP/1.1 301 Moved PermanentlynLocation: /@{user.login}nSet-Cookie: {user.cookie}; Expires={DateTime.Now.AddDays(2):R}; Secure; HttpOnlynn");
ClientStream.BeginWrite(response, 0, response.Length, WriteController.OnClientSend, null);
}
else
{
RazorController.ErrorLoader(401);
}
}
}
}
๋ฐ์ดํฐ๋ฒ ์ด์ค๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.๋ฐ์ดํฐ๋ฒ ์ด์ค
using ClearServer.Core.UserController; using System; using System.Data.Linq; using System.Linq; namespace ClearServer { class DatabaseWorker { private readonly Table<User> users = null; private readonly DataContext DataBase = null; private const string connectionStr = @"ะฟัััะบะฑะฐะทะต"; public DatabaseWorker() { DataBase = new DataContext(connectionStr); users = DataBase.GetTable<User>(); } public User UserAuth(User User) { try { var user = users.SingleOrDefault(t => t.login.ToLower() == User.login.ToLower() && t.password == User.password); if (user != null) return user; else return null; } catch (Exception) { return null; } } public void UserRegister(User user) { try { users.InsertOnSubmit(user); DataBase.SubmitChanges(); Console.WriteLine(
quot;User{user.name} with id {user.uid} added");
foreach (var item in users)
{
Console.WriteLine(item.login + "n");
}
}
catch (Exception e)
{
Console.WriteLine(e);
}}
public bool LoginValidate(string login)
{
if (users.Any(x => x.login.ToLower() == login.ToLower()))
{
Console.WriteLine("Login already exists");
return false;
}
return true;
}
public void UserUpdate(User user)
{
var UserToUpdate = users.FirstOrDefault(x => x.uid == user.uid);
UserToUpdate = user;
DataBase.SubmitChanges();
Console.WriteLine(quot;User {UserToUpdate.name} with id {UserToUpdate.uid} updated");
foreach (var item in users)
{
Console.WriteLine(item.login + "n");
}
}
public User CookieValidate(string CookieInput)
{
User user = null;
try
{
user = users.SingleOrDefault(x => x.cookie == CookieInput);
}
catch
{
return null;
}
if (user != null) return user;
else return null;
}
public User FindUser(string login)
{
User user = null;
try
{
user = users.Single(x => x.login.ToLower() == login.ToLower());
if (user != null)
{
return user;
}
else
{
return null;
}
}
catch (Exception)
{
return null;
}
}
}
}
๊ทธ๋ฆฌ๊ณ ๋ชจ๋ ๊ฒ์ด ์๊ณ, ์ธ์ฆ ๋ฐ ๋ฑ๋ก ์์ ์ฒ๋ผ ์๋ํ๋ฉฐ ์๋น์ค์ ๋ํ ์ก์ธ์ค์ ์ต์ ๊ธฐ๋ฅ์ ์ด๋ฏธ ์ฌ์ฉ ๊ฐ๋ฅํ๋ฉฐ ์์ฉ ํ๋ก๊ทธ๋จ์ ์์ฑํ๊ณ ๋ชจ๋ ๊ฒ์ ์ํํ๋ ์ฃผ์ ๊ธฐ๋ฅ๊ณผ ๋ชจ๋ ๊ฒ์ ์ฐ๊ฒฐํ ๋์ ๋๋ค.4์ฅ
๋ ํ๋ซํผ์ ๋ ๊ฐ์ ์ ํ๋ฆฌ์ผ์ด์ ์ ์์ฑํ๋ ์ธ๊ฑด๋น๋ฅผ ์ค์ด๊ธฐ ์ํด Xamarin.Forms์์ ํฌ๋ก์ค ํ๋ซํผ์ ๋ง๋ค๊ธฐ๋ก ํ์ต๋๋ค. ๋ค์ ๋งํ์ง๋ง C#์ ์๋ค๋ ์ฌ์ค ๋๋ถ์ ๋๋ค. ๋จ์ํ ๋ฐ์ดํฐ๋ฅผ ์๋ฒ๋ก ๋ณด๋ด๋ ํ ์คํธ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ง๋ ํ ํฅ๋ฏธ๋ก์ด ์๊ฐ์ ๋ง๋ฌ์ต๋๋ค. ์ฅ์น์ ์์ฒญ์ ๋ํด์๋ ์ฌ๋ฏธ๋ก HttpClient์ ๊ตฌํํ๊ณ json ํ์์ ์น์ธ ์์ ๋ฐ์ดํฐ๊ฐ ํฌํจ๋ HttpRequestMessage ์๋ฒ์ ๋์ก์ต๋๋ค. ํน๋ณํ ๊ฒ์ ๊ธฐ๋ํ์ง ์๊ณ ์๋ฒ ๋ก๊ทธ๋ฅผ ์ด์๊ณ ๋ชจ๋ ๋ฐ์ดํฐ๊ฐ ์๋ ์ฅ์น์ ์์ฒญ์ ๋ณด์์ต๋๋ค. ๊ฐ๋ฒผ์ด ๋ฌด๊ฐ๊ฐ, ๋๋ฅธํ ์ ๋ ์ ์ง๋ 3์ฃผ ๋์ ํํด์ง ๋ชจ๋ ๊ฒ์ ๋ํ ์๊ฐ. ์ ์ก๋ ๋ฐ์ดํฐ์ ์ ํ์ฑ์ ํ์ธํ๊ธฐ ์ํด HttpListner์ ํ ์คํธ ์๋ฒ๋ฅผ ๊ตฌ์ฑํ์ต๋๋ค. ๋ค์ ์์ฒญ์ ์ด๋ฏธ ๋ฐ์์ผ๋ฏ๋ก ๋ช ์ค์ ์ฝ๋๋ก ๋ถํดํ๊ณ ์์์์ KeyValuePair ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์์ต๋๋ค. ์ฟผ๋ฆฌ ๊ตฌ๋ฌธ ๋ถ์์ด ๋ ์ค๋ก ์ค์์ต๋๋ค.
์ถ๊ฐ ํ ์คํธ๋ฅผ ์์ํ๋๋ฐ ์ด์ ์๋ ์ธ๊ธ๋์ง ์์์ง๋ง ์ด์ ์๋ฒ์์๋ ์ฌ์ ํ ์น ์์ผ์ ๊ตฌ์ถ๋ ์ฑํ ์ ๊ตฌํํ์ต๋๋ค. ๊ทธ๊ฒ์ ๊ฝค ์ ์๋ํ์ง๋ง Tcp๋ฅผ ํตํ ์ํธ ์์ฉ์ ์๋ฆฌ ์์ฒด๊ฐ ์ฐ์ธํ์ต๋๋ค. ์์ ๋ก๊น ์ผ๋ก ๋ ์ฌ์ฉ์์ ์ํธ ์์ฉ์ ์ฌ๋ฐ๋ฅด๊ฒ ๊ตฌ์ถํ๋ ค๋ฉด ๋๋ฌด ๋ง์ ์ถ๊ฐ ์์ ์ด ํ์ํ์ต๋๋ค. ์ฌ๊ธฐ์๋ RFC 6455 ํ๋กํ ์ฝ์ ์ฌ์ฉํ์ฌ ์ฐ๊ฒฐ ์ ํ ์์ฒญ์ ๊ตฌ๋ฌธ ๋ถ์ํ๊ณ ์๋ต์ ์์งํ๋ ๊ฒ์ด ํฌํจ๋๋ฏ๋ก ํ ์คํธ ์๋ฒ์์ ๊ฐ๋จํ ์น ์์ผ ์ฐ๊ฒฐ์ ๋ง๋ค๊ธฐ๋ก ๊ฒฐ์ ํ์ต๋๋ค. ์์ ํ ๊ด์ฌ์ ์ํด.
์ฑํ ์ฐ๊ฒฐ
private static async void HandleWebsocket(HttpListenerContext context) { var socketContext = await context.AcceptWebSocketAsync(null); var socket = socketContext.WebSocket; Locker.EnterWriteLock(); try { Clients.Add(socket); } finally { Locker.ExitWriteLock(); } while (true) { var buffer = new ArraySegment<byte>(new byte[1024]); var result = await socket.ReceiveAsync(buffer, CancellationToken.None); var str = Encoding.Default.GetString(buffer); Console.WriteLine(str); for (int i = 0; i < Clients.Count; i++) { WebSocket client = Clients[i]; try { if (client.State == WebSocketState.Open) { await client.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None); } } catch (ObjectDisposedException) { Locker.EnterWriteLock(); try { Clients.Remove(client); i--; } finally { Locker.ExitWriteLock(); } } } } }
๊ทธ๋ฆฌ๊ณ ๊ทธ๊ฒ์ ํจ๊ณผ๊ฐ ์์์ต๋๋ค. ์๋ฒ ์์ฒด๊ฐ ์ฐ๊ฒฐ์ ์ค์ ํ๊ณ ์๋ต ํค๋ฅผ ์์ฑํ์ต๋๋ค. SSL์ ํตํด ์๋ฒ ๋ฑ๋ก์ ๋ณ๋๋ก ๊ตฌ์ฑํ ํ์๋ ์์์ต๋๋ค. ์์คํ ์ ์ด๋ฏธ ํ์ํ ํฌํธ์ ์ธ์ฆ์๊ฐ ์ค์น๋์ด ์์ผ๋ฉด ์ถฉ๋ถํฉ๋๋ค.
์ฅ์น ์ธก๊ณผ ์ฌ์ดํธ ์ธก์์ ๋ ํด๋ผ์ด์ธํธ๊ฐ ๋ฉ์์ง๋ฅผ ๊ตํํ๊ณ ์ด ๋ชจ๋ ๊ฒ์ด ๊ธฐ๋ก๋์์ต๋๋ค. ์๋ฒ ์๋๋ฅผ ๋ฆ์ถ๋ ๊ฑฐ๋ํ ํ์๊ฐ ์์์ต๋๋ค. ์ด ์ค ์ด๋ ๊ฒ๋ ํ์ํ์ง ์์์ต๋๋ค. ์๋ต ์๊ฐ์ด 200ms์์ 40-30ms๋ก ๋จ์ถ๋์์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ ๋๋ ์ ์ผํ๊ฒ ์ฌ๋ฐ๋ฅธ ๊ฒฐ์ ์ ๋ด๋ ธ์ต๋๋ค.
Tcp์์ ํ์ฌ ์๋ฒ ๊ตฌํ์ ๋ฒ๋ฆฌ๊ณ Http์์ ๋ชจ๋ ๊ฒ์ ๋ค์ ์์ฑํ์ญ์์ค. ์ด์ ํ๋ก์ ํธ๋ ์ฌ์ค๊ณ ๋จ๊ณ์ ์์ง๋ง ์์ ํ ๋ค๋ฅธ ์ํธ ์์ฉ ์์น์ ๋ฐ๋ฆ ๋๋ค. ์ฅ์น์ ์ฌ์ดํธ์ ์๋์ ๋๊ธฐํ๋๊ณ ๋๋ฒ๊น ๋๋ฉฐ ์ฅ์น๊ฐ html ํ์ด์ง๋ฅผ ์์ฑํ ํ์๊ฐ ์๋ค๋ ์ ์ผํ ์ฐจ์ด์ ์ ์ ์ธํ๊ณ ๋ ๊ณตํต ๊ฐ๋ ์ ๊ฐ์ง๋๋ค.
์ถ๋ ฅ
"๋๋ฃจ๋ ๋ชจ๋ฅด๊ณ ๋ฌผ์ ๋จธ๋ฆฌ๋ฅผ ์ฐ๋ฅด์ง ๋ง๋ผ" ์์ ์ ์์ํ๊ธฐ ์ ์ ๋ชฉํ์ ๋ชฉํ๋ฅผ ๋ ๋ช ํํ๊ฒ ์ ์ํ๊ณ ๋ค์ํ ํด๋ผ์ด์ธํธ์์ ๊ตฌํํ๋ ๋ฐ ํ์ํ ๊ธฐ์ ๊ณผ ๋ฐฉ๋ฒ์ ์ฐ๊ตฌํด์ผ ํ๋ค๊ณ ์๊ฐํฉ๋๋ค. ์ด ํ๋ก์ ํธ๋ ์ด๋ฏธ ์์ฑ์ ๊ฐ๊น์์ง๊ณ ์์ง๋ง, ๋ด๊ฐ ์ด๋ค ์ผ์ ๋ ์ด๋ป๊ฒ ๋ง์ณค๋์ง์ ๋ํด ์ด์ผ๊ธฐํ๊ธฐ ์ํด ๋ค์ ๋์์ฌ ๊ฒ์ ๋๋ค. ๊ฐ๋ฐ ๊ณผ์ ์์ ๋ง์ ๊ฒ์ ๋ฐฐ์ ์ง๋ง ์์ผ๋ก ๋ฐฐ์ธ ๊ฒ์ด ๋ ๋ง์ต๋๋ค. ์ฌ๊ธฐ๊น์ง ์ฝ์ผ์ จ๋ค๋ฉด ์ฝ์ด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค.
์ถ์ฒ : habr.com