-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeminiWarcCreator.cs
213 lines (178 loc) · 6.76 KB
/
GeminiWarcCreator.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
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
208
209
210
211
212
213
using System;
using System.Collections.Specialized;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Gemini.Net;
using Kennedy.Data;
using Warc;
namespace Kennedy.Warc
{
public class GeminiWarcCreator : WarcWriter
{
public const string RequestContentType = "application/gemini; msgtype=request";
public const string ResponseContentType = "application/gemini; msgtype=response";
public Uri WarcInfoID { get; private set; }
/// <summary>
/// Tracks what authorities we have written metadata records about their certificates
/// </summary>
private Dictionary<string, bool> WrittenCertificates;
public GeminiWarcCreator(string outputFile)
: base(outputFile)
{
WarcInfoID = WarcRecord.CreateId();
WrittenCertificates = new Dictionary<string, bool>();
}
public void WriteWarcInfo(WarcFields fields)
{
Write(new WarcInfoRecord
{
Id = WarcInfoID,
ContentType = WarcFields.ContentType,
ContentText = fields.ToString()
});
}
public void WriteSession(GeminiResponse response, TlsConnectionInfo? connectionInfo)
{
var requestRecord = CreateRequestRecord(response.RequestUrl);
requestRecord.SetDate(response.RequestSent);
requestRecord.IpAddress = response.RemoteAddress?.ToString();
AppendTlsInfo(requestRecord, connectionInfo);
Write(requestRecord);
var responseRecord = new ResponseRecord
{
ContentBlock = GeminiParser.CreateResponseBytes(response),
ContentType = ResponseContentType,
WarcInfoId = WarcInfoID,
TargetUri = response.RequestUrl.Url
};
SetBlockDigest(responseRecord);
responseRecord.SetDate(response.ResponseReceived);
responseRecord.IpAddress = response.RemoteAddress?.ToString();
AppendTlsInfo(responseRecord, connectionInfo);
responseRecord.ConcurrentTo.Add(requestRecord.Id);
if (response.MimeType != null)
{
responseRecord.IdentifiedPayloadType = response.MimeType;
}
if (response.IsBodyTruncated)
{
responseRecord.Truncated = "length";
}
Write(responseRecord);
if(connectionInfo != null && connectionInfo.RemoteCertificate != null && ShouldCreateCertificateRecord(response.RequestUrl))
{
var metadataRecord = new MetadataRecord
{
WarcInfoId = WarcInfoID,
ReferersTo = responseRecord.Id,
ContentText = connectionInfo.RemoteCertificate.ExportCertificatePem(),
ContentType = "application/x-pem-file",
TargetUri = response.RequestUrl.Url
};
metadataRecord.SetDate(response.ResponseReceived);
SetBlockDigest(metadataRecord);
Write(metadataRecord);
}
}
public void WriteLegacySession(GeminiUrl url, DateTime sent, int statusCode, string meta, string mime, byte[]? bytes, bool isTruncated = false)
{
var requestRecord = CreateRequestRecord(url);
requestRecord.Date = sent;
Write(requestRecord);
var responseRecord = new ResponseRecord
{
ContentBlock = GeminiParser.CreateResponseBytes(statusCode, meta, bytes),
ContentType = ResponseContentType,
Date = sent,
WarcInfoId = WarcInfoID,
TargetUri = url.Url
};
responseRecord.ConcurrentTo.Add(requestRecord.Id);
//do we have a mime?
if (!string.IsNullOrEmpty(mime))
{
responseRecord.IdentifiedPayloadType = mime;
}
//was it truncated?
if (isTruncated)
{
responseRecord.Truncated = "length";
}
Write(responseRecord);
}
private bool ShouldCreateCertificateRecord(GeminiUrl url)
{
if(!WrittenCertificates.ContainsKey(url.Authority))
{
WrittenCertificates.Add(url.Authority, true);
return true;
}
return false;
}
private RequestRecord CreateRequestRecord(GeminiUrl url)
{
var record = new RequestRecord
{
ContentBlock = GeminiParser.CreateRequestBytes(url),
ContentType = RequestContentType,
WarcInfoId = WarcInfoID,
TargetUri = url.Url
};
SetBlockDigest(record);
return record;
}
private void SetBlockDigest(WarcRecord record)
{
if(record.ContentBlock != null)
{
record.BlockDigest = GeminiParser.GetStrongHash(record.ContentBlock);
}
}
private void AppendTlsInfo(WarcRecord record, TlsConnectionInfo? connectionInfo)
{
if (connectionInfo != null)
{
if (connectionInfo.Protocol.HasValue)
{
record.AddCustomHeader("WARC-Protocol", GetProtocolHeader(connectionInfo));
}
if (connectionInfo.CipherSuite.HasValue)
{
record.AddCustomHeader("WARC-TLS-Cipher-Suite", GetCipherSuite(connectionInfo));
}
}
}
private string? GetCipherSuite(TlsConnectionInfo connection)
{
if (connection.CipherSuite != null)
{
return connection.CipherSuite.ToString();
}
return null;
}
private string? GetProtocolHeader(TlsConnectionInfo connection)
{
if(connection.Protocol == null)
{
return null;
}
// Disabling warning because we are only using these to parse
#pragma warning disable SYSLIB0039 // Type or member is obsolete
switch (connection.Protocol)
{
case SslProtocols.Tls13:
return "tls/1.3";
case SslProtocols.Tls12:
return "tls/1.2";
case SslProtocols.Tls11:
return "tls/1.1";
case SslProtocols.Tls:
return "tls/1.0";
default:
return null;
}
#pragma warning restore SYSLIB0039 // Type or member is obsolete
}
}
}