Update from https://gitlab.com/justuser31/p2p_justuser
This commit is contained in:
parent
2aaa5b0db5
commit
800885367d
152
client.py
152
client.py
@ -1,15 +1,80 @@
|
||||
import socket #send/recive packets
|
||||
import threading #threads
|
||||
from time import sleep #wait
|
||||
import socket # send/recive packets
|
||||
import threading # threads
|
||||
from time import sleep # wait
|
||||
|
||||
from random import randint #gen username + random int
|
||||
from random import randint # gen username + random int
|
||||
|
||||
import os.path # Check file
|
||||
|
||||
##############SETTINGS##################
|
||||
global host, port
|
||||
#YOUR/SERVER IP
|
||||
host = "127.0.0.1"
|
||||
#YOUR/SERVER PORT
|
||||
port = 7015
|
||||
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##################
|
||||
@ -17,12 +82,12 @@ def server_listen(conn_temp):
|
||||
global conn
|
||||
|
||||
while True:
|
||||
#Recive data and check for empty data
|
||||
# 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
|
||||
# Send to others clients data from user
|
||||
for i in range(len(conn)):
|
||||
if conn[i] == conn_temp:
|
||||
continue
|
||||
@ -33,15 +98,15 @@ def server_accept(s):
|
||||
|
||||
while True:
|
||||
|
||||
#Listen socket ip:port and accept connection
|
||||
# Listen socket ip:port and accept connection
|
||||
s.listen(2) ; conn_temp, address = s.accept()
|
||||
conn.append(conn_temp)
|
||||
|
||||
#If accepted start listen
|
||||
# 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 )
|
||||
# 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)):
|
||||
@ -52,15 +117,16 @@ def server():
|
||||
global host, port
|
||||
global conn, adress
|
||||
conn = []
|
||||
print(port)
|
||||
|
||||
#Bind server socket
|
||||
# Bind server socket
|
||||
s = socket.socket() ; s.bind((host, port))
|
||||
|
||||
#Start accept-connections func
|
||||
# Start accept-connections func
|
||||
th = threading.Thread(target=server_accept, args=(s,), daemon=True)
|
||||
th.start()
|
||||
|
||||
#Chat with clients
|
||||
# Chat with clients
|
||||
while True:
|
||||
data = "Server > " + input('')
|
||||
for i in range(len(conn)):
|
||||
@ -76,7 +142,7 @@ def client_listen(s):
|
||||
global host, port
|
||||
|
||||
while True:
|
||||
#Recive and print data
|
||||
# Recive and print data
|
||||
sleep(0.01)
|
||||
data = s.recv(1024).decode()
|
||||
if data:
|
||||
@ -85,29 +151,53 @@ def client_listen(s):
|
||||
def client():
|
||||
global host, port
|
||||
|
||||
#Bind client socket
|
||||
# Bind client socket
|
||||
s = socket.socket() ; s.connect((host, port))
|
||||
|
||||
#Start listen server
|
||||
# Start listen server
|
||||
th = threading.Thread(target=client_listen, args=(s,), daemon=True)
|
||||
th.start()
|
||||
|
||||
#Gen id and start chatting
|
||||
id = str(randint(100,1000))
|
||||
# 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"User{id} > " + input('')
|
||||
message = f"{username} > " + input('')
|
||||
s.send(message.encode())
|
||||
|
||||
s.close()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
##############START##################
|
||||
try:
|
||||
client()
|
||||
except:
|
||||
server()
|
||||
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.
|
||||
|
133
list.py
Normal file
133
list.py
Normal file
@ -0,0 +1,133 @@
|
||||
mode = 0 # CHANGE ME
|
||||
# 0 - client mode
|
||||
# 1 - server mode
|
||||
|
||||
#port = 14618
|
||||
|
||||
import socket
|
||||
from os import system as ss
|
||||
from random import randint
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
import threading
|
||||
|
||||
global list_info
|
||||
list_info = [['15464 - None server', datetime.now()]]
|
||||
|
||||
def bore():
|
||||
port = randint(10000, 65000)
|
||||
print(f'YOUR SERVER PORT\n ----> {port} <----')
|
||||
|
||||
f = open('list_port.info', 'w')
|
||||
f.write(str(port))
|
||||
f.close()
|
||||
|
||||
f = open('platform','r')
|
||||
platform = f.read() ; f.close()
|
||||
|
||||
if platform == '0' or platform == '2':
|
||||
ss('./bore local 5000 --to bore.pub --port '+str(port))
|
||||
elif platform == '3':
|
||||
ss('bore local 5000 --to bore.pub --port '+str(port))
|
||||
elif platform == '1':
|
||||
ss('bore.exe local 5000 --to bore.pub --port '+str(port))
|
||||
else:
|
||||
print('Platform error!')
|
||||
|
||||
def conv_list():
|
||||
global list_info
|
||||
|
||||
st = 'Avaible servers:'
|
||||
for i in list_info:
|
||||
st = st + '\n' + i[0] + ' (' + i[1].strftime("%H:%M") + ')'
|
||||
|
||||
return st
|
||||
|
||||
def server_listen(conn_temp):
|
||||
global conn, list_info
|
||||
|
||||
while True:
|
||||
# Recive data and check for empty data
|
||||
data = conn_temp.recv(1024).decode()
|
||||
if data:
|
||||
if data == 'get':
|
||||
send = conv_list()
|
||||
conn_temp.send(send.encode())
|
||||
print(data)
|
||||
else:
|
||||
print(type(data))
|
||||
print(data[0])
|
||||
exit()
|
||||
|
||||
list_info.append([str(data), datetime.now()])
|
||||
|
||||
# Clear list
|
||||
now = datetime.now()
|
||||
for i in list_info:
|
||||
t = (now - i[1]).total_seconds()
|
||||
|
||||
if t > 21600:
|
||||
list_info.remove(i)
|
||||
|
||||
|
||||
|
||||
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()
|
||||
|
||||
def server():
|
||||
th = threading.Thread(target=bore, daemon=True)
|
||||
th.start()
|
||||
|
||||
host = '127.0.0.1' ; port = 5000
|
||||
|
||||
global conn, adress
|
||||
conn = []
|
||||
|
||||
# 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()
|
||||
|
||||
while True:
|
||||
pass
|
||||
|
||||
s.close()
|
||||
|
||||
def client():
|
||||
from requests import get
|
||||
|
||||
host = 'bore.pub'
|
||||
#file = get('https://gitlab.com/justuser31/p2p_justuser/-/raw/main/list_server.info')
|
||||
#port = int(file.content)
|
||||
port = 28938
|
||||
|
||||
client_socket = socket.socket()
|
||||
client_socket.connect((host, port))
|
||||
|
||||
message = 'get'
|
||||
client_socket.send(message.encode())
|
||||
data = client_socket.recv(1024).decode()
|
||||
|
||||
print(data)
|
||||
|
||||
client_socket.close()
|
||||
|
||||
if mode == 0:
|
||||
print('To start own server change python file.\n')
|
||||
client()
|
||||
else:
|
||||
server()
|
||||
|
31
public.py
Normal file
31
public.py
Normal file
@ -0,0 +1,31 @@
|
||||
import sys
|
||||
import socket
|
||||
from requests import get # Download file
|
||||
|
||||
port = sys.argv[1]
|
||||
name = sys.argv[2]
|
||||
|
||||
##### Send our port to global list
|
||||
# Get port
|
||||
host = 'bore.pub'
|
||||
#file = get('https://gitlab.com/justuser31/p2p_justuser/-/raw/main/list_server.info')
|
||||
#s_port = int(file.content)
|
||||
|
||||
|
||||
|
||||
s_port = 43560
|
||||
|
||||
|
||||
|
||||
|
||||
# Send our port
|
||||
client_socket = socket.socket()
|
||||
client_socket.connect((host, s_port))
|
||||
|
||||
# Input name of server
|
||||
message = str([port, name])
|
||||
|
||||
client_socket.send(message.encode())
|
||||
client_socket.close()
|
||||
|
||||
print('SERVER POSTED')
|
Loading…
Reference in New Issue
Block a user