分類
程序

免費使用 AWS Lambda 的一個示例

這裏演示一下免費使用 AWS Lambda 創建一個查詢網頁大小的接口。之所以直接使用 Lambda 生成的鏈接而不是 API Gateway 的鏈接,是因爲 API Gateway 的免費額度是在註冊帳號的一年內有效。而且 API Gateway 最大超時只有 30 秒,而 Lambda 可以設置到 15 分鐘。

首先註冊一個帳號,我記得只要郵箱和手機號就行,不用綁定支付方式。然後在控制台搜索 Lambda 進入 Lambda 控制台。點擊 Funtction > Create function. 選擇 Author from scratch,輸入函數名字沒有特殊要求,Runtime 選 Python 3.10,Architecture 選 x86 和 arm64 都可以,各有免費額度。點開 Advanced settings,勾選 Enable function URL,勾選 None。CORS 看需要也可以勾上,然後點擊右下角 Create function 就可以了。

在函數頁面 Code 標籤頁貼上如下代碼:

import json
import os
import random
import urllib.request

UA = '''Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edge/19041.423
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 OPR/87.0.4390.99
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Vivaldi/6.1.3035.111
Mozilla/5.0 (Windows NT 10.0; WOW64; rv:85.0) Gecko/20100101 Firefox/85.0
Mozilla/5.0 (Windows NT 10.0; Win64; x64; Trident/7.0; rv:11.0) like Gecko
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.51 Safari/537.36
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36'''

US_LIST=UA.split()

def lambda_handler(event, context):
    event_body_str = event['body']
    event_body = json.loads(event_body_str)
    # print(str(event_body))
    if event_body['secret'] == os.environ.get('secret'):
        try:
            t_ua = random.choice(US_LIST)
            res = urllib.request.urlopen(urllib.request.Request(
                url=event_body['url'],
                headers={"User-Agent": t_ua,},
                method='GET'),
                timeout=180)
            content = res.read()
            return {
                'statusCode': 200,
                'body': json.dumps(str(len(content))+' '+event_body['url'])
            }
        except:
            return {
                'statusCode': 500,
                'body': json.dumps('request failed.')
            }
    else:
        return {
            'statusCode': 500,
            'body': json.dumps('unauthorized.')
        }

secret 的環境在 Configuration > Environment variables 中添加。在 General configuration 中可以設置函數的超時時長 Timeout,默認是 3 秒。在 Function URL 裏可以看到我們需要的 URL 類似:https://4xyqiuexi7l3fcnuyuevjgclei0jhpuy.lambda-url.us-east-1.on.aws/ 。使用如下 curl 命令測試下:

curl -X POST -H "Content-Type: application/json" -d '{"secret":"Dn2Xk7d7RDkK_8bILy7lVe2NJlE4y7T_", "url":"http://ip-api.com/json"}' https://4xyqiuexi7l3fcnuyuevjgclei0jhpuy.lambda-url.us-east-1.on.aws/
#應該會得到如下結果
"296 http://ip-api.com/json"

免費額度使用情況可以在 Billing > Free Tier 頁面查看。Lambda 服務每月有 400000 seconds(GB-Second,如果的函數和我的一樣用的是 128 MB,那就可以乘以 8)和 1000000 次請求。

分類
其它

中國移動用戶不要購買AU索尼手機

時間來到 2023 年,國內的二手機市場也迎來了新一代的索尼手機,其中 1、5、10 系列都各有所長。唯一需要注意的就是 AU 的手機對於中國移動用戶來說缺少的頻段過多,輕則導致上網不暢,重則漏接電話短信,除非你只是連 Wi-Fi。而 Docomo 雖然也缺一個移動頻段,但是日常使用中並不影響。關於二手索尼的版本,我推薦的順序是港版(雙卡雙待可刷機) > 歐版(可刷機) > Docomo日版 > Au日版。 當然市場價格應該也是這個順序。

分類
Linux

Fedora 35 失去支持後升級 37

Fedora 失去支持後

失去支持後,dnf upgrade 會報錯 "Failed to download metadata for repo 'updates'" 。可以修改 /etc/yum.repos.d/ 中如下四個文件:fedora.repo fedora-modular.repo fedora-updates.repo fedora-updates-modular.repo 中的兩行

