-
Notifications
You must be signed in to change notification settings - Fork 2
/
PlayURLoginWebService.cs
133 lines (112 loc) · 5.11 KB
/
PlayURLoginWebService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using UnityEngine;
using System.Linq;
using System.Text;
namespace PlayUR
{
public class PlayURLoginWebServer
{
private TcpListener myListener;
private static int port = 5050;
private static IPAddress localAddr = IPAddress.Parse("127.0.0.1");
public delegate void OnLoginSuccess(string json);
OnLoginSuccess callback;
public PlayURLoginWebServer(OnLoginSuccess callback)
{
this.callback = callback;
myListener = new TcpListener(localAddr, port);
myListener.Start();
PlayURPlugin.Log($"Login Web Server Running on {localAddr.ToString()} on port {port}... ");
Thread th = new Thread(new ThreadStart(StartListen));
th.Start();
//now we can launch playur login page
Application.OpenURL("https://playur.io/api/Login/standalone.php");
}
private void StartListen()
{
//while (true) //just need one good request then done
{
TcpClient client = myListener.AcceptTcpClient();
NetworkStream stream = client.GetStream();
//read request
byte[] requestBytes = new byte[1024];
int bytesRead = stream.Read(requestBytes, 0, requestBytes.Length);
string request = Encoding.UTF8.GetString(requestBytes, 0, bytesRead);
var requestHeaders = ParseHeaders(request);
string[] requestFirstLine = requestHeaders.requestType.Split(" ");
string httpVersion = requestFirstLine.LastOrDefault();
string contentType = requestHeaders.headers.GetValueOrDefault("Accept");
string contentEncoding = requestHeaders.headers.GetValueOrDefault("Acept-Encoding");
if (!request.StartsWith("GET"))
{
SendHeaders(httpVersion, 405, "Method Not Allowed", contentType, contentEncoding, 0, ref stream);
}
else
{
var requestedPath = requestFirstLine[1];
if (requestedPath.StartsWith("/?auth"))
{
var fileContent = GetContent(requestedPath);
if (fileContent != null)
{
SendHeaders(httpVersion, 200, "OK", contentType, contentEncoding, 0, ref stream);
stream.Write(fileContent, 0, fileContent.Length);
}
}
else
{
SendHeaders(httpVersion, 404, "Page Not Found", contentType, contentEncoding, 0, ref stream);
}
}
client.Close();
//keep this thread alive just a little while before spinning down server
//Thread.Sleep(5000);
}
myListener.Stop();
}
private byte[] GetContent(string requestedPath)
{
if (requestedPath == "/") requestedPath = "default.html";
var text = requestedPath.Replace("/?auth=", "");
callback(text);
//convert text to byte array
var redirect = "<html><head><meta http-equiv=\"refresh\" content=\"0; url=https://playur.io/standaloneLoggedIn.php\" /></head><body>Redirecting...</body></html>";
byte[] file = Encoding.UTF8.GetBytes(redirect);
return file;
}
private void SendHeaders(string httpVersion, int statusCode, string statusMsg, string contentType, string contentEncoding,
int byteLength, ref NetworkStream networkStream)
{
string responseHeaderBuffer = "";
responseHeaderBuffer = $"HTTP/1.1 {statusCode} {statusMsg}\r\n" +
$"Connection: Keep-Alive\r\n" +
$"Date: {DateTime.UtcNow.ToString()}\r\n" +
$"Location: https://playur.io/standaloneLoggedIn.php\r\n\r\n";
byte[] responseBytes = Encoding.UTF8.GetBytes(responseHeaderBuffer);
networkStream.Write(responseBytes, 0, responseBytes.Length);
}
private (Dictionary<string, string> headers, string requestType) ParseHeaders(string headerString)
{
var headerLines = headerString.Split('\r', '\n');
string firstLine = headerLines[0];
var headerValues = new Dictionary<string, string>();
foreach (var headerLine in headerLines)
{
var headerDetail = headerLine.Trim();
var delimiterIndex = headerLine.IndexOf(':');
if (delimiterIndex >= 0)
{
var headerName = headerLine.Substring(0, delimiterIndex).Trim();
var headerValue = headerLine.Substring(delimiterIndex + 1).Trim();
headerValues.Add(headerName, headerValue);
}
}
return (headerValues, firstLine);
}
}
}