# Gavin Andresen # 2010-06-12 04:09:43 # https://bitcointalk.org/index.php?topic=186.msg1513#msg1513 Joozero asked if I'd make the source to freebitcoins.appspot.com available, and I promised to start a thread explaining how I created it@p{--} and here it is. @p{par} freebitcoins is running on the Google App Engine. Well, the part you see is running on App Engine. @p{par} There's also a back-end bitcoind server running on a debian VPS that I've had for years. The App Engine code makes JSON-RPC calls via the App Engine url fetching API. Making that connection secure was a bit tricky; here's how I did it: @p{par} First, I connect to the VPS using https so the traffic is encrypted. @p{par} I need a secure connection because I add a secret pre-shared value to the JSON call. I'm not going to make the full source for freebitcoins fully open source because I don't want to bother with constantly working to keeping that secret value secret. I should hash the entire request with a secret key and an ever-increasing nonce, but I was lazy and I'm just not very worried about a man-in-the-middle attack between Google and my VPS. @p{par} I could have hacked my copy of the bitcoind C++ code to check for the secret value in requests and also modified it so it accepted connections from the Internet... but instead I wrote this little proxy server in Python that runs on the same box as bitcoind: @p{brk} Code: # @p{brk} # WSGI Proxy into the bitcoind daemon (which only listens on 127.0.0.1 for security). @p{brk} # Rejects non-HTTPs connections, or non-POST requests @p{brk} # Rejects queries that don't include a pre-shared secret value @p{brk} # @p{brk} # Apache calls this because of a directive in /etc/apache2/sites-available/xyz.com @p{brk} # @p{par} import hashlib @p{brk} import urllib2 @p{par} def application(environ, start_response): @p{brk} serverURL = "@s{(link)}" @p{par} def error_response(message): @p{brk} response_headers = [('Content-type', 'text/plain'), @p{brk} ('Content-Length', str(len(message)))] @p{brk} start_response('500 Internal Server error', response_headers) @p{brk} return message @p{par} if environ.get("HTTPS") != "1": @p{brk} return error_response("Insecure connections not allowed.") @p{par} request = environ.get("wsgi.input").read() @p{brk} secret = request[0:32] @p{brk} json_request = request[32:] @p{par} if hashlib.md5(" pre shared secret goes here "+json_request).hexdigest() != secret: @p{brk} return error_response("Authentication failed.") @p{par} req = urllib2.Request(serverURL, json_request) @p{brk} response = urllib2.urlopen(req) @p{brk} json_response = response.read() @p{par} status = '200 OK' @p{par} response_headers = [('Content-type', 'text/plain'), @p{brk} ('Content-Length', str(len(json_response)))] @p{brk} start_response(status, response_headers) @p{par} return [json_response] @p{brk} The other end of the connection is also django+Python code running on App Engine: @p{brk} Code: import hashlib @p{brk} import jsonrpc @p{brk} from google.appengine.api import urlfetch @p{par} def make_bitcoin_request(server_url, server_secret, method, params): @p{brk} json_request = { @p{brk} "jsonrpc": "2.0", @p{brk} "id": str(time.time()), @p{brk} "method": method, "params": params @p{brk} } @p{par} json_string = jsonrpc.dumps(json_request) @p{brk} secret = hashlib.md5(server_secret+json_string).hexdigest() @p{par} try: @p{brk} fetch_result = urlfetch.fetch(payment_server, @p{brk} method="POST", payload=secret+json_string, @p{brk} headers={ 'Content-Type' : 'text/plain' }) @p{brk} except urlfetch.Error, e: @p{brk} logging.error('make_bitcoin_request failed: '+str(e)) @p{brk} logging.error('Request:'+json_string) @p{brk} return None @p{par} if fetch_result.status_code != 200: @p{brk} logging.error('make_bitcoin_request failed; urlfetch status code %d'%(fetch_result.status_code)) @p{brk} logging.error('Request:'+json_string) @p{brk} return None @p{par} r = jsonrpc.loads(fetch_result.content) @p{brk} if r['error'] is not None: @p{brk} logging.error('make_bitcoin_request failed; JSON error returned') @p{brk} logging.error('Request:'+json_string) @p{brk} logging.error('Result:'+fetch_result.content) @p{brk} return None @p{par} return r['result'] @p{brk} @p{brk} I'm happy with how it is working. My biggest worry is that the bitcoind process might unexpectedly exit, or fill up the disk with debug.log messages, or just generally be flaky. But so far so good... @p{brk}