-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex_replace.cc
More file actions
288 lines (231 loc) · 9.6 KB
/
Copy pathregex_replace.cc
File metadata and controls
288 lines (231 loc) · 9.6 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/insert_linebreaks.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/archive/iterators/ostream_iterator.hpp>
#include <openssl/aes.h>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <streambuf>
#include <regex>
#include <iterator>
#include <cstdlib>
#include <nan.h>
#include "urilite.h"
//using namespace boost::archive::iterators;
using namespace urilite;
namespace std {
template<class BidirIt, class Traits, class CharT, class UnaryFunction>
std::basic_string<CharT> regex_replace(BidirIt first, BidirIt last,
const std::basic_regex<CharT,Traits>& re, UnaryFunction f)
{
std::basic_string<CharT> s;
typename std::match_results<BidirIt>::difference_type
positionOfLastMatch = 0;
auto endOfLastMatch = first;
auto callback = [&](const std::match_results<BidirIt>& match)
{
auto positionOfThisMatch = match.position(0);
auto diff = positionOfThisMatch - positionOfLastMatch;
auto startOfThisMatch = endOfLastMatch;
std::advance(startOfThisMatch, diff);
s.append(endOfLastMatch, startOfThisMatch);
s.append(f(match));
auto lengthOfMatch = match.length(0);
positionOfLastMatch = positionOfThisMatch + lengthOfMatch;
endOfLastMatch = startOfThisMatch;
std::advance(endOfLastMatch, lengthOfMatch);
};
std::sregex_iterator begin(first, last, re), end;
std::for_each(begin, end, callback);
s.append(endOfLastMatch, last);
return s;
}
template<class Traits, class CharT, class UnaryFunction>
std::string regex_replace(const std::string& s,
const std::basic_regex<CharT,Traits>& re, UnaryFunction f)
{
return regex_replace(s.cbegin(), s.cend(), re, f);
}
}
using namespace std;
using namespace v8;
void aes_init()
{
static int init=0;
if (init==0)
{
EVP_CIPHER_CTX e_ctx, d_ctx;
//initialize openssl ciphers
OpenSSL_add_all_ciphers();
//initialize random number generator (for IVs)
int rv = RAND_load_file("/dev/urandom", 32);
}
}
std::vector<unsigned char> aes_128_gcm_encrypt(std::string plaintext, std::string key)
{
aes_init();
size_t enc_length = plaintext.length()*3;
std::vector<unsigned char> output;
output.resize(enc_length,'\0');
unsigned char tag[AES_BLOCK_SIZE];
unsigned char iv[AES_BLOCK_SIZE];
RAND_bytes(iv, sizeof(iv));
std::copy( iv, iv+16, output.begin()+16);
int actual_size=0, final_size=0;
EVP_CIPHER_CTX* e_ctx = EVP_CIPHER_CTX_new();
EVP_CIPHER_CTX_ctrl(e_ctx, EVP_CTRL_GCM_SET_IVLEN, 16, NULL);
EVP_EncryptInit(e_ctx, EVP_aes_128_gcm(), (const unsigned char*)key.c_str(), iv);
EVP_EncryptUpdate(e_ctx, &output[32], &actual_size, (const unsigned char*)plaintext.data(),plaintext.length() );
EVP_EncryptFinal(e_ctx, &output[32+actual_size], &final_size);
EVP_CIPHER_CTX_ctrl(e_ctx, EVP_CTRL_GCM_GET_TAG, 16, tag);
std::copy( tag, tag+16, output.begin() );
std::copy( iv, iv+16, output.begin()+16);
output.resize(32 + actual_size+final_size);
EVP_CIPHER_CTX_free(e_ctx);
return output;
}
std::string aes_128_gcm_decrypt(std::vector<unsigned char> ciphertext, std::string key)
{
aes_init();
unsigned char tag[AES_BLOCK_SIZE];
unsigned char iv[AES_BLOCK_SIZE];
std::copy( ciphertext.begin(), ciphertext.begin()+16, tag);
std::copy( ciphertext.begin()+16, ciphertext.begin()+32, iv);
std::vector<unsigned char> plaintext; plaintext.resize(ciphertext.size(), '\0');
int actual_size=0, final_size=0;
EVP_CIPHER_CTX *d_ctx = EVP_CIPHER_CTX_new();
EVP_DecryptInit(d_ctx, EVP_aes_128_gcm(), (const unsigned char*)key.c_str(), iv);
EVP_DecryptUpdate(d_ctx, &plaintext[0], &actual_size, &ciphertext[32], ciphertext.size()-32 );
EVP_CIPHER_CTX_ctrl(d_ctx, EVP_CTRL_GCM_SET_TAG, 16, tag);
EVP_DecryptFinal(d_ctx, &plaintext[actual_size], &final_size);
EVP_CIPHER_CTX_free(d_ctx);
plaintext.resize(actual_size + final_size, '\0');
return std::string(plaintext.begin(),plaintext.end());
}
std::string replace_callback(const std::smatch& m) {
aes_init();
//const uri = redirectionutils.getClickLinkURI(r_domain, client_id, p4, message_uuid)
std::string r_domain = "Stackoverflow.com";
std::string client_id = std::to_string(123);
std::string message_uuid = "a86sf992";
//std::string mstr = m.str(0).c_str();
std::string p1 = m.str(1).c_str();
//std::string p2 = m.str(2).c_str();
//std::string p3 = m.str(3).c_str();
std::string p4 = m.str(4).c_str();
std::transform(r_domain.begin(), r_domain.end(), r_domain.begin(), ::tolower);
std::string encodedUri = uri::encodeURIComponent(p4);
std::string encodedDomainLower = uri::encodeURIComponent(r_domain);
//std::cout << p1 << endl << p2 << endl << p3 << endl << p4 << endl;
std::string plaintext = "u=" + message_uuid + "&c=" + client_id + "&r=" + encodedUri + "&d=" + encodedDomainLower;
std::string encryptionKey = "18ade47e50a8a679618e20e317535df6";
std::vector<unsigned char> ciphertext = aes_128_gcm_encrypt(plaintext, encryptionKey);
std::string encrypted;
static const char *chars="0123456789ABCDEF";
for(int i=0; i<ciphertext.size(); i++)
{
std::stringstream n1, n2;
n1 << std::hex << chars[ciphertext[i]/16];
n2 << std::hex << chars[ciphertext[i]%16];
encrypted += n1.str() + n2.str();
//std::cout << chars[ciphertext[i]/16];
//std::cout << chars[ciphertext[i]%16];
}
std::stringstream oscontent;
typedef
boost::archive::iterators::base64_from_binary<
boost::archive::iterators::transform_width<
const char *,
6,
8
>
>
base64_text;
std::copy(base64_text(encrypted.c_str()), base64_text(encrypted.c_str() + encrypted.size()), boost::archive::iterators::ostream_iterator<char>(oscontent));
std::string content = oscontent.str();
unsigned char tag[AES_BLOCK_SIZE]; //AES_BLOCK_SIZE=16
unsigned char iv[AES_BLOCK_SIZE];
RAND_bytes(iv, sizeof(iv));
std::vector<unsigned char> concat;
std::copy(iv, iv+sizeof(iv), std::back_inserter(concat));
std::copy(base64_text(content.c_str()), base64_text(content.c_str() + content.size()), std::back_inserter(concat));
std::copy(tag, tag+sizeof(tag), std::back_inserter(concat));
std::stringstream data;
data << concat.data();
std::string concat_data(data.str());
std::string plus = R"(\+)";
std::string slash = R"(\/)";
std::string equal = R"(=)";
std::regex reg_plus(plus);
std::regex reg_slash(slash);
std::regex reg_equal(equal);
std::string result, result1, result2;
std::regex_replace (std::back_inserter(result), concat_data.begin(), concat_data.end(), reg_plus, "_");
std::regex_replace (std::back_inserter(result1), result.begin(), result.end(), reg_slash, "_");
std::regex_replace (std::back_inserter(result2), result1.begin(), result1.end(), reg_equal, "");
std::string uri = "http://" + r_domain + "/tr/cl/" + result2;
std::string replaced = p1 + " href=\"" + uri + "\"";
//std::string uri = p4; //getClickLinkURI(r_domain, client_id, p4, message_uuid);
//
// const domainLower = domain.toLowerCase()
// const e = `u=${uuid}&c=${clientID}&r=${encodeURIComponent(uri)}&d=${encodeURIComponent(domainLower)}`
//
// const aes = new cryptoutils.Aes128Gcm(encryptionKey)
// const crypted = aes.encrypt(e)
//
// // concating buffers of iv, content and auth_tag
// let concat = Buffer.concat([aes.iv, Buffer.from(crypted, 'base64'), aes.tag])
//
// // URL safe base64 encoding
// concat = cryptoutils.UrlSafeBase64(concat)
//
// const urlObject = {
// protocol: 'http:',
// host: domain,
// pathname: pathnameClick + concat,
// }
// return url.format(urlObject)
return replaced;
}
void RegexReplace(const Nan::FunctionCallbackInfo<v8::Value>& info){
if (info.Length() < 3) {
Nan::ThrowTypeError("Wrong number of arguments");
return;
}
// if (!info[0]->IsString()){
// Nan::ThrowTypeError("Regex agrument 1 must be a string");
// return;
// }
if (!info[1]->IsString()){
Nan::ThrowTypeError("Regex agrument 2 must be a string");
return;
}
if (!info[2]->IsString()){
Nan::ThrowTypeError("Regex agrument 3 must be a string");
return;
}
//Nan::Local<Object> bufferObj = info[0]->ToObject();
char* buf = node::Buffer::Data(info[0]);
size_t bufLength = node::Buffer::Length(info[0]);
std::string s(buf, bufLength);
//Nan::Utf8String data(info[0]);
//std::string s = std::string(*data);
Nan::Utf8String regexp(info[1]);
std::regex e = std::regex(*regexp);
Nan::Utf8String replace(info[2]);
std::string r = std::string(*replace);
//std::string result;
//std::regex_replace (std::back_inserter(result), s.begin(), s.end(), e, r);//"$1 href=\"$4\""
std::string result = regex_replace(s, e, replace_callback);
info.GetReturnValue().Set(Nan::New<v8::String>(result).ToLocalChecked());
}
void Init(v8::Local<v8::Object> exports) {
exports->Set(Nan::New("RegexReplace").ToLocalChecked(), Nan::New<v8::FunctionTemplate>(RegexReplace)->GetFunction());
}
NODE_MODULE(RegexReplace, Init)