forked from pixlbreaker/ReminderBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.js
151 lines (134 loc) · 5.33 KB
/
tools.js
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
// Constants
var mysql = require('mysql');
var Tunnel = require('tunnel-ssh');
module.exports = function (server) {
return new Object({
tunnelPort: 33333, // can really be any free port used for tunneling
/**
* DB server configuration. Please note that due to the tunneling the server host
* is localhost and the server port is the tunneling port. It is because the tunneling
* creates a local port on localhost
*/
dbServer: server || {
host: '127.0.0.1',
port: 3306 ,
user: '',
password: 'yourpwd',
database: 'yourdb'
},
/**
* Default configuration for the SSH tunnel
*/
tunnelConfig: {
remoteHost: '127.0.0.1', // mysql server host
remotePort: 3306, // mysql server port
localPort: 33333, // a available local port
verbose: true, // dump information to stdout
disabled: false, //set this to true to disable tunnel (useful to keep architecture for local connections)
sshConfig: { //ssh2 configuration (https://github.com/mscdex/ssh2)
host: 'your_tunneling_host',
port: 22,
username: 'user_on_tunneling',
password: 'pwd'
//privateKey: require('fs').readFileSync('<pathToKeyFile>'),
//passphrase: 'verySecretString' // option see ssh2 config
}
},
/**
* Initialise the mysql connection via the tunnel. Once it is created call back the caller
*
* @param callback
*/
init: function (callback) {
/* tunnel-ssh < 1.0.0
//
// SSH tunnel creation
// tunnel-ssh < 1.0.0
var me = this;
me.tunnel = new Tunnel(this.tunnelConfig);
me.tunnel.connect(function (error) {
console.log('Tunnel connected', error);
//
// Connect to the db
//
me.connection = me.connect(callback);
});
*/
/* tunnel-ssh 1.1.0 */
//
// SSH tunnel creation
//
var me = this;
// Convert original Config to new style config:
var config = this.tunnelConfig;
var newStyleConfig = {
username: config.sshConfig.username,
port: config.sshConfig.port,
host: config.sshConfig.host,
// SSH2 Forwarding...
dstPort: config.remotePort,
dstHost: config.remoteHost,
srcPort: config.localPort,
srcHost: config.localHost,
// Local server or something...
localPort: config.localPort,
localHost: config.localHost,
privateKey: config.privateKey
}
me.tunnel = tunnel(newStyleConfig, function (err) {
console.log('Tunnel connected', err);
if (err) {
return callback(err);
}
me.connection = me.connect(callback);
});
},
/**
* Mysql connection error handling
*
* @param err
*/
errorHandler: function (err) {
var me = this;
//
// Check for lost connection and try to reconnect
//
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
console.log('MySQL connection lost. Reconnecting.');
me.connection = me.connect();
} else if (err.code === 'ECONNREFUSED') {
//
// If connection refused then keep trying to reconnect every 3 seconds
//
console.log('MySQL connection refused. Trying soon again. ' + err);
setTimeout(function () {
me.connection = me.connect();
}, 3000);
}
},
/**
* Connect to the mysql server with retry in every 3 seconds if connection fails by any reason
*
* @param callback
* @returns {*} created mysql connection
*/
connect: function (callback) {
var me = this;
//
// Create the mysql connection object
//
var connection = mysql.createConnection(me.dbServer);
connection.on('error', me.errorHandler);
//
// Try connecting
//
connection.connect(function (err) {
if (err) throw err;
console.log('Mysql connected as id ' + connection.threadId);
if (callback) callback();
});
return connection;
}
}
);
};