Skip to content

Commit

Permalink
Merge pull request #2 from Baccount/download_tor
Browse files Browse the repository at this point in the history
Large refactor and lots of new features
  • Loading branch information
Baccount authored Feb 14, 2023
2 parents b9b3362 + 159bada commit a8fdd5c
Show file tree
Hide file tree
Showing 7 changed files with 742 additions and 129 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

# Python Cache
/**/__pycache__
README.md
.DS_Store
server/private_key
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# AnonChat - Secure Terminal Chatroom Over Tor Onion Service

AnonChat is a secure, anonymous, and terminal-based chatroom application that operates over the Tor network. With AnonChat, you can communicate with others in a private and encrypted manner, ensuring that your conversations remain untraceable and protected from prying eyes.

## Key Features

- Secure and anonymous communication over the Tor network
- Terminal-based interface for a unique and intuitive experience
- Strong encryption to protect your conversations
- Easy to use and fast-loading interface

## Getting Started

AnonChat can be installed and run on most operating systems, including macOS, Linux and Windows. To get started with AnonChat, simply follow these steps:

1. Install the Tor on your computer.
2. Clone this repository to your local machine using the following command:

`git clone https://github.com/Baccount/Anon_Chat.git`

`pip install -r requirements.txt`
161 changes: 105 additions & 56 deletions client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Client(Cmd):
client
"""
prompt = ''
intro = '[Welcome] Simple chat room client (Cli version)\n' + '[Welcome] Type help to get help\n'
intro = 'Welcom to AnonChat \n'

def __init__(self):
"""
Expand All @@ -26,96 +26,145 @@ def __init__(self):

def __receive_message_thread(self):
"""
accept message thread
Continuously receive messages from the server.
"""
while self.__isLogin:
# noinspection PyBroadException
try:
buffer = self.__socket.recv(1024).decode()
obj = json.loads(buffer)
print('[' + str(obj['sender_nickname']) + ']', obj['message'])
except BrokenPipeError as e:
print('[Client] Connection to the server was broken', e)
self.__isLogin = False
break
stripped = self.decode(buffer)
if not stripped:
print('[Client] Server offline, exiting LINE 37')
exit()
break
for i in stripped:
obj = json.loads(i)
print('[' + str(obj['sender_nickname']) + ']', obj['message'])
except Exception as e:
print('client line 38')
print('[Client] Unable to get data from server', e)
self.__isLogin = False
break

def decode(self, buffer):
"""
Decode the buffer into individual JSON objects.
:param buffer: The buffer to be decoded.
:return: A list of JSON objects.
"""
objects = []
start = 0
end = buffer.find('{', start)
while end != -1:
start = end
count = 1
end = start + 1
while count > 0 and end < len(buffer):
if buffer[end] == '{':
count += 1
elif buffer[end] == '}':
count -= 1
end += 1
objects.append(buffer[start:end])
start = end
end = buffer.find('{', start)
return objects


def __send_message_thread(self, message):
"""
send message thread
:param message: Message content
Send a message to the server.
:param message: The message to be sent.
"""
self.__socket.send(json.dumps({
'type': 'broadcast',
'sender_id': self.__id,
'message': message
}).encode())
try:
self.__socket.send(json.dumps({
'type': 'broadcast',
'sender_id': self.__id,
'message': message
}).encode())
except BrokenPipeError as e:
print(f'{e} client line 86')
exit(0)
except Exception as e:
print('client line 87')
print(e)

def start(self):
"""
start the client
Connect to the server using the onion address.
"""
onion = input("Enter your onion address: ")
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
socket.socket = socks.socksocket
server = (onion, 80)
self.__socket.connect(server)
# run cmdloop with the folling arguments
self.cmdloop()

def do_login(self, args):
"""
login chat room
:param args: parameter
Login to the chat room.
:param args: The nickname to be used for the login.
"""
nickname = args.split(' ')[0]


# Send the nickname to the server to get the user id
self.__socket.send(json.dumps({
'type': 'login',
'nickname': nickname
}).encode())
# only login once
if not self.__isLogin:
# Send the nickname to the server to get the user id
self.__socket.send(json.dumps({
'type': 'login',
'nickname': nickname
}).encode())


try:
buffer = self.__socket.recv(1024).decode()
obj = json.loads(buffer)
if obj['id'] and obj['id'] != -1:
self.__nickname = nickname
self.__id = obj['id']
self.__isLogin = True
print('[Client] Successfully logged into the chat room')


thread = threading.Thread(target=self.__receive_message_thread)
thread.setDaemon(True)
thread.start()
else:
print('User name already exists, please choose another user name')
except Exception as e:
print(e)
try:
buffer = self.__socket.recv(1024).decode()
print(buffer)
obj = json.loads(buffer)
if obj['id']:
# if id = -1 means the nickname is already in use
if obj['id'] == -1:
print('[Client] The nickname is already in use')
return
self.__nickname = nickname
self.__id = obj['id']
self.__isLogin = True
print('[Client] Successfully logged into the chat room')


thread = threading.Thread(target=self.__receive_message_thread)
thread.setDaemon(True)
thread.start()
except Exception as e:
print('client line 138')
print(e)
print("Server likely down")
exit(0)
else:
print('[Client] You have already logged in')

def do_s(self, args):
"""
Send a message
:param args: parameter
Send a message to the chat room.
:param args: The message to be sent.
"""
message = args
# Show messages sent by yourself
print('[' + str(self.__nickname) + ']', message)
# Open child thread for sending data
thread = threading.Thread(target=self.__send_message_thread, args=(message,))
thread.setDaemon(True)
thread.start()
# only send message after login
if self.__isLogin:
message = args
# Show messages sent by yourself
print('[' + str(self.__nickname) + ']', message)
# Open child thread for sending data
thread = threading.Thread(target=self.__send_message_thread, args=(message,))
thread.setDaemon(True)
thread.start()
else:
print('[Client] You have not logged in yet')

def do_logout(self, args=None):
"""
Sign out
:param args: parameter
Logout from the chat room.
"""
self.__socket.send(json.dumps({
'type': 'logout',
Expand All @@ -126,8 +175,8 @@ def do_logout(self, args=None):

def do_help(self, arg):
"""
help
:param arg: parameter
Display help information for the given command.
:param arg: the command for which help information is needed
"""
command = arg.split(' ')[0]
if command == '':
Expand Down
26 changes: 24 additions & 2 deletions client_start.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
from client.client import Client
#from download_tor import start

# downlaod to support work in progress

client = Client()
client.start()
#start("macos")














try:
client = Client()
client.start()
except KeyboardInterrupt:
# clear the terminal screen
print('\033[2J')
exit(0)
Loading

0 comments on commit a8fdd5c

Please sign in to comment.