-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
124 lines (91 loc) · 3.47 KB
/
server.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
require('dotenv/config')
const express = require('express')
const bodyParser = require('body-parser')
const twilio = require('twilio');
const ngrok = require('ngrok');
const colors = require('colors');
const readlineSync = require('readline-sync');
const { validatePhoneNumber } = require('./js/validator')
const { spawn } = require('child_process');
// Globals
let proxyNumber = process.env.PROXY_NUMBER
let userNumber
/**
* INITIALIZE TWILIO AND NGROK
*/
// Initialize Ngrok tunnel
(async function () {
try {
const url = await ngrok.connect({
proto: 'http',
addr: 8081, // port or network address, defaults to 8080
subdomain: 'proxyblock', // my reserved tunnel name
authtoken: process.env.NGROK_TOKEN, // Your ngrok tunnel url
region: 'us', // region
});
console.log("SERVER IS LIVE AT ".yellow + url + "\n")
} catch (error) {
console.log('FAILED TO ESTABLISH TUNNEL'.red + error)
}
})();
// initialize Twilio API
var accountSid = process.env.TWILIO_SID; // Your Account SID from www.twilio.com/console
var authToken = process.env.TWILIO_TOKEN; // Your Auth Token from www.twilio.com/console
var client = new twilio(accountSid, authToken);
// initialize Express app
const app = express()
const port = 8081
// configs
app.use(bodyParser.urlencoded({ extended: false }))
/**
* USER INPUTS
*/
// Prompt for getting users real number
console.log("\nWELCOME TO PROXYBLOCK\n".rainbow)
// User input
let input = readlineSync.question("Enter your phone number ex. 646-222-2222: ");
// validate phone number
while (!validatePhoneNumber(input)) {
input = readlineSync.question("Invalid phone number try again: ");
}
// Set user number
userNumber = input
console.log("\nYOUR PROXY NUMBER IS " + proxyNumber.green + "\n")
console.log("YOUR MAY NOW USE YOUR PROXY NUMBER\nALL NON-SPAM MESSAGES WILL BE FOWARDED TO " + `${userNumber}`.green + "\n")
/**
* ROUTES
*/
// main route
app.get('/', (req, res) => { res.send("Hello this is a `ProxyBlock` Server") })
// test route
app.get('/test', (req, res) => { res.send("Test response") })
// Route for incoming messages.
app.post('/message', (req, res) => {
let senderNumber = req.body.From;
let message = req.body.Body;
// log inbound message
console.log("INBOUND SMS: ".green + `${senderNumber}` + " BODY: ".green + `${message}`)
// Initialize Neural Net Prediction using (predict.py)
const process = spawn('python3', ['./ml_model/predict.py', message]);
// Process Neural Net output
process.stdout.on('data', function (data) {
let buffer = Buffer.from(data)
let prediction = escape(buffer.toString());
// If prediction is Not Spam foward to users actual phone number
if (prediction == "NOT%20SPAM%0A") {
console.log("MESSAGE NOT SPAM FOWARDING TO YOUR PHONE".yellow)
// compose and relay message
client.messages.create({
body: message, // message
to: userNumber, // recipient number
from: proxyNumber // proxy number
}).then((message) => {
console.log("OUTBOUND SMS: ".green + userNumber + " BODY: ".green + `${message}`)
})
// If prediction is "Spam" foward to users actual phone number
} else {
console.log("MESSAGE IS SPAM BLOCKED".red)
}
});
})
app.listen(port, () => console.log("SERVER RUNNING ON PORT ".yellow + `${port}`))