imapflow 1.0.76 → 1.0.77

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ - 1.0.77 - 2021-11-25
4
+
5
+ - Testing out proxy connections
6
+ - Add X-GM-RAW support for search (jhi721 #68)
7
+
3
8
  - 1.0.75 - 2021-10-15
4
9
 
5
10
  - Fixed an exception with failing IDLE (ramiroaisen #60)
@@ -66,7 +66,7 @@ module.exports = async connection => {
66
66
 
67
67
  response = await connection.exec('IDLE', false, {
68
68
  onPlusTag: async () => {
69
- connection.log.debug({ msg: `Initiated IDLE, waiting for server input` });
69
+ connection.log.debug({ msg: `Initiated IDLE, waiting for server input`, doneRequested });
70
70
  canEnd = true;
71
71
  if (doneRequested) {
72
72
  preCheck();
package/lib/imap-flow.js CHANGED
@@ -27,6 +27,8 @@ const libbase64 = require('libbase64');
27
27
  const FlowedDecoder = require('mailsplit/lib/flowed-decoder');
28
28
  const { PassThrough } = require('stream');
29
29
 
30
+ const { proxyConnection } = require('./proxy-connection');
31
+
30
32
  const { comparePaths, updateCapabilities, getFolderTree, formatMessageResponse, getDecoder, packMessageRange, normalizePath } = require('./tools');
31
33
 
32
34
  const imapCommands = require('./imap-commands.js');
@@ -1054,6 +1056,22 @@ class ImapFlow extends EventEmitter {
1054
1056
  this.untaggedHandlers.EXISTS = (...args) => this.untaggedExists(...args);
1055
1057
  this.untaggedHandlers.EXPUNGE = (...args) => this.untaggedExpunge(...args);
1056
1058
 
1059
+ let socket = false;
1060
+ if (this.options.proxy) {
1061
+ try {
1062
+ socket = await proxyConnection(this.log, this.options.proxy, this.host, this.port);
1063
+ if (!socket) {
1064
+ throw new Error('Failed to setup proxy connection');
1065
+ }
1066
+ } catch (err) {
1067
+ let error = new Error('Failed to setup proxy connection');
1068
+ error.code = err.code || 'ProxyError';
1069
+ error._err = err;
1070
+ this.log.error({ error, cid: this.id });
1071
+ throw error;
1072
+ }
1073
+ }
1074
+
1057
1075
  await new Promise((resolve, reject) => {
1058
1076
  this.connectTimeout = setTimeout(() => {
1059
1077
  let err = new Error('Failed to established connection in required time');
@@ -1063,7 +1081,7 @@ class ImapFlow extends EventEmitter {
1063
1081
  reject(err);
1064
1082
  }, CONNECT_TIMEOUT);
1065
1083
 
1066
- this.socket = connector.connect(opts, () => {
1084
+ let onConnect = () => {
1067
1085
  clearTimeout(this.connectTimeout);
1068
1086
  this.socket.setKeepAlive(true, 5 * 1000);
1069
1087
  this.socket.setTimeout(SOCKET_TIMEOUT);
@@ -1105,7 +1123,23 @@ class ImapFlow extends EventEmitter {
1105
1123
  // executed by initial "* OK"
1106
1124
  this.initialResolve = resolve;
1107
1125
  this.initialReject = reject;
1108
- });
1126
+ };
1127
+
1128
+ if (socket) {
1129
+ // socket is already establised via proxy
1130
+ if (this.secureConnection) {
1131
+ // TLS socket requires a handshake
1132
+ opts.socket = socket;
1133
+ this.socket = connector.connect(opts, onConnect);
1134
+ } else {
1135
+ // cleartext socket is already usable
1136
+ this.socket = socket;
1137
+ setImmediate(onConnect);
1138
+ }
1139
+ } else {
1140
+ this.socket = connector.connect(opts, onConnect);
1141
+ }
1142
+
1109
1143
  this.writeSocket = this.socket;
1110
1144
 
1111
1145
  this.socket.once('error', err => {
@@ -0,0 +1,120 @@
1
+ 'use strict';
2
+
3
+ const httpProxyClient = require('nodemailer/lib/smtp-connection/http-proxy-client');
4
+ const { SocksClient } = require('socks');
5
+ const util = require('util');
6
+ const httpProxyClientAsync = util.promisify(httpProxyClient);
7
+ const dns = require('dns').promises;
8
+ const net = require('net');
9
+
10
+ const proxyConnection = async (logger, connectionUrl, host, port) => {
11
+ let proxyUrl = new URL(connectionUrl);
12
+
13
+ let protocol = proxyUrl.protocol.replace(/:$/, '').toLowerCase();
14
+
15
+ if (!net.isIP(host)) {
16
+ let resolveResult = await dns.resolve(host);
17
+ if (resolveResult && resolveResult.length) {
18
+ host = resolveResult[0];
19
+ }
20
+ }
21
+
22
+ switch (protocol) {
23
+ // Connect using a HTTP CONNECT method
24
+ case 'http':
25
+ case 'https': {
26
+ try {
27
+ let socket = await httpProxyClientAsync(proxyUrl.href, port, host);
28
+ if (socket) {
29
+ if (proxyUrl.password) {
30
+ proxyUrl.password = '(hidden)';
31
+ }
32
+ logger.info({
33
+ msg: 'Established a socket via HTTP proxy',
34
+ proxyUrl: proxyUrl.href,
35
+ port,
36
+ host
37
+ });
38
+ }
39
+ return socket;
40
+ } catch (err) {
41
+ if (proxyUrl.password) {
42
+ proxyUrl.password = '(hidden)';
43
+ }
44
+ logger.error({
45
+ msg: 'Failed to establish a socket via HTTP proxy',
46
+ proxyUrl: proxyUrl.href,
47
+ port,
48
+ host,
49
+ err
50
+ });
51
+ throw err;
52
+ }
53
+ }
54
+
55
+ // SOCKS proxy
56
+ case 'socks':
57
+ case 'socks5':
58
+ case 'socks4':
59
+ case 'socks4a': {
60
+ let proxyType = Number(protocol.replace(/\D/g, '')) || 5;
61
+
62
+ let targetHost = proxyUrl.hostname;
63
+ if (!net.isIP(targetHost)) {
64
+ let resolveResult = await dns.resolve(targetHost);
65
+ if (resolveResult && resolveResult.length) {
66
+ targetHost = resolveResult[0];
67
+ }
68
+ }
69
+
70
+ let connectionOpts = {
71
+ proxy: {
72
+ host: targetHost,
73
+ port: Number(proxyUrl.port) || 1080,
74
+ type: proxyType
75
+ },
76
+ destination: {
77
+ host,
78
+ port
79
+ },
80
+ command: 'connect',
81
+ set_tcp_nodelay: true
82
+ };
83
+
84
+ if (proxyUrl.username || proxyUrl.password) {
85
+ connectionOpts.proxy.userId = proxyUrl.username;
86
+ connectionOpts.proxy.password = proxyUrl.password;
87
+ }
88
+
89
+ try {
90
+ const info = await SocksClient.createConnection(connectionOpts);
91
+ if (info && info.socket) {
92
+ if (proxyUrl.password) {
93
+ proxyUrl.password = '(hidden)';
94
+ }
95
+ logger.info({
96
+ msg: 'Established a socket via SOCKS proxy',
97
+ proxyUrl: proxyUrl.href,
98
+ port,
99
+ host
100
+ });
101
+ }
102
+ return info.socket;
103
+ } catch (err) {
104
+ if (proxyUrl.password) {
105
+ proxyUrl.password = '(hidden)';
106
+ }
107
+ logger.error({
108
+ msg: 'Failed to establish a socket via SOCKS proxy',
109
+ proxyUrl: proxyUrl.href,
110
+ port,
111
+ host,
112
+ err
113
+ });
114
+ throw err;
115
+ }
116
+ }
117
+ }
118
+ };
119
+
120
+ module.exports = { proxyConnection };
@@ -117,6 +117,11 @@ module.exports.searchCompiler = (connection, query) => {
117
117
  setOpt(attributes, 'X-GM-THRID', params[term]);
118
118
  }
119
119
  break;
120
+ case 'GMRAW':
121
+ if (connection.capabilities.has('X-GM-EXT-1')) {
122
+ setOpt(attributes, 'X-GM-RAW', params[term]);
123
+ }
124
+ break;
120
125
 
121
126
  case 'BEFORE':
122
127
  case 'ON':
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imapflow",
3
- "version": "1.0.76",
3
+ "version": "1.0.77",
4
4
  "description": "IMAP Client for Node",
5
5
  "main": "./lib/imap-flow.js",
6
6
  "scripts": {
@@ -26,13 +26,13 @@
26
26
  },
27
27
  "homepage": "https://imapflow.com/",
28
28
  "devDependencies": {
29
- "@babel/eslint-parser": "7.15.8",
29
+ "@babel/eslint-parser": "7.16.3",
30
30
  "@babel/eslint-plugin": "7.14.5",
31
31
  "@babel/plugin-syntax-class-properties": "7.12.13",
32
- "@babel/preset-env": "7.15.8",
33
- "@types/node": "16.11.1",
32
+ "@babel/preset-env": "7.16.4",
33
+ "@types/node": "16.11.10",
34
34
  "braintree-jsdoc-template": "3.3.0",
35
- "eslint": "8.0.1",
35
+ "eslint": "8.3.0",
36
36
  "eslint-config-nodemailer": "1.2.0",
37
37
  "eslint-config-prettier": "8.3.0",
38
38
  "grunt": "1.4.1",
@@ -49,6 +49,8 @@
49
49
  "libmime": "5.0.0",
50
50
  "libqp": "1.1.0",
51
51
  "mailsplit": "5.3.1",
52
- "pino": "7.0.2"
52
+ "nodemailer": "6.7.1",
53
+ "pino": "7.4.1",
54
+ "socks": "2.6.1"
53
55
  }
54
56
  }