Skip to content

Commit

Permalink
Adds inspection with CDP
Browse files Browse the repository at this point in the history
  • Loading branch information
waldekmastykarz committed Oct 23, 2023
1 parent 8bb806b commit 04b11b6
Show file tree
Hide file tree
Showing 5 changed files with 698 additions and 1 deletion.
3 changes: 2 additions & 1 deletion m365-developer-proxy-abstractions/ProxyUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public static IList<HttpHeader> BuildGraphResponseHeaders(Request request, strin
new HttpHeader("Strict-Transport-Security", ""),
new HttpHeader("request-id", requestId),
new HttpHeader("client-request-id", requestId),
new HttpHeader("Date", requestDate)
new HttpHeader("Date", requestDate),
new HttpHeader("Content-Type", "application/json")
};
if (request.Headers.FirstOrDefault((h) => h.Name.Equals("Origin", StringComparison.OrdinalIgnoreCase)) is not null) {
headers.Add(new HttpHeader("Access-Control-Allow-Origin", "*"));
Expand Down
294 changes: 294 additions & 0 deletions m365-developer-proxy-plugins/Inspection/CDPModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Text.Json.Serialization;
using Microsoft365.DeveloperProxy.Abstractions;

// CDP = Chrome DevTools Protocol
namespace Microsoft365.DeveloperProxy.Plugins.Inspection.CDP;

public class RequestWillBeSentExtraInfoMessage : Message<RequestWillBeSentExtraInfoParams>
{
public RequestWillBeSentExtraInfoMessage()
{
Method = "Network.requestWillBeSentExtraInfo";
}
}

public class RequestWillBeSentExtraInfoParams
{
[JsonPropertyName("requestId")]
public string? RequestId { get; set; }

[JsonPropertyName("associatedCookies")]
public object[]? AssociatedCookies { get; set; }

[JsonPropertyName("headers")]
public Dictionary<string, string>? Headers { get; set; }
}

public class RequestWillBeSentMessage : Message<RequestWillBeSentParams>
{
public RequestWillBeSentMessage()
{
Method = "Network.requestWillBeSent";
}
}

public class RequestWillBeSentParams
{
[JsonPropertyName("requestId")]
public string? RequestId { get; set; }
[JsonPropertyName("loaderId")]
public string? LoaderId { get; set; }
[JsonPropertyName("documentURL")]
public string? DocumentUrl { get; set; }
[JsonPropertyName("request")]
public Request? Request { get; set; }
[JsonPropertyName("timestamp")]
public double? Timestamp { get; set; }
[JsonPropertyName("wallTime")]
public long? WallTime { get; set; }
[JsonPropertyName("initiator")]
public Initiator? Initiator { get; set; }
}

public class ResponseReceivedMessage : Message<ResponseReceivedParams>
{
public ResponseReceivedMessage()
{
Method = "Network.responseReceived";
}
}

public class ResponseReceivedParams
{
[JsonPropertyName("requestId")]
public string? RequestId { get; set; }

[JsonPropertyName("loaderId")]
public string? LoaderId { get; set; }

[JsonPropertyName("timestamp")]
public double? Timestamp { get; set; }

[JsonPropertyName("type")]
public string? Type { get; set; }

[JsonPropertyName("response")]
public Response? Response { get; set; }

[JsonPropertyName("hasExtraInfo")]
public bool? HasExtraInfo { get; set; }

[JsonPropertyName("frameId")]
public string? FrameId { get; set; }
}

public class Response
{
[JsonPropertyName("url")]
public string? Url { get; set; }

[JsonPropertyName("status")]
public int? Status { get; set; }

[JsonPropertyName("statusText")]
public string? StatusText { get; set; }

[JsonPropertyName("headers")]
public Dictionary<string, string>? Headers { get; set; }

[JsonPropertyName("mimeType")]
public string? MimeType { get; set; }

[JsonPropertyName("connectionReused")]
public bool? ConnectionReused { get; set; }

[JsonPropertyName("connectionId")]
public int? ConnectionId { get; set; }

[JsonPropertyName("fromDiskCache")]
public bool? FromDiskCache { get; set; }

[JsonPropertyName("fromServiceWorker")]
public bool? FromServiceWorker { get; set; }

[JsonPropertyName("fromPrefetchCache")]
public bool? FromPrefetchCache { get; set; }

[JsonPropertyName("encodedDataLength")]
public int? EncodedDataLength { get; set; }

[JsonPropertyName("protocol")]
public string? Protocol { get; set; }

[JsonPropertyName("alternateProtocolUsage")]
public string? AlternateProtocolUsage { get; set; }

[JsonPropertyName("securityState")]
public string? SecurityState { get; set; }
}

public class Request
{
[JsonPropertyName("url")]
public string? Url { get; set; }
[JsonPropertyName("method")]
public string? Method { get; set; }
[JsonPropertyName("headers")]
public Dictionary<string, string>? Headers { get; set; }
[JsonPropertyName("postData")]
public string? PostData { get; set; }
}

public class Initiator
{
[JsonPropertyName("type")]
public string? Type { get; set; }
}

public class LoadingFinishedMessage : Message<LoadingFinishedParams>
{
public LoadingFinishedMessage()
{
Method = "Network.loadingFinished";
}
}

public class LoadingFinishedParams
{
[JsonPropertyName("requestId")]
public string? RequestId { get; set; }

[JsonPropertyName("timestamp")]
public double? Timestamp { get; set; }

[JsonPropertyName("encodedDataLength")]
public long? EncodedDataLength { get; set; }
}

public class LoadingFailedMessage : Message<LoadingFailedParams>
{
public LoadingFailedMessage()
{
Method = "Network.loadingFailed";
}
}

public class LoadingFailedParams
{
[JsonPropertyName("requestId")]
public string? RequestId { get; set; }

[JsonPropertyName("timestamp")]
public double? Timestamp { get; set; }

[JsonPropertyName("type")]
public string? Type { get; set; }

[JsonPropertyName("errorText")]
public string? ErrorText { get; set; }

[JsonPropertyName("canceled")]
public bool? Canceled { get; set; }

[JsonPropertyName("blockedReason")]
public string? BlockedReason { get; set; }
}

public class EntryAddedMessage : Message<EntryAddedParams>
{
public EntryAddedMessage()
{
Method = "Log.entryAdded";
}
}

public class EntryAddedParams
{
[JsonPropertyName("entry")]
public Entry? Entry { get; set; }
}

public class Entry
{
[JsonPropertyName("source")]
public string? Source { get; set; }
[JsonPropertyName("level")]
public string? Level { get; set; }
[JsonPropertyName("text")]
public string? Text { get; set; }
[JsonPropertyName("timestamp")]
public double? Timestamp { get; set; }
[JsonPropertyName("url")]
public string? Url { get; set; }
[JsonPropertyName("networkRequestId")]
public string? NetworkRequestId { get; set; }

public static string GetLevel(MessageType messageType)
{
return messageType switch
{
MessageType.Normal => "info",
MessageType.InterceptedRequest => "info",
MessageType.PassedThrough => "info",
MessageType.Warning => "warning",
MessageType.Tip => "info",
MessageType.Failed => "error",
MessageType.Chaos => "error",
MessageType.Mocked => "info",
MessageType.InterceptedResponse => "info",
_ => "info"
};
}
}

public class GetResponseBodyMessage : Message<GetResponseBodyParams>
{
public GetResponseBodyMessage()
{
Method = "Network.getResponseBody";
}
}

public class GetResponseBodyParams
{
[JsonPropertyName("requestId")]
public string? RequestId { get; set; }
}

public class GetResponseBodyResult : MessageResult<GetResponseBodyResultParams>
{
}

public class GetResponseBodyResultParams
{
[JsonPropertyName("body")]
public string? Body { get; set; }
[JsonPropertyName("base64Encoded")]
public bool? Base64Encoded { get; set; }
}

public abstract class MessageResult<TResult>
{
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonPropertyName("result")]
public TResult? Result { get; set; }
}

public abstract class Message
{
[JsonPropertyName("id")]
public int? Id { get; set; }

[JsonPropertyName("method")]
public string? Method { get; set; }
}

public abstract class Message<TParams> : Message
{
[JsonPropertyName("params")]
public TParams? Params { get; set; }
}
Loading

0 comments on commit 04b11b6

Please sign in to comment.