def start(app, host, port): fortunes_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../data/fortunes')) app.add_content(PaywalledContent("doggo", 2, get_doggo)) app.add_content(PaywalledFortune("wisdom", 1, fortunes_path)) app.add_content(PaywalledContent("teapot", 3, lambda _: ("HI I AM A TEAPOT", 418))) app.run(host=host, port=port, debug=True) # nosec app.join()
def doggo_proxy(channel_manager, receiver_privkey, proxy_state_filename): app = PaywalledProxy(channel_manager) app.add_content( PaywalledContent("kitten.jpg", 1, lambda _: ("HI I AM A KITTEN", 200))) app.add_content( PaywalledContent("doggo.jpg", 2, lambda _: ("HI I AM A DOGGO", 200))) app.add_content( PaywalledContent("teapot.jpg", 3, lambda _: ("HI I AM A TEAPOT", 418))) # app.add_content(PaywalledFile("test.txt", 10, "/tmp/test.txt")) app.run() yield app app.stop()
def test_dynamic_price(empty_proxy: PaywalledProxy, api_endpoint_address: str, client: Client, wait_for_blocks, receiver_privkey: str): proxy = empty_proxy endpoint_url = "http://" + api_endpoint_address price_cycle = cycle([1, 2, 3, 4, 5]) url_to_price = {} def price_fn(url: str): if url in url_to_price: price = url_to_price[url] else: price = next(price_cycle) url_to_price[url] = price return price content = PaywalledContent( 'resource_\d', price=price_fn, get_fn=lambda url: (url.split("_")[1], 200), ) proxy.add_content(content) response = requests.get(endpoint_url + '/resource_3') assert response.status_code == 402 headers = HTTPHeaders.deserialize(response.headers) assert int(headers.price) == 1 response = requests.get(endpoint_url + '/resource_5') assert response.status_code == 402 headers = HTTPHeaders.deserialize(response.headers) assert int(headers.price) == 2 response = requests.get(endpoint_url + '/resource_2') assert response.status_code == 402 headers = HTTPHeaders.deserialize(response.headers) assert int(headers.price) == 3 response = requests.get(endpoint_url + '/resource_3') assert response.status_code == 402 headers = HTTPHeaders.deserialize(response.headers) assert int(headers.price) == 1 channel = client.get_suitable_channel(headers.receiver_address, 2) wait_for_blocks(6) channel.balance = 2 headers = Munch() headers.balance = str(channel.balance) headers.balance_signature = encode_hex(channel.balance_sig) headers.sender_address = channel.sender headers.open_block = str(channel.block) headers = HTTPHeaders.serialize(headers) response = requests.get(endpoint_url + '/resource_5', headers=headers) assert response.status_code == 200 assert response.text.strip() == '"5"' close_channel_cooperatively(channel, receiver_privkey, client.channel_manager_address, 0)
def test_static_price(empty_proxy: PaywalledProxy, api_endpoint_address: str, client: Client, wait_for_blocks, receiver_privkey: str): proxy = empty_proxy endpoint_url = "http://" + api_endpoint_address content = PaywalledContent( 'resource', price=3, get_fn=lambda url: ('SUCCESS', 200), ) proxy.add_content(content) response = requests.get(endpoint_url + '/resource') assert response.status_code == 402 headers = HTTPHeaders.deserialize(response.headers) assert int(headers.price) == 3 channel = client.get_suitable_channel(headers.receiver_address, 3) wait_for_blocks(6) channel.balance = 3 headers = Munch() headers.balance = str(channel.balance) headers.balance_signature = encode_hex(channel.balance_sig) headers.sender_address = channel.sender headers.open_block = str(channel.block) headers = HTTPHeaders.serialize(headers) response = requests.get(endpoint_url + '/resource', headers=headers) assert response.status_code == 200 assert response.text.strip() == '"SUCCESS"' close_channel_cooperatively(channel, receiver_privkey, client.channel_manager_address, 0)
def get_webUI_reply(self, content: str, proxy_handle: PaywalledContent, price: int, headers: dict): headers.update({ "Content-Type": "text/html", }) data = proxy_handle.get_paywall(content, self.receiver_address, price, config.TOKEN_ADDRESS) reply = make_response(data, 402, headers) for hdr in (header.GATEWAY_PATH, header.CONTRACT_ADDRESS, header.RECEIVER_ADDRESS, header.PRICE, header.NONEXISTING_CHANNEL, header.TOKEN_ADDRESS): if hdr in headers: reply.set_cookie(hdr, str(headers[hdr])) return reply
def start(app, host, port): app.add_content(PaywalledContent("teapot", 1, lambda _: ("HI I AM A TEAPOT", 418))) app.run(host=host, port=port, debug=True) # nosec app.join()
state_file_name = 'echo_server.json' app_dir = click.get_app_dir('microraiden') if not os.path.exists(app_dir): os.makedirs(app_dir) state_file_name = os.path.join(app_dir, state_file_name) # set up a paywalled proxy # arguments are: # - private key to use for receiving funds # - file for storing state information (balance proofs) app = make_paywalled_proxy(private_key, state_file_name) # Add resource defined by regex and with a fixed price of 1 token # We setup the resource to return whatever is supplied as a second argument # in the URL. app.add_content(PaywalledContent( "echofix\/[a-zA-Z0-9]+", 1 * TKN_DECIMALS, lambda request: (request.split("/")[1], 200))) # Resource with a price determined by the second parameter app.add_content(PaywalledContent( "echodyn\/[0-9]+", lambda request: int(request.split("/")[1]) * TKN_DECIMALS, lambda request: (int(request.split("/")[1]), 200))) app.add_content(PaywalledProxyUrl( "p\/[0-9]+", 1 * TKN_DECIMALS, lambda request: 'google.com/search?q=' + request.split("/")[1])) # start the app. proxy is a WSGI greenlet, so you must join it properly app.run(debug=True) app.join() # now use echo_client to get the resources
if __name__ == '__main__': private_key = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' tempfile = os.path.join(os.path.expanduser('~'), '.raiden/echo_server.pkl') # set up a paywalled proxy # arguments are: # - channel manager contract # - private key to use for receiving funds # - temporary file for storing state information (balance proofs) app = PaywalledProxy(CHANNEL_MANAGER_ADDRESS, private_key, tempfile) # Add resource defined by regex and with a fixed price of 1 token # We setup the resource to return whatever is supplied as a second argument # in the URL. app.add_content( PaywalledContent("echofix\/[a-zA-Z0-9]+", 1, lambda request: (request.split("/")[1], 200))) # Resource with a price determined by the second parameter app.add_content( PaywalledContent("echodyn\/[0-9]+", lambda request: int(request.split("/")[1]), lambda request: (int(request.split("/")[1]), 200))) app.add_content( PaywalledProxyUrl( "p\/[0-9]+", 1, lambda request: 'google.com/search?q=' + request.split("/")[1])) # start the app. proxy is a WSGI greenlet, so you must join it properly app.run(debug=True) app.join() # now use echo_client to get the resources