FPW: Fast Python Worker / Feieryun PHP Worker
This program uses HTTP long polling technology to connect to Feieryun platform, transmitting the local web service to the public network, thus achieving remote access to the local service.
By default, it will forward request from visitor to the local service http://127.0.0.1/<url>
. If you want to modify this default address, you can modify the code to implement it.
You can customize the domain name prefix to forward, for example: xxxxx
, so you can access the local service through xxxxx.28820.com
. If the environment variable FPW_HOST
is not set, it will be generated as the domain name prefix by default, such as moon-flower-snake-red-apple.28820.com
.
Features:
-
No usage times and traffic restrictions
-
Customizable domain name prefix
-
Currently supports multiple request methods in the HTTP protocol
- Including but not limited to: GET, POST, PUT, DELETE, HEAD, OPTIONS, PATCH, TRACE
- WebSocket protocol is not supported currently
-
Supports HTTPS
-
Currently using Cloudflare CDN, may use other CDN acceleration in the future
-
Install Python
-
Install requests library
pip install requests
-
[Optional] Modify environment variables
FPW_HOST = 'xxxxx.28820.com' # xxxxx is the custom domain name prefix
-
Run main.py ~~
python main.py
-
Open your site ~~
If you want to implement a similar program by yourself, you can refer to this section.
The code uses Python, but the principle is universal.
-
Send a POST request to Feieryun's API, if the creation fails, an error message will be returned, and if it succeeds, the connection will be kept until a visitor visits this domain name.
-
The content of the POST request is:
url = 'https://' + FPW_HOST + '/' header = { 'fpw-host': FPW_HOST, 'fpw-token': FPW_TOKEN, 'user-agent': 'php-worker-v1', 'content-type': 'application/octet-stream' } body = ''
-
Send the request
r = requests.post(url, headers=header, data=body)
-
If a visitor visits this domain name at this time, the POST request that was just sent will return the visitor's request information, including the request header and request body.
-
The visitor's request header will be returned in JSON format in the Feieryun response header
fpw-header
forward_header = json.loads(r.headers["fpw-header"])
-
The visitor's request body will be returned in binary data in the Feieryun response body
forward_body = r.content
-
The visitor's request will be assigned a unique
fpw-id
, which will be used in subsequent reply requests to identify the requestfpw_id = r.headers["fpw-id"]
-
The visitor's request IP address, request address, and request method will be returned in the Feieryun response header
fpw-ip
,fpw-url
, andfpw-method
user_ip = r.headers["fpw-ip"] forward_url = r.headers["fpw-url"] forward_method = r.headers["fpw-method"]
-
-
Forward the visitor's request information to the local service
r = requests.request(forward_method, forward_url, headers=fpw_header, data=fpw_body)
-
Forward the response information of the local service to Feieryun Put the response header and response body of the local service into the Feieryun request header and request body respectively, and send them to Feieryun
url = 'https://' + FPW_HOST + '/' header = { 'fpw-host': FPW_HOST, 'fpw-token': FPW_TOKEN, 'fpw-id': fpw_id, # Used to identify this request 'fpw-status' = r.status_code, # Response status code of the local service 'fpw-header' = r.headers, # Response header of the local service 'user-agent': 'php-worker-v1', 'content-type': 'application/octet-stream' } body = r.content # Response body of the local service
-
Feieryun will send the response information of this request to the visitor, and this request will be suspended until a new visitor visits this domain name.
-
Repeat steps 1.3 - 4 until the program exits.
Note: The POST request connection may be disconnected, in which case you need to resend the POST request. If there is no online worker, accessing the domain name will return the default Feieryun page.