Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Luciano Barletta
/
message-service
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
1
Wiki
Network
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Commit 99716152
authored
2020-07-16 20:58:32 -0300
by
Tu Nombre
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
se optimiso la entrada de mails, y se controla la salida
1 parent
dd45ed58
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
64 additions
and
34 deletions
Wpp1State.py
config.py
deploy.py
process.py
services.py
Wpp1State.py
View file @
9971615
...
@@ -18,7 +18,7 @@ class StatePhoneWs():
...
@@ -18,7 +18,7 @@ class StatePhoneWs():
def
ChequearTelefono
(
self
,
telefono
):
def
ChequearTelefono
(
self
,
telefono
):
"""Chequea estado es el server remoto si esta conectado se almasena la
"""Chequea estado es el server remoto si esta conectado se almasena la
respuesta en cache durante
3
0 segundos para mejorar performance y si no esta
respuesta en cache durante
6
0 segundos para mejorar performance y si no esta
disponible no se almasena y la proxima respuesta debera hacerse contra el server
disponible no se almasena y la proxima respuesta debera hacerse contra el server
remoto para asegurarme de q se solucione el problema"""
remoto para asegurarme de q se solucione el problema"""
test
=
self
.
revisarCache
(
telefono
)
test
=
self
.
revisarCache
(
telefono
)
...
@@ -27,9 +27,10 @@ class StatePhoneWs():
...
@@ -27,9 +27,10 @@ class StatePhoneWs():
W
=
self
.
chequear_stado_WB
(
telefono
)
W
=
self
.
chequear_stado_WB
(
telefono
)
if
"error"
in
W
.
keys
():
#si hay algun error no actualiso el estado en cache
if
"error"
in
W
.
keys
():
#si hay algun error no actualiso el estado en cache
return
(
W
)
return
(
W
)
hora
=
datetime
.
datetime
.
now
()
+
datetime
.
timedelta
(
seconds
=
3
0
)
hora
=
datetime
.
datetime
.
now
()
+
datetime
.
timedelta
(
seconds
=
6
0
)
self
.
Consultas
[
telefono
]
=
{
"hora"
:
hora
,
self
.
Consultas
[
telefono
]
=
{
"hora"
:
hora
,
"estado"
:
W
}
"estado"
:
W
}
print
(
"Chequeando telefono saliente"
,
W
)
return
(
W
)
return
(
W
)
def
revisarCache
(
self
,
telefono
):
def
revisarCache
(
self
,
telefono
):
...
@@ -69,6 +70,7 @@ class StateMail():
...
@@ -69,6 +70,7 @@ class StateMail():
hora
=
datetime
.
datetime
.
now
()
+
datetime
.
timedelta
(
seconds
=
120
)
hora
=
datetime
.
datetime
.
now
()
+
datetime
.
timedelta
(
seconds
=
120
)
self
.
Consultas
[
self
.
_GenerarKey
(
User
,
Pass
,
Port
,
Host
)]
=
{
"hora"
:
hora
,
self
.
Consultas
[
self
.
_GenerarKey
(
User
,
Pass
,
Port
,
Host
)]
=
{
"hora"
:
hora
,
"estado"
:
W
}
"estado"
:
W
}
print
(
"chequeando servidor de salida"
,
W
)
return
(
W
)
return
(
W
)
def
revisarCache
(
self
,
User
,
Pass
,
Port
,
Host
):
def
revisarCache
(
self
,
User
,
Pass
,
Port
,
Host
):
...
...
config.py
View file @
9971615
...
@@ -7,4 +7,6 @@ SMTP_username = "anac.avisos@gmail.com"
...
@@ -7,4 +7,6 @@ SMTP_username = "anac.avisos@gmail.com"
SMTP_password
=
"xvc7733455"
SMTP_password
=
"xvc7733455"
SMTP_HOST
=
"smtp.gmail.com"
SMTP_HOST
=
"smtp.gmail.com"
SMTP_PORT
=
587
SMTP_PORT
=
587
SMS_URL
=
"http://192.168.15.120:8080/v1/sms/send/"
\ No newline at end of file
\ No newline at end of file
SMS_URL
=
"http://192.168.15.120:8080/v1/sms/send/"
folder
=
"msg/"
db
=
"/dev/shm/messages.db"
\ No newline at end of file
\ No newline at end of file
deploy.py
View file @
9971615
...
@@ -9,9 +9,10 @@ import os, ipdb, time, threading, random, datetime, config
...
@@ -9,9 +9,10 @@ import os, ipdb, time, threading, random, datetime, config
app
=
Flask
(
__name__
)
app
=
Flask
(
__name__
)
TelState
=
StatePhoneWs
()
MailState
=
StateMail
()
# carpeta de los mensajes
# carpeta de los mensajes
msgfolder
=
"msg/"
msgfolder
=
config
.
folder
#
"msg/"
# id mínimo para una tabla
# id mínimo para una tabla
minid
=
1
minid
=
1
# id máximo para una tabla
# id máximo para una tabla
...
@@ -25,10 +26,9 @@ prefix_lenght = 16
...
@@ -25,10 +26,9 @@ prefix_lenght = 16
# tiempo límite que debe pasar para eliminar cosas
# tiempo límite que debe pasar para eliminar cosas
operation_timer
=
86400
operation_timer
=
86400
# conección a base de datos
# conección a base de datos
process
=
Process
(
"messages.db"
)
process
=
Process
(
config
.
db
,
TelState
,
MailState
)
#Cache de estado de telefonos
#Cache de estado de telefonos
TelState
=
StatePhoneWs
()
MailState
=
StateMail
()
@app.route
(
'/'
)
@app.route
(
'/'
)
def
main
():
def
main
():
...
@@ -66,25 +66,26 @@ def data():
...
@@ -66,25 +66,26 @@ def data():
os
.
mkdir
(
path
)
os
.
mkdir
(
path
)
key
=
request
.
files
.
get
(
'key'
)
key
=
request
.
files
.
get
(
'key'
)
if
key
:
#if key:
request
.
files
[
'key'
]
.
save
(
path
+
"rand.key.enc"
)
# print("desencripto deploy.py 70")
# request.files['key'].save(path + "rand.key.enc")
# denecripto llave simétrica con mi llave privada y la guardo en la carpeta del emisor
# denecripto llave simétrica con mi llave privada y la guardo en la carpeta del emisor
os
.
system
(
"openssl rsautl -decrypt -inkey rsa_key.pri -in "
+
path
+
"rand.key.enc -out "
+
path
+
"rand.key"
)
#
os.system("openssl rsautl -decrypt -inkey rsa_key.pri -in " + path + "rand.key.enc -out " + path + "rand.key")
os
.
remove
(
path
+
"rand.key.enc"
)
#
os.remove(path + "rand.key.enc")
for
file
in
request
.
files
:
for
file
in
request
.
files
:
# si hay llave y no es esta
# si hay llave y no es esta
filepath
=
path
+
file
filepath
=
path
+
file
if
key
and
file
!=
"key"
:
#
if key and file != "key":
request
.
files
[
file
]
.
save
(
filepath
+
".enc"
)
#
request.files[file].save(filepath + ".enc")
# desencripto archivo con la llave simétrica y lo guardo en la carpeta del emisor
# desencripto archivo con la llave simétrica y lo guardo en la carpeta del emisor
os
.
system
(
"openssl enc -d -aes-256-cbc -in "
+
filepath
+
".enc -out "
+
filepath
+
" -pass file:"
+
path
+
"rand.key"
)
#
os.system("openssl enc -d -aes-256-cbc -in " + filepath + ".enc -out " + filepath + " -pass file:" + path + "rand.key")
os
.
remove
(
filepath
+
".enc"
)
#
os.remove(filepath + ".enc")
else
:
#
else:
request
.
files
[
file
]
.
save
(
filepath
)
request
.
files
[
file
]
.
save
(
filepath
)
if
key
:
#
if key:
os
.
remove
(
path
+
"rand.key"
)
#
os.remove(path + "rand.key")
return
str
(
process
.
datastore
(
return
str
(
process
.
datastore
(
random
.
randrange
(
minid
,
maxid
),
random
.
randrange
(
minid
,
maxid
),
...
@@ -132,13 +133,13 @@ def cons():
...
@@ -132,13 +133,13 @@ def cons():
# Intenta enviar mensajes
# Intenta enviar mensajes
def
attempt
():
def
attempt
():
p
=
Process
(
'messages.db'
)
p
=
Process
(
config
.
db
,
TelState
,
MailState
)
p
.
send
()
p
.
send
()
threading
.
Timer
(
retry_timer
,
attempt
)
.
start
()
threading
.
Timer
(
retry_timer
,
attempt
)
.
start
()
# Limpia basura
# Limpia basura
def
clean
():
def
clean
():
p
=
Process
(
'messages.db'
)
p
=
Process
(
config
.
db
,
TelState
,
MailState
)
paths
=
p
.
paths
()
paths
=
p
.
paths
()
now
=
datetime
.
datetime
.
now
()
now
=
datetime
.
datetime
.
now
()
...
@@ -158,8 +159,8 @@ def clean():
...
@@ -158,8 +159,8 @@ def clean():
# Inicio del servicio
# Inicio del servicio
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
# Genero llaves
# Genero llaves
os
.
system
(
"openssl genrsa -out rsa_key.pri 4096"
)
# private key
#
os.system("openssl genrsa -out rsa_key.pri 4096") # private key
os
.
system
(
"openssl rsa -in rsa_key.pri -out rsa_key.pub -outform PEM -pubout"
)
# public key
#
os.system("openssl rsa -in rsa_key.pri -out rsa_key.pub -outform PEM -pubout") # public key
# Demonio attempt
# Demonio attempt
attempt
()
attempt
()
# Demonio clean
# Demonio clean
...
...
process.py
View file @
9971615
...
@@ -5,9 +5,11 @@ import ipdb, os, json
...
@@ -5,9 +5,11 @@ import ipdb, os, json
class
Process
:
class
Process
:
def
__init__
(
self
,
db
):
def
__init__
(
self
,
db
,
estadoTelefono
,
estadoMail
):
self
.
db
=
db
self
.
db
=
db
self
.
conn
=
DBconnection
(
db
)
self
.
conn
=
DBconnection
(
db
)
self
.
statePhone
=
estadoTelefono
self
.
stateMail
=
estadoMail
# Guarda la data
# Guarda la data
def
datastore
(
self
,
id
,
path
):
def
datastore
(
self
,
id
,
path
):
...
@@ -61,9 +63,13 @@ class Process:
...
@@ -61,9 +63,13 @@ class Process:
if
not
os
.
path
.
exists
(
query
[
Table
.
path
]):
if
not
os
.
path
.
exists
(
query
[
Table
.
path
]):
self
.
conn
.
query
(
"DELETE FROM msg WHERE id = ?"
,(
query
[
Table
.
id
],))
self
.
conn
.
query
(
"DELETE FROM msg WHERE id = ?"
,(
query
[
Table
.
id
],))
continue
continue
try
:
serv
=
serviceFactory
(
query
[
Table
.
serv
])
serv
=
serviceFactory
(
query
[
Table
.
serv
])
success
=
serv
.
send
(
query
)
success
=
serv
.
send
(
query
,
self
.
statePhone
,
self
.
stateMail
)
#envia al servicio se salida
except
:
success
=
{
'texto'
:
False
}
print
(
"Rompio el proseso de envio procces 71"
)
#print("proces linea 72 success=", success)
d
=
json
.
loads
(
query
[
Table
.
type
])
d
=
json
.
loads
(
query
[
Table
.
type
])
partial
=
False
partial
=
False
...
...
services.py
View file @
9971615
...
@@ -41,7 +41,7 @@ class Wpp1(ServiceBase):
...
@@ -41,7 +41,7 @@ class Wpp1(ServiceBase):
uid
=
config
.
WS_uid
uid
=
config
.
WS_uid
server
=
config
.
FILE_server
server
=
config
.
FILE_server
def
send
(
self
,
data
):
def
send
(
self
,
data
,
stadotel
,
stadomail
):
tel
=
data
[
"dest"
]
tel
=
data
[
"dest"
]
v
=
ValidacionTelefonosArgentinos
()
v
=
ValidacionTelefonosArgentinos
()
tel
=
v
.
ValidarTelefono
(
tel
)
tel
=
v
.
ValidarTelefono
(
tel
)
...
@@ -50,10 +50,10 @@ class Wpp1(ServiceBase):
...
@@ -50,10 +50,10 @@ class Wpp1(ServiceBase):
tel
=
"Error"
tel
=
"Error"
#print("telefono a envia ",tel)
#print("telefono a envia ",tel)
data
[
"dest"
]
=
tel
data
[
"dest"
]
=
tel
n
=
self
.
_send
(
data
)
n
=
self
.
_send
(
data
,
stadotel
)
return
(
n
)
return
(
n
)
def
_send
(
self
,
data
):
def
_send
(
self
,
data
,
stadotel
):
print
(
data
)
print
(
data
)
types
=
json
.
loads
(
data
[
Table
.
type
])
types
=
json
.
loads
(
data
[
Table
.
type
])
try
:
try
:
...
@@ -76,6 +76,7 @@ class Wpp1(ServiceBase):
...
@@ -76,6 +76,7 @@ class Wpp1(ServiceBase):
f
.
close
()
f
.
close
()
if
type
(
text
)
==
bytes
:
if
type
(
text
)
==
bytes
:
text
=
text
.
decode
(
"utf-8"
)
text
=
text
.
decode
(
"utf-8"
)
self
.
controlSalida
(
stadotel
,
origen
)
result
=
requests
.
get
(
url
=
Wpp1
.
URL
+
Wpp1
.
URLmode
[
types
[
file
]],
params
=
{
'token'
:
Wpp1
.
token
,
'uid'
:
origen
,
'to'
:
data
[
Table
.
dest
],
'text'
:
text
})
result
=
requests
.
get
(
url
=
Wpp1
.
URL
+
Wpp1
.
URLmode
[
types
[
file
]],
params
=
{
'token'
:
Wpp1
.
token
,
'uid'
:
origen
,
'to'
:
data
[
Table
.
dest
],
'text'
:
text
})
succ
[
file
]
=
result
.
json
()[
'success'
]
succ
[
file
]
=
result
.
json
()[
'success'
]
else
:
else
:
...
@@ -87,6 +88,11 @@ class Wpp1(ServiceBase):
...
@@ -87,6 +88,11 @@ class Wpp1(ServiceBase):
succ
[
file
]
=
False
succ
[
file
]
=
False
return
succ
return
succ
def
controlSalida
(
self
,
stadotel
,
telefono
):
n
=
stadotel
.
ChequearTelefono
(
telefono
)
if
not
n
[
"success"
]
==
True
:
raise
NameError
(
'Telefono_Desconectado'
)
def
validatetype
(
self
,
type
):
def
validatetype
(
self
,
type
):
return
type
in
Wpp1
.
Allowed
return
type
in
Wpp1
.
Allowed
...
@@ -109,6 +115,9 @@ class Mail(ServiceBase):
...
@@ -109,6 +115,9 @@ class Mail(ServiceBase):
def
__init__
(
self
):
def
__init__
(
self
):
self
.
__username
=
config
.
SMTP_username
self
.
__username
=
config
.
SMTP_username
self
.
__password
=
config
.
SMTP_password
self
.
__password
=
config
.
SMTP_password
def
iniciarConn
(
self
):
#print("inicializo cliente default mail")
self
.
s
=
smtplib
.
SMTP
(
host
=
config
.
SMTP_HOST
,
port
=
config
.
SMTP_PORT
)
self
.
s
=
smtplib
.
SMTP
(
host
=
config
.
SMTP_HOST
,
port
=
config
.
SMTP_PORT
)
self
.
s
.
starttls
()
self
.
s
.
starttls
()
self
.
s
.
login
(
self
.
__username
,
self
.
__password
)
self
.
s
.
login
(
self
.
__username
,
self
.
__password
)
...
@@ -117,7 +126,7 @@ class Mail(ServiceBase):
...
@@ -117,7 +126,7 @@ class Mail(ServiceBase):
# N = self._send(data)
# N = self._send(data)
# return(N)
# return(N)
def
send
(
self
,
data
):
def
send
(
self
,
data
,
stadotel
,
stadomail
):
types
=
json
.
loads
(
data
[
Table
.
type
])
types
=
json
.
loads
(
data
[
Table
.
type
])
info
=
json
.
loads
(
data
[
Table
.
info
])
info
=
json
.
loads
(
data
[
Table
.
info
])
msg
=
MIMEMultipart
()
msg
=
MIMEMultipart
()
...
@@ -138,7 +147,7 @@ class Mail(ServiceBase):
...
@@ -138,7 +147,7 @@ class Mail(ServiceBase):
succ
[
file
]
=
True
succ
[
file
]
=
True
#self.s.send_message(msg)
#self.s.send_message(msg)
try
:
try
:
self
.
Envio
(
msg
,
info
)
self
.
Envio
(
msg
,
info
,
stadomail
)
except
:
except
:
print
(
"ROMPIO ALGO EN EL ENVIO"
)
print
(
"ROMPIO ALGO EN EL ENVIO"
)
succ
[
file
]
=
False
succ
[
file
]
=
False
...
@@ -151,7 +160,7 @@ class Mail(ServiceBase):
...
@@ -151,7 +160,7 @@ class Mail(ServiceBase):
u
=
self
.
_Mail__username
u
=
self
.
_Mail__username
return
(
u
)
return
(
u
)
def
Envio
(
self
,
msg
,
info
):
def
Envio
(
self
,
msg
,
info
,
stadomail
):
try
:
try
:
smtp
=
info
[
"smtp"
]
smtp
=
info
[
"smtp"
]
except
:
except
:
...
@@ -159,11 +168,21 @@ class Mail(ServiceBase):
...
@@ -159,11 +168,21 @@ class Mail(ServiceBase):
if
smtp
==
False
:
if
smtp
==
False
:
#print(info, type(info))
#print(info, type(info))
print
(
"mail sale por default"
)
print
(
"mail sale por default"
)
self
.
iniciarConn
()
n
=
self
.
s
.
send_message
(
msg
)
n
=
self
.
s
.
send_message
(
msg
)
else
:
else
:
print
(
"mail sale por cliente"
)
print
(
"mail sale por cliente"
)
self
.
ControlMail
(
stadomail
,
smtp
)
n
=
self
.
SMTPcliente
(
msg
,
smtp
)
n
=
self
.
SMTPcliente
(
msg
,
smtp
)
return
(
n
)
return
(
n
)
def
ControlMail
(
self
,
stadomail
,
smtp
):
est
=
stadomail
.
ChequearMailServer
(
User
=
smtp
[
"username"
],
Pass
=
smtp
[
"password"
],
Port
=
smtp
[
"port"
],
Host
=
smtp
[
"host"
])
if
est
==
False
:
raise
NameError
(
"Mail_Server_Fall"
)
def
SMTPcliente
(
self
,
msg
,
Serversmtp
):
def
SMTPcliente
(
self
,
msg
,
Serversmtp
):
print
(
Serversmtp
,
type
(
Serversmtp
))
print
(
Serversmtp
,
type
(
Serversmtp
))
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment