|
|
|
import socket # send/recive packets
|
|
|
|
import threading # threads
|
|
|
|
from time import sleep # wait
|
|
|
|
|
|
|
|
from random import randint # gen username + random int
|
|
|
|
|
|
|
|
import os.path # Check file
|
|
|
|
|
|
|
|
##############SETTINGS##################
|
|
|
|
global host, port, platform, mode, name
|
|
|
|
host = '127.0.0.1'
|
|
|
|
mode = 0 # CHANGE ME
|
|
|
|
# 0 - client mode
|
|
|
|
# 1 - Always server mode (also change name)
|
|
|
|
name = 'Test01'
|
|
|
|
|
|
|
|
######DEV#####
|
|
|
|
port = ''
|
|
|
|
|
|
|
|
# Install library
|
|
|
|
from os import system as ss
|
|
|
|
ss('python3 -m pip install patool')
|
|
|
|
ss('python3 -m pip install requests')
|
|
|
|
print('\n\n')
|
|
|
|
|
|
|
|
from patoolib import extract_archive as extract
|
|
|
|
from requests import get # Download file
|
|
|
|
|
|
|
|
# Load platform
|
|
|
|
if not os.path.isfile('platform'):
|
|
|
|
print('Your platfrom:\n 0 - Linux\n 1 - Windows\n 2 - MacOS\n 3 - Android (Termux)')
|
|
|
|
platform = input('> ')
|
|
|
|
|
|
|
|
f = open('platform','w')
|
|
|
|
f.write(platform)
|
|
|
|
f.close()
|
|
|
|
else:
|
|
|
|
f = open('platform','r')
|
|
|
|
platform = f.read()
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
# Download file if not exist
|
|
|
|
if not os.path.isfile('bore') or os.path.isfile('bore.exe'):
|
|
|
|
if platform == '0':
|
|
|
|
file = get('https://github.com/ekzhang/bore/releases/download/v0.5.0/bore-v0.5.0-x86_64-unknown-linux-musl.tar.gz')
|
|
|
|
if platform == '1':
|
|
|
|
file = get('https://github.com/ekzhang/bore/releases/download/v0.5.0/bore-v0.5.0-x86_64-pc-windows-msvc.zip')
|
|
|
|
if platform == '2':
|
|
|
|
file = get('https://github.com/ekzhang/bore/releases/download/v0.5.0/bore-v0.5.0-x86_64-apple-darwin.tar.gz')
|
|
|
|
if platform == '3':
|
|
|
|
ss('touch bore')
|
|
|
|
ss('pkg install bore')
|
|
|
|
print('Now must be installed bore! (pkg)')
|
|
|
|
|
|
|
|
if platform != '3':
|
|
|
|
open('down.tar.gz', 'wb').write(file.content)
|
|
|
|
|
|
|
|
extract('down.tar.gz')
|
|
|
|
extract('down.tar')
|
|
|
|
|
|
|
|
if mode == 0:
|
|
|
|
print('Enter port for connection to exist server\n Just click enter for create server:')
|
|
|
|
try:
|
|
|
|
port = int(input('> '))
|
|
|
|
except:
|
|
|
|
port = ''
|
|
|
|
|
|
|
|
def bore():
|
|
|
|
global port, platform
|
|
|
|
if platform == '0' or platform == '2':
|
|
|
|
ss('./bore local '+str(port)+' --to bore.pub --port '+str(port))
|
|
|
|
elif platform == '3':
|
|
|
|
ss('bore local '+str(port)+' --to bore.pub --port '+str(port))
|
|
|
|
elif platform == '1':
|
|
|
|
ss('bore.exe local '+str(port)+' --to bore.pub --port '+str(port))
|
|
|
|
else:
|
|
|
|
print('Platform error!')
|
|
|
|
|
|
|
|
|
|
|
|
##############SERVER##################
|
|
|
|
def server_listen(conn_temp):
|
|
|
|
global conn
|
|
|
|
|
|
|
|
while True:
|
|
|
|
# Recive data and check for empty data
|
|
|
|
data = conn_temp.recv(1024).decode()
|
|
|
|
if data:
|
|
|
|
print(str(data))
|
|
|
|
|
|
|
|
# Send to others clients data from user
|
|
|
|
for i in range(len(conn)):
|
|
|
|
if conn[i] == conn_temp:
|
|
|
|
continue
|
|
|
|
conn[i].send(data.encode())
|
|
|
|
|
|
|
|
def server_accept(s):
|
|
|
|
global conn, adress
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
|
|
|
# Listen socket ip:port and accept connection
|
|
|
|
s.listen(2) ; conn_temp, address = s.accept()
|
|
|
|
conn.append(conn_temp)
|
|
|
|
|
|
|
|
# If accepted start listen
|
|
|
|
th = threading.Thread(target=server_listen, args=(conn_temp,), daemon=True)
|
|
|
|
th.start()
|
|
|
|
|
|
|
|
# Send to all message about new user ( exclude user )
|
|
|
|
data = f"New connection from {str(address[1])}"
|
|
|
|
print(data)
|
|
|
|
for i in range(len(conn)):
|
|
|
|
if conn[i] == conn_temp:
|
|
|
|
continue
|
|
|
|
conn[i].send(data.encode())
|
|
|
|
def server():
|
|
|
|
global host, port
|
|
|
|
global conn, adress
|
|
|
|
conn = []
|
|
|
|
print(port)
|
|
|
|
|
|
|
|
# Bind server socket
|
|
|
|
s = socket.socket() ; s.bind((host, port))
|
|
|
|
|
|
|
|
# Start accept-connections func
|
|
|
|
th = threading.Thread(target=server_accept, args=(s,), daemon=True)
|
|
|
|
th.start()
|
|
|
|
|
|
|
|
# Chat with clients
|
|
|
|
while True:
|
|
|
|
data = "Server > " + input('')
|
|
|
|
for i in range(len(conn)):
|
|
|
|
conn[i].send(data.encode())
|
|
|
|
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##############CLIENT##################
|
|
|
|
def client_listen(s):
|
|
|
|
global host, port
|
|
|
|
|
|
|
|
while True:
|
|
|
|
# Recive and print data
|
|
|
|
sleep(0.01)
|
|
|
|
data = s.recv(1024).decode()
|
|
|
|
if data:
|
|
|
|
print(data)
|
|
|
|
|
|
|
|
def client():
|
|
|
|
global host, port
|
|
|
|
|
|
|
|
# Bind client socket
|
|
|
|
s = socket.socket() ; s.connect((host, port))
|
|
|
|
|
|
|
|
# Start listen server
|
|
|
|
th = threading.Thread(target=client_listen, args=(s,), daemon=True)
|
|
|
|
th.start()
|
|
|
|
|
|
|
|
# Gen id or set username
|
|
|
|
print('(Click enter for random)')
|
|
|
|
username = input('Username > ')
|
|
|
|
if username == '':
|
|
|
|
id = str(randint(100,1000))
|
|
|
|
username = f'User{id}'
|
|
|
|
|
|
|
|
while True:
|
|
|
|
message = f"{username} > " + input('')
|
|
|
|
s.send(message.encode())
|
|
|
|
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
|
|
|
|
##############START##################
|
|
|
|
if port == '':
|
|
|
|
port = randint(10000, 65000)
|
|
|
|
|
|
|
|
th = threading.Thread(target=bore, daemon=True)
|
|
|
|
th.start()
|
|
|
|
|
|
|
|
sleep(2)
|
|
|
|
print(f'YOUR SERVER PORT\n ----> {port} <----')
|
|
|
|
|
|
|
|
if mode == 1:
|
|
|
|
f = open('client_port.info', 'w')
|
|
|
|
f.write(str(port))
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
if mode == 0:
|
|
|
|
name = input('Name of server > ')
|
|
|
|
ss("python3 public.py "+str(port)+" '"+name+"'")
|
|
|
|
|
|
|
|
server()
|
|
|
|
else:
|
|
|
|
host = 'bore.pub'
|
|
|
|
|
|
|
|
client()
|
|
|
|
#try:
|
|
|
|
# client()
|
|
|
|
#except:
|
|
|
|
# server()
|
|
|
|
#If no server (can't connect to server - error), then start server or start client.
|