SHARE
TWEET

Untitled

a guest Aug 13th, 2019 224 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. ###########################################
  4. #   Duino-Coin public-server version 0.3  #
  5. # https://github.com/revoxhere/duino-coin #
  6. #      copyright by revox 2019         #
  7. ###########################################
  8. # Important: this version of the server is different than one used in "real" duino-coin network.
  9.  
  10. import socket, threading, time, random, hashlib
  11. from pathlib import Path
  12.  
  13. hashing = hashlib.sha1()
  14.  
  15. def percentage(part, whole):
  16.   return 100 * float(part)/float(whole)
  17.  
  18. class ClientThread(threading.Thread): #separate thread for every user
  19.     def __init__(self,ip,port):
  20.         threading.Thread.__init__(self)
  21.         self.ip = ip
  22.         self.port = port
  23.         print("[+] New thread started for "+ip+":"+str(port))
  24.  
  25.     def run(self):
  26.         print("Connection from : "+ip+":"+str(port))
  27.         while True:
  28.             data = clientsock.recv(1024)
  29.             data = data.decode()
  30.             data = data.split(",")
  31.             if data[0] == "REGI": #registration
  32.                 username = data[1]
  33.                 password = data[2]
  34.                 print(">>>>>>>>>>>>>> started registration")
  35.                 print("Username:", username)
  36.                 print("Password:", password)
  37.                 regf = Path(username+".txt")
  38.                 if not regf.is_file(): #checking if user already exists
  39.                     file = open(username+".txt", "w")
  40.                     file.write(username+":"+password)
  41.                     file.close()
  42.                     clientsock.send(bytes("OK", encoding='utf8'))
  43.                 if regf.is_file():
  44.                     print("Account already exists!")
  45.                     clientsock.send(bytes("NO", encoding='utf8'))
  46.  
  47.             if data[0] == "LOGI": #login
  48.                 username = data[1]
  49.                 password = data[2]
  50.                 print(">>>>>>>>>>>>>> started login")
  51.                 print("Username:", username)
  52.                 print("Password:", password)
  53.                 try:
  54.                     file = open(username+".txt", "r")
  55.                     data = file.readline()
  56.                     file.close()
  57.                     if data == username+":"+password:
  58.                         clientsock.send(bytes("OK", encoding='utf8'))
  59.                         print("sent ok signal")
  60.                 except:
  61.                     clientsock.send(bytes("NO", encoding='utf8'))
  62.                     print("send nope signal")
  63.    
  64.             if data[0] == "JOB": #main, mining section
  65.                 print(">>>>>>>>>>>>>> started mining")
  66.                 try:
  67.                     file = open(username+"balance.txt", "r")
  68.                     balance = file.readline()
  69.                     file.close()
  70.                 except:
  71.                     file = open(username+"balance.txt", "w")
  72.                     file.write(str(0))
  73.                     file.close()
  74.                 file = open("lastblock", "r+")
  75.                 lastblock = file.readline()
  76.                 rand = 1
  77.                 hashing.update((lastblock + str(rand)).encode("utf-8"))
  78.                 clientsock.send(bytes(lastblock + "," + hashing.hexdigest(), encoding='utf8'))
  79.                 result = clientsock.recv(1024)
  80.                 if result.decode() == hashing.hexdigest():
  81.                     print("Good hash")
  82.             if data == "BALA": #check balance section
  83.                 time.sleep(0.1)
  84.                 print(">>>>>>>>>>>>>> sent balance values")
  85.                 try:
  86.                     file = open(username+"balance.txt", "r")
  87.                     balance = file.readline()
  88.                     file.close()
  89.                 except:
  90.                     file = open(username+"balance.txt", "w")
  91.                     file.write(str(0))
  92.                     file.close()
  93.                     print("Had to set", username, "balance to 0!")
  94.                     file = open(username+"balance.txt", "r")
  95.                     balance = file.readline()
  96.                     file.close()
  97.                 clientsock.send(bytes(balance, encoding='utf8'))
  98.  
  99.             if data == "SEND": #sending funds section
  100.                 time.sleep(0.1)
  101.                 username = clientsock.recv(16)
  102.                 name = clientsock.recv(16)
  103.                 amount = clientsock.recv(16)
  104.                 username=username.decode()
  105.                 name=name.decode()
  106.                 amount=amount.decode()
  107.                 print(">>>>>>>>>>>>>> started send funds protocol")
  108.                 print("Username:", username)
  109.                 print("Receiver username:", name)
  110.                 print("Amount", amount)
  111.                 #now we have all data needed to transfer money
  112.                 #firstly, get current amount of funds in bank
  113.                 print("Sent balance values")
  114.                 try:
  115.                     file = open(username+"balance.txt", "r")
  116.                     balance = file.readline()
  117.                     file.close()
  118.                 except:
  119.                     print("Error occured while checking funds!")
  120.                 #verify that the balance is higher or equal to transfered amount
  121.                 if amount >= balance:
  122.                     clientsock.send(bytes("Error! Your balance is lower than amount you want to transfer!", encoding='utf8'))
  123.                 if amount <= balance: #if ok, check if recipient adress exists
  124.                     bankf = Path(name+"balance.txt")
  125.                     if bankf.is_file():
  126.                         #it exists, now -amount from username and +amount to name
  127.                         try:
  128.                             print("Amount after 0.1% fee:", recieveramount)
  129.                             #get senders' balance
  130.                             file = open(username+"balance.txt", "r")
  131.                             balance = file.readline()
  132.                             file.close()
  133.                             #remove amount from senders' balance
  134.                             balance = float(balance) - float(amount)
  135.                             file = open(username+"balance.txt", "w")
  136.                             file.write(str(balance))
  137.                             file.close()
  138.                             #get recipients' balance
  139.                             file = open(name+"balance.txt", "r")
  140.                             namebal = file.readline()
  141.                             file.close()
  142.                             #add amount to recipients' balance
  143.                             namebal = float(namebal) + float(recieveramount)
  144.                             file = open(name+"balance.txt", "w")
  145.                             file.write(str(namebal))
  146.                             file.close()
  147.                             clientsock.send(bytes("Successfully transfered funds!!!", encoding='utf8'))
  148.                         except:
  149.                             clientsock.send(bytes("Unknown error occured while sending funds.", encoding='utf8'))
  150.                     if not bankf.is_file(): #message if recipient doesn't exist
  151.                         print("The recepient", name, "doesn't exist!")
  152.                         clientsock.send(bytes("Error! The recipient doesn't exist! Make sure he submited at least one share!", encoding='utf8'))
  153.  
  154. host = "localhost"
  155. port = 14808
  156.  
  157. tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  158. tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  159.  
  160. tcpsock.bind((host,port))
  161. threads = []
  162.  
  163.  
  164. while True:
  165.     try:
  166.         tcpsock.listen(16)
  167.         print("\nListening for incoming connections...")
  168.         (clientsock, (ip, port)) = tcpsock.accept()
  169.         newthread = ClientThread(ip, port)
  170.         newthread.start()
  171.         threads.append(newthread)
  172.     except:
  173.         print("Error in main loop!")
  174.  
  175. for t in threads:
  176.     t.join()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top