客户端代码:
import ssl
import sys
from os import path
from socket import socket, AF_INET, SOCK_DGRAM
from dtls import do_patch
# from past.builtins import raw_input
do_patch()
blocksize = 1024
def main():
cert_path = path.join(path.abspath(path.dirname(__file__)), "certs")
s = ssl.wrap_socket(socket(AF_INET, SOCK_DGRAM), cert_reqs= ssl.CERT_NONE, ca_certs = path.join(cert_path, 'ca-cert.pem'))
s.connect(('127.0.0.1', 28000))
# data transfer
try:
while True:
print("input is <dir> to list files in dir.\n")
print("input get <filename> to get files from dir.\n")
# send_msg = raw_input(">")
send_msg = input(">")
cmd, filename = send_msg.split(" ")
try:
s.send(send_msg.encode())
except Exception as e:
print("[-]Can not send data")
try:
if cmd == "ls":
data = s.recv(blocksize)
print(data.decode())
else:
filename = filename.split("/")[-1]
filedir = "./" + filename
with open(filedir, "wb") as fd:
while True:
data = s.recv(blocksize)
if data.decode() == "Already Send":
print("Already Receive.")
break
fd.write(data)
except Exception as e:
print("[-]Can not receive Data")
except KeyboardInterrupt:
s.close()
sys.exit(0)
if __name__ == "__main__":
main()
服务端代码:
# socket build communication
import socket
import subprocess
from os import path
from logging import basicConfig, DEBUG
basicConfig(level=DEBUG)
from dtls.sslconnection import SSLConnection
from dtls.err import SSLError, SSL_ERROR_WANT_READ, SSL_ERROR_ZERO_RETURN
blocksize = 1024
def main():
current_path = path.abspath(path.dirname(__file__))
cert_path = path.join(current_path, "certs")
sck = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sck.bind(("127.0.0.1", 28000))
sck.settimeout(30)
# sslconnection socket to dtls
scn = SSLConnection(
sck,
keyfile = path.join(cert_path, "keycert.pem"),
certfile = path.join(cert_path, "kercert.pem"),
server_side=True,
ca_certs=path.join(cert_path, "ca-cert.pem"),
do_handshake_on_connect=False
)
# listen
cnt = 0
while True:
cnt += 1
peer_address = scn.listen()
if peer_address:
print("Comleted listening for peer: %s" % str(peer_address))
break
else:
print("continue")
continue
print("Accepting...")
conn = scn.accept()[0]
sck.settimeout(5)
conn.get_socket(True).settimeout(5)
#blocksize = 1024
# handshake
cnt = 0
while True:
cnt += 1
try:
conn.do_handshake()
except SSLError as err:
if err.errno == 504:
continue
raise
print("Completed handshaking with peer")
break
# data transfer
cnt = 0
while True:
cnt += 1
try:
message = conn.read()
except SSLError as err:
if err.errno == 502:
continue
if err.args[0] == SSL_ERROR_ZERO_RETURN:
break
raise
# remote ls
data = message.decode()
print("from client: ", data)
cmd_filename = data.split(' ')
if cmd_filename[0] != "ls" and cmd_filename[0] != "get":
conn.write("please input true cmd")
continue
else:
if cmd_filename[0] == "ls":
obj = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE)
cmd_result = obj.stdout.read()
conn.write(cmd_result)
# remote get
else:
filename = cmd_filename[1]
if filename[0] == "/":
filedir = filename
else:
if filename[:2] == "./":
filename = filename[2:-1]
filedir = path.join(current_path, filename)
with open(filedir, "r") as fd:
while True:
byte = fd.read(blocksize)
if not byte:
conn.write("Already Send".encode())
break
conn.write(byte.encode())
# unwrap
cnt = 0
while True:
cnt += 1
print("Shutdown inovocation: %d" % cnt)
try:
s = conn.unwrap()
s.close()
except SSLError as err:
if err.errno == 502:
continue
raise
break
sck.close()
pass
if __name__ == "__main__":
main()
相关链接
|