Info Security Memo
  • Blog
  • Sitemap
    • Categories
  • Contact
  • About
  • Resources
  • Tools
  • 51sec.org

Build Confidence

Focusing on Information Security 

Info Security Notes

Cloudflare Workers and API Usage Collection

12/16/2020

0 Comments

 
Cloudflare Workers and API Usage Collection
Cloudflare Workers provides a serverless execution environment that allows you to create entirely new applications or augment existing ones without configuring or maintaining infrastructure.  Your Service Worker will intercept all HTTP requests destined for your domain, and can return any valid HTTP response. Your worker can make outgoing HTTP requests to any server on the public internet.


In this post, I lists some of my usage how Cloudflare Workers helps me to achieve my ideas. Before you can start creating your Cloudflare Workers, at least, you will need to have a free Cloudflare account created. It will work better if your own domain integrated with Cloudflare. 
  • Hosting a Serverless Static Web Page
  • Using your own domain for your Workers

Other posts:
  • Build a Serverless Bookmark Website Use Cloudflare Worker
  • Using Cloudflare Workers to Deploy A Free Google Drive Directory Indexer in 5 Minutes
  • Set Up CloudFlare Workers to Use Your Own Domain
  • Build a Reverse Proxy Using Cloudflare Workers


Hosting a Serverless Static Web Page


1 Check Cloudflare examples

Return HTML example page: https://developers.cloudflare.com/workers/examples

Deliver an HTML page from an HTML string directly inside the Worker script.

const html = `<!DOCTYPE html><body>  <h1>Hello World</h1>  <p>This markup was generated by a Cloudflare Worker.</p></body>`
async function handleRequest(request) {  return new Response(html, {    headers: {      "content-type": "text/html;charset=UTF-8",    },  })}
addEventListener("fetch", event => {  return event.respondWith(handleRequest(event.request))})

2 Copy example code into Workers app


3 Save and deploy example app

4 Get HTML code using WYSIWYG HTML Editor 
I used a Blogger HTML Editor to create a page then toggle it to HTML view to get code. Then copy it back to Workers app code area between <body> and </body>

const html = `<!DOCTYPE html><body>"copy your html code here"</body>`
async function handleRequest(request) {  return new Response(html, {    headers: {      "content-type": "text/html;charset=UTF-8",    },  })}
addEventListener("fetch", event => {  return event.respondWith(handleRequest(event.request))})



YouTube video:



Using your own domain for your Workers


1 Create a  sub-domain name 

As mentioned earlier, domain names, such as 51sec,org must be hosted on or pointing to CloudFlare, and other DNS registrars will result in various weird errors.
Domain hosting or pointing to CloudFlare is easy, create a dns A record and point to any valid ip address.



After a new DNS record is added, this A record name will be used for our Workers App's sub-domain. When adding this A record, name is the subdomain to be customized (such as  proxy.51sec.org). The value of IPv4 address does not matter, but the key is to enable the Proxied.


2 Associated domain names to Workers

After you create Workers switch to your domain's Workers tab for the domain name (note that the domain name is here, where in Workers you won't find a place to associate it with your domain and then add the association to Add route



3 Create a Workers Route



Route fills in the subdomain with /*, (proxy.51sec.org/*) ,Worker selects the worker application we created before, who needs to customize the domain name access, so you need to create a Worker and then associate , to represent access to the Worker service through this custom domain name: proxy.51sec.org/*

When you’re done, you can access the  Worker service through a custom domain name, such as https://proxy.51sec.org/



API to Set Up DDNS 

CloudFlare DDNS

CloudFlare itself does not have official DDNS support, but it can be implemented using CloudFlare API.  GitHub Project Cloudflare-ddns provides a nice script to update your CloudFlare DNS IP using API. I have forked it to my repository to use. 


Example : Ubuntu 18.04


Sudo -i
apt-get update -y & apt-get upgrade -y

First clone code from GitHub

git clone https://github.com/51sec/cloudflare-ddns.git

Enter the catalog


cd cloudflare-ddns/

Installation Pip


apt-get install python-pip

Installation dependencies

pip install -r requirements.txt

Rename the config.yaml.template file


mv config.yaml.template config.yaml

Modify config.yaml


nano config.yaml

It's almost like this:


%YAML 1.2
# CloudFlare DDNS updater script config.
---

# CloudFlare API key
# You can find this under Account > My account after logging into CloudFlare.
cf_key: 'cloudflare API Key'

# Email address for your CloudFlare account.
cf_email: 'CloudFlare log in email'

# Domain you're using CloudFlare to manage.
# If the host name you're updating is "ddns.domain.com", make this "domain.com".
cf_domain: 'root domain'

# The subdomain you're using for your DDNS A record.
# If the host name you're updating is "ddns.domain.com", make this "ddns".
# However, if you're updating the A record for the naked domain (that is, just
# "domain.com" without a subdomain), then set cf_subdomain to an empty value.
cf_subdomain: 'sub domain'

# CloudFlare service mode. This enables/disables CF's traffic acceleration.
# Enabled (orange cloud) is 1. Disabled (grey cloud) is 0.
cf_service_mode: 0

# If set to true, prints a message only when the record changes or when
# there's an error.  If set to 'false', prints a message every time even if
# the record didn't change.
quiet: false

# If set to true then we call the ec2metadata service for the instance
# public ip address rather than an external service.
aws_use_ec2metadata: false

# If set to true dig will be used to fetch the public IP which is better
# but not available on all systems.
use_dig: false

Get API key



Create subdomain



Run script


python cloudflare_ddns.py config.yaml

Add a scheduled task


crontab -e

Fill in the following


# Every 15 minutes, check the current public IP, and update the A record on CloudFlare.
*/15 * * * * /root/cloudflare-ddns/cloudflare_ddns.py /root/cloudflare-ddns/config.yaml >> /var/log/cloudflare_ddns.log

After scheduled job configuration completed, the IP will be updated to Cloudflare every 15 minutes







via Blogger https://ift.tt/2KrFo4P
December 15, 2020 at 10:38PM Cloud
0 Comments



Leave a Reply.

    Categories

    All
    Architecture
    Blog
    Checkpoint
    Cisco
    Cloud
    CyberArk
    F5
    Fortigate
    Guardium
    Juniper
    Linux
    Network
    Others
    Palo Alto
    Qualys
    Raspberry Pi
    Security
    SIEM
    Software
    Vmware
    VPN
    Wireless

    Archives

    March 2024
    February 2024
    January 2024
    December 2023
    November 2023
    October 2023
    September 2023
    August 2023
    July 2023
    June 2023
    May 2023
    April 2023
    March 2023
    February 2023
    January 2023
    December 2022
    November 2022
    October 2022
    September 2022
    August 2022
    July 2022
    June 2022
    May 2022
    April 2022
    March 2022
    February 2022
    January 2022
    December 2021
    November 2021
    October 2021
    September 2021
    August 2021
    July 2021
    June 2021
    May 2021
    April 2021
    March 2021
    February 2021
    January 2021
    December 2020
    November 2020
    October 2020
    September 2020
    August 2020
    July 2020
    October 2019
    September 2019
    June 2019
    July 2018
    May 2018
    December 2017
    August 2017
    April 2017
    March 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015
    September 2015
    August 2015
    July 2015
    June 2015
    May 2015
    April 2015
    March 2015

    Print Page:

    RSS Feed

    Email Subscribe
Powered by Create your own unique website with customizable templates.
  • Blog
  • Sitemap
    • Categories
  • Contact
  • About
  • Resources
  • Tools
  • 51sec.org