-
Notifications
You must be signed in to change notification settings - Fork 654
Expand file tree
/
Copy pathSessionListItem.cs
More file actions
207 lines (174 loc) · 5.97 KB
/
Copy pathSessionListItem.cs
File metadata and controls
207 lines (174 loc) · 5.97 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using System.ComponentModel;
using System.Net;
using System.Runtime.CompilerServices;
using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Examples.Wpf.Annotations;
using Titanium.Web.Proxy.Http;
namespace Titanium.Web.Proxy.Examples.Wpf
{
public class SessionListItem : INotifyPropertyChanged
{
private long? bodySize;
private long? clientConnectionId;
private Exception exception;
private string host;
private int processId;
private string protocol;
private long receivedDataCount;
private long sentDataCount;
private long? serverConnectionId;
private string statusCode;
private string url;
public int Number { get; set; }
public long? ClientConnectionId
{
get => clientConnectionId;
set => SetField(ref clientConnectionId, value);
}
public long? ServerConnectionId
{
get => serverConnectionId;
set => SetField(ref serverConnectionId, value);
}
public HttpWebClient HttpClient { get; set; }
public IPEndPoint ClientLocalEndPoint { get; set; }
public IPEndPoint ClientRemoteEndPoint { get; set; }
public bool IsTunnelConnect { get; set; }
public string StatusCode
{
get => statusCode;
set => SetField(ref statusCode, value);
}
public string Protocol
{
get => protocol;
set => SetField(ref protocol, value);
}
public string Host
{
get => host;
set => SetField(ref host, value);
}
public string Url
{
get => url;
set => SetField(ref url, value);
}
public long? BodySize
{
get => bodySize;
set => SetField(ref bodySize, value);
}
public int ProcessId
{
get => processId;
set
{
if (SetField(ref processId, value)) OnPropertyChanged(nameof(Process));
}
}
public string Process
{
get
{
try
{
var process = System.Diagnostics.Process.GetProcessById(processId);
return process.ProcessName + ":" + processId;
}
catch (Exception)
{
return string.Empty;
}
}
}
public long ReceivedDataCount
{
get => receivedDataCount;
set => SetField(ref receivedDataCount, value);
}
public long SentDataCount
{
get => sentDataCount;
set => SetField(ref sentDataCount, value);
}
public Exception Exception
{
get => exception;
set => SetField(ref exception, value);
}
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Brief client↔proxy | proxy↔server label (e.g. "HTTP/2 ↔ HTTP/3").
/// </summary>
private static string FormatClientServerProtocol(Version clientVersion, Version serverVersion)
{
var client = FormatHttpProtocol(clientVersion);
var server = FormatHttpProtocol(serverVersion);
if (server == "unknown")
return client;
return client + " ↔ " + server;
}
private static string FormatHttpProtocol(Version version)
{
if (version == null || version.Major == 0)
return "unknown";
if (version.Major >= 2)
return "HTTP/" + version.Major;
return "HTTP/" + version.Major + "." + version.Minor;
}
/// <summary>
/// 0 is the library's "no connection bound" sentinel; show an empty cell for it.
/// </summary>
private static long? IdOrNull(long id) => id == 0 ? null : id;
protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
if (!Equals(field, value))
{
field = value;
OnPropertyChanged(propertyName);
return true;
}
return false;
}
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public void Update(SessionEventArgsBase args)
{
var request = HttpClient.Request;
var response = HttpClient.Response;
var statusCode = response?.StatusCode ?? 0;
StatusCode = statusCode == 0 ? "-" : statusCode.ToString();
// e.g. "HTTP/2 ↔ HTTP/3" (client↔proxy | proxy↔server).
Protocol = FormatClientServerProtocol(request.HttpVersion, response?.HttpVersion);
ClientConnectionId = IdOrNull(args.ClientConnectionId);
ServerConnectionId = IdOrNull(args.ServerConnectionId);
if (IsTunnelConnect)
{
Host = "Tunnel to";
Url = request.RequestUri.Host + ":" + request.RequestUri.Port;
}
else
{
Host = request.RequestUri.Host;
Url = request.RequestUri.AbsolutePath;
}
if (!IsTunnelConnect)
{
long responseSize = -1;
if (response != null)
{
if (response.ContentLength != -1)
responseSize = response.ContentLength;
else if (response.IsBodyRead && response.Body != null) responseSize = response.Body.Length;
}
BodySize = responseSize;
}
ProcessId = HttpClient.ProcessId.Value;
}
}
}