Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F1880392
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Size
6 KB
Subscribers
None
View Options
diff --git a/sockpy.wsgi b/sockpy.wsgi
--- a/sockpy.wsgi
+++ b/sockpy.wsgi
@@ -1,155 +1,145 @@
#!/usr/bin/env python2
from cgi import parse_qs, escape
import simplejson as json
import socket
import select
import time
import base64
+import sockpyAuth
MAXLEN = 16348
gbl_keys = {}
def ExpireOldUIDs():
currTime = time.time()
for uid in gbl_keys.keys():
if gbl_keys[uid].has_key("timeStamp"):
if currTime - gbl_keys[uid]["timeStamp"] > 3600:
del gbl_keys[uid]
def GetValueForUID(uid, key):
if gbl_keys.has_key(uid):
if gbl_keys[uid].has_key(key):
gbl_keys[uid]["timeStamp"] = time.time()
return gbl_keys[uid][key]
return False
def SetValueForUID(uid, key, val):
if not gbl_keys.has_key(uid):
gbl_keys[uid] = {}
gbl_keys[uid][key] = val
gbl_keys[uid]["timeStamp"] = time.time()
def application(environ, start_response):
# the environment variable CONTENT_LENGTH may be empty or missing
try:
request_body_size = int(environ.get('CONTENT_LENGTH', 0))
except (ValueError):
request_body_size = 0
# When the method is POST the query string will be sent
# in the HTTP request body which is passed by the WSGI server
# in the file like wsgi.input environment variable.
request_body = environ['wsgi.input'].read(request_body_size)
d = parse_qs(request_body)
command = escape(d.get('command', [''])[0])
uid = escape(d.get('uid', [''])[0])
if command != "" and uid != "":
payload = json.loads(d.get('payload', [''])[0])
response_array = {}
if command == 'status':
- print "status"
response_array['status'] = GetValueForUID(uid, 'status')
elif command == 'login':
- print "login"
- if payload['username'] == "admin" and payload['password'] == "admin":
- print "success"
+ if sockpyAuth.login(payload['username'], payload['password'])
response_array['status'] = True
SetValueForUID(uid, 'status', True)
else:
- print "fail"
response_array['status'] = False
SetValueForUID(uid, 'status', False)
elif command == 'send':
- print "send"
if GetValueForUID(uid, 'connected') and GetValueForUID(uid, 'status'):
data = base64.b64decode(payload['data'])
sock = GetValueForUID(uid, 'socket')
readable, writable, errored = select.select([], [sock], [sock], 0)
if sock in writable:
sock.sendall(data)
response_array['status'] = True
response_array['connected'] = True
elif sock in errored:
SetValueForUID(uid, 'connected', False)
response_array['status'] = False
response_array['connected'] = False
sock.close()
else:
response_array['status'] = False
elif command == 'connect':
print "connect"
if GetValueForUID(uid, 'status') and not GetValueForUID(uid, 'connected'):
host = payload['host']
port = payload['port']
- print "host: " + host
- print "port: " + port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, int(port)))
- #s.setblocking(0)
SetValueForUID(uid, 'socket', s)
SetValueForUID(uid, 'connected', True)
response_array['status'] = True
response_array['connected'] = True
else:
response_array['status'] = False
response_array['connected'] = False
elif command == 'quit':
- print "quit"
if GetValueForUID(uid, 'connected') and GetValueForUID(uid, 'status'):
sock = GetValueForUID(uid, 'socket')
sock.close()
SetValueForUID(uid, 'connected', False)
response_array['status'] = True
response_array['connected'] = False
elif command == 'recv':
- print "recv"
if GetValueForUID(uid, 'connected') and GetValueForUID(uid, 'status'):
sock = GetValueForUID(uid, 'socket')
readable, writable, errored = select.select([sock], [], [sock], 0)
if sock in readable:
data = sock.recv(MAXLEN)
if len(data) == 0:
response_array["status"] = True
response_array["connected"] = False
sock.close()
else:
response_array['status'] = True
response_array['connected'] = True
elif sock in errored:
response_array['connected'] = False
response_array['status'] = False
data = ""
sock = GetValueForUID(uid, 'socket')
sock.close()
else:
data = ""
response_array['status'] = True
response_array['connected'] = True
response_array['data'] = base64.b64encode(data)
else:
response_array['status'] = False
response_array['connected'] = False
response_body = json.dumps(response_array)
- print response_body
status = '200 OK'
response_headers = [('Content-Type', 'text/html'),
('Content-Length', str(len(response_body)))]
start_response(status, response_headers)
ExpireOldUIDs()
return [response_body]
if __name__ == "__main__":
from wsgiref.simple_server import make_server
httpd = make_server('localhost', 8051, application)
httpd.serve_forever()
diff --git a/sockpyAuth.py b/sockpyAuth.py
new file mode 100644
--- /dev/null
+++ b/sockpyAuth.py
@@ -0,0 +1,24 @@
+import MySQLdb
+import crypt
+
+DBPASSWORD="dbPassword"
+DBUSER="dbUser"
+DBNAME="dbName"
+DBHOST="dbHost"
+DBTABLE="dbTable"
+DBPWCOL="dbPasswordColumn"
+DBUSERCOL="dbUserColumn"
+DBACTIVECOL="dbActiveColumn"
+
+def login(user, password):
+ db=MySQLdb.connect(host=DBHOST,passwd=DBPASSWORD,db=DBNAME,user=DBUSER)
+ c=db.cursor()
+ c.execute("SELECT '" +DBPWCOL + "' FROM '" + DBTABLE + "' WHERE '" + DBUSERCOL + "'='" + user + "' AND '" + DBACTIVECOL + "'=1")
+ resp = c.fetchone()
+ if resp:
+ if crypt.crypt(password, resp[0]) == resp[0]:
+ return True
+ else:
+ return False
+ else:
+ return False
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Fri, Jan 24, 4:20 AM (1 d, 20 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
525137
Default Alt Text
(6 KB)
Attached To
rSOCKPY Socket-over-HTTP Bridge
Event Timeline
Log In to Comment