-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestorer.cs
216 lines (178 loc) · 9.12 KB
/
Restorer.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
214
215
using Capnp.Rpc;
using System;
using System.Collections.Concurrent;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using Mas.Schema.Climate;
using Crypt = NSec.Cryptography;
using P = Mas.Schema.Persistence;
namespace Mas.Infrastructure.Common
{
public class Restorer : P.IRestorer {
private ConcurrentDictionary<string, Proxy> _srToken2Capability = new();
public string TcpHost { get; set; } = "127.0.0.1";
public ushort TcpPort { get; set; } = 0;
public byte[] VatId { get; set; }
public Mas.Schema.Storage.Store.IContainer StorageContainer { get; set; } = null;
private ConcurrentDictionary<string, ((ulong, ulong, ulong, ulong), string)> _extSRT2VatIdAndIntSRT = new(); // mapping of external sturdy ref token to internal one
private ConcurrentDictionary<(ulong, ulong, ulong, ulong), Mas.Schema.Persistence.IRestorer> _vatId2Restorer = new();
private Crypt.Key _vatKey;
public Restorer() {
//TcpHost = Dns.GetHostName(); //"localhost";// GetLocalIPAddress();
var algorithm = Crypt.SignatureAlgorithm.Ed25519;
_vatKey = Crypt.Key.Create(algorithm);
VatId = _vatKey.PublicKey.Export(Crypt.KeyBlobFormat.RawPublicKey);
}
public static string ToBase64Url(string base64)
{
return base64.Replace('+', '-').Replace('/', '_').Replace("=", "");
}
public static string FromBase64Url(string base64Url)
{
return base64Url.Replace('-', '+').Replace('_', '/').PadRight(base64Url.Length + (4 - base64Url.Length % 4) % 4, '=');
}
public struct SaveRes {
public Mas.Schema.Persistence.SturdyRef SturdyRef { get; set; }
public Mas.Schema.Persistence.SturdyRef UnsaveSR { get; set; }
//public string SturdyRefStr { get; set; }
//public string SRToken { get; set; }
//public string UnsaveSR { get; set; }
//public string UnsaveSRToken { get; set; }
public ReleaseSturdyRef UnsaveAction { get; set; }
}
public class ReleaseSturdyRef : P.Persistent.IReleaseSturdyRef {
private readonly Func<Task<bool>> _releaseFunc;
public ReleaseSturdyRef(Func<Task<bool>> func) {
_releaseFunc = func;
}
public Task<bool> Release(CancellationToken cancellationToken_ = default) {
return _releaseFunc();
}
public void Dispose()
{
}
}
public (string, string) SaveStr(Capnp.Rpc.Proxy proxy, string fixedSrToken = null,
string sealForOwner = null, bool includeUnsave = true) {
var srToken = fixedSrToken ?? System.Guid.NewGuid().ToString();
_srToken2Capability[srToken] = proxy;
if(includeUnsave) {
var unsaveSRToken = System.Guid.NewGuid().ToString();
var unsaveAction = new ReleaseSturdyRef(async () => await Unsave(srToken) && await Unsave(unsaveSRToken));
_srToken2Capability[unsaveSRToken] = BareProxy.FromImpl(unsaveAction);
return (SturdyRefStr(srToken), SturdyRefStr(unsaveSRToken));
} else {
return (SturdyRefStr(srToken), null);
}
}
public string SturdyRefStr(string srToken) {
var vatIdBase64Url = ToBase64Url(Convert.ToBase64String(VatId));
//var srTokenBase64Url = ToBase64Url(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(srToken)));
//return $"capnp://{vatIdBase64Url}@{TcpHost}:{TcpPort}/{srTokenBase64Url}";
return $"capnp://{vatIdBase64Url}@{TcpHost}:{TcpPort}/{srToken}";
}
static public string SturdyRefStr(P.SturdyRef sturdyRef) {
var id = sturdyRef.Vat.Id;
var vatIdBytes = new byte[4 * 8];
BitConverter.GetBytes(id.PublicKey0).CopyTo(vatIdBytes, 0);
BitConverter.GetBytes(id.PublicKey1).CopyTo(vatIdBytes, 8);
BitConverter.GetBytes(id.PublicKey2).CopyTo(vatIdBytes, 16);
BitConverter.GetBytes(id.PublicKey3).CopyTo(vatIdBytes, 24);
var vatIdBase64Url = ToBase64Url(Convert.ToBase64String(vatIdBytes));
//var srTokenBase64Url = ToBase64Url(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes((string)sturdyRef.TheTransient.LocalRef)));
return $"capnp://{vatIdBase64Url}@{sturdyRef.Vat.Address.Host}:{sturdyRef.Vat.Address.Port}/{sturdyRef.LocalRef.Text}";
}
public void InstallCrossDomainMapping(string extSRT, P.VatId vatId, string intSRT) {
_extSRT2VatIdAndIntSRT[extSRT] =
((vatId.PublicKey0, vatId.PublicKey1, vatId.PublicKey2, vatId.PublicKey3), intSRT);
}
public SaveRes Save(Capnp.Rpc.Proxy proxy, string fixedSRToken = null, string sealForOwner = null,
bool includeUnsave = true) {
var srToken = fixedSRToken ?? System.Guid.NewGuid().ToString();
_srToken2Capability[srToken] = proxy;
if(includeUnsave) {
var unsaveSRToken = System.Guid.NewGuid().ToString();
var unsaveAction = new ReleaseSturdyRef(async () => await Unsave(srToken) && await Unsave(unsaveSRToken));
_srToken2Capability[unsaveSRToken] = BareProxy.FromImpl(unsaveAction);
return new SaveRes {
SturdyRef = SturdyRef(srToken),
//SRToken = srToken,
UnsaveSR = SturdyRef(unsaveSRToken),
//UnsaveSRToken = unsaveSRToken,
UnsaveAction = unsaveAction
};
} else {
return new SaveRes {
SturdyRef = SturdyRef(srToken),
//SRToken = srToken
};
}
}
public Mas.Schema.Persistence.SturdyRef SturdyRef(string srToken) {
var vid = VatId;
if(!BitConverter.IsLittleEndian) Array.Reverse(vid, 0, vid.Length);
return new Schema.Persistence.SturdyRef {
Vat = new Schema.Persistence.VatPath {
Id = new Schema.Persistence.VatId {
PublicKey0 = BitConverter.ToUInt64(vid, 0),
PublicKey1 = BitConverter.ToUInt64(vid, 8),
PublicKey2 = BitConverter.ToUInt64(vid, 16),
PublicKey3 = BitConverter.ToUInt64(vid, 24)
},
Address = new Schema.Persistence.Address {
Host = TcpHost,
Port = TcpPort
}
},
LocalRef = new Schema.Persistence.SturdyRef.Token { Text = srToken }
};
}
public Task<bool> Unsave(string srToken) {
return Task.FromResult(_srToken2Capability.TryRemove(srToken, out _));
}
public void AddOrUpdateCrossDomainRestore(P.VatId vatId, P.IRestorer restorer) {
_vatId2Restorer.AddOrUpdate(
(vatId.PublicKey0, vatId.PublicKey1, vatId.PublicKey2, vatId.PublicKey3),
(k) => restorer,
(k,oldRestorer) => { oldRestorer?.Dispose(); return restorer; });
}
#region implementation of Mas.Schema.Persistence.Restorer
public async Task<BareProxy> Restore(Mas.Schema.Persistence.Restorer.RestoreParams ps,
CancellationToken cancellationToken_ = default) {
//var ds = (Capnp.DeserializerState)ps.LocalRef.Text;
var srToken = ps.LocalRef.Text; //Capnp.CapnpSerializable.Create<string>(ds);
//var sealedFor = ps.SealedFor;
// is cross domain restore? then always query the remote restorer
if (_extSRT2VatIdAndIntSRT.TryGetValue(srToken, out var value)){
var (vatId, intSRT) = value;
if (_vatId2Restorer.TryGetValue(vatId, out var restorer)) {
return await restorer.Restore(new Mas.Schema.Persistence.Restorer.RestoreParams
{
LocalRef=new Mas.Schema.Persistence.SturdyRef.Token { Text = intSRT }
}, cancellationToken_);
}
}
// no remote restorer for cross domain available or just a normal restore
if (!_srToken2Capability.ContainsKey(srToken)) return null;
var cap = _srToken2Capability[srToken];
if (cap is BareProxy bareProxy) {
return Proxy.Share(bareProxy);
} else {
var sharedProxy = Proxy.Share(cap);
var bareProxy2 = new BareProxy(sharedProxy.ConsumedCap);
return bareProxy2;// Proxy.Share(_srToken2Proxy[srToken]));
}
}
#endregion
public void Dispose() {
GC.SuppressFinalize(this);
// dispose sturdy ref caps
//foreach (var sr2p in _srToken2Capability)
// sr2p.Value?.Dispose();
Console.WriteLine("Dispose");
}
}
}