baseurl=https://archives.fedoraproject.org/pub/archive/fedora/linux/updates/$releasever/Everything/$basearch/
#metalink=https://mirrors.fedoraproject.org/metalink?repo=fedora-$releasever&arch=$basearch

修改後便不再報錯,但是由於 35 已失去官方支持,所以不會再有更新。

升級 Fedora 系統

!畢竟是更新系統,所以重要資料記得備份。!

可以通過 dnf system-upgrade 來方便的升級到 36 或 37(但是不能直接升級到38)。升級系統請參考官方的 DNF System Upgrade 以及 Upgrade to Fedora 37 from Fedora 36 using DNF。大約要下載 2G 大小的文件,下載後升級過程需要半個小時左右。主要命令如下:

#更新一下,如果有更新之後要重啓一下
sudo dnf upgrade --refresh
#安裝系統升級 dnf 插件
sudo dnf install dnf-plugin-system-upgrade
#下載要升級到的版本,如 37
sudo dnf system-upgrade download --releasever=37
#下載完畢,執行系統更新
sudo dnf system-upgrade reboot

執行 system-upgrade download 前需要把前面四個文件改回去,不然 archive 站點是沒有新系統的資料包的。

分類
程序

在 VS Code 中調試 Django Q 任務

在 .vscode 文件夾中創建一個 lanch.json 文件, 內容如下:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Django Q",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "args": [
                "qcluster"
            ],
            "django": true,
            "env": {
                "DJANGO_SETTINGS_MODULE": "YOUR_PROJECT_NAME.settings"
            },
            "gevent": true
        },
        // Your other configurations go here
    ]
}

只需修改兩個地方:program 裏的 ${workspaceFolder} 是變量,如果你項目的 manage.py 在 VS Code 項目根目錄下,那這行就不用修改。 DJANGO_SETTINGS_MODULE 這裏需要把 YOUR_PROJECT_NAME 替換成你項目的名字。然後按 Debug 按鈕就可以調試 Django Q task 了。

對於這個問題,ChatGPT 4 前前後後給出了五次錯誤配置,不得不記錄一下。

分類
方法

Bislama introduction from Air Vanuatu

In the late 19th century at the height of the practice of Blackbirding, thousands of Ni-Vanuatu were forced to work on plantations in Australia and Fiji. With several languages being spoken in these plantations, a form of pidgin English was developed, combining English vocabulary with grammatical structures typical of languages in the region. This early plantation pidgin is the origin not only of Bislama, but also of Tok Pisin of Papua New Guinea and Pijin of the Solomon Islands.

This pidgin started spreading over the Vanuatu archipelago at the turn of the 20th century, as the survivors of Blackbirding began to come back to their native islands. Knowledge of this pidgin would facilitate communication not only with European traders and settlers, but also between native populations of remote islands within the archipelago.

Ni-Vanuatu began to add their own words and pronunciation (today 95% of Bislama is based on English, with a few dozen French words and ‘island language’ thrown in).

Over the past century or so, Bislama has evolved to what is currently spoken and written in Vanuatu.

In order to understand Bislama, common advice is to throw in the words "long" and "blong" a few times every sentence and you'll just about have It.

For example,

Stoa kolosap long haos: The store next to the house.

Mi bin stap long ples ia bifo: I have stayed at this place before. Breakdown of the sentence:
- "Mi" means "I"
- "bin" is a past tense marker, indicating that the action happened in the past.
- "stap" means "staying" or "living."
- "long" is a preposition that means "in" or "at."
- "ples" means "place."
- "ia" is a demonstrative marker that can mean "this" or "that" depending on the context.
- "bifo" means "before" or "previously."

Mi stap long stoa: I am at the store.

Jea long haos: The chair in the house.

Buk blong mi: The book that belongs to me, my book

Man Amerika: Man from America, American.

Hemi woman blong saiens: She is a woman of science, she is a scientist.

