LOC-29 Fix NO_RECP exception when calling gpg.encrypt on Debian9
LOC-29 Fix NO_RECP exception when calling gpg.encrypt on Debian9

file:a/README.md -> file:b/README.md
# LocalChat # LocalChat
   
   
## About ## About
   
Localchat is a simple and lightweight chat application. It's primary purpose is to provide a means to have a multi-user Off-The-Record transient chat, minimising the likelihood that anyone but the chat participants has even a record that the chat took place. Localchat is a simple and lightweight chat application. It's primary purpose is to provide a means to have a multi-user Off-The-Record transient chat, minimising the likelihood that anyone but the chat participants has even a record that the chat took place.
   
It is not designed to be exposed to the internet at large. Instead, the primary intended means of use is to deploy it onto a new system (such as an EC2 instance), have user's SSH tunnel in (or use other supported methods) to use it and then discard the system once that chat has completed. It is not designed to be exposed to the internet at large. Instead, the primary intended means of use is to deploy it onto a new system (such as an EC2 instance), have user's SSH tunnel in (or use other supported methods) to use it and then discard the system once that chat has completed.
   
In other words, it's not designed as a generic chat application, but as one to be used for clandestine chats that are hard to monitor/intercept. In other words, it's not designed as a generic chat application, but as one to be used for clandestine chats that are hard to monitor/intercept.
   
The provided client uses basic End To End encryption (currently using PGP as the encryption mechanism), and the server holds encrypted payloads in memory only (to ensure the ciphertext doesn't end up captured on the hosting provider's SAN for a time). Message payloads are purged after a short interval to help reduce the potential exposure were someone to be monitoring the server's memory. The provided client uses basic End To End encryption (currently using PGP as the encryption mechanism), and the server holds encrypted payloads in memory only (to ensure the ciphertext doesn't end up captured on the hosting provider's SAN for a time). Message payloads are purged after a short interval to help reduce the potential exposure were someone to be monitoring the server's memory.
   
The default behaviour is a Multi-User Chat (MUC), however direct messaging between participants within a room is also supported. The default behaviour is a Multi-User Chat (MUC), however direct messaging between participants within a room is also supported.
   
   
  ## Project Management
   
  Planned features, bugfixes etc can all be viewed at [project.bentasker.co.uk](https://projects.bentasker.co.uk/jira_projects/browse/LOC.html). Release notes are at [bentasker.co.uk](https://www.bentasker.co.uk/documentation/release-notes/91-localchat)
   
   
   
   
   
   
## Dependancies ## Dependancies
   
### Server ### Server
   
`TODO` See [server/README.md](server/README.md)
   
   
### Client ### Client
   
`TODO` See [client/README.md](client/README.md)
   
   
   
   
   
## Deployment Options ## Deployment Options
   
LocalChat is not designed to be publicly accessible and discoverable. It's been written to be relatively light-weight for ease of deployment (and subsequent destruction). LocalChat is not designed to be publicly accessible and discoverable. It's been written to be relatively light-weight for ease of deployment (and subsequent destruction).
   
Under no circumstances should you configure it to bind to `0.0.0.0` as doing so would allow adversaries to not only discover it, but to start probing it in order to try and establish whether it's in use (and who's using it). There should *always* be some additional step (i.e. being able to SSH into a server, or knowing exactly where to find it) required before access is available. Under no circumstances should you configure it to bind to `0.0.0.0` as doing so would allow adversaries to not only discover it, but to start probing it in order to try and establish whether it's in use (and who's using it). There should *always* be some additional step (i.e. being able to SSH into a server, or knowing exactly where to find it) required before access is available.
   
Over time, it will likely be hardened further, but it's unlikely it will ever be considered safe for completely unrestricted access - allowing discoverability would allow adversaries to establish likely meeting places in advance. Over time, it will likely be hardened further, but it's unlikely it will ever be considered safe for completely unrestricted access - allowing discoverability would allow adversaries to establish likely meeting places in advance.
   
   
   
### Direct Communication ### Direct Communication
   
The most simple deployment method is to run the server component somewhere, and then simply run the client on the same system. The problem with this is it means that the E2E encryption keys are in memory on the same system as the server. The most simple deployment method is to run the server component somewhere, and then simply run the client on the same system. The problem with this is it means that the E2E encryption keys are in memory on the same system as the server.
   
./server/LocalChat.py ./server/LocalChat.py
./client/LocalChatClient.py ./client/LocalChatClient.py
   
   
So, it's recommended that you run the client on a seperate system and use a SSH tunnel to allow communication So, it's recommended that you run the client on a seperate system and use a SSH tunnel to allow communication
   
user@server1 $ ./server/LocalChat.py user@server1 $ ./server/LocalChat.py
   
remuser@mymachine $ ssh -L 8090:127.0.0.1:8090 user@server1 remuser@mymachine $ ssh -L 8090:127.0.0.1:8090 user@server1
remuser@mymachine $ ./client/LocalChatClient.py remuser@mymachine $ ./client/LocalChatClient.py
   
This ensures that anyone able to observe memory on the server cannot see the E2E keys. This ensures that anyone able to observe memory on the server cannot see the E2E keys.
   
As a variation of this, if you do not want the server component to appear on the server's filesystem (you will still need to install dependancies though) then you can also create a reverse tunnel back to another machine. Just be aware that if the connection drops, the server will be unavailable, so ensure you've a reliable connection As a variation of this, if you do not want the server component to appear on the server's filesystem (you will still need to install dependancies though) then you can also create a reverse tunnel back to another machine. Just be aware that if the connection drops, the server will be unavailable, so ensure you've a reliable connection
   
   
user@server2 $ ./server/LocalChat.py user@server2 $ ./server/LocalChat.py
user@server2 $ ssh -R 127.0.0.1:8090:127.0.0.1:8090 user@server1 user@server2 $ ssh -R 127.0.0.1:8090:127.0.0.1:8090 user@server1
   
remuser@mymachine $ ssh -L 8090:127.0.0.1:8090 user@server1 remuser@mymachine $ ssh -L 8090:127.0.0.1:8090 user@server1
remuser@mymachine $ ./client/LocalChatClient.py remuser@mymachine $ ./client/LocalChatClient.py
   
### Proxied Communications ### Proxied Communications
   
It may be that you decide it's better to "hide" the server within an existing website, so that chat connections are mixed in with traffic to that site. It may be that you decide it's better to "hide" the server within an existing website, so that chat connections are mixed in with traffic to that site.
   
To do this, you simply need to proxy a path on that website back to the server. So you may have an existing Nginx server block like this: To do this, you simply need to proxy a path on that website back to the server. So you may have an existing Nginx server block like this:
   
server { server {
listen 443; listen 443;
   
root /usr/share/nginx/example.com/; root /usr/share/nginx/example.com/;
index index.html; index index.html;
   
server_name example.com; server_name example.com;
   
ssl on; ssl on;
ssl_certificate /etc/pki/tls/certs/example.com.crt; ssl_certificate /etc/pki/tls/certs/example.com.crt;
ssl_certificate_key /etc/pki/tls/private/example.com.key; ssl_certificate_key /etc/pki/tls/private/example.com.key;
   
ssl_session_timeout 5m; ssl_session_timeout 5m;
   
location / { location / {
try_files $uri $uri/ =404; try_files $uri $uri/ =404;
} }
   
} }
   
You'd then add a `location` statement with a hard to guess path to handle the chat client You'd then add a `location` statement with a hard to guess path to handle the chat client
   
server { server {
listen 443; listen 443;
   
root /usr/share/nginx/example.com/; root /usr/share/nginx/example.com/;
index index.html; index index.html;
   
server_name example.com; server_name example.com;
   
ssl on; ssl on;
ssl_certificate /etc/pki/tls/certs/example.com.crt; ssl_certificate /etc/pki/tls/certs/example.com.crt;
ssl_certificate_key /etc/pki/tls/private/example.com.key; ssl_certificate_key /etc/pki/tls/private/example.com.key;
   
ssl_session_timeout 5m; ssl_session_timeout 5m;
   
location / { location / {
try_files $uri $uri/ =404; try_files $uri $uri/ =404;
} }
location /SM9vbtNrnZ0d6WQa1ByLjZEX/ { location /SM9vbtNrnZ0d6WQa1ByLjZEX/ {
proxy_pass https://127.0.0.1:8090/; proxy_pass https://127.0.0.1:8090/;
} }
   
} }
   
And make the server component available on that port (either by running directly, or using a SSH reverse tunnel as described above). And make the server component available on that port (either by running directly, or using a SSH reverse tunnel as described above).
   
