2018-09-24 11:52:34 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2020-04-30 09:23:15 +00:00
|
|
|
import argparse
|
2018-09-24 11:52:34 +00:00
|
|
|
import base64
|
|
|
|
import errno
|
|
|
|
import json
|
|
|
|
import socket
|
2020-04-30 09:23:15 +00:00
|
|
|
import ssl
|
2018-09-24 11:52:34 +00:00
|
|
|
import sys
|
|
|
|
try:
|
|
|
|
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
|
|
|
|
except ImportError:
|
|
|
|
from http.server import HTTPServer
|
|
|
|
from http.server import BaseHTTPRequestHandler
|
2021-06-23 18:38:41 +00:00
|
|
|
from rpc.client import print_json
|
2018-09-24 11:52:34 +00:00
|
|
|
|
|
|
|
rpc_sock = None
|
|
|
|
|
2020-04-30 09:23:15 +00:00
|
|
|
parser = argparse.ArgumentParser(description='http(s) proxy for SPDK RPC calls')
|
|
|
|
parser.add_argument('host', help='Host name / IP representing proxy server')
|
|
|
|
parser.add_argument('port', help='Port number', type=int)
|
|
|
|
parser.add_argument('user', help='User name used for authentication')
|
|
|
|
parser.add_argument('password', help='Password used for authentication')
|
|
|
|
parser.add_argument('-s', dest='sock', help='RPC domain socket path', default='/var/tmp/spdk.sock')
|
|
|
|
parser.add_argument('-c', dest='cert', help='SSL certificate')
|
|
|
|
|
2018-09-24 11:52:34 +00:00
|
|
|
|
|
|
|
def print_usage_and_exit(status):
|
|
|
|
print('Usage: rpc_http_proxy.py <server IP> <server port> <user name>' +
|
|
|
|
' <password> <SPDK RPC socket (optional, default: /var/tmp/spdk.sock)>')
|
|
|
|
sys.exit(status)
|
|
|
|
|
|
|
|
|
|
|
|
def rpc_call(req):
|
|
|
|
global rpc_sock
|
|
|
|
|
|
|
|
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
|
|
sock.connect(rpc_sock)
|
|
|
|
sock.sendall(req)
|
|
|
|
|
|
|
|
if 'id' not in json.loads(req.decode('ascii')):
|
|
|
|
sock.close()
|
|
|
|
return None
|
|
|
|
|
|
|
|
buf = ''
|
|
|
|
closed = False
|
|
|
|
response = None
|
|
|
|
|
2021-06-23 18:38:41 +00:00
|
|
|
print_json(req.decode('ascii'))
|
|
|
|
|
2018-09-24 11:52:34 +00:00
|
|
|
while not closed:
|
|
|
|
newdata = sock.recv(1024)
|
|
|
|
if (newdata == b''):
|
|
|
|
closed = True
|
|
|
|
buf += newdata.decode('ascii')
|
|
|
|
try:
|
|
|
|
response = json.loads(buf)
|
|
|
|
except ValueError:
|
|
|
|
continue # incomplete response; keep buffering
|
|
|
|
break
|
|
|
|
|
|
|
|
sock.close()
|
|
|
|
|
|
|
|
if not response and len(buf) > 0:
|
|
|
|
raise
|
|
|
|
|
2021-06-23 18:38:41 +00:00
|
|
|
print_json(buf)
|
|
|
|
|
2018-09-24 11:52:34 +00:00
|
|
|
return buf
|
|
|
|
|
|
|
|
|
|
|
|
class ServerHandler(BaseHTTPRequestHandler):
|
|
|
|
|
|
|
|
key = ""
|
|
|
|
|
|
|
|
def do_HEAD(self):
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header('Content-type', 'text/html')
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
def do_AUTHHEAD(self):
|
|
|
|
self.send_response(401)
|
|
|
|
self.send_header('WWW-Authenticate', 'text/html')
|
|
|
|
self.send_header('Content-type', 'text/html')
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
def do_INTERNALERROR(self):
|
|
|
|
self.send_response(500)
|
|
|
|
self.send_header('Content-type', 'text/html')
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
def do_POST(self):
|
|
|
|
if self.headers['Authorization'] != 'Basic ' + self.key:
|
2019-04-03 07:47:43 +00:00
|
|
|
self.do_AUTHHEAD()
|
2018-09-24 11:52:34 +00:00
|
|
|
else:
|
|
|
|
data_string = self.rfile.read(int(self.headers['Content-Length']))
|
|
|
|
|
|
|
|
try:
|
|
|
|
response = rpc_call(data_string)
|
|
|
|
if response is not None:
|
|
|
|
self.do_HEAD()
|
|
|
|
self.wfile.write(bytes(response.encode(encoding='ascii')))
|
|
|
|
except ValueError:
|
|
|
|
self.do_INTERNALERROR()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
global rpc_sock
|
|
|
|
|
2020-04-30 09:23:15 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
rpc_sock = args.sock
|
2018-09-24 11:52:34 +00:00
|
|
|
|
|
|
|
# encoding user name and password
|
2020-04-30 09:23:15 +00:00
|
|
|
key = base64.b64encode((args.user+':'+args.password).encode(encoding='ascii')).decode('ascii')
|
2018-09-24 11:52:34 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
ServerHandler.key = key
|
2020-04-30 09:23:15 +00:00
|
|
|
httpd = HTTPServer((args.host, args.port), ServerHandler)
|
|
|
|
if args.cert is not None:
|
|
|
|
httpd.socket = ssl.wrap_socket(httpd.socket, certfile=args.cert, server_side=True)
|
2018-09-24 11:52:34 +00:00
|
|
|
print('Started RPC http proxy server')
|
|
|
|
httpd.serve_forever()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print('Shutting down server')
|
|
|
|
httpd.socket.close()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|