SHARE
TWEET

Untitled

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