Assuming `example.com` serves a publicly signed and trusted certificate (you'd hope it does), we'll also want to re-enable cert verification. So the client is called with verification enabled and being passed the URL to use for requests Assuming `example.com` serves a publicly signed and trusted certificate (you'd hope it does), we'll also want to re-enable cert verification. So the client is called with verification enabled and being passed the URL to use for requests
   
./client/LocalChatClient.py --verify https://example.com/SM9vbtNrnZ0d6WQa1ByLjZEX/ ./client/LocalChatClient.py --verify https://example.com/SM9vbtNrnZ0d6WQa1ByLjZEX/
Later versions will implement the ability to include auth headers in the request so that you can 404 unauthorised requests to the 'hidden' path. Until then, unless there's a particularly strong reason not to, the SSH methods described above are the recommended routes of access. Later versions will implement the ability to include auth headers in the request so that you can 404 unauthorised requests to the 'hidden' path. Until then, unless there's a particularly strong reason not to, the SSH methods described above are the recommended routes of access.
   
   
   
   
## Copyright ## Copyright
   
LocalChat is Copyright (C) 2018 B Tasker. All Rights Reserved. LocalChat is Copyright (C) 2018 B Tasker. All Rights Reserved.
   
Released Under GNU GPL V2 License, see LICENSE. Released Under GNU GPL V2 License, see LICENSE.
   
   
   
   
   
   
  ansible_user: ansible
 
  [localchat-servers]
  192.168.7.253
 
 
  [Unit]
  Description=LocalChat
  After=multi-user.target
 
  [Service]
  WorkingDirectory=/usr/local/localchat
  Type=idle
  ExecStart=/usr/bin/python LocalChat.py
  Restart=always
  RestartSec=3
 
  [Install]
  WantedBy=multi-user.target
 
 
  ---
 
  - hosts: localchat-servers
  roles:
  - localchat-server
 
 
  localchat_server_install_path: /usr/local/localchat
 
 
  ---
 
  # Handlers
 
 
 
  - name: Start service
  # See above.
  command: systemctl start localchat.service
  become: true
 
  - name: reload systemd
  command: systemctl daemon-reload
  become: true
 
  ---
 
  - name: Install Dependancies
  apt: name={{item}} state=installed
  with_items:
  - python-flask
  - python-urllib3
  - python-sqlite
  - python-bcrypt
  - python-gnupg
  become: true
  tags: deps
 
 
 
 
 
 
 
  - name: Create Unit File
  template: src=localchat.service dest=/etc/systemd/system/localchat.service
  notify: reload systemd
  become: true
 
 
 
  [Unit]
  Description=LocalChat
  After=multi-user.target
 
  [Service]
  WorkingDirectory={{localchat_server_install_path}}
  Type=idle
  ExecStart=/usr/bin/python LocalChat.py
  Restart=always
  RestartSec=3
 
  [Install]
  WantedBy=multi-user.target
 
file:b/ansible/site.yml (new)
  - include: localchat-servers.yml
 
#!/usr/bin/env python #!/usr/bin/env python
# #
# #
# Interface based upon https://github.com/izderadicka/xmpp-tester/blob/master/commander.py # Interface based upon https://github.com/izderadicka/xmpp-tester/blob/master/commander.py
# #
# apt-get install: # apt-get install:
# python-urwid # python-urwid
# python-gnupg # python-gnupg
   
   
import urwid import urwid
from collections import deque from collections import deque
from threading import Thread from threading import Thread
import threading import threading
   
import json import json
import urllib2 import urllib2
import ssl import ssl
import string import string
import random import random
import sys import sys
import datetime as dt import datetime as dt
   
import gnupg import gnupg
   
   
   
# This can be changed from the commandline # This can be changed from the commandline
SERVER='https://127.0.0.1:8090' SERVER='https://127.0.0.1:8090'
   
class msgHandler(object): class msgHandler(object):
def __init__(self): def __init__(self):
self.last = 0 self.last = 0
self.user = False self.user = False
self.server = SERVER self.server = SERVER
self.room = False self.room = False
self.roompass = False self.roompass = False
self.sesskey = False self.sesskey = False
self.syskey = False self.syskey = False
self.verifycert = False self.verifycert = False
self.gpg = gnupg.GPG() self.gpg = gnupg.GPG()
def pollForMessage(self): def pollForMessage(self):
if not self.room: if not self.room:
# We're not connected to a room # We're not connected to a room
return False return False
payload = {"roomName": self.room, payload = {"roomName": self.room,
"mylast":self.last, "mylast":self.last,
"user": self.user, "user": self.user,
"sesskey": self.sesskey "sesskey": self.sesskey
} }
request = {"action":"pollMsg", request = {"action":"pollMsg",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
   
resp = self.sendRequest(request) resp = self.sendRequest(request)
if resp == "BROKENLINK": if resp == "BROKENLINK":
return resp return resp
if resp['status'] == "unchanged": if resp['status'] == "unchanged":
return False return False
if resp['status'] == "errmessage": if resp['status'] == "errmessage":
# Got an error, we need to stop the poll and then return the text # Got an error, we need to stop the poll and then return the text
self.room = False self.room = False
self.roompass = False self.roompass = False
self.sesskey = False self.sesskey = False
return [['reversed',resp['text']]] return [['reversed',resp['text']]]
to_print = [] to_print = []
# Otherwise, process the messages # Otherwise, process the messages
for i in resp["messages"]: for i in resp["messages"]:
self.last = i[0] self.last = i[0]
upstruser = i[3] upstruser = i[3]
try: try:
msgbody = json.loads(self.decrypt(i[1],upstruser)) msgbody = json.loads(self.decrypt(i[1],upstruser))
except: except:
# Means an invalid message was received - LOC-8 # Means an invalid message was received - LOC-8
to_print.append(['error','Received message which could not be decrypted']) to_print.append(['error','Received message which could not be decrypted'])
continue continue
# TODO - We'll need to json decode and extract the sending user's name # TODO - We'll need to json decode and extract the sending user's name
# but not currently including that info in my curl tests. Also means test that part of the next block # but not currently including that info in my curl tests. Also means test that part of the next block
color = "green" color = "green"
   
if upstruser == self.user: if upstruser == self.user:
# One of our own, change the color # One of our own, change the color
color = "blue" color = "blue"
elif upstruser == "SYSTEM": elif upstruser == "SYSTEM":
color = "magenta" color = "magenta"
if msgbody['verb'] == "sysalert": if msgbody['verb'] == "sysalert":
color = 'reversed' color = 'reversed'
elif msgbody['verb'] == 'syswarn': elif msgbody['verb'] == 'syswarn':
color = 'cyan' color = 'cyan'
ts = dt.datetime.utcfromtimestamp(i[2]).strftime("[%H:%M:%S]") ts = dt.datetime.utcfromtimestamp(i[2]).strftime("[%H:%M:%S]")
if msgbody["verb"] == "do": if msgbody["verb"] == "do":
color = 'yellow' color = 'yellow'
line = [ line = [
" ** %s %s **" % (upstruser,msgbody['text']) " ** %s %s **" % (upstruser,msgbody['text'])
] ]
else: else:
if i[4] != "0": if i[4] != "0":
color = 'brown' color = 'brown'
upstruser = 'DM %s' % (upstruser,) upstruser = 'DM %s' % (upstruser,)
line = [ line = [
ts, # timestamp ts, # timestamp
"%s>" % (upstruser,), # To be replaced later "%s>" % (upstruser,), # To be replaced later
msgbody['text'] msgbody['text']
] ]
to_print.append([color,' '.join(line)]) to_print.append([color,' '.join(line)])
return to_print return to_print
def sendMsg(self,line,verb='say'): def sendMsg(self,line,verb='say'):
''' Send a message ''' Send a message
''' '''
if not self.room: if not self.room:
# We're not in a room. Abort # We're not in a room. Abort
return False return False
# Otherwise, build a payload # Otherwise, build a payload
msg = { msg = {
'user': self.user, 'user': self.user,
'text': line, 'text': line,
"verb": verb "verb": verb
} }
payload = {"roomName": self.room, payload = {"roomName": self.room,
"msg":self.encrypt(json.dumps(msg)), "msg":self.encrypt(json.dumps(msg)),
"user": self.user, "user": self.user,
"sesskey": self.sesskey "sesskey": self.sesskey
} }
request = {"action":"sendMsg", request = {"action":"sendMsg",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
   
resp = self.sendRequest(request) resp = self.sendRequest(request)
if "status" in resp and resp['status'] == "ok": if "status" in resp and resp['status'] == "ok":
return True return True
return False return False
   
   
def sendDirectMsg(self,line,user,verb='say'): def sendDirectMsg(self,line,user,verb='say'):
''' Send a direct message ''' Send a direct message
''' '''
if not self.room: if not self.room:
# We're not in a room. Abort # We're not in a room. Abort
return False return False
# Otherwise, build a payload # Otherwise, build a payload
msg = { msg = {
'user': self.user, 'user': self.user,
'text': line, 'text': line,
"verb": verb "verb": verb
} }
payload = {"roomName": self.room, payload = {"roomName": self.room,
"msg":self.encrypt(json.dumps(msg)), # TODO this should use the user's key "msg":self.encrypt(json.dumps(msg)), # TODO this should use the user's key
"to": user, "to": user,
"user": self.user, "user": self.user,
"sesskey": self.sesskey "sesskey": self.sesskey
} }
request = {"action":"sendDirectMsg", request = {"action":"sendDirectMsg",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
   
resp = self.sendRequest(request) resp = self.sendRequest(request)
if "status" in resp and resp['status'] == "ok": if "status" in resp and resp['status'] == "ok":
return True return True
return False return False
   
   
   
   
def joinRoom(self,user,room,passw): def joinRoom(self,user,room,passw):
''' Join a room ''' Join a room
''' '''
# We only want to send the user password section of the password # We only want to send the user password section of the password
p = passw.split(":") p = passw.split(":")
userpass = p[1] userpass = p[1]
payload = {"roomName": room, payload = {"roomName": room,
"userpass": userpass.encode('utf-8'), "userpass": userpass.encode('utf-8'),
"user": user "user": user
} }
request = {"action":"joinRoom", request = {"action":"joinRoom",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
   
   
resp = self.sendRequest(request) resp = self.sendRequest(request)
   
if resp == "BROKENLINK" or resp['status'] != "ok": if resp == "BROKENLINK" or resp['status'] != "ok":
return False return False
self.room = room self.room = room
self.user = user self.user = user
self.last = resp['last'] self.last = resp['last']
self.roompass = p[0] # The room password is the first section of the password self.roompass = p[0] # The room password is the first section of the password
self.sesskey = resp['session'] self.sesskey = resp['session']
self.syskey = resp['syskey'] self.syskey = resp['syskey']
return True return True
   
   
   
   
def leaveRoom(self): def leaveRoom(self):
''' Leave the current room ''' Leave the current room
''' '''
if not self.room: if not self.room:
return False return False
payload = {"roomName": self.room, payload = {"roomName": self.room,
"user": self.user, "user": self.user,
"sesskey": self.sesskey "sesskey": self.sesskey
} }
request = {"action":"leaveRoom", request = {"action":"leaveRoom",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
   
   
resp = self.sendRequest(request) resp = self.sendRequest(request)
   
if resp == "BROKENLINK" or resp['status'] != "ok": if resp == "BROKENLINK" or resp['status'] != "ok":
return False return False
self.room = False self.room = False
self.user = False self.user = False
self.last = 0 self.last = 0
self.roompass = False self.roompass = False
self.sesskey = False self.sesskey = False
return True return True
   
   
def createRoom(self,room,user=False): def createRoom(self,room,user=False):
''' Create a new room ''' Create a new room
''' '''
if not user and not self.user: if not user and not self.user:
return False return False
if not user: if not user:
user = self.user user = self.user
   
   
# Generate a password for the admin # Generate a password for the admin
passw = self.genpassw() passw = self.genpassw()
payload = {"roomName": room, payload = {"roomName": room,
"owner": user, "owner": user,
"pass": passw "pass": passw
} }
request = {"action":"createRoom", request = {"action":"createRoom",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
resp = self.sendRequest(request) resp = self.sendRequest(request)
   
if resp == "BROKENLINK" or resp['status'] != "ok": if resp == "BROKENLINK" or resp['status'] != "ok":
return False return False
return [resp['name'],passw] return [resp['name'],passw]
   
def closeRoom(self): def closeRoom(self):
if not self.room: if not self.room:
return False return False
   
payload = {"roomName": self.room, payload = {"roomName": self.room,
"user": self.user, "user": self.user,
"sesskey": self.sesskey "sesskey": self.sesskey
} }
request = {"action":"closeRoom", request = {"action":"closeRoom",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
   
resp = self.sendRequest(request) resp = self.sendRequest(request)
if resp == "BROKENLINK" or resp['status'] != "ok": if resp == "BROKENLINK" or resp['status'] != "ok":
return False return False
return True return True
   
def inviteUser(self,user): def inviteUser(self,user):
''' Invite a user into a room ''' Invite a user into a room
#TODO - Authentication #TODO - Authentication
''' '''
# Generate a password for the new user # Generate a password for the new user
passw = self.genpassw() passw = self.genpassw()
payload = {"roomName": self.room, payload = {"roomName": self.room,
"user": self.user, "user": self.user,
"invite": user, "invite": user,
"pass": passw, "pass": passw,
"sesskey": self.sesskey "sesskey": self.sesskey
} }
request = {"action":"inviteUser", request = {"action":"inviteUser",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
resp = self.sendRequest(request) resp = self.sendRequest(request)
   
if resp == "BROKENLINK" or resp['status'] != "ok": if resp == "BROKENLINK" or resp['status'] != "ok":
return False return False
return [self.room,self.roompass,passw,user] return [self.room,self.roompass,passw,user]
   
   
   
def kickUser(self,user,ban=False): def kickUser(self,user,ban=False):
''' Kick a user out of the room ''' Kick a user out of the room
''' '''
action = 'banUser' action = 'banUser'
if not ban: if not ban:
action = 'kickUser' action = 'kickUser'
payload = {"roomName": self.room, payload = {"roomName": self.room,
"user": self.user, "user": self.user,
"kick": user, "kick": user,
"sesskey": self.sesskey "sesskey": self.sesskey
} }
request = {"action":action, request = {"action":action,
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
resp = self.sendRequest(request) resp = self.sendRequest(request)
   
if resp == "BROKENLINK" or resp['status'] != "ok": if resp == "BROKENLINK" or resp['status'] != "ok":
return False return False
return True return True
   
   
def sendRequest(self,data): def sendRequest(self,data):
data = json.dumps(data) data = json.dumps(data)
try: try:
ctx = ssl.create_default_context() ctx = ssl.create_default_context()
if not self.verifycert: if not self.verifycert:
ctx.check_hostname = False ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE ctx.verify_mode = ssl.CERT_NONE
   
req = urllib2.Request(self.server, data, {'Content-Type': 'application/json'}) req = urllib2.Request(self.server, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req,context=ctx) f = urllib2.urlopen(req,context=ctx)
response = f.read() response = f.read()
f.close() f.close()
return json.loads(response) return json.loads(response)
except: except:
return "BROKENLINK" return "BROKENLINK"
   
   
   
def decrypt(self,msg,sender): def decrypt(self,msg,sender):
''' Placeholder ''' Placeholder
''' '''
try: try:
key = self.roompass key = self.roompass
if sender == "SYSTEM": if sender == "SYSTEM":
key = self.syskey key = self.syskey
return str(self.gpg.decrypt(msg.decode("base64"),passphrase=key)) return str(self.gpg.decrypt(msg.decode("base64"),passphrase=key))
except: except:
return False return False
   
def encrypt(self,msg): def encrypt(self,msg):
''' Placeholder ''' Placeholder
''' '''
crypted = self.gpg.encrypt(msg,None,passphrase=self.roompass,symmetric="AES256",armor=False) crypted = self.gpg.encrypt(msg,None,passphrase=self.roompass,symmetric="AES256",armor=False)
return crypted.data.encode('base64') return crypted.data.encode('base64')
   
   
def hashpw(self,passw): def hashpw(self,passw):
''' Placeholder ''' Placeholder
''' '''
return passw return passw
   
   
def genpassw(self,N=16): def genpassw(self,N=16):
''' Generate a random string of chars to act as a password ''' Generate a random string of chars to act as a password
''' '''
return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits).encode('utf-8') for _ in range(N)) return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits).encode('utf-8') for _ in range(N))
   
   
   
class NotInRoom(Exception): class NotInRoom(Exception):
def __init__(self,cmd): def __init__(self,cmd):
Exception.__init__(self,'Message not sent') Exception.__init__(self,'Message not sent')
   
   
class UnableTo(Exception): class UnableTo(Exception):
def __init__(self,action,cmd): def __init__(self,action,cmd):
Exception.__init__(self,'Could not %s: %s' % (action,cmd)) Exception.__init__(self,'Could not %s: %s' % (action,cmd))
   
   
class InvalidCommand(Exception): class InvalidCommand(Exception):
def __init__(self,cmd): def __init__(self,cmd):
Exception.__init__(self,'Command is invalid: %s' % (cmd,)) Exception.__init__(self,'Command is invalid: %s' % (cmd,))
   
   
class Command(object): class Command(object):
""" Base class to manage commands in commander """ Base class to manage commands in commander
similar to cmd.Cmd in standard library similar to cmd.Cmd in standard library
just extend with do_something method to handle your commands""" just extend with do_something method to handle your commands"""
   
def __init__(self,quit_commands=['q','quit','exit'], help_commands=['help','?', 'h']): def __init__(self,quit_commands=['/q','/quit','/exit'], help_commands=['/help','/?', '/h']):
self._quit_cmd=quit_commands self._quit_cmd=quit_commands
self._help_cmd=help_commands self._help_cmd=help_commands
def __call__(self,line): def __call__(self,line):
global msg global msg
tokens=line.split() tokens=line.split()
cmd=tokens[0].lower() cmd=tokens[0].lower()
args=tokens[1:] args=tokens[1:]
if cmd[0] == "/": if cmd[0] == "/":
# It's a command # It's a command
cmd = cmd[1:] cmd = cmd[1:]
if cmd == "me": if cmd == "me":
#/me [string] #/me [string]
r = msg.sendMsg(' '.join(args),'do') r = msg.sendMsg(' '.join(args),'do')
if not r: if not r:
raise NotInRoom(line) raise NotInRoom(line)
return return
   
if cmd == "ban": if cmd == "ban":
# /kick [user] # /kick [user]
if len(args) < 1: if len(args) < 1:
raise InvalidCommand(line) raise InvalidCommand(line)
m = msg.kickUser(args[0],True) m = msg.kickUser(args[0],True)
return return
if cmd == "join": if cmd == "join":
# /join [room] [password] [username] # /join [room] [password] [username]
if len(args) < 3: if len(args) < 3:
raise InvalidCommand(line) raise InvalidCommand(line)
return return
if not msg.joinRoom(args[2],args[0],args[1]): if not msg.joinRoom(args[2],args[0],args[1]):
raise UnableTo('join',line) raise UnableTo('join',line)
return return
if cmd == "kick": if cmd == "kick":
# /kick [user] # /kick [user]
if len(args) < 1: if len(args) < 1:
raise InvalidCommand(line) raise InvalidCommand(line)
m = msg.kickUser(args[0]) m = msg.kickUser(args[0])
return return
if cmd == "leave": if cmd == "leave":
# /leave # /leave
if not msg.leaveRoom(): if not msg.leaveRoom():
raise UnableTo('leave',line) raise UnableTo('leave',line)
return return
global c global c
c.output('Left the room','magenta') c.output('Left the room','magenta')
return return
   
   
if cmd == 'msg': if cmd == 'msg':
# /msg ben Hello ben this is a DM # /msg ben Hello ben this is a DM
line = ' '.join(args[1:]) line = ' '.join(args[1:])
r = msg.sendDirectMsg(line,args[0]) r = msg.sendDirectMsg(line,args[0])
if not r: if not r:
raise NotInRoom(line) raise NotInRoom(line)
# Otherwise push a copy of the message to display # Otherwise push a copy of the message to display
# cos we won't get this one back from pollMsg # cos we won't get this one back from pollMsg
global c global c
m = "%s DM %s>%s" % (msg.user,args[0],line) m = "%s DM %s>%s" % (msg.user,args[0],line)
c.output(m,'blue') c.output(m,'blue')
return return
   
if cmd == "room": if cmd == "room":
# /room close [roompass] # /room close [roompass]
if args[0] == "close": if args[0] == "close":
if len(args) < 2: if len(args) < 2:
raise InvalidCommand('/room close [pass]') raise InvalidCommand('/room close [pass]')
if not msg.closeRoom(): if not msg.closeRoom():
raise UnableTo('Close Room','') raise UnableTo('Close Room','')
return return
# /room create [roomname] [[user]] # /room create [roomname] [[user]]
if args[0] == "create": if args[0] == "create":
if len(args) < 3: if len(args) < 3:
args[2] = False args[2] = False
n = msg.createRoom(args[1],args[2]) n = msg.createRoom(args[1],args[2])
if not n: if not n:
raise UnableTo('create room',line) raise UnableTo('create room',line)
return return
# Seperate out the return value # Seperate out the return value
rm = n[0] rm = n[0]
up = n[1] # user specific password up = n[1] # user specific password
global c global c
# Generate a room password # Generate a room password
p = msg.genpassw() p = msg.genpassw()
c.output('Created Room %s' %(rm)) c.output('Created Room %s' %(rm))
c.output('To join the room, do /join %s %s:%s %s' %(args[1],p,up,args[2])) c.output('To join the room, do /join %s %s:%s %s' %(args[1],p,up,args[2]))
return return
elif args[0] == "invite": elif args[0] == "invite":
if len(args) < 2: if len(args) < 2:
raise InvalidCommand(line) raise InvalidCommand(line)
n = msg.inviteUser(args[1]) n = msg.inviteUser(args[1])
if not n: if not n:
raise UnableTo('invite user',line) raise UnableTo('invite user',line)
return return
global c global c
c.output('User %s may now join room' %(args[1],)) c.output('User %s may now join room' %(args[1],))
c.output('To join the room, they should do /join %s %s:%s %s' %(n[0],n[1],n[2],n[3])) c.output('To join the room, they should do /join %s %s:%s %s' %(n[0],n[1],n[2],n[3]))
return return
  if cmd in ["help",'h','?']:
  return self.help(args[0] if args else None)
   
  if cmd in ["exit",'q','quit']:
  return Commander.Exit
   
   
if cmd in self._quit_cmd: if cmd in self._quit_cmd:
return Commander.Exit return Commander.Exit
elif cmd in self._help_cmd:  
return self.help(args[0] if args else None)  
elif hasattr(self, 'do_'+cmd): elif hasattr(self, 'do_'+cmd):
return getattr(self, 'do_'+cmd)(*args) return getattr(self, 'do_'+cmd)(*args)
else: else:
# If it's not a command, then we're trying to send a message # If it's not a command, then we're trying to send a message
r = msg.sendMsg(line) r = msg.sendMsg(line)
if not r: if not r:
raise NotInRoom(line) raise NotInRoom(line)
def help(self,cmd=None): def help(self,cmd=None):
def std_help(): def std_help():
qc='|'.join(self._quit_cmd) qc='|'.join(self._quit_cmd)
hc ='|'.join(self._help_cmd) hc ='|'.join(self._help_cmd)
res='Type [%s] to quit program\n' % qc res='Type [%s] to quit program\n' % qc
res += """Available commands: res += """Available commands:
   
  /h Print this help text
   
/join [room] [password] [username] Join a room /join [room] [password] [username] Join a room
/leave Leave current room /leave Leave current room
/room create [roomname] [roompass] [admin user] New room management /room create [roomname] [roompass] [admin user] New room management
/room invite [user] Invite a user into the current room /room invite [user] Invite a user into the current room
/me [string] Send an 'action' instead of a message /me [string] Send an 'action' instead of a message
  /msg [user] message Send a direct message to [user]
Room Admin commands: Room Admin commands:
/kick [user] Kick a user out of the room (they can return) /kick [user] Kick a user out of the room (they can return)
/ban [user] Kick a user out and disinvite them (they cannot return) /ban [user] Kick a user out and disinvite them (they cannot return)
/room close [roompass] Kick all users out and close the room /room close [roompass] Kick all users out and close the room
Once in a room, to send a message, just type it. Once in a room, to send a message, just type it.
""" """
return res return res
if not cmd: if not cmd:
return std_help() return std_help()
else: else:
try: try:
fn=getattr(self,'do_'+cmd) fn=getattr(self,'do_'+cmd)
doc=fn.__doc__ doc=fn.__doc__
return doc or 'No documentation available for %s'%cmd return doc or 'No documentation available for %s'%cmd
except AttributeError: except AttributeError:
return std_help() return std_help()
class FocusMixin(object): class FocusMixin(object):
def mouse_event(self, size, event, button, x, y, focus): def mouse_event(self, size, event, button, x, y, focus):
if focus and hasattr(self, '_got_focus') and self._got_focus: if focus and hasattr(self, '_got_focus') and self._got_focus:
self._got_focus() self._got_focus()
return super(FocusMixin,self).mouse_event(size, event, button, x, y, focus) return super(FocusMixin,self).mouse_event(size, event, button, x, y, focus)
class ListView(FocusMixin, urwid.ListBox): class ListView(FocusMixin, urwid.ListBox):
def __init__(self, model, got_focus, max_size=None): def __init__(self, model, got_focus, max_size=None):
urwid.ListBox.__init__(self,model) urwid.ListBox.__init__(self,model)
self._got_focus=got_focus self._got_focus=got_focus
self.max_size=max_size self.max_size=max_size
self._lock=threading.Lock() self._lock=threading.Lock()
def add(self,line): def add(self,line):
with self._lock: with self._lock:
was_on_end=self.get_focus()[1] == len(self.body)-1 was_on_end=self.get_focus()[1] == len(self.body)-1
if self.max_size and len(self.body)>self.max_size: if self.max_size and len(self.body)>self.max_size:
del self.body[0] del self.body[0]
self.body.append(urwid.Text(line)) self.body.append(urwid.Text(line))
last=len(self.body)-1 last=len(self.body)-1
if was_on_end: if was_on_end:
self.set_focus(last,'above') self.set_focus(last,'above')
   
class Input(FocusMixin, urwid.Edit): class Input(FocusMixin, urwid.Edit):
signals=['line_entered'] signals=['line_entered']
def __init__(self, got_focus=None): def __init__(self, got_focus=None):
urwid.Edit.__init__(self) urwid.Edit.__init__(self)
self.history=deque(maxlen=1000) self.history=deque(maxlen=1000)
self._history_index=-1 self._history_index=-1
self._got_focus=got_focus self._got_focus=got_focus
def keypress(self, size, key): def keypress(self, size, key):
if key=='enter': if key=='enter':
line=self.edit_text.strip() line=self.edit_text.strip()
if line: if line:
urwid.emit_signal(self,'line_entered', line) urwid.emit_signal(self,'line_entered', line)
self.history.append(line) self.history.append(line)
self._history_index=len(self.history) self._history_index=len(self.history)
self.edit_text=u'' self.edit_text=u''
if key=='up': if key=='up':
self._history_index-=1 self._history_index-=1
if self._history_index< 0: if self._history_index< 0:
self._history_index= 0 self._history_index= 0
else: else:
self.edit_text=self.history[self._history_index] self.edit_text=self.history[self._history_index]
if key=='down': if key=='down':
self._history_index+=1 self._history_index+=1
if self._history_index>=len(self.history): if self._history_index>=len(self.history):
self._history_index=len(self.history) self._history_index=len(self.history)
self.edit_text=u'' self.edit_text=u''
else: else:
self.edit_text=self.history[self._history_index] self.edit_text=self.history[self._history_index]
else: else:
urwid.Edit.keypress(self, size, key) urwid.Edit.keypress(self, size, key)
   
   
class Commander(urwid.Frame): class Commander(urwid.Frame):
""" Simple terminal UI with command input on bottom line and display frame above """ Simple terminal UI with command input on bottom line and display frame above
similar to chat client etc. similar to chat client etc.
Initialize with your Command instance to execute commands Initialize with your Command instance to execute commands
and the start main loop Commander.loop(). and the start main loop Commander.loop().
You can also asynchronously output messages with Commander.output('message') """ You can also asynchronously output messages with Commander.output('message') """
   
class Exit(object): class Exit(object):
pass pass
PALLETE=[('reversed', urwid.BLACK, urwid.LIGHT_GRAY), PALLETE=[('reversed', urwid.BLACK, urwid.LIGHT_GRAY),
('normal', urwid.LIGHT_GRAY, urwid.BLACK), ('normal', urwid.LIGHT_GRAY, urwid.BLACK),
('error', urwid.LIGHT_RED, urwid.BLACK), ('error', urwid.LIGHT_RED, urwid.BLACK),
('green', urwid.DARK_GREEN, urwid.BLACK), ('green', urwid.DARK_GREEN, urwid.BLACK),
('blue', urwid.LIGHT_BLUE, urwid.BLACK), ('blue', urwid.LIGHT_BLUE, urwid.BLACK),
('magenta', urwid.DARK_MAGENTA, urwid.BLACK), ('magenta', urwid.DARK_MAGENTA, urwid.BLACK),
('yellow', urwid.YELLOW, urwid.BLACK), ('yellow', urwid.YELLOW, urwid.BLACK),
('cyan', urwid.LIGHT_CYAN, urwid.BLACK), ('cyan', urwid.LIGHT_CYAN, urwid.BLACK),
('brown', urwid.BROWN, urwid.BLACK), ('brown', urwid.BROWN, urwid.BLACK),
] ]
def __init__(self, title, command_caption='Message: (Tab to switch focus to upper frame, where you can scroll text)', cmd_cb=None, max_size=1000): def __init__(self, title, command_caption='Message: (Tab to switch focus to upper frame, where you can scroll text)', cmd_cb=None, max_size=1000):
self.header=urwid.Text(title) self.header=urwid.Text(title)
self.model=urwid.SimpleListWalker([]) self.model=urwid.SimpleListWalker([])
self.body=ListView(self.model, lambda: self._update_focus(False), max_size=max_size ) self.body=ListView(self.model, lambda: self._update_focus(False), max_size=max_size )
self.input=Input(lambda: self._update_focus(True)) self.input=Input(lambda: self._update_focus(True))
foot=urwid.Pile([urwid.AttrMap(urwid.Text(command_caption), 'reversed'), foot=urwid.Pile([urwid.AttrMap(urwid.Text(command_caption), 'reversed'),
urwid.AttrMap(self.input,'normal')]) urwid.AttrMap(self.input,'normal')])
urwid.Frame.__init__(self, urwid.Frame.__init__(self,
urwid.AttrWrap(self.body, 'normal'), urwid.AttrWrap(self.body, 'normal'),
urwid.AttrWrap(self.header, 'reversed'), urwid.AttrWrap(self.header, 'reversed'),
foot) foot)
self.set_focus_path(['footer',1]) self.set_focus_path(['footer',1])
self._focus=True self._focus=True
urwid.connect_signal(self.input,'line_entered',self.on_line_entered) urwid.connect_signal(self.input,'line_entered',self.on_line_entered)
self._cmd=cmd_cb self._cmd=cmd_cb
self._output_styles=[s[0] for s in self.PALLETE] self._output_styles=[s[0] for s in self.PALLETE]
self.eloop=None self.eloop=None
def loop(self, handle_mouse=False): def loop(self, handle_mouse=False):
self.eloop=urwid.MainLoop(self, self.PALLETE, handle_mouse=handle_mouse) self.eloop=urwid.MainLoop(self, self.PALLETE, handle_mouse=handle_mouse)
self._eloop_thread=threading.current_thread() self._eloop_thread=threading.current_thread()
self.eloop.run() self.eloop.run()
def on_line_entered(self,line): def on_line_entered(self,line):
if self._cmd: if self._cmd:
try: try:
res = self._cmd(line) res = self._cmd(line)
except Exception,e: except Exception,e:
self.output('Error: %s'%e, 'error') self.output('Error: %s'%e, 'error')
return return
if res==Commander.Exit: if res==Commander.Exit:
raise urwid.ExitMainLoop() raise urwid.ExitMainLoop()
elif res: elif res:
self.output(str(res)) self.output(str(res))
else: else:
if line in ('q','quit','exit'): if line in ('q','quit','exit'):
raise urwid.ExitMainLoop() raise urwid.ExitMainLoop()
else: else:
self.output(line) self.output(line)
def output(self, line, style=None): def output(self, line, style=None):
if style and style in self._output_styles: if style and style in self._output_styles:
line=(style,line) line=(style,line)
self.body.add(line) self.body.add(line)
#since output could be called asynchronously form other threads we need to refresh screen in these cases #since output could be called asynchronously form other threads we need to refresh screen in these cases
if self.eloop and self._eloop_thread != threading.current_thread(): if self.eloop and self._eloop_thread != threading.current_thread():
self.eloop.draw_screen() self.eloop.draw_screen()
def _update_focus(self, focus): def _update_focus(self, focus):
self._focus=focus self._focus=focus
def switch_focus(self): def switch_focus(self):
if self._focus: if self._focus:
self.set_focus('body') self.set_focus('body')
self._focus=False self._focus=False
else: else:
self.set_focus_path(['footer',1]) self.set_focus_path(['footer',1])
self._focus=True self._focus=True
def keypress(self, size, key): def keypress(self, size, key):
if key=='tab': if key=='tab':
self.switch_focus() self.switch_focus()
return urwid.Frame.keypress(self, size, key) return urwid.Frame.keypress(self, size, key)
   
   
def handle_cmdline_opts(argv,msg): def handle_cmdline_opts(argv,msg):
''' Process any commandline options ''' Process any commandline options
''' '''
if argv[-1][0:2] == 'ht': if argv[-1][0:2] == 'ht':
msg.server=argv[-1] msg.server=argv[-1]
if "--verify" in argv: if "--verify" in argv:
msg.verifycert = True msg.verifycert = True
   
return msg return msg
   
if __name__=='__main__': if __name__=='__main__':
msg = msgHandler() msg = msgHandler()
if len(sys.argv) > 1: if len(sys.argv) > 1:
msg = handle_cmdline_opts(sys.argv,msg) msg = handle_cmdline_opts(sys.argv,msg)
class TestCmd(Command): class TestCmd(Command):
def do_echo(self, *args): def do_echo(self, *args):
'''echo - Just echos all arguments''' '''echo - Just echos all arguments'''
return ' '.join(args) return ' '.join(args)
def do_raise(self, *args): def do_raise(self, *args):
raise Exception('Some Error') raise Exception('Some Error')
c=Commander('LocalChat', cmd_cb=TestCmd()) c=Commander('LocalChat', cmd_cb=TestCmd())
#Test asynch output - e.g. comming from different thread #Test asynch output - e.g. comming from different thread
import time import time
def run(): def run():
state=1 state=1
while True: while True:
time.sleep(1) time.sleep(1)
m = msg.pollForMessage() m = msg.pollForMessage()
if m == "BROKENLINK": if m == "BROKENLINK":
if state == 1: if state == 1:
c.output("Server went away", 'Red') c.output("Server went away", 'Red')
state = 0 state = 0
continue continue
if m: if m:
state = 1 state = 1
for i in m: for i in m:
c.output(i[1], i[0]) c.output(i[1], i[0])
t=Thread(target=run) t=Thread(target=run)
t.daemon=True t.daemon=True
t.start() t.start()
#start main loop #start main loop
c.loop() c.loop()
   
file:b/client/README.md (new)
  # LocalChat Client
 
 
  ## About
 
  Localchat is a simple and lightweight chat application. It's primary purpose is to provide a means to have a multi-user Off-The-Record transient chat, minimising the likelihood that anyone but the chat participants has even a record that the chat took place.
 
  This client uses basic End To End encryption (currently using PGP as the encryption mechanism), and the server holds encrypted payloads in memory only (to ensure the ciphertext doesn't end up captured on the hosting provider's SAN for a time).
 
  The default behaviour is a Multi-User Chat (MUC), however direct messaging between participants within a room is also supported.
 
 
 
 
  ## Dependancies
 
  The following Python modules are required (other client dependancies should all already be present so long as you have python installed)
 
  * urllib2
  * urwid
  * gnupg
 
 
 
 
 
  ## Usage Instructions
 
  Commands are IRC like:
 
  /help Print Usage information
 
  /join [room] [password] [username] Join a room
  /leave Leave current room
  /room create [roomname] [roompass] [admin user] New room management
 
 
  /room invite [user] Invite a user into the current room
  /me [string] Send an 'action' instead of a message
  /msg [user] message Send a direct message to [user]
 
  Room Admin commands:
 
  /kick [user] Kick a user out of the room (they can return)
  /ban [user] Kick a user out and disinvite them (they cannot return)
  /room close [roompass] Kick all users out and close the room
 
 
  /exit Quit the client
 
 
 
  ## Commandline Arguments
 
  The client takes a limited number of commandline arguments. By default, none are needed, but depending on your deployment methodology, some of the following may be required.
 
 
  ./LocalChatClient.py [--verify] [server]
 
 
  When `--verify` is present, SSL certificate verification will be enabled (which means the server must present a certificate trusted by your system and valid for the server's address. By default that's not the case).
 
  If specified, `server` should be the last argument and must be of the format `https://[servername/ip[:port]]/[path]` (port is optional, default is `8090`).
 
  See the main [README](../README.md) for examples of deployments where these flags may be required.
 
 
 
 
  ## Client Message API
 
  To send and receive messages, the client uses the [Server API](../docs/server-api.md), however, messages are contained within an encrypted payload that the server cannot see into. In order to be compatible with this client, message payloads need to be formed as a JSON encapsulated string with the following structure
 
  {
  "text":"[message text]",
  "verb":[message verb]
  }
 
  Message verbs were defined in [LOC-16](https://projects.bentasker.co.uk/jira_projects/browse/LOC-16.html). The default is `say` and any unrecognised verbs will be treated as such - an ordinary message.
 
  The verb `do` will cause the client to print a `/me` message:
 
  ** ben writes documentation **
 
  Once the JSON string has been generated, it should be PGP encrypted using the room key as a passphrase. The client uses Symmetric AES-256 encryption when encrypting, but will support decrypting anything that the Python PGP bindings support.
Server API Server API
============ ============
   
All requests to the server are placed with the `POST` method, using a JSON request body All requests to the server are placed with the `POST` method, using a JSON request body
   
The basic structure is The basic structure is
   
{ {
"action":"[api action]", "action":"[api action]",
"payload": [json payload] "payload": [json payload]
} }
   
Which, if successful, will result in a `HTTP 200` and a JSON encapsulated response body. Which, if successful, will result in a `HTTP 200` and a JSON encapsulated response body.
   
  ----
   
Response Codes Response Codes
--------------- ---------------
   
The following HTTP statuses may be returned The following HTTP statuses may be returned
   
* `200` - Request was successful * `200` - Request was successful
* `400` - Invalid request * `400` - Invalid request
* `403` - Authentication Invalid/Permission Denied * `403` - Authentication Invalid/Permission Denied
* `500` - Server had an issue * `500` - Server had an issue
   
  ----
   
  Authentication
  ---------------
   
  Authentication is performed on a per-room basis, so certain calls (such as `createRoom`) are currently entirely unauthenticated (a form of auth may be introduced for those later).
   
  User's are required to authenticate themselves (via password) when they attempt to enter a room with a `joinRoom` call. If that call is successful then a session key will be provided in the response. This must be embedded into every payload (as attribute `sesskey`) after that.
   
  The server uses `bcrypt` for storing authentication credentials.
   
  ----
   
Supported Actions Supported Actions
------------------- -------------------
   
   
  ### banUser
### PollMsg  
  Can only be called by the room admin, kicks a user out of the room, and does not allow them to return (unless re-invited)
   
  {
  "action":"banUser",
  "payload": {
  "roomName": "[name of room]",
  "user": user,
  "kick": [user to kick],
  "sesskey": sessionkey
  }
  }
   
  *Response*
   
  If successful, the value of `status` will be `ok` and the kicked user's session will be terminated and their user record removed. A message will be pushed to the room by `SYSTEM` to note that that user has been kicked and banned.
   
  If the requesting user lacks the privileges to kick a user, `SYSTEM` will push a message to warn that `user` tried to ban `tokick`.
   
  ----
   
  ### closeRoom
   
  This can only be successfully called by the room's owner. `closeRoom` will close the current room, remove all associated messages, sessions and user accounts.
   
   
  {
  "action":"closeRoom",
  "payload": {
  "roomName": "[name of room]",
  "user": user,
  "sesskey": sessionkey
  }
  }
   
  *Response*
   
  If successful, the value of `status` will be `ok`.
   
  Rooms should always be closed when they are no longer required. The server will (by default) automatically close inactive rooms after a period, but this is intended as a safety net (in case the admin gets disconnected and cannot reconnect for some reason).
   
  ----
   
  ### createRoom
   
  This is used to create a room. Calls are currently unauthenticated because user accounts are tied to a room rather than existing in a global namespace (this is done to limit the risk of a list of possible users sitting on the server until they're likely to be needed).
   
  {
  "action":"createRoom",
  "payload": {
  "roomName": "[name of room]",
  "owner": [name of user],
  "pass": password
  }
  }
   
  When creating a room, we specify the username of the user who will be the room owner/admin, and the password they will use to join the room. In the background this will set up the requisite user account.
   
   
  *Response*
   
  If the room is successfully created, the value of `status` will be `ok`. The roomname will also be confirmed back as the attribute `name`.
   
  The user will then need to call `joinRoom` in order to enter the room (the client could do this automatically, but the current client version does not).
   
  ----
   
  ### inviteUser
   
  Used to invite a user into the current room. Can be called by admin's and users alike (`SYSTEM` will push a message into the room to notify others of the invite)
   
  {
  "action":"inviteUser",
  "payload": {
  "roomName": "[name of room]",
  "user": user,
  "invite": [user to invite],
  "pass": [new users pass]
  "sesskey": sessionkey
  }
  }
   
  When inviting a user, we need to specify their username as `invite` and their authentication password as `pass`
   
  *Response*
   
  If successful, the value of `status` will be `ok`. `SYSTEM` will also push a message into the room that `user` invited `invite`
   
  ----
   
  ### joinRoom
   
  Used to enter a room.
   
  When the user was invited to the room, a password will have been set for them (the supplied client currently auto-generates that password), it will need to be provided for the user to authenticate.
   
  {
  "action":"joinRoom",
  "payload": {
  "roomName": "[name of room]",
  "userpass": password,
  "user": user
  }
  }
   
  *Response*
   
  If authentication is successful, the response will include a number of details that the client will need to take note of for future requests
   
  {
  "status": "ok",
  "last": last,
  "session": sessionkey,
  "syskey": systemkey
  }
   
  The value `last` is the ID of the latest message in the room you've just joined. It should be included in `pollMsg` calls.
   
  The value `sessionkey` is a server generated sessionkey. It must be included in the payload of all future requests (it's used to help authenticate those requests).
   
  The value `syskey` is a decryption passphrase. The server's internal user `SYSTEM` will E2E encrypt any messages it pushes into rooms, this is the key you should use to decrypt those messages.
   
  ----
   
  ### kickUser
   
  Can only be called by the room admin, kicks a user out of the room (but allows them to return). Useful where you think a user's session has hung (for example)
   
  {
  "action":"kickUser",
  "payload": {
  "roomName": "[name of room]",
  "user": user,
  "kick": [user to kick],
  "sesskey": sessionkey
  }
  }
   
  *Response*
   
  If successful, the value of `status` will be `ok` and the kicked user's session will be terminated. A message will be pushed to the room by `SYSTEM` to note that that user has been kicked.
   
  If the requesting user lacks the privileges to kick a user, `SYSTEM` will push a message to warn that `user` tried to kick `tokick`.
   
  ----
   
  ### leaveRoom
   
  Used to leave the current room
   
  {
  "action":"leaveRoom",
  "payload": {
  "roomName": "[name of room]",
  "user": user,
  "sesskey": sessionkey
  }
  }
   
  *Response*
   
  If successful, the value of `status` will be `ok`. Client's should then disable any automated message polling they are running.
   
  ----
   
  ### pollMsg
   
Used to check whether there are any messages since the last the client received. It will return room-wide messages and direct messages addressed to the polling user. Used to check whether there are any messages since the last the client received. It will return room-wide messages and direct messages addressed to the polling user.
   
Client provides the ID of the last message they've received, as well as the room they're polling. Where this is the first request, `last` should be 0 Client provides the ID of the last message they've received, as well as the room they're polling. Where this is the first request, `last` should be 0
   
{ {
"action":"pollMsg", "action":"pollMsg",
"payload": { "payload": {
"roomName": "[name of room]", "roomName": "[name of room]",
"mylast": "[last]", "mylast": "[last]",
"user": user, "user": user,
"sesskey": sessionkey "sesskey": sessionkey
} }
} }
   
   
*Response* *Response*
   
If there are no messages, then the response status will be `unchanged`. If there are no messages, then the response status will be `unchanged`.
If a specific error is being returned, the response status will be `errmessage` - you should disable any automatic polling and the print the content of `text` If a specific error is being returned, the response status will be `errmessage` - you should disable any automatic polling and the print the content of `text`
   
If status is `updated` then messages have been returned: If status is `updated` then messages have been returned:
   
{ {
"status": "updated", "status": "updated",
"messages" : [ "messages" : [
[msgid,msgtext,timestamp,fromuser,touser], [msgid,msgtext,timestamp,fromuser,touser],
[msgid,msgtext,timestamp,fromuser,touser] [msgid,msgtext,timestamp,fromuser,touser]
] ]
   
} }
   
Once the client has processed the received messages, the value of `last` in the next `pollMsg` request should be the maximum ID in the resultset. Once the client has processed the received messages, the value of `last` in the next `pollMsg` request should be the maximum ID in the resultset.
   
  ----
   
  ### sendDirectMsg
### sendMsg  
  Used to send a message to a specific user within the room. The message will not be visible to other room occupants.
Used to send a message to the entire room  
  {
{ "action":"sendDirectMsg",
"action":"sendMsg",  
"payload": { "payload": {
"roomName": "[name of room]", "roomName": "[name of room]",
"msg": "[message payload]", "msg": "[message payload]",
  "to": recipient
"user": user, "user": user,
"sesskey": sessionkey "sesskey": sessionkey
} }
} }
   
The message payload is encrypted and base64 encoded by the client, so the server only sees a base64 string. The message payload is encrypted and base64 encoded by the client, so the server only sees a base64 string.
   
However, to keep compatability with the supplied client, your message payload (prior to encryption) should be a JSON encapsulated string of the format However, to keep compatability with the supplied client, your message payload (prior to encryption) should be a JSON encapsulated string of the format
   
msg = { msg = {
'user': user, 'user': user,
'text': msg, 'text': msg,
"verb": verb "verb": verb
} }
   
This should then be PGP encrypted using the room passphrase, and then base64 encoded for embedding into the API payload. This should then be PGP encrypted using the room passphrase, and then base64 encoded for embedding into the API payload.
Where `verb` is one of `do` or `say` (other values will be treated as `say` by the supplied client). Where `verb` is one of `do` or `say` (other values will be treated as `say` by the supplied client).
   
   
*Response* *Response*
   
If message sending is successful, the response will contain `status:"ok"` If message sending is successful, the response will contain `status:"ok"`
   
  ----
   
  ### sendMsg
   
  Used to send a message to the entire room
   
  {
  "action":"sendMsg",
  "payload": {
  "roomName": "[name of room]",
  "msg": "[message payload]",
  "user": user,
  "sesskey": sessionkey
  }
  }
   
  The message payload is encrypted and base64 encoded by the client, so the server only sees a base64 string.
   
  However, to keep compatability with the supplied client, your message payload (prior to encryption) should be a JSON encapsulated string of the format
   
  msg = {
  'user': user,
  'text': msg,
  "verb": verb
  }
   
  This should then be PGP encrypted using the room passphrase, and then base64 encoded for embedding into the API payload.
   
  Where `verb` is one of `do` or `say` (other values will be treated as `say` by the supplied client).
   
   
  *Response*
   
  If message sending is successful, the response will contain `status:"ok"`
   
   
  ----
   
   
   
   
#!/usr/bin/env python #!/usr/bin/env python
# #
# #
# LocalChat Server Script # LocalChat Server Script
# #
# #
# apt-get install: # apt-get install:
# python-flask # python-flask
# python-openssl # python-openssl
# python-bcrypt # python-bcrypt
# #
   
from flask import Flask from flask import Flask
from flask import request, make_response from flask import request, make_response
   
import thread import thread
import urllib2 import urllib2
import ssl import ssl
import sqlite3 import sqlite3
import time import time
import os import os
import json import json
import bcrypt import bcrypt
import random import random
import string import string
import gnupg import gnupg
import sys import sys
   
   
app = Flask(__name__) app = Flask(__name__)
   
   
   
@app.route('/', defaults={'path': ''},methods=["POST"]) @app.route('/', defaults={'path': ''},methods=["POST"])
@app.route('/<path:path>',methods=["POST"]) @app.route('/<path:path>',methods=["POST"])
def index(path): def index(path):
print "%.6f Request start" % (time.time()) # DEBUGONLY print "%.6f Request start" % (time.time()) # DEBUGONLY
reqdata = request.get_data() reqdata = request.get_data()
try: try:
reqjson = json.loads(reqdata) reqjson = json.loads(reqdata)
except: except:
return make_response("",400) return make_response("",400)
a = msghandler.processSubmission(reqjson) a = msghandler.processSubmission(reqjson)
   
# Check the status # Check the status
if a in [400,403,500]: if a in [400,403,500]:
response = make_response("",a) response = make_response("",a)
return response return response
return json.dumps(a) return json.dumps(a)
   
   
class MsgHandler(object): class MsgHandler(object):
   
   
def __init__(self,cronpass,bindpoint,purgeinterval,closethresh,testingmode): def __init__(self,cronpass,bindpoint,purgeinterval,closethresh,testingmode):
self.conn = False self.conn = False
self.cursor = False self.cursor = False
# Generate a key for encryption of SYSTEM messages (LOC-13) # Generate a key for encryption of SYSTEM messages (LOC-13)
self.syskey = self.genpassw(16) self.syskey = self.genpassw(16)
self.gpg = gnupg.GPG() self.gpg = gnupg.GPG()
self.cronpass = cronpass self.cronpass = cronpass
self.bindpoint = bindpoint self.bindpoint = bindpoint
self.purgeInterval = purgeinterval self.purgeInterval = purgeinterval
self.roomCloseThresh = closethresh self.roomCloseThresh = closethresh
self.testingMode = testingmode self.testingMode = testingmode
if self.testingMode: if self.testingMode:
print "WARNING - Messages will be written to disk" print "WARNING - Messages will be written to disk"
   
   
   
def createDB(self): def createDB(self):
''' Create the in-memory database ready for use ''' Create the in-memory database ready for use
''' '''
dbpath=':memory:' dbpath=':memory:'
if self.testingMode: if self.testingMode:
# LOC-15 - allow DB to written to disk in test mode # LOC-15 - allow DB to written to disk in test mode
dbpath = "%s/localchat-testing.db" % (os.getcwd(),) dbpath = "%s/localchat-testing.db" % (os.getcwd(),)
self.conn = sqlite3.connect(dbpath) self.conn = sqlite3.connect(dbpath)
self.cursor = self.conn.cursor() self.cursor = self.conn.cursor()
sql = """ sql = """
DROP TABLE IF EXISTS rooms; DROP TABLE IF EXISTS rooms;
CREATE TABLE rooms ( CREATE TABLE rooms (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
name TEXT NOT NULL UNIQUE, name TEXT NOT NULL UNIQUE,
owner TEXT NOT NULL, owner TEXT NOT NULL,
lastactivity INTEGER DEFAULT 0 lastactivity INTEGER DEFAULT 0
); );
DROP TABLE IF EXISTS messages; DROP TABLE IF EXISTS messages;
CREATE TABLE messages ( CREATE TABLE messages (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
ts INTEGER NOT NULL, ts INTEGER NOT NULL,
room INTEGER NOT NULL, room INTEGER NOT NULL,
user NOT NULL, user NOT NULL,
touser TEXT DEFAULT '0', touser TEXT DEFAULT '0',
msg TEXT NOT NULL msg TEXT NOT NULL
); );
DROP TABLE IF EXISTS users; DROP TABLE IF EXISTS users;
CREATE TABLE users ( CREATE TABLE users (
username TEXT NOT NULL, username TEXT NOT NULL,
room INTEGER NOT NULL, room INTEGER NOT NULL,
active INTEGER DEFAULT 0, active INTEGER DEFAULT 0,
passhash TEXT NOT NULL, passhash TEXT NOT NULL,
PRIMARY KEY (username,room) PRIMARY KEY (username,room)
); );
DROP TABLE IF EXISTS sessions; DROP TABLE IF EXISTS sessions;
CREATE TABLE sessions ( CREATE TABLE sessions (
username TEXT NOT NULL, username TEXT NOT NULL,
sesskey TEXT NOT NULL, sesskey TEXT NOT NULL,
PRIMARY KEY(sesskey) PRIMARY KEY(sesskey)
); );
DROP TABLE IF EXISTS failuremsgs; DROP TABLE IF EXISTS failuremsgs;
CREATE TABLE failuremsgs ( CREATE TABLE failuremsgs (
username TEXT NOT NULL, username TEXT NOT NULL,
room INTEGER NOT NULL, room INTEGER NOT NULL,
expires INTEGER NOT NULL, expires INTEGER NOT NULL,
msg TEXT NOT NULL, msg TEXT NOT NULL,
PRIMARY KEY (username,room) PRIMARY KEY (username,room)
); );
""" """
self.conn.executescript(sql) self.conn.executescript(sql)
# We also need to start the scheduler thread (LOC-6) # We also need to start the scheduler thread (LOC-6)
thread.start_new_thread(taskScheduler,(self.cronpass,self.bindpoint)) thread.start_new_thread(taskScheduler,(self.cronpass,self.bindpoint))
   
   
   
def processSubmission(self,reqjson): def processSubmission(self,reqjson):
''' Process an incoming request and route it to ''' Process an incoming request and route it to
the correct function the correct function
''' '''
if not self.conn or not self.cursor: if not self.conn or not self.cursor:
self.createDB() self.createDB()
print reqjson print reqjson
if "action" in reqjson and reqjson['action'] == 'schedulerTrigger': if "action" in reqjson and reqjson['action'] == 'schedulerTrigger':
return self.triggerClean(reqjson) return self.triggerClean(reqjson)
if "action" not in reqjson or "payload" not in reqjson: if "action" not in reqjson or "payload" not in reqjson:
return self.returnFailure(400) return self.returnFailure(400)
# Decrypt the payload # Decrypt the payload
reqjson['payload'] = self.decrypt(reqjson['payload']) reqjson['payload'] = self.decrypt(reqjson['payload'])
try: try:
reqjson['payload'] = json.loads(reqjson['payload']) reqjson['payload'] = json.loads(reqjson['payload'])
except: except:
return self.returnFailure(400) return self.returnFailure(400)
if reqjson['action'] == "createRoom": if reqjson['action'] == "createRoom":
return self.createRoom(reqjson) return self.createRoom(reqjson)
if reqjson['action'] == "closeRoom": if reqjson['action'] == "closeRoom":
return self.closeRoom(reqjson) return self.closeRoom(reqjson)
elif reqjson['action'] == "joinRoom": elif reqjson['action'] == "joinRoom":
return self.processjoinRoom(reqjson) return self.processjoinRoom(reqjson)
elif reqjson['action'] == "leaveRoom": elif reqjson['action'] == "leaveRoom":
return self.processleaveRoom(reqjson) return self.processleaveRoom(reqjson)
   
elif reqjson['action'] == "banUser": elif reqjson['action'] == "banUser":
return self.kickUser(reqjson,True) return self.kickUser(reqjson,True)
elif reqjson['action'] == "inviteUser": elif reqjson['action'] == "inviteUser":
return self.inviteUser(reqjson) return self.inviteUser(reqjson)
elif reqjson['action'] == "kickUser": elif reqjson['action'] == "kickUser":
return self.kickUser(reqjson,False) return self.kickUser(reqjson,False)
elif reqjson['action'] == 'sendDirectMsg': elif reqjson['action'] == 'sendDirectMsg':
return self.sendDirectMsg(reqjson) return self.sendDirectMsg(reqjson)
elif reqjson['action'] == 'sendMsg': elif reqjson['action'] == 'sendMsg':
return self.sendMsg(reqjson) return self.sendMsg(reqjson)
elif reqjson['action'] == 'pollMsg': elif reqjson['action'] == 'pollMsg':
return self.fetchMsgs(reqjson) return self.fetchMsgs(reqjson)
   
   
   
   
   
def decrypt(self,msg): def decrypt(self,msg):
''' This is currently just a placeholder ''' This is currently just a placeholder
Will be updated later Will be updated later
''' '''
return msg return msg
   
   
   
def createRoom(self,reqjson): def createRoom(self,reqjson):
''' '''
Payload should contain a JSON object consisting of Payload should contain a JSON object consisting of
roomName roomName
owner owner
passhash passhash
e.g. e.g.
curl -v -X POST http://127.0.0.1:8090/ -H "Content-Type: application/json" --data '{"action":"createRoom","payload":"{ curl -v -X POST http://127.0.0.1:8090/ -H "Content-Type: application/json" --data '{"action":"createRoom","payload":"{
\"roomName\":\"BenTest\", \"roomName\":\"BenTest\",
\"owner\":\"ben\", \"owner\":\"ben\",
\"passhash\":\"abcdefg\" \"passhash\":\"abcdefg\"
}" }"
}' }'
''' '''
# Validate the request # Validate the request
# #
# All validation snippets will change to this format soon # All validation snippets will change to this format soon
required = ['roomName','owner','pass'] required = ['roomName','owner','pass']
for i in required: for i in required:
if i not in reqjson['payload']: if i not in reqjson['payload']:
return self.returnFailure(400) return self.returnFailure(400)
print "Creating room %s" % (reqjson['payload']) print "Creating room %s" % (reqjson['payload'])
# Create a tuple for sqlite3 # Create a tuple for sqlite3
t = (reqjson['payload']['roomName'], t = (reqjson['payload']['roomName'],
reqjson['payload']['owner'] reqjson['payload']['owner']
) )
try: try:
self.cursor.execute("INSERT INTO rooms (name,owner) VALUES (?,?)",t) self.cursor.execute("INSERT INTO rooms (name,owner) VALUES (?,?)",t)
roomid = self.cursor.lastrowid roomid = self.cursor.lastrowid
except: except:
# Probably a duplicate name, but we don't want to give the other end a reason anyway # Probably a duplicate name, but we don't want to give the other end a reason anyway
return self.returnFailure(500) return self.returnFailure(500)
# Generate a password hash for the owners password # Generate a password hash for the owners password
passhash = bcrypt.hashpw(reqjson['payload']['pass'].encode('utf-8'),bcrypt.gensalt()) passhash = bcrypt.hashpw(reqjson['payload']['pass'].encode('utf-8'),bcrypt.gensalt())
self.cursor.execute("INSERT INTO users (username,room,passhash) values (?,?,?)",(reqjson['payload']['owner'],roomid,passhash)) self.cursor.execute("INSERT INTO users (username,room,passhash) values (?,?,?)",(reqjson['payload']['owner'],roomid,passhash))
self.conn.commit() self.conn.commit()
return { return {
'status':'ok', 'status':'ok',
'roomid': roomid, 'roomid': roomid,
'name' : reqjson['payload']['roomName'] 'name' : reqjson['payload']['roomName']
} }
   
   
def closeRoom(self,reqjson): def closeRoom(self,reqjson):
''' Close a room. ''' Close a room.
Means we need to Means we need to
- Ban all the users - Ban all the users
- Scrub the message queue - Scrub the message queue
- Remove the room record - Remove the room record
''' '''
if "roomName" not in reqjson['payload'] or "user" not in reqjson['payload']: if "roomName" not in reqjson['payload'] or "user" not in reqjson['payload']:
return self.returnFailure(400) return self.returnFailure(400)
room = self.getRoomID(reqjson['payload']["roomName"]) room = self.getRoomID(reqjson['payload']["roomName"])
if not room: if not room:
return self.returnFailure(400) return self.returnFailure(400)
# Check the requesting user is the admin # Check the requesting user is the admin
self.cursor.execute("SELECT * from rooms where id=? and owner=?",(room,reqjson["payload"]["user"])) self.cursor.execute("SELECT * from rooms where id=? and owner=?",(room,reqjson["payload"]["user"]))
n = self.cursor.fetchone() n = self.cursor.fetchone()
if not n: if not n:
return self.returnFailure(403) return self.returnFailure(403)
   
self.pushSystemMsg("Room has been closed. Buh-Bye",room,'syswarn') self.pushSystemMsg("Room has been closed. Buh-Bye",room,'syswarn')
self.cursor.execute("DELETE FROM users where room=?",(room,)) self.cursor.execute("DELETE FROM users where room=?",(room,))
self.cursor.execute("DELETE FROM rooms where id=?",(room,)) self.cursor.execute("DELETE FROM rooms where id=?",(room,))
self.cursor.execute("DELETE FROM messages where room=?",(room,)) self.cursor.execute("DELETE FROM messages where room=?",(room,))
self.cursor.execute("DELETE FROM sessions where sesskey like ?", (reqjson['payload']["roomName"] + '-%',)) self.cursor.execute("DELETE FROM sessions where sesskey like ?", (reqjson['payload']["roomName"] + '-%',))
self.conn.commit() self.conn.commit()
return { "status" : "ok" } return { "status" : "ok" }
   
   
   
   
   
   
def inviteUser(self,reqjson): def inviteUser(self,reqjson):
''' Link a username into a room ''' Link a username into a room
''' '''
if "roomName" not in reqjson['payload'] or "pass" not in reqjson['payload'] or "invite" not in reqjson['payload']: if "roomName" not in reqjson['payload'] or "pass" not in reqjson['payload'] or "invite" not in reqjson['payload']:
return self.returnFailure(400) return self.returnFailure(400)
room = self.getRoomID(reqjson['payload']["roomName"]) room = self.getRoomID(reqjson['payload']["roomName"])
if not room: if not room:
return self.returnFailure(400) return self.returnFailure(400)
if not self.validateUser(reqjson['payload']): if not self.validateUser(reqjson['payload']):
return self.returnFailure(403,reqjson['payload'],room) return self.returnFailure(403,reqjson['payload'],room)
if reqjson['payload']['invite'] == "SYSTEM": if reqjson['payload']['invite'] == "SYSTEM":
# Push a notification into the group # Push a notification into the group
self.pushSystemMsg("ALERT: User %s tried to invite SYSTEM" % (reqjson['payload']['user']),room,'sysalert') self.pushSystemMsg("ALERT: User %s tried to invite SYSTEM" % (reqjson['payload']['user']),room,'sysalert')
return self.returnFailure(403) return self.returnFailure(403)
# Generate a hash of the submitted password # Generate a hash of the submitted password
passhash = bcrypt.hashpw(reqjson['payload']['pass'].encode('utf-8'),bcrypt.gensalt()) passhash = bcrypt.hashpw(reqjson['payload']['pass'].encode('utf-8'),bcrypt.gensalt())
# Otherwise, link the user in # Otherwise, link the user in
self.cursor.execute("INSERT INTO users (username,room,passhash) values (?,?,?)",(reqjson['payload']['invite'],room,passhash)) self.cursor.execute("INSERT INTO users (username,room,passhash) values (?,?,?)",(reqjson['payload']['invite'],room,passhash))
# Push a notification into the group # Push a notification into the group
self.pushSystemMsg("User %s invited %s to the room" % (reqjson['payload']['user'],reqjson['payload']['invite']),room) self.pushSystemMsg("User %s invited %s to the room" % (reqjson['payload']['user'],reqjson['payload']['invite']),room)
return { return {
"status":'ok' "status":'ok'
} }
   
   
def kickUser(self,reqjson,ban=False): def kickUser(self,reqjson,ban=False):
''' Kick a user out of room ''' Kick a user out of room
Default is just to boot them out, but can also remove their authorisation to enter Default is just to boot them out, but can also remove their authorisation to enter
''' '''
if "roomName" not in reqjson['payload'] or "user" not in reqjson['payload'] or "kick" not in reqjson['payload']: if "roomName" not in reqjson['payload'] or "user" not in reqjson['payload'] or "kick" not in reqjson['payload']:
return self.returnFailure(400) return self.returnFailure(400)
room = self.getRoomID(reqjson['payload']["roomName"]) room = self.getRoomID(reqjson['payload']["roomName"])
if not room: if not room:
return self.returnFailure(400) return self.returnFailure(400)
# Check the requesting user is the admin # Check the requesting user is the admin
self.cursor.execute("SELECT * from rooms where id=? and owner=?",(room,reqjson["payload"]["user"])) self.cursor.execute("SELECT * from rooms where id=? and owner=?",(room,reqjson["payload"]["user"]))
n = self.cursor.fetchone() n = self.cursor.fetchone()
if not n: if not n:
return self.returnFailure(403) return self.returnFailure(403)
   
   
self.cursor.execute("UPDATE users set active=0 where room=? and username=?",(room,reqjson["payload"]["kick"])) self.cursor.execute("UPDATE users set active=0 where room=? and username=?",(room,reqjson["payload"]["kick"]))
# Delete their session # Delete their session
self.cursor.execute("DELETE FROM sessions where username=? and sesskey like ?", (reqjson['payload']['kick'],reqjson['payload']["roomName"] + '-%')) self.cursor.execute("DELETE FROM sessions where username=? and sesskey like ?", (reqjson['payload']['kick'],reqjson['payload']["roomName"] + '-%'))
self.pushSystemMsg("User %s kicked %s from the room" % (reqjson['payload']['user'],reqjson['payload']['kick']),room,'syswarn') self.pushSystemMsg("User %s kicked %s from the room" % (reqjson['payload']['user'],reqjson['payload']['kick']),room,'syswarn')
self.pushFailureMessage(reqjson['payload']['kick'],room,'You have been kicked from the room') self.pushFailureMessage(reqjson['payload']['kick'],room,'You have been kicked from the room')
if ban: if ban:
# If we're banning them, also need to disinvite them # If we're banning them, also need to disinvite them
self.cursor.execute("DELETE from users where room=? and username=?",(room,reqjson["payload"]["kick"])) self.cursor.execute("DELETE from users where room=? and username=?",(room,reqjson["payload"]["kick"]))
self.pushSystemMsg("User %s banned %s from the room" % (reqjson['payload']['user'],reqjson['payload']['kick']),room,'syswarn') self.pushSystemMsg("User %s banned %s from the room" % (reqjson['payload']['user'],reqjson['payload']['kick']),room,'syswarn')
return { "status" : "ok" } return { "status" : "ok" }
   
   
   
def processjoinRoom(self,reqjson): def processjoinRoom(self,reqjson):
''' Process a request from a user to login to a room ''' Process a request from a user to login to a room
Not yet defined the authentication mechanism to use, so that's a TODO Not yet defined the authentication mechanism to use, so that's a TODO
''' '''
   
# Check the required information is present # Check the required information is present
required = ['roomName','user','userpass'] required = ['roomName','user','userpass']
for i in required: for i in required:
if i not in reqjson['payload']: if i not in reqjson['payload']:
return self.returnFailure(400) return self.returnFailure(400)
   
room = self.getRoomID(reqjson['payload']["roomName"]) room = self.getRoomID(reqjson['payload']["roomName"])
if not room: if not room:
return self.returnFailure(400) return self.returnFailure(400)
   
if reqjson["payload"]["user"] == "SYSTEM": if reqjson["payload"]["user"] == "SYSTEM":
return self.returnFailure(403) return self.returnFailure(403)
# Check whether that user is authorised to connect to that room # Check whether that user is authorised to connect to that room
self.cursor.execute("SELECT username, room,passhash from users where username=? and room=?",(reqjson['payload']['user'],room)) self.cursor.execute("SELECT username, room,passhash from users where username=? and room=?",(reqjson['payload']['user'],room))
r = self.cursor.fetchone() r = self.cursor.fetchone()
if not r: if not r:
return { "status": "NOK" } return { "status": "NOK" }
   
# Now we need to verify they've supplied a correct password for that user # Now we need to verify they've supplied a correct password for that user
stored = r[2].encode("utf-8") stored = r[2].encode("utf-8")
if stored != bcrypt.hashpw(reqjson['payload']['userpass'].encode('utf-8'),stored): if stored != bcrypt.hashpw(reqjson['payload']['userpass'].encode('utf-8'),stored):
return self.returnFailure(403) return self.returnFailure(403)
   
# Tidy older messages away. # Tidy older messages away.
# #
# We do this so that a user who joins can't then send a poll with last:0 to retrieve the full history # We do this so that a user who joins can't then send a poll with last:0 to retrieve the full history
# #
# Basically, anything older than 10 seconds should go. Users who were already present will be able # Basically, anything older than 10 seconds should go. Users who were already present will be able
# to scroll up and down in their client anyway # to scroll up and down in their client anyway
self.tidyMsgs(time.time()-10,room) self.tidyMsgs(time.time()-10,room)
   
# Push a message to the room to note that the user joined # Push a message to the room to note that the user joined
msgid = self.pushSystemMsg("User %s joined the room" % (reqjson['payload']['user']),room) msgid = self.pushSystemMsg("User %s joined the room" % (reqjson['payload']['user']),room)
   
   
# If we're in testing mode, push a warning so the new user can see it # If we're in testing mode, push a warning so the new user can see it
if self.testingMode: if self.testingMode:
msgid = self.pushSystemMsg("Server is in testing mode. Messages are being written to disk",room,'syswarn') msgid = self.pushSystemMsg("Server is in testing mode. Messages are being written to disk",room,'syswarn')
   
   
# Check the latest message ID for that room # Check the latest message ID for that room
self.cursor.execute("SELECT id from messages WHERE room=? and id != ? ORDER BY id DESC",(room,msgid)) self.cursor.execute("SELECT id from messages WHERE room=? and id != ? ORDER BY id DESC",(room,msgid))
r = self.cursor.fetchone() r = self.cursor.fetchone()
if not r: if not r:
last = 0 last = 0
else: else:
last = r[0] last = r[0]
# Mark the user as active in the users table # Mark the user as active in the users table
self.cursor.execute("UPDATE users set active=1 where username=? and room=?", (reqjson['payload']['user'],room)) self.cursor.execute("UPDATE users set active=1 where username=? and room=?", (reqjson['payload']['user'],room))
   
# Create a session for the user # Create a session for the user
sesskey = "%s-%s" % (reqjson['payload']["roomName"],self.genSessionKey()) sesskey = "%s-%s" % (reqjson['payload']["roomName"],self.genSessionKey())
self.cursor.execute("INSERT INTO sessions (username,sesskey) values (?,?)", (reqjson['payload']['user'],sesskey)) self.cursor.execute("INSERT INTO sessions (username,sesskey) values (?,?)", (reqjson['payload']['user'],sesskey))
self.conn.commit() self.conn.commit()
return {"status":"ok","last":last,"session":sesskey,"syskey":self.syskey} return {"status":"ok","last":last,"session":sesskey,"syskey":self.syskey}
   
   
def processleaveRoom(self,reqjson): def processleaveRoom(self,reqjson):
''' Process a user's request to leave a room ''' Process a user's request to leave a room
''' '''
if "roomName" not in reqjson['payload'] or "user" not in reqjson['payload']: if "roomName" not in reqjson['payload'] or "user" not in reqjson['payload']:
return self.returnFailure(400) return self.returnFailure(400)
room = self.getRoomID(reqjson['payload']["roomName"]) room = self.getRoomID(reqjson['payload']["roomName"])
if not room: if not room:
return self.returnFailure(400) return self.returnFailure(400)
# Check the user is actually in the room and authorised # Check the user is actually in the room and authorised
if not self.validateUser(reqjson['payload']): if not self.validateUser(reqjson['payload']):
return self.returnFailure(400) return self.returnFailure(400)
# Mark them as not in the room # Mark them as not in the room
self.cursor.execute("UPDATE users set active=0 where username=? and room=?", (reqjson['payload']['user'],room)) self.cursor.execute("UPDATE users set active=0 where username=? and room=?", (reqjson['payload']['user'],room))
self.conn.commit() self.conn.commit()
# Delete their session # Delete their session
self.cursor.execute("DELETE FROM sessions where username=? and sesskey = ?", (reqjson['payload']['user'],reqjson['payload']["sesskey"])) self.cursor.execute("DELETE FROM sessions where username=? and sesskey = ?", (reqjson['payload']['user'],reqjson['payload']["sesskey"]))
# Push a message to the room to note they left # Push a message to the room to note they left
self.pushSystemMsg("User %s left the room" % (reqjson['payload']['user']),room) self.pushSystemMsg("User %s left the room" % (reqjson['payload']['user']),room)
return {"status":"ok"} return {"status":"ok"}
   
   
def sendMsg(self,reqjson): def sendMsg(self,reqjson):
''' Push a message into a room ''' Push a message into a room
curl -v -X POST http://127.0.0.1:8090/ -H "Content-Type: application/json" --data '{"action":"sendMsg","payload":"{\"roomName\":\"BenTest\", \"msg\":\"ENCRYPTED-DATA\",\"user\":\"ben2\"}"}' curl -v -X POST http://127.0.0.1:8090/ -H "Content-Type: application/json" --data '{"action":"sendMsg","payload":"{\"roomName\":\"BenTest\", \"msg\":\"ENCRYPTED-DATA\",\"user\":\"ben2\"}"}'
''' '''
if not self.validateUser(reqjson['payload']): if not self.validateUser(reqjson['payload']):
return self.returnFailure(403) return self.returnFailure(403)
   
if "roomName" not in reqjson['payload'] or "msg" not in reqjson['payload']: if "roomName" not in reqjson['payload'] or "msg" not in reqjson['payload']:
return self.returnFailure(400) return self.returnFailure(400)
room = self.getRoomID(reqjson['payload']["roomName"]) room = self.getRoomID(reqjson['payload']["roomName"])
print room print room
if not room: if not room:
return self.returnFailure(400) return self.returnFailure(400)
   
   
self.cursor.execute("INSERT INTO messages (ts,room,msg,user) VALUES (?,?,?,?)",(time.time(),room,reqjson['payload']['msg'],reqjson['payload']['user'])) self.cursor.execute("INSERT INTO messages (ts,room,msg,user) VALUES (?,?,?,?)",(time.time(),room,reqjson['payload']['msg'],reqjson['payload']['user']))
msgid = self.cursor.lastrowid msgid = self.cursor.lastrowid
self.conn.commit() self.conn.commit()
# Check the latest message ID for that room # Check the latest message ID for that room
self.cursor.execute("SELECT id from messages WHERE room=? and id != ? ORDER BY id DESC",(room,msgid)) self.cursor.execute("SELECT id from messages WHERE room=? and id != ? ORDER BY id DESC",(room,msgid))
r = self.cursor.fetchone() r = self.cursor.fetchone()
if not r: if not r:
last = 0 last = 0
else: else:
last = r[0] last = r[0]
   
# Update the last activity field in the DB # Update the last activity field in the DB
# See LOC-11 # See LOC-11
self.cursor.execute("UPDATE rooms set lastactivity=? where id=?",(time.time(),room)) self.cursor.execute("UPDATE rooms set lastactivity=? where id=?",(time.time(),room))
self.conn.commit() self.conn.commit()
return { return {
"status" : "ok", "status" : "ok",
"msgid" : msgid, "msgid" : msgid,
"last" : last "last" : last
} }
   
   
   
def sendDirectMsg(self,reqjson): def sendDirectMsg(self,reqjson):
''' Push a message to a user in the same room ''' Push a message to a user in the same room
''' '''
if not self.validateUser(reqjson['payload']): if not self.validateUser(reqjson['payload']):
return self.returnFailure(403) return self.returnFailure(403)
required = ['roomName','msg','to'] required = ['roomName','msg','to']
for i in required: for i in required:
if i not in reqjson['payload']: if i not in reqjson['payload']:
return self.returnFailure(400) return self.returnFailure(400)
room = self.getRoomID(reqjson['payload']["roomName"]) room = self.getRoomID(reqjson['payload']["roomName"])
print room print room
if not room: if not room:
return self.returnFailure(400) return self.returnFailure(400)
   
# Check the other user is in the room and active # Check the other user is in the room and active
self.cursor.execute("SELECT username from users where username=? and room=? and active=1",(reqjson['payload']['to'],room)) self.cursor.execute("SELECT username from users where username=? and room=? and active=1",(reqjson['payload']['to'],room))
r = self.cursor.fetchone() r = self.cursor.fetchone()
if not r: if not r:
return self.returnFailure(400) return self.returnFailure(400)
self.cursor.execute("INSERT INTO messages (ts,room,msg,user,touser) VALUES (?,?,?,?,?)",(time.time(),room,reqjson['payload']['msg'],reqjson['payload']['user'],reqjson['payload']['to'])) self.cursor.execute("INSERT INTO messages (ts,room,msg,user,touser) VALUES (?,?,?,?,?)",(time.time(),room,reqjson['payload']['msg'],reqjson['payload']['user'],reqjson['payload']['to']))
msgid = self.cursor.lastrowid msgid = self.cursor.lastrowid
self.conn.commit() self.conn.commit()
# Update the last activity field in the DB # Update the last activity field in the DB
# See LOC-11 # See LOC-11
self.cursor.execute("UPDATE rooms set lastactivity=? where id=?",(time.time(),room)) self.cursor.execute("UPDATE rooms set lastactivity=? where id=?",(time.time(),room))
self.conn.commit() self.conn.commit()
# Check the latest message ID for that room # Check the latest message ID for that room
self.cursor.execute("SELECT id from messages WHERE room=? and id != ? ORDER BY id DESC",(room,msgid)) self.cursor.execute("SELECT id from messages WHERE room=? and id != ? ORDER BY id DESC",(room,msgid))
r = self.cursor.fetchone() r = self.cursor.fetchone()
if not r: if not r:
last = 0 last = 0
else: else:
last = r[0] last = r[0]
return { return {
"status" : "ok", "status" : "ok",
"msgid" : msgid, "msgid" : msgid,
"last" : last "last" : last
} }
   
   
   
   
def fetchMsgs(self,reqjson): def fetchMsgs(self,reqjson):
''' Check to see if there are any new messages in the room ''' Check to see if there are any new messages in the room
curl -v -X POST http://127.0.0.1:8090/ -H "Content-Type: application/json" --data '{"action":"pollMsg","payload":"{\"roomName\":\"BenTest\", \"mylast\":1,\"user\":\"ben2\"}"}' curl -v -X POST http://127.0.0.1:8090/ -H "Content-Type: application/json" --data '{"action":"pollMsg","payload":"{\"roomName\":\"BenTest\", \"mylast\":1,\"user\":\"ben2\"}"}'
''' '''
   
if "mylast" not in reqjson['payload']: if "mylast" not in reqjson['payload']:
return self.returnFailure(400) return self.returnFailure(400)
room = self.getRoomID(reqjson['payload']["roomName"]) room = self.getRoomID(reqjson['payload']["roomName"])
print room print room
if not room: if not room:
return self.returnFailure(400) return self.returnFailure(400)
   
   
if not self.validateUser(reqjson['payload']): if not self.validateUser(reqjson['payload']):
return self.returnFailure(403,reqjson['payload'],room) return self.returnFailure(403,reqjson['payload'],room)
self.cursor.execute("""SELECT id,msg,ts,user,touser FROM messages self.cursor.execute("""SELECT id,msg,ts,user,touser FROM messages
WHERE room=? AND WHERE room=? AND
(touser = 0 OR touser = ?) AND (touser = 0 OR touser = ?) AND
id > ? id > ?
ORDER BY ts ASC ORDER BY ts ASC
""",(room,reqjson['payload']['user'],reqjson['payload']['mylast'])) """,(room,reqjson['payload']['user'],reqjson['payload']['mylast']))
r = self.cursor.fetchall() r = self.cursor.fetchall()
if not r: if not r:
# No changes # No changes
return {"status":"unchanged","last":reqjson['payload']['mylast']} return {"status":"unchanged","last":reqjson['payload']['mylast']}
# Otherwise, return the messages # Otherwise, return the messages
return {"status":"updated", return {"status":"updated",
"messages" : r "messages" : r
} }
   
   
   
   
   
   
def validateUser(self,payload): def validateUser(self,payload):
''' Placeholder for now. Auth will be handled in more depth later ''' Placeholder for now. Auth will be handled in more depth later
''' '''
if "user" not in payload or "roomName" not in payload: if "user" not in payload or "roomName" not in payload:
return False return False
   
# Validate the session information # Validate the session information
self.cursor.execute("SELECT username from sessions where username=? and sesskey=?",(payload['user'],payload['sesskey'])) self.cursor.execute("SELECT username from sessions where username=? and sesskey=?",(payload['user'],payload['sesskey']))
r = self.cursor.fetchone(); r = self.cursor.fetchone();
if not r: if not r:
return False return False
   
room = self.getRoomID(payload["roomName"]) room = self.getRoomID(payload["roomName"])
if not room: if not room:
return False return False
   
   
# Check whether the user has been marked as active # Check whether the user has been marked as active
self.cursor.execute("SELECT username, room from users where username=? and room=? and active=1",(payload['user'],room)) self.cursor.execute("SELECT username, room from users where username=? and room=? and active=1",(payload['user'],room))
r = self.cursor.fetchone() r = self.cursor.fetchone()
if not r: if not r:
return False return False
return True return True
   
   
   
def getRoomID(self,roomname): def getRoomID(self,roomname):
''' Get a room's ID from its name ''' Get a room's ID from its name
''' '''
t = (roomname,) t = (roomname,)
self.cursor.execute("SELECT id from rooms where name=?",t) self.cursor.execute("SELECT id from rooms where name=?",t)
r = self.cursor.fetchone() r = self.cursor.fetchone()
if not r: if not r:
return False return False
return r[0] return r[0]
   
   
   
def triggerClean(self,reqjson): def triggerClean(self,reqjson):
''' Trigger a clean of old messages etc ''' Trigger a clean of old messages etc
''' '''
if 'pass' not in reqjson: if 'pass' not in reqjson:
# No need for failure messages here # No need for failure messages here
return 403 return 403
if reqjson['pass'] != self.cronpass: if reqjson['pass'] != self.cronpass:
return 403 return 403
# Tidy messages older than 10 minutes # Tidy messages older than 10 minutes
self.tidyMsgs(time.time() - self.purgeInterval); self.tidyMsgs(time.time() - self.purgeInterval);
# Auto-close any rooms beyond the threshold # Auto-close any rooms beyond the threshold
self.autoCloseRooms() self.autoCloseRooms()
return {'status':'ok'} return {'status':'ok'}
   
   
   
def tidyMsgs(self,thresholdtime,room=False): def tidyMsgs(self,thresholdtime,room=False):
''' Remove messages older than the threshold time ''' Remove messages older than the threshold time
''' '''
   
if room: if room:
# Tidy from a specific room # Tidy from a specific room
self.cursor.execute("DELETE FROM messages where ts < ? and room = ?",(thresholdtime,room)) self.cursor.execute("DELETE FROM messages where ts < ? and room = ?",(thresholdtime,room))
self.conn.commit() self.conn.commit()
else: else:
self.cursor.execute("DELETE FROM messages where ts < ?",(thresholdtime,)) self.cursor.execute("DELETE FROM messages where ts < ?",(thresholdtime,))
self.conn.commit() self.conn.commit()
   
# Tidy away any failure messages # Tidy away any failure messages
self.cursor.execute("DELETE FROM failuremsgs where expires < ?",(time.time(),)) self.cursor.execute("DELETE FROM failuremsgs where expires < ?",(time.time(),))
   
   
   
   
def autoCloseRooms(self): def autoCloseRooms(self):
''' Automatically close any rooms that have been sat idle for too long ''' Automatically close any rooms that have been sat idle for too long
''' '''
self.cursor.execute("SELECT id,name from rooms where lastactivity < ? and lastactivity > 0",(time.time() - self.roomCloseThresh,)) self.cursor.execute("SELECT id,name from rooms where lastactivity < ? and lastactivity > 0",(time.time() - self.roomCloseThresh,))
rooms = self.cursor.fetchall() rooms = self.cursor.fetchall()
# Messages probably have been auto-purged, but make sure # Messages probably have been auto-purged, but make sure
for r in rooms: for r in rooms:
self.cursor.execute("DELETE FROM messages where room=?",(r[0],)) self.cursor.execute("DELETE FROM messages where room=?",(r[0],))
self.cursor.execute("DELETE FROM failuremsgs where room=?",(r[0],)) self.cursor.execute("DELETE FROM failuremsgs where room=?",(r[0],))
self.cursor.execute("DELETE FROM users where room=?",(r[0],)) self.cursor.execute("DELETE FROM users where room=?",(r[0],))
self.cursor.execute("DELETE FROM sessions where sesskey like ?", (r[1] + '-%',)) self.cursor.execute("DELETE FROM sessions where sesskey like ?", (r[1] + '-%',))
self.cursor.execute("DELETE FROM rooms where id=?",(r[0],)) self.cursor.execute("DELETE FROM rooms where id=?",(r[0],))
self.conn.commit() self.conn.commit()
   
   
   
   
   
   
def pushSystemMsg(self,msg,room,verb="sysinfo"): def pushSystemMsg(self,msg,room,verb="sysinfo"):
''' Push a message from the SYSTEM user into the queue ''' Push a message from the SYSTEM user into the queue
''' '''
m = { m = {
"text":msg, "text":msg,
"verb":verb "verb":verb
} }
m = self.encryptSysMsg(json.dumps(m)) m = self.encryptSysMsg(json.dumps(m))
self.cursor.execute("INSERT INTO messages (ts,room,msg,user) VALUES (?,?,?,'SYSTEM')",(time.time(),room,m)) self.cursor.execute("INSERT INTO messages (ts,room,msg,user) VALUES (?,?,?,'SYSTEM')",(time.time(),room,m))
msgid = self.cursor.lastrowid msgid = self.cursor.lastrowid
# Update the last activity field in the DB # Update the last activity field in the DB
# See LOC-11 # See LOC-11
self.cursor.execute("UPDATE rooms set lastactivity=? where id=?",(time.time(),room)) self.cursor.execute("UPDATE rooms set lastactivity=? where id=?",(time.time(),room))
self.conn.commit() self.conn.commit()
return msgid return msgid
   
   
def pushFailureMessage(self,user,room,msg): def pushFailureMessage(self,user,room,msg):
''' Record a failure message against a user ''' Record a failure message against a user
See LOC-14 See LOC-14
''' '''
self.cursor.execute("INSERT INTO failuremsgs (username,room,expires,msg) values (?,?,?,?)",(user,room,time.time() + 300,msg)) self.cursor.execute("INSERT INTO failuremsgs (username,room,expires,msg) values (?,?,?,?)",(user,room,time.time() + 300,msg))
self.conn.commit() self.conn.commit()
   
   
   
   
def returnFailure(self,status,reqjson=False,room=False): def returnFailure(self,status,reqjson=False,room=False):
''' For whatever reason, a request isn't being actioned. We need to return a status code ''' For whatever reason, a request isn't being actioned. We need to return a status code
However, in some instances, we may allow a HTTP 200 just once in order to send the user However, in some instances, we may allow a HTTP 200 just once in order to send the user
information on why their next request will fail information on why their next request will fail
''' '''
# TODO - implement the failure handling stuff # TODO - implement the failure handling stuff
if reqjson and room: if reqjson and room:
# Check whether there's a failure message or not # Check whether there's a failure message or not
self.cursor.execute("SELECT msg from failuremsgs where username=? and room=?",(reqjson['user'],room)) self.cursor.execute("SELECT msg from failuremsgs where username=? and room=?",(reqjson['user'],room))
r = self.cursor.fetchone() r = self.cursor.fetchone()
if not r: if not r:
# No message to return # No message to return
return status return status
# Otherwise, return the message and remove it # Otherwise, return the message and remove it
self.cursor.execute("DELETE from failuremsgs where username=? and room=?",(reqjson['user'],room)) self.cursor.execute("DELETE from failuremsgs where username=? and room=?",(reqjson['user'],room))
self.conn.commit() self.conn.commit()
return {"status":"errmessage","text":r[0]} return {"status":"errmessage","text":r[0]}
   
return status return status
   
   
   
def encryptSysMsg(self,msg): def encryptSysMsg(self,msg):
''' Encrypt a message from system - LOC-13 ''' Encrypt a message from system - LOC-13
This isn't so much for protection of the data in memory (as the key is also in memory) as it This isn't so much for protection of the data in memory (as the key is also in memory) as it
is to protect against a couple of things you could otherwise do in the client. See LOC-13 for is to protect against a couple of things you could otherwise do in the client. See LOC-13 for
more info. more info.
''' '''
   
crypted = self.gpg.encrypt(msg,None,passphrase=self.syskey,symmetric="AES256",armor=False) crypted = self.gpg.encrypt(msg,None,passphrase=self.syskey,symmetric="AES256",armor=False,encrypt=False)
return crypted.data.encode('base64') return crypted.data.encode('base64')
   
   
   
def genSessionKey(self,N=48): def genSessionKey(self,N=48):
return ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits + '/=?&@#%^()+,.<>:!').encode('utf-8') for _ in range(N)) return ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits + '/=?&@#%^()+,.<>:!').encode('utf-8') for _ in range(N))
   
   
   
def genpassw(self,N=16): def genpassw(self,N=16):
''' Generate a random string of chars to act as an encryption password ''' Generate a random string of chars to act as an encryption password
''' '''
return ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits).encode('utf-8') for _ in range(N)) return ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits).encode('utf-8') for _ in range(N))
   
   
   
# Create the scheduler function # Create the scheduler function
def taskScheduler(passw,bindpoint): def taskScheduler(passw,bindpoint):
   
# Ignore cert errors # Ignore cert errors
ctx = ssl.create_default_context() ctx = ssl.create_default_context()
ctx.check_hostname = False ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE ctx.verify_mode = ssl.CERT_NONE
data = json.dumps({"action":'schedulerTrigger', data = json.dumps({"action":'schedulerTrigger',
"pass": passw "pass": passw
}) })
while True: while True:
time.sleep(60) time.sleep(60)
try: try:
req = urllib2.Request(bindpoint, data, {'Content-Type': 'application/json'}) req = urllib2.Request(bindpoint, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req,context=ctx) f = urllib2.urlopen(req,context=ctx)
response = f.read() response = f.read()
f.close() f.close()
except: except:
# Don't let the thread abort just because one request went wrong # Don't let the thread abort just because one request went wrong
continue continue
   
   
   
   
if __name__ == '__main__': if __name__ == '__main__':
   
# These will be handled properly later  
passw = ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits).encode('utf-8') for _ in range(64)) passw = ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits).encode('utf-8') for _ in range(64))
bindpoint = "https://127.0.0.1:8090" bindpoint = "https://127.0.0.1:8090"
purgeinterval = 600 # Wipe messages older than 10 mins purgeinterval = 600 # Wipe messages older than 10 mins
closethresh = 3600 * 6 # Auto-close rooms after 6 hours of inactivity closethresh = 3600 * 6 # Auto-close rooms after 6 hours of inactivity
# LOC-15 # LOC-15
testingmode = False testingmode = False
if '--testing-mode-enable' in sys.argv: if '--testing-mode-enable' in sys.argv:
testingmode = True testingmode = True
purgeinterval = 30 purgeinterval = 30
closethresh = 180 closethresh = 180
# Create a global instance of the wrapper so that state can be retained # Create a global instance of the wrapper so that state can be retained
# #
# We pass in the password we generated for the scheduler thread to use # We pass in the password we generated for the scheduler thread to use
# as well as the URL it should POST to # as well as the URL it should POST to
msghandler = MsgHandler(passw,bindpoint,purgeinterval,closethresh,testingmode) msghandler = MsgHandler(passw,bindpoint,purgeinterval,closethresh,testingmode)
   
   
# Bind to PORT if defined, otherwise default to 8090. # Bind to PORT if defined, otherwise default to 8090.
  #
  # This will likely become a CLI argument later
port = int(os.environ.get('PORT', 8090)) port = int(os.environ.get('PORT', 8090))
app.run(host='0.0.0.0', port=port,debug=True,ssl_context='adhoc') app.run(host='127.0.0.1', port=port,debug=True,ssl_context='adhoc')
   
   
   
   
   
   
   
file:b/server/README.md (new)
  # LocalChat Server
 
 
  ## About
 
  Localchat is a simple and lightweight chat application. It's primary purpose is to provide a means to have a multi-user Off-The-Record transient chat, minimising the likelihood that anyone but the chat participants has even a record that the chat took place.
 
  It binds to the loopback adapter, and uses ad-hoc SSL to ensure that chat messages aren't available to anyone capable of sniffing loopback traffic. Where clients are remote, there are a number of possible deployment options, see [The main README](../README.md) for more information on these.
 
  The internal database is stored in memory only, to ensure that metadata isn't written to (and therefore recoverable from) disk.
 
  It's intended to be incredibly lightweight, so is provided as a single Python file rather than being broken out into multiple files. Although it can support a reasonable number of active clients, it *is* a single threaded application and isn't designed to support 1000's of active users.
 
 
 
 
  ## Dependancies
 
  The following non-standard modules are required
 
  * flask
  * urllib2
  * sqlite3
  * bcrypt
  * gnupg
 
 
 
  ## Usage Instructions
 
  To start the server, simply run it
 
  ./LocalChat.py
 
 
  ## Commandline Arguments
 
  The server takes a limited number of commandline arguments. By default, none are needed.
 
 
  ./LocalChat.py [--testing-mode-enable]
 
 
  When `--testing-mode-enable` is present, the internal database is written to disk rather than being kept in memory. This is for testing purposes only, and is outright dangerous for use in production. When this mode is enabled, whenever a user joins a room, `SYSTEM` will push a message to warn the room that messages are being written to disk. Testing mode also changes purge thresholds to a lower number so that the automated cleans will complete in a time more conduicive to testing. *Do Not* use this argument without very, very good reason.