Here's a few other phrases to get you going on your journey to discovering this fascinating and fun new language.

  • How much is that? Hamas long hem?
  • How much is this? Hamas long hemia?
  • Do you know..? Yu save (pronounced savvy)
  • I don’t know. Mi no save
  • This is broken Samting ia hemi bugarup or Samting ia i brok
  • The best Nambawan
  • How are you? Olsem wanem
  • I'm okay la oreat
  • Water Wota
  • Drinking water Freswota
  • Ocean Solwota
  • Full / too much Fulap
  • Thank you Tankyu
  • Thank you very much Tankyu tumas
  • My name is... Nem blong mi
  • What time does the plane land? Wanem taem plen ia lan?
  • Food/eat Kai Kai
  • Dictionary Diksonari

本文更新於 2023/02/20。

分類
程序

使用 Cloudflare Worker 中轉 HTTP 請求

Cloudflare Worker 可以方便的中轉 HTTP 請求,下面示例是我之前用過的,算是密碼保護的中轉特定請求。其中的 X_Custom_PSK 算是密碼,在 Settings > Variables 設置,這樣就只有我的程序可以請求。

addEventListener("fetch", event => {

  const psk = event.request.headers.get("X_Custom_PSK");
  if (psk === X_Custom_PSK) {
    event.respondWith(handleRequest(event.request));
  }else{
    const failed_response = new Response('Sorry, you have supplied an invalid key.', {
      status: 403,
    });
    event.respondWith(failed_response);
  }
})

async function handleRequest(request) {
  const resp = await fetch('https://domain.ltd/checkpoint/list', {
      method: 'POST',
      headers: {
          'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.3 Safari/537.36',
          'Accept': '*/*',
          'Accept-Language': 'zh-CN;q=0.8,zh;q=0.6,en-US;q=0.4,en;q=0.2',
          'Origin': 'https://domain.ltd',
          'DNT': '1',
          'Connection': 'keep-alive',
          'Referer': 'https://domain.ltd/',
          'Sec-Fetch-Dest': 'empty',
          'Sec-Fetch-Mode': 'cors',
          'Sec-Fetch-Site': 'cross-site'
      },
      body: new URLSearchParams({
          'page': '1',
          'pageSize': '10',
          'latitude': '22.5',
          'longitude': '114.0',
          'queryType': '0'
      })
  });
  return resp;
}

下面這個則是用一個 worker 代理多個網站。

addEventListener("fetch", event => {
  let url=new URL(event.request.url);
  if (event.request.url.indexOf('shaman')>-1){
      url.hostname="ft.shaman.eu.org";
  }else{
      url.hostname="www.rfi.fr";
  }  
  let request=new Request(url,event.request);
  event.respondWith(fetch(request));
});
分類
软件

Proton VPN stops network after suspend in Fedora

Proton VPN的免費用戶有日本、荷蘭和美國三個國家的節點可供使用,安卓用戶可以從 F-Driod 下載官方客戶端,Fedora 需要先在添加軟件源然後就可以方便安裝。使用的方便程度以及免費節點的速度都非常好。但是牆內直接是用不了的。

有一個問題是,在Fedora 中,連接着 Proton VPN 的時候 Suspend,然後喚醒的時候有機率會網絡不通,其實是 Proton VPN 異常關閉導致的。Proton VPN 生成了一個包含 ipv6leak 的連接,用來防止泄露 IP,但是異常關閉的時候,卻沒來得及刪除它,所以網絡都斷了。可以嘗試這樣解決:

#list all the links, find the link contains 'ipv6leak'
ip link
#remove the link
sudo ip link delete ipv6leakintrf0

值得一提的是,如果你同時有使用 NextDNS,DNS 也可能成爲網絡中斷假象的原因之一。測試辦法有兩個,一是使用 Tor 打開任意網頁,因爲 Tor 並不依賴系統的 DNS 設置,如果能上網證明本地 DNS 可能有問題。二是使用 dig 命令,如果運行 dig ft.shaman.eu.org 返回 no servers could be reached 但是運行 dig ft.shaman.eu.org @45.90.28.136( 牆內可用 114.114.114.114 )返回正確 IP 地址,則也證明 DNS 可能有問題。可嘗試重啓 NextDNS 來解決:

nextdns restart