Created
August 12, 2019 18:39
-
-
Save spit4520/790b665b742386fd107f15b08c117243 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"vehicles": { | |
"IMEI1":"host1", | |
"IMEI2":"host2" | |
}, | |
"servers": { | |
"host1":{ | |
"host":"tcp.host1.com", | |
"port":8081 | |
}, | |
"host2":{ | |
"host":"tcp.host2.com", | |
"port":8080 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
#simple bidirectional python proxy. Written for KUHMUTE LLC. | |
#Scott Spitler II, SS Kuhmtue LLC. 2019 | |
#MIT License | |
import socket | |
import select | |
import time | |
import sys | |
import json | |
#Changing the delay will allow for more bandwidth/performance at the cost of CPU and network | |
buffer_size = 4096 | |
delay = 0.0001 | |
forward_to = ('tcp.defaulthost.com', 8080) #default forward location if not in proxy dict | |
proxyList = {} | |
class Forward: | |
def __init__(self): | |
self.forward = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
def start(self, host, port): | |
try: | |
self.forward.connect((host, port)) | |
return self.forward | |
except Exception as e: | |
print(str(e)) | |
return False | |
class TheServer: | |
input_list = [] | |
channel = {} | |
def __init__(self, host, port): | |
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
self.server.bind((host, port)) | |
self.server.listen(200) | |
def main_loop(self): | |
self.input_list.append(self.server) | |
while 1: | |
time.sleep(delay) | |
ss = select.select | |
inputready, outputready, exceptready = ss(self.input_list, [], []) | |
for self.s in inputready: | |
if self.s == self.server: | |
self.on_accept() | |
break | |
self.data = self.s.recv(buffer_size) | |
if len(self.data) == 0: | |
self.on_close() | |
break | |
else: | |
self.on_recv() | |
def on_accept(self): | |
proxyHost = forward_to[0] | |
proxyPort = forward_to[1] | |
clientsock, clientaddr = self.server.accept() | |
data = clientsock.recv(buffer_size) | |
datum = data.decode("utf-8").rstrip() | |
cmds = [x.strip() for x in datum[:-1].split(',')] | |
print("COMMANDS ON ACCEPT: "+str(cmds)) | |
if len(cmds) <= 3: | |
print("empty message") | |
else: | |
if cmds[3] == "Q0" : | |
#search for the matching pair | |
proxyHost, proxyPort = locateProxy(cmds[2]) | |
forward = Forward().start(proxyHost, proxyPort) | |
if forward: | |
print(str(clientaddr) + "has connected") | |
self.input_list.append(clientsock) | |
self.input_list.append(forward) | |
self.channel[clientsock] = forward | |
self.channel[forward] = clientsock | |
else: | |
print("Can't establish connection with remote server.") | |
print("closing connection to remote server...") | |
clientsock.close() | |
def on_close(self): | |
print("has disconnected") | |
self.input_list.remove(self.s) | |
self.input_list.remove(self.channel[self.s]) | |
out = self.channel[self.s] | |
self.channel[out].close() | |
self.channel[self.s].close() | |
del self.channel[out] | |
del self.channel[self.s] | |
def on_recv(self): | |
data = self.data | |
datum = self.data.decode("utf-8").rstrip() | |
cmds = [x.strip() for x in datum[:-1].split(',')] | |
dispatchMiddleware(cmds) | |
print(str(data)) | |
self.channel[self.s].send(data) | |
def dispatchMiddleware(cmds): | |
imei = cmds[2] | |
cmd = cmds[3] | |
if cmd == 'Q0': | |
#append to log file | |
with open("vehicles.txt", "a") as logFile: | |
logFile.write(imei + '\n') | |
logFile.close() | |
def locateProxy(imei): | |
#perform the proxy pass here. | |
proxyList = loadProxyOpts() | |
#print(str(json.dumps(proxyList))) | |
#print(str(proxyList['vehicles'])) | |
if imei not in proxyList['vehicles']: | |
#there is no proxy for this imei number | |
print("using default proxy for this vehicle") | |
return forward_to[0], forward_to[1] | |
else: | |
print("locating proxy in list for: "+ str(imei)) | |
proxyId = proxyList['vehicles'][imei] | |
proxyHost = proxyList['servers'][proxyId] | |
return proxyHost['host'], proxyHost['port'] | |
def loadProxyOpts(): | |
with open('proxy.json', 'r') as proxyFile: | |
proxyList = json.load(proxyFile) | |
print("loaded proxy") | |
proxyFile.close() | |
return proxyList | |
if __name__ == '__main__': | |
server = TheServer('0.0.0.0', 12233) | |
try: | |
loadProxyOpts() | |
server.main_loop() | |
except KeyboardInterrupt: | |
print("Ctrl C - Stopping the server....") | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment