Commit b02f4996 authored by andy's avatar andy
Browse files

initial

parents
MIT License
Copyright (c) 2019 Arthur Zopellaro
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-----BEGIN CERTIFICATE-----
MIIEDzCCAvegAwIBAgIUXZ8RNCv/4dTH+skxaUbUx+wl1wMwDQYJKoZIhvcNAQEL
BQAwgZYxJTAjBgNVBAgMHEF1c3RyYWxpYW4gQ2FwaXRhbCBUZXJyaXRvcnkxETAP
BgNVBAcMCENhbmJlcnJhMQwwCgYDVQQKDANVTkUxEDAOBgNVBAsMB0NPU0M1NDAx
FDASBgNVBAMMC2V4YW1wbGUub3JnMSQwIgYJKoZIhvcNAQkBFhVhdGF5bG84NUBt
eXVuZS5lZHUuYXUwHhcNMjEwNTIyMDA1MDM1WhcNMjIwNTIyMDA1MDM1WjCBljEl
MCMGA1UECAwcQXVzdHJhbGlhbiBDYXBpdGFsIFRlcnJpdG9yeTERMA8GA1UEBwwI
Q2FuYmVycmExDDAKBgNVBAoMA1VORTEQMA4GA1UECwwHQ09TQzU0MDEUMBIGA1UE
AwwLZXhhbXBsZS5vcmcxJDAiBgkqhkiG9w0BCQEWFWF0YXlsbzg1QG15dW5lLmVk
dS5hdTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAOINQF8BIYHv1KA8
wiz0P5JMMEMiH2avX02ua39xgOO6oujVc4O64EBpABPddsilUeTarg0dJ0EsRGWi
NRvQKZijLoLzB4duVFElxVTP5AAvNTQQ8yN74mr5QHPxkWH0yGxInVJFWtEbOHgj
BiLOVwXlg3bTcnjQQ7AZTQtRhdBbPPdzn/qh3df5qq1Dh0xva1vcBcZa8oif6LuY
WO5ZavjpISDe8fsUI/hvkG7BWFbG+k0Lr4NsUQuM1FGzbp8sUhmh/gmmvJehzeW9
jbI5H3EnF1iuCHsP1tNXohnfH9tL49lOTr0s5UEE7Cl7ocXaeKUWHooH/bPQIGOF
xk7ZGbMCAwEAAaNTMFEwHQYDVR0OBBYEFL/cTTfFMgc5w2hB5JcE9bHkrqnpMB8G
A1UdIwQYMBaAFL/cTTfFMgc5w2hB5JcE9bHkrqnpMA8GA1UdEwEB/wQFMAMBAf8w
DQYJKoZIhvcNAQELBQADggEBADE0z6UQCa1eE8qdgnFUPG1ayXk3xdkq6jPJA64M
ZDFpUh7kEVO0z1qsa5Az403SUz9jK48y7OPAzhd8yhpPguwcpiubSTSQthcia7G4
K+7TQPmy22nNUYddPlcUvhg64kOBcTAYOYPpKCCc5ToIk7IA0FcFN2xgY7CTCfv6
LWZgR1WhqkFK+8cN4kQfmFBoocR6UK6cTww6aO/80Tk2O5QAx/jzQFifx9Yqsf2F
8Pd0M3GsQQk0KQ5X3Mpcmj+diozmpo64uBnAH7kzw2xxvBfMmETGMFpTHnWRpIja
KGSRaODru2i+fnG5kQnh08xK7oaOHzy+OeC8HfJsiOrVpH8=
-----END CERTIFICATE-----
from socket import create_connection
from ssl import SSLContext, PROTOCOL_TLS_CLIENT
import sys
hostname='example.org'
ip = '10.1.1.53'
port = 3000
context = SSLContext(PROTOCOL_TLS_CLIENT)
context.load_verify_locations('cert.pem')
FORMAT = 'ASCII'
HEADER = 64
DISCONNECT_MESSAGE = "x"
#this variable is for handling entry from file or user
TAKING_CODONS_INPUT = True
#port from start args or get input
if len(sys.argv) < 3:
PORT = int(input("please define the port you would like to connect to"))
else:
PORT = int(sys.argv[2])
#ip from start args or get input
if len(sys.argv) < 2:
SERVER = input("please define the server you would like to connect to")
else:
SERVER = sys.argv[1]
#send by file or prompt input
CODONS = None
if len(sys.argv) == 4:
infile = open(sys.argv[3], 'r')
CODONS = infile.read()
print("here are the codons")
print(CODONS)
TAKING_CODONS_INPUT = False
#handles just the sending of the codon data, no validity checking
def send(msg, tls):
print("sending " + msg)
message = msg.encode(FORMAT)
msg_length = len(message)
#calculate length of string and send as header, instead of set START RNA message
send_length = str(msg_length).encode(FORMAT)
send_length += b' ' * (HEADER - len(send_length))
tls.sendall(send_length)
tls.sendall(message)
response = tls.recv(2048).decode(FORMAT)
if is_valid_codon(response) and msg != "x":
print(response)
#put codons in a dictionary and use it for checking validity
def init_codon_dict(codon_info_csv):
CODONS = dict()
f = open(codon_info_csv)
for line in f:
line = line.strip('\n')
(codon, protein) = line.split(",")
CODONS[codon] = protein
return CODONS
VALID_CODONS = init_codon_dict("codon-aminoacid.csv")
#check if the data entered is valid before sending and on receipt
def is_valid_codon(codon):
print(codon)
valid = True
codons_length = len(codon)
print("length:", codons_length)
#no empties
if codons_length == 0:
valid = False
print("Error - length of zero")
if codons_length%3 != 0:
valid = False
print("Error - not divisible by 3")
if valid:
for x in range(int(codons_length/3)):
start = x
end = x+3
if codon[start:end] not in VALID_CODONS:
valid = False
print("error - not valid codon")
if valid:
print("passed codon check")
return True
else:
print("failed codon check")
return False
with create_connection((ip, port)) as client:
with context.wrap_socket(client, server_hostname=hostname) as tls:
print(f'Using {tls.version()}\n')
while TAKING_CODONS_INPUT:
CODONS = input("Enter codons to optimize, or enter X to disconnect")
CODONS = CODONS.upper()
if (is_valid_codon(CODONS)):
send(CODONS, tls)
else:
send(DISCONNECT_MESSAGE, tls)
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3kSW/VEEpDgbjd2Yu2Vs2xGL84tXDKWdcpYY2rurIPVDl9Z4
+6wLdBoB3eNxDsfo7TJo5w5SneV9iTeON1R0+3Ju5tS6MTCreCJaAtZw98Em7DqX
oTiw5y5+PaCQ3XD6VbmH/3ETim/1fyjl1ZkBVgSi84IU8BT5zNQzwOD5FwKBgFOg
Z8WSh/d0vZJ4pTbKkSv1r6QNb2+BwnCm4upHQCX8kyMQHROm9dSQmtCoraBh27MU
PdLzfyhfEGnU3GpwXvtceOj5J4e91pGFjVY6LHCJIRyCZVyej3sIUj9DdgY9h131
hyUpdpMG49hoXXB+roNi8HRBrWxLu2xsgnuSGUS7AoGAUn4pSZhzS6MV0O3oEG3p
5CYttJ0HfCMbASmQHyjLEFgpju8X5QBL7Sya7SFVW5i/B5T8x/iqeDI/ve+OhPmo
Zbuq4cZDP9UfrhJxwHqbs8rk5Km8CrteN2PxScRRBok4n0RHE5zpAjJdhqUEIIaU
w4mPXoPtAPF1jfzXH5UL5zg=
-----END PRIVATE KEY-----
from socket import socket, AF_INET, SOCK_STREAM, gethostname, gethostbyname
from ssl import SSLContext, PROTOCOL_TLS_SERVER
import threading
import sys
SERVER = gethostbyname(gethostname())
port = int(sys.argv[1])
context = SSLContext(PROTOCOL_TLS_SERVER)
context.load_cert_chain('cert.pem', 'key.pem')
HEADER = 64
FORMAT = "ASCII"
def is_valid_codon(codon):
if codon in optimals:
print(codon + " this codon is valid")
return True
else:
print(codon + " this codon is NOT valid")
return False
#need to score codons based on their efficiency (G or C is better)
def score_codon(codon):
score = 0
for letter in codon:
if letter == "G" or letter == "C":
score+=1
return score
#create a data store as a dictionary, where we can look up the codon to return
def init_codon_dict(codon_info_csv):
OPTIMAL_CODONS = dict()
CODON_AS_KEY = dict()
f = open(codon_info_csv)
for line in f:
line = line.strip('\n')
(codon, protein) = line.split(",")
if protein in OPTIMAL_CODONS:
if score_codon(codon) > score_codon(OPTIMAL_CODONS[protein]):
OPTIMAL_CODONS[protein] = codon
else:
OPTIMAL_CODONS[protein] = codon
CODON_AS_KEY[codon] = protein
for codon in CODON_AS_KEY:
CODON_AS_KEY[codon] = OPTIMAL_CODONS[CODON_AS_KEY[codon]]
return CODON_AS_KEY
optimals = init_codon_dict("codon-aminoacid.csv")
#this is where the ip address would get validated.
#I have not implemented any validation, so just return true for everyone.
def is_whitelisted_ip(ip_address):
return True
def handle_client(conn, addr):
connected = True
print(f'Connected by {addr}\n')
print("checking IP address")
if is_whitelisted_ip(addr):
print("IP validated")
else:
print("Unauthorised user... disconnecting...")
connected = False
while connected:
msg_length = conn.recv(HEADER).decode(FORMAT)
if msg_length:
msg_length = int(msg_length)
msg = conn.recv(msg_length).decode(FORMAT)
print(msg)
if msg == "x":
connected = False
else:
if msg_length%3 != 0:
connected = False
else:
codons_all_valid = True
msg = msg.upper()
#validate codons: length of string should be multiple of three, all codons should be valid
startpoint = 0
endpoint = 3
result = ""
while endpoint <= msg_length:
print(endpoint, " ? ", msg_length)
checking = msg[startpoint:endpoint]
if is_valid_codon(checking):
print(checking)
result += optimals[checking]
else:
connected = False
codons_all_valid = False
print("should exit here")
startpoint+=3
endpoint+=3
print("finished checking all codons")
if codons_all_valid:
conn.send(result.encode(FORMAT))
else:
connected = False
else:
connected = False
conn.close()
with socket(AF_INET, SOCK_STREAM) as server:
server.bind((SERVER, port))
server.listen()
with context.wrap_socket(server, server_side=True) as tls:
connection, address = tls.accept()
thread = threading.Thread(target=handle_client, args=(connection, address))
thread.start()
#!/bin/sh
python client.py $1 $2 $3
\ No newline at end of file
#!/bin/sh
python server.py $1
\ No newline at end of file
atg
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment