抢占式实例搭建 FRP 服务

出差需求增加时,FRP 服务需要更新到最近的地域,降低延迟。

  1. 安装 FRP 服务
    # 获取安装包
    wget https://github.com/fatedier/frp/releases/download/v0.53.0/frp_0.53.0_linux_amd64.tar.gz
    # 解压缩包
    ls
    tar -xzvf frp_0.53.0_linux_amd64.tar.gz
    # 改名文件夹
    mv frp_0.53.0_linux_amd64 frp
    cd frp
    # 修改frp服务端配置文件
    vim frps.ini
    [common]
    bind_port = 7000
    kcp_bind_port = 7000
    bind_udp_port = 7001
    token = J_
    dashboard_port = 7500
    dashboard_user = admin
    dashboard_pwd = J_
    enable_prometheus = true
    # 后台运行
    nohup ./frps -c frps.ini >/dev/null 2>&1 &
  2. 结合抢占式实例特点,设置开机自动运行 FRP 服务,同步 IP 到域名,减少额外操作
    配置 FRP
    # 创建 systemd 服务文件
    sudo nano /etc/systemd/system/frps.service
    # 配置文件内容
    [Unit]
    Description=FRP Server Service
    After=network.target
    [Service]
    Type=simple
    ExecStart=/root/frp/frps -c /root/frp/frps.ini
    Restart=on-failure
    User=root
    WorkingDirectory=/root/frp
    [Install]
    WantedBy=multi-user.target
    # 启用并启动服务
    sudo systemctl enable frps.service
    sudo systemctl start frps.service
    sudo systemctl status frps.service

    配置 IP 同步

    import json
    import requests
    import time
    from tencentcloud.common import credential
    from tencentcloud.common.profile.client_profile import ClientProfile
    from tencentcloud.common.profile.http_profile import HttpProfile
    from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
    from tencentcloud.dnspod.v20210323 import dnspod_client, models
    def get_public_ip():
     while True:
         try:
             response = requests.get('https://api.ipify.org?format=json')
             response.raise_for_status()
             return response.json()['ip']
         except requests.RequestException:
             time.sleep(5)  # 如果请求失败,等待5秒钟
    def update_dns_record(client, ip_value):
     try:
         req = models.ModifyRecordRequest()
         params = {
             "Domain": "Domain", # 主域名
             "SubDomain": "SubDomain", # 子域名
             "RecordType": "A",
             "RecordLine": "默认",
             "Value": ip_value,
             "RecordId": x # api查询
         }
         req.from_json_string(json.dumps(params))
         resp = client.ModifyRecord(req)
         print(resp.to_json_string())
     except TencentCloudSDKException as err:
         print(err)
    # 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey
    cred = credential.Credential("Id", "Key")
    # 实例化一个http选项
    httpProfile = HttpProfile()
    httpProfile.endpoint = "dnspod.tencentcloudapi.com"
    # 实例化一个client选项
    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    # 实例化要请求产品的client对象,clientProfile是可选的
    client = dnspod_client.DnspodClient(cred, "", clientProfile)
    # 初始IP地址设置为空字符串
    last_ip = ""
    while True:
     current_ip = get_public_ip()
     if current_ip != last_ip:
         print(f"IP has changed to {current_ip}. Updating DNS record...")
         update_dns_record(client, current_ip)
         last_ip = current_ip
     else:
         print(f"IP has not changed. Next check in 5 minutes.")
     time.sleep(300)  # 每5分钟检查一次

    配置配置 IP 同步开机自启动

    # 创建开机服务
    sudo nano /etc/systemd/system/update_dns.service
    # 修改开机文件
    [Unit]
    Description=Update DNS Record Service
    After=network.target
    [Service]
    Type=simple
    ExecStart=/usr/bin/python3 <your-script-path>
    Restart=on-failure
    [Install]
    WantedBy=multi-user.target
    # 启用并启动服务
    sudo systemctl enable update_dns.service
    sudo systemctl start update_dns.service
    sudo systemctl status update_dns.service

本文链接:

https://ma.ge/archives/684.html