Page MenuHomePhabricator

No OneTemporary

diff --git a/sockpy.ini b/sockpy.ini
--- a/sockpy.ini
+++ b/sockpy.ini
@@ -1,14 +1,15 @@
[General]
username = myRemoteUsername
password = myRemotePassword
host = https://my.remote.host
+proxy = mein-proxy:port
[server-imap]
localport = 1143
remoteport = 143
remotehost = localhost
[server-smtp]
localport = 1125
remoteport = 25
remotehost = localhost
diff --git a/sockpy.py b/sockpy.py
--- a/sockpy.py
+++ b/sockpy.py
@@ -1,268 +1,302 @@
#!/usr/bin/env python2
import requests
import simplejson as json
import threading
from optparse import OptionParser
import ConfigParser
import socket
import select
import uuid
import time
import base64
import sys
+import getpass
+import os
MAXLEN = 16384
parser = OptionParser()
parser.add_option("-c", "--connect", dest="connect", help="connect local port to remote host and port, use localPort:remoteHost:remotePort; separate multiple hosts by \",\"", default="")
parser.add_option("-f", "--file", dest="file", help="config file to load add values from", default="")
-parser.add_option("-s", "--server", dest="host", help="the host running the sockpy server application", default="http://127.0.0.1:8051")
-parser.add_option("-u", "--user", dest="user", help="the username for logging into the remote sockpy server", default="admin")
-parser.add_option("-p", "--password", dest="password", help="the password for logging into the remote sockpy server", default="admin")
+parser.add_option("-s", "--server", dest="host", help="the host running the sockpy server application", default="")
+parser.add_option("-u", "--user", dest="user", help="the username for logging into the remote sockpy server", default="")
+parser.add_option("-p", "--password", dest="password", help="the password for logging into the remote sockpy server", default="")
+parser.add_option("-r", "--proxy", dest="proxy", help="an optional proxy to connect via", default="")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="enable verbose output", default=False)
(opts, args) = parser.parse_args()
class SocketServerWrapper(threading.Thread):
- def __init__(self, host, user, password, connStr, verbose):
+ def __init__(self, host, user, password, connStr, verbose, proxy):
self.url = host
self.user = user
self.password = password
self.connStr = connStr
+ self.proxy = proxy
connArr = connStr.split(":")
self.localPort = int(connArr[0])
self.error = False
self.verbose = verbose
self.running = True
self.sock = None
self.socketThreads = []
threading.Thread.__init__(self)
self.setDaemon(True)
def close(self):
for thread in self.socketThreads:
thread.close()
def run(self):
print "Spawning socket and waiting for connections on port: " + str(self.localPort)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 5)
self.sock.bind(('', self.localPort))
self.sock.listen(1)
while self.running:
clientSocket, addr = self.sock.accept()
- self.socketThreads.append(SocketWrapper(self.url, self.user, self.password, self.connStr, clientSocket, self.verbose))
+ self.socketThreads.append(SocketWrapper(self.url, self.user, self.password, self.connStr, clientSocket, self.verbose, self.proxy))
self.socketThreads[-1].start()
print "ThreadedSocketServer exit."
class SocketWrapper(threading.Thread):
- def __init__(self, host, user, password, connStr, clientSocket, verbose):
+ def __init__(self, host, user, password, connStr, clientSocket, verbose, proxy):
self.url = host
self.user = user
self.verbose = verbose
self.password = password
connArr = connStr.split(":")
self.remotePort = connArr[-1]
self.error = False
self.connected = False
self.running = True
+ if proxy != "":
+ self.proxyDict = { "http" : proxy,
+ }
+ else:
+ self.proxyDict = {}
self.uid = str(uuid.uuid4())
self.clientSocket = clientSocket
if len(connArr) == 3:
self.remoteHost = connArr[1]
else:
self.remoteHost = "127.0.0.1"
threading.Thread.__init__(self)
self.setDaemon(True)
def run(self):
if self.connect():
if self.login():
self.connectRemote()
while self.connected:
self.sendRemote(self.recvLocal())
self.sendLocal(self.recvRemote())
self.disconnectRemote()
self.disconnectLocal()
print "Thread exit"
def close(self):
print "Closing Thread..."
if self.connected:
self.disconnectRemote()
self.disconnectLocal()
self.connected = False
self.running = False
def disconnectRemote(self):
if self.verbose:
print "Disconnecting Remote..."
payload = {}
payload["command"] = "quit"
payload["uid"] = self.uid
payload["payload"] = json.dumps({})
self.connected = False
try:
- r = requests.post(self.url, data=payload, verify=False)
+ r = requests.post(self.url, data=payload, verify=False, proxies=self.proxyDict)
except:
return False
return True
def disconnectLocal(self):
if self.verbose:
print "Disconnecting Local..."
try:
self.clientSocket.shutdown(socket.SHUT_RDWR)
self.clientSocket.close()
except:
pass
def connect(self):
if self.verbose:
print "Connecting..."
payload = {}
payload["command"] = "status"
payload["payload"] = json.dumps({})
payload["uid"] = self.uid
- try:
- r = requests.post(self.url, data=payload, verify=False)
- except:
- return False
+ #try:
+ r = requests.post(self.url, data=payload, verify=False, proxies=self.proxyDict)
+ #except:
+ # return False
reply = json.loads(r.content)
return True
def login(self):
if self.verbose:
print "Logging in..."
payload = {}
payload["command"] = "login"
payload["uid"] = self.uid
payload["payload"] = json.dumps({"username" : self.user, "password" : self.password})
try:
- r = requests.post(self.url, data=payload, verify=False)
+ r = requests.post(self.url, data=payload, verify=False, proxies=self.proxyDict)
except:
return False
reply = json.loads(r.content)
if reply["status"]:
return True
else:
return False
def connectRemote(self):
if self.verbose:
print "Connecting Remote..."
payload = {}
payload["command"] = "connect"
payload["payload"] = json.dumps({"host" : self.remoteHost, "port" : self.remotePort})
payload["uid"] = self.uid
- r = requests.post(self.url, data=payload, verify=False)
+ r = requests.post(self.url, data=payload, verify=False, proxies=self.proxyDict)
reply = json.loads(r.content)
if reply["connected"]:
self.connected = True
return True
else:
self.connected = False
return False
def recvLocal(self):
readable, writable, errored = select.select([self.clientSocket], [], [self.clientSocket], 0)
if self.clientSocket in readable:
data = self.clientSocket.recv(MAXLEN)
if len(data) == 0:
self.connected = False
print "Local disconnect, closing socket..."
elif self.clientSocket in errored:
self.connected = False
else:
data = ""
return data
def sendLocal(self, data):
read, writable, errored = select.select([], [self.clientSocket], [self.clientSocket], 0)
if self.clientSocket in writable and data != "":
sent = self.clientSocket.sendall(data)
if sent is not None:
self.connected = False
print "Error sending to local socket, disconnecting..."
elif self.clientSocket in errored:
self.connected = False
def recvRemote(self):
payload = {}
payload["command"] = "recv"
payload["payload"] = json.dumps({})
payload["uid"] = self.uid
try:
- r = requests.post(self.url, data=payload, verify=False)
+ r = requests.post(self.url, data=payload, verify=False, proxies=self.proxyDict)
except:
self.connected = False
return ""
reply = json.loads(r.content)
if reply["status"] == False:
self.connected = False
if reply["connected"] == False:
self.connected = False
print "Remote disconnect, closing socket..."
data = base64.b64decode(reply["data"])
if self.verbose:
print "Got from Remote: " + data
return data
def sendRemote(self, data):
if data != "":
if self.verbose:
print "Sending to Remote: " + data
payload = {}
payload["command"] = "send"
payload["payload"] = json.dumps({"data" : base64.b64encode(data)})
payload["uid"] = self.uid
try:
- r = requests.post(self.url, data=payload, verify=False)
+ r = requests.post(self.url, data=payload, verify=False, proxies=self.proxyDict)
except:
self.connected = False
return
reply = json.loads(r.content)
if reply["status"] == False:
self.connected = False
if reply["connected"] == False:
print "Remote disconnect, closing socket..."
self.connected = False
def parseConfigFile(filename):
config = ConfigParser.SafeConfigParser()
config.read(filename)
username = config.get("General", "username")
- password = config.get("General", "password")
+ password = base64.b64decode(config.get("General", "password"))
hostname = config.get("General", "host")
+ proxy = config.get("General", "proxy")
hostList = []
for section in config.sections():
if section != "General":
host = config.get(section, "remotehost")
localport = config.get(section, "localport")
remoteport = config.get(section, "remoteport")
hostList.append(localport + ":" + host + ":" + remoteport)
- return hostList, hostname, username, password
+ return hostList, hostname, username, password, proxy
if __name__ == "__main__":
+ cfg = ""
if opts.connect == "" and opts.file == "":
- sys.exit()
+ cfg = "sockpy.ini"
+ if opts.file == "" and cfg != "":
+ if not os.path.exists(cfg):
+ sys.exit()
if opts.file != "":
- hostList, host, user, password = parseConfigFile(opts.file)
+ cfg = opts.file
+ if cfg != "":
+ hostList, host, user, password, proxy = parseConfigFile(cfg)
+ if password == "":
+ pass1 = getpass.getpass("No password found. Please enter your password, it will be stored obfuscatedly in the config file: ")
+ pass2 = getpass.getpass("Please repeat the password: ")
+ if pass1 != pass2:
+ print "Your passwords do not match."
+ sys.exit(1)
+ else:
+ config = ConfigParser.SafeConfigParser()
+ config.read(cfg)
+ config.set("General", "password", base64.b64encode(pass1))
+ with open(cfg, 'wb') as configfile:
+ config.write(configfile)
+ password = pass1
else:
+ hostList = opts.connect.split(",")
+ if opts.host != "":
host = opts.host
+ if opts.user != "":
user = opts.user
+ if opts.password != "":
password = opts.password
- hostList = opts.connect.split(",")
+ if opts.proxy != "":
+ proxy = opts.proxy
sockConns = []
for connStr in hostList:
- sockConns.append(SocketServerWrapper(host, user, password, connStr, opts.verbose))
+ sockConns.append(SocketServerWrapper(host, user, password, connStr, opts.verbose, proxy))
sockConns[-1].start()
try:
while True:
time.sleep(1)
finally:
for conn in sockConns:
conn.close()
print "done"
sys.exit(0)

File Metadata

Mime Type
text/x-diff
Expires
Tue, Dec 24, 7:43 PM (1 d, 16 m ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
534125
Default Alt Text
(12 KB)

Event Timeline