Skip to content

Commit 7311123

Browse files
committed
v1 - Initial version
1 parent 07348d8 commit 7311123

41 files changed

Lines changed: 2502 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/csharp/UbisoftConnectProxy.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31729.503
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UbisoftConnectProxy", "UbisoftConnectProxy\UbisoftConnectProxy.csproj", "{BBAECC19-04E3-4D0C-8F8D-24567CD41659}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{BBAECC19-04E3-4D0C-8F8D-24567CD41659}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{BBAECC19-04E3-4D0C-8F8D-24567CD41659}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{BBAECC19-04E3-4D0C-8F8D-24567CD41659}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{BBAECC19-04E3-4D0C-8F8D-24567CD41659}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {1F093FF1-AAEC-4F66-AF03-EB9D03233006}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#nullable enable
2+
3+
using Microsoft.AspNetCore.Http;
4+
using System;
5+
using System.IO;
6+
using System.Net.Http;
7+
using System.Threading.Tasks;
8+
9+
namespace UbisoftConnectProxy
10+
{
11+
public interface IProxy
12+
{
13+
Task ForwardAsync(HttpContext context);
14+
}
15+
16+
//public class DirectProxy : IProxy
17+
//{
18+
// public async Task ForwardAsync(HttpContext context)
19+
// {
20+
// try
21+
// {
22+
// using HttpClient httpClient = new();
23+
24+
// Uri targetUri = new("https://" + context.Request.Host + context.Request.Path +
25+
// (context.Request.QueryString.HasValue ? context.Request.QueryString.Value : ""));
26+
27+
// Console.WriteLine($"Forwarding {context.Request.Method} {targetUri}");
28+
29+
// HttpRequestMessage hrm = new(new(context.Request.Method), targetUri);
30+
31+
// foreach (var header in context.Request.Headers)
32+
// {
33+
// if (header.Key.StartsWith("Content"))
34+
// {
35+
// continue;
36+
// }
37+
38+
// foreach (var headerValue in header.Value)
39+
// {
40+
// hrm.Headers.Add(header.Key, headerValue);
41+
// }
42+
// }
43+
44+
// await using (MemoryStream memoryStream = new())
45+
// {
46+
// await context.Request.Body.CopyToAsync(memoryStream);
47+
// hrm.Content = new ByteArrayContent(memoryStream.ToArray());
48+
// }
49+
50+
// using var response = await httpClient.SendAsync(hrm);
51+
52+
// var content = await response.Content.ReadAsByteArrayAsync();
53+
54+
// foreach (var header in response.Headers)
55+
// {
56+
// foreach (var headerValue in header.Value)
57+
// {
58+
// context.Response.Headers.Add(header.Key, headerValue);
59+
// }
60+
// }
61+
62+
// context.Response.StatusCode = (int)response.StatusCode;
63+
// Console.WriteLine("Status of response: " + response.StatusCode);
64+
65+
// context.Response.ContentType = response.Content.Headers.ContentType?.ToString()!;
66+
// context.Response.ContentLength = response.Content.Headers.ContentLength;
67+
// foreach (var encoding in response.Content.Headers.ContentEncoding)
68+
// {
69+
// context.Response.Headers.Add("Content-Encoding", encoding);
70+
// }
71+
72+
// await context.Response.Body.WriteAsync(content);
73+
// await context.Response.Body.FlushAsync();
74+
// }
75+
// catch (Exception e)
76+
// {
77+
// Console.WriteLine("ERROR: " + e);
78+
// }
79+
// }
80+
//}
81+
}
22 KB
Binary file not shown.
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
#nullable enable
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Text;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
10+
namespace UbisoftConnectProxy.JavaInterop.Dtos
11+
{
12+
public interface IDataObject
13+
{
14+
Task ReadAsync(Stream stream);
15+
void Write(Stream stream);
16+
}
17+
18+
public class HeaderDto : IDataObject
19+
{
20+
public string Name { get; set; } = null!;
21+
public string[] Values { get; set; } = null!;
22+
23+
public async Task ReadAsync(Stream stream)
24+
{
25+
Name = await stream.ReadJUTF8Async();
26+
Values = await stream.ReadListAsync(() => stream.ReadJUTF8Async());
27+
}
28+
29+
public void Write(Stream stream)
30+
{
31+
stream.WriteJUTF8(Name);
32+
stream.WriteList(Values, stream.WriteJUTF8);
33+
}
34+
}
35+
36+
public abstract class HttpDataDto : IDataObject
37+
{
38+
public HeaderDto[] Headers { get; set; } = null!;
39+
public byte[] Content { get; set; } = null!;
40+
41+
public virtual async Task ReadAsync(Stream stream)
42+
{
43+
Headers = await stream.ReadListAsync(async () =>
44+
{
45+
var header = new HeaderDto();
46+
await header.ReadAsync(stream);
47+
return header;
48+
});
49+
Content = await stream.ReadFullyAsync(await stream.ReadJIntAsync());
50+
}
51+
52+
public virtual void Write(Stream stream)
53+
{
54+
stream.WriteList(Headers, x => x.Write(stream));
55+
stream.WriteJInt(Content.Length);
56+
stream.Write(Content);
57+
}
58+
}
59+
60+
public abstract class MessageDto<T> : IDataObject where T : IDataObject, new()
61+
{
62+
public int RequestId { get; set; }
63+
public T Data { get; set; } = default!;
64+
65+
public async Task ReadAsync(Stream stream)
66+
{
67+
RequestId = await stream.ReadJIntAsync();
68+
Data = new T();
69+
await Data.ReadAsync(stream);
70+
}
71+
72+
public void Write(Stream stream)
73+
{
74+
stream.WriteJInt(RequestId);
75+
Data.Write(stream);
76+
}
77+
}
78+
79+
public class ResponseDto : MessageDto<ResponseDataDto>
80+
{
81+
public const byte MsgId = 1;
82+
}
83+
84+
public static class JavaDnsReady
85+
{
86+
public const byte MsgId = 2;
87+
}
88+
89+
public class RequestDto : MessageDto<RequestDataDto>
90+
{
91+
public const byte MsgId = 10;
92+
}
93+
94+
public enum Ready : byte
95+
{
96+
Hosts, Starting, Running
97+
}
98+
99+
public class WebserverReady : IDataObject
100+
{
101+
public const byte MsgId = 11;
102+
103+
public Ready Ready { get; set; }
104+
105+
public async Task ReadAsync(Stream stream)
106+
{
107+
Ready = (Ready) await stream.ReadByteAsync();
108+
}
109+
110+
public void Write(Stream stream)
111+
{
112+
stream.WriteByte((byte)Ready);
113+
}
114+
}
115+
116+
public class WebserverErrorDto : IDataObject
117+
{
118+
public const byte MsgId = 12;
119+
120+
public bool Fatal;
121+
public string Text { get; set; } = "";
122+
123+
public async Task ReadAsync(Stream stream)
124+
{
125+
Fatal = await stream.ReadByteAsync() == 1;
126+
Text = await stream.ReadJUTF8Async();
127+
}
128+
129+
public void Write(Stream stream)
130+
{
131+
stream.WriteByte(Fatal ? (byte)1 : (byte)0);
132+
stream.WriteJUTF8(Text);
133+
}
134+
}
135+
136+
public class RequestDataDto : HttpDataDto
137+
{
138+
public string Uri { get; set; } = null!;
139+
public string Method { get; set; } = null!;
140+
141+
public override async Task ReadAsync(Stream stream)
142+
{
143+
Uri = await stream.ReadJUTF8Async();
144+
Method = await stream.ReadJUTF8Async();
145+
await base.ReadAsync(stream);
146+
}
147+
148+
public override void Write(Stream stream)
149+
{
150+
stream.WriteJUTF8(Uri);
151+
stream.WriteJUTF8(Method);
152+
base.Write(stream);
153+
}
154+
}
155+
156+
public class ResponseDataDto : HttpDataDto
157+
{
158+
public int StatusCode { get; set; }
159+
160+
public override async Task ReadAsync(Stream stream)
161+
{
162+
StatusCode = await stream.ReadJIntAsync();
163+
await base.ReadAsync(stream);
164+
}
165+
166+
public override void Write(Stream stream)
167+
{
168+
stream.WriteJInt(StatusCode);
169+
base.Write(stream);
170+
}
171+
}
172+
173+
public static class JavaBinaryStuff
174+
{
175+
public static void WriteList<T>(this Stream stream, IReadOnlyList<T> list, Action<T> writeItem)
176+
{
177+
stream.WriteJInt(list.Count);
178+
foreach (var item in list)
179+
{
180+
writeItem(item);
181+
}
182+
}
183+
184+
public static async Task<T[]> ReadListAsync<T>(this Stream stream, Func<Task<T>> readItem)
185+
{
186+
var list = new T[await stream.ReadJIntAsync()];
187+
for (int i = 0; i < list.Length; i++)
188+
{
189+
list[i] = await readItem();
190+
}
191+
return list;
192+
}
193+
194+
public static void WriteJInt(this Stream stream, int value)
195+
{
196+
stream.WriteByte((byte)(value >> 24));
197+
stream.WriteByte((byte)(value >> 16));
198+
stream.WriteByte((byte)(value >> 8));
199+
stream.WriteByte((byte)(value >> 0));
200+
}
201+
202+
public static void WriteJUShort(this Stream stream, ushort value)
203+
{
204+
stream.WriteByte((byte)(value >> 8));
205+
stream.WriteByte((byte)(value >> 0));
206+
}
207+
208+
public static void WriteJUTF8(this Stream stream, string value)
209+
{
210+
var bytes = Encoding.UTF8.GetBytes(value);
211+
if (bytes.Length > ushort.MaxValue)
212+
{
213+
throw new IOException($"String value too long ({bytes.Length})");
214+
}
215+
stream.WriteJUShort((ushort)bytes.Length);
216+
stream.Write(bytes);
217+
}
218+
219+
public static async Task<int> ReadJIntAsync(this Stream stream)
220+
{
221+
byte[] data = await stream.ReadFullyAsync(4);
222+
return (data[0] << 24)
223+
| (data[1] << 16)
224+
| (data[2] << 8)
225+
| (data[3] << 0);
226+
}
227+
228+
public static async Task<byte> ReadByteAsync(this Stream stream)
229+
{
230+
return (await stream.ReadFullyAsync(1))[0];
231+
}
232+
233+
public static async Task<ushort> ReadJUShortAsync(this Stream stream)
234+
{
235+
byte[] data = await stream.ReadFullyAsync(2);
236+
return (ushort)((data[0] << 8)
237+
| (data[1] << 0));
238+
}
239+
240+
public static async Task<string> ReadJUTF8Async(this Stream stream)
241+
{
242+
int length = await stream.ReadJUShortAsync();
243+
byte[] bytes = await stream.ReadFullyAsync(length);
244+
return Encoding.UTF8.GetString(bytes);
245+
}
246+
247+
public static async Task<byte[]> ReadFullyAsync(this Stream stream, int numBytes, CancellationToken cancellationToken = default)
248+
{
249+
var buffer = new byte[numBytes];
250+
await stream.ReadFullyAsync(buffer, 0, numBytes, cancellationToken);
251+
return buffer;
252+
}
253+
254+
public static async Task ReadFullyAsync(this Stream stream, byte[] buffer, int offset, int length, CancellationToken cancellationToken = default)
255+
{
256+
await stream.ReadFullyAsync(buffer.AsMemory(offset, length), cancellationToken);
257+
}
258+
259+
public static async Task ReadFullyAsync(this Stream stream, Memory<byte> buffer, CancellationToken cancellationToken = default)
260+
{
261+
int offset = 0;
262+
while (offset < buffer.Length)
263+
{
264+
int read = await stream.ReadAsync(buffer[offset..], cancellationToken);
265+
if (read <= 0)
266+
{
267+
throw new EndOfStreamException();
268+
}
269+
offset += read;
270+
}
271+
}
272+
}
273+
}

0 commit comments

Comments
 (0)