main
parent
2aaa5b0db5
commit
800885367d
@ -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()
|
||||
|
@ -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