FRP配置内网穿透

服务器端配置

FRP 仓库: https://github.com/fatedier/frp

下载并解压 FRP

使用以下命令下载并解压 FRP:

wget https://github.com/fatedier/frp/releases/download/v0.61.1/frp_0.61.1_linux_amd64.tar.gz
tar -zxvf frp_0.61.1_linux_amd64.tar.gz

FRP 配置

编辑 FRP 服务器配置文件:

vim /path/to/frp/frps.toml

配置内容示例如下:

bindPort = 7000
auth.token = "<TOKEN>"

将服务设为系统任务

在 Ubuntu 系统中设置 FRP 服务器(frps)为开机自启动,按照以下步骤进行配置:

1. 创建 frps.service 服务文件

/etc/systemd/system/ 目录下创建 frps.service 文件:

sudo vim /etc/systemd/system/frps.service

添加以下内容,确保 ExecStart 的路径指向实际的 frps 可执行文件和配置文件路径:

[Unit]
Description=FRP Server Service
After=network.target
Wants=network.target

[Service]
Restart=on-failure
RestartSec=5
ExecStart=/path/to/frp/frps -c /path/to/frp/frps.toml

[Install]
WantedBy=multi-user.target

2. 重新加载 systemd 并设置开机自启

执行以下命令以重新加载 systemd 配置并设置 frps 服务开机自启:

# 重新加载 systemd 配置
sudo systemctl daemon-reload

# 设置 frps 服务开机自启
sudo systemctl enable frps.service

# 禁用 frps 服务开机自启(如需)
sudo systemctl disable frps.service

3. 启动并验证 frps 服务

启动 frps 服务并检查其状态:

# 启动 frps 服务
sudo systemctl start frps.service

# 查看 frps 服务状态
sudo systemctl status frps.service

# 重启 frps 服务
sudo systemctl restart frps.service

# 停止 frps 服务
sudo systemctl stop frps.service

如果服务状态显示为 active (running),则表示 frps 已成功配置为开机自启动。

注意事项

  • 确保 frps 可执行文件和配置文件路径正确无误。
  • 如果 frps 未能成功启动,可使用 journalctl -xe 命令查看日志以排查问题。

客户端配置

1. 正确下载并解压文件

下载 FRP

使用 PowerShell 下载 FRP 客户端:

Invoke-WebRequest -Uri "https://github.com/fatedier/frp/releases/download/v0.61.1/frp_0.61.1_windows_amd64.zip" -OutFile "C:PathToDownloadfrp_0.61.1_windows_amd64.zip"

解锁并解压文件

下载后,可能会被 Windows 防火墙阻止。请按照以下步骤操作:

  1. 解锁文件
    Unblock-File -Path "C:PathToDownloadfrp_0.61.1_windows_amd64.zip"
  2. 添加到 Windows Defender 排除项
    Add-MpPreference -ExclusionPath "C:PathToDownloadfrp_0.61.1_windows_amd64.zip"
  3. 解压缩文件
    Expand-Archive -Path "C:PathToDownloadfrp_0.61.1_windows_amd64.zip" -DestinationPath "C:PathToExtractfrp_0.61.1_windows_amd64"

2. 客户端文件配置

编辑 FRP 客户端配置文件 frpc.toml

serverAddr = "<服务器IP>"
serverPort = 7000
auth.token = "<TOKEN>"

[[proxies]]    # SSH 内网穿透
name = "ssh"
type = "tcp"
localIP = "127.0.0.1"
localPort = 22
remotePort = 6000

[[proxies]]    # 远程桌面内网穿透
name = "rdp"
type = "tcp"
localIP = "127.0.0.1"  # 本地 RDP 服务 IP 地址
localPort = 3389       # RDP 默认端口
remotePort = 6001      # 公网服务器上的端口,可自定义

3. 以正确的权限运行文件

添加 frpc.exe 到 Windows Defender 排除列表

Add-MpPreference -ExclusionPath "C:PathTofrpfrpc.exe"

以管理员身份运行 PowerShell

确保 PowerShell 以管理员身份运行:

Start-Process PowerShell -Verb RunAs

在新的管理员 PowerShell 窗口中执行 frpc.exe

4. 在 Windows 后台运行 frpc.exe

将以下脚本保存为 .ps1 文件并运行:

# 设置变量
$exePath    = "C:PathTofrpfrpc.exe"    # frpc 可执行文件路径
$configPath = "C:PathTofrpfrpc.toml"  # 配置文件路径
$outputLog  = "C:PathTofrpoutput.log" # 日志文件路径
$errorLog   = "C:PathTofrperror.log"  # 错误日志文件路径
$pidFile    = "C:PathTofrppid.txt"    # PID 文件路径

# 启动后台作业
$job = Start-Job -ScriptBlock {
    param($exe, $config, $outLog, $errLog, $pidFile)

    Write-Host "在后台作业中启动 frpc.exe ..."
    $process = Start-Process -FilePath $exe `
                             -ArgumentList ("-c $config") `
                             -RedirectStandardOutput $outLog `
                             -RedirectStandardError $errLog `
                             -NoNewWindow `
                             -PassThru

    # 保存进程 ID 到文件
    if ($process -ne $null) {
        $process.Id | Out-File -FilePath $pidFile
        Write-Host "进程已启动。PID 写入到 $pidFile。"
        Write-Host "标准输出记录在 $outLog。"
        Write-Host "错误输出记录在 $errLog。"
    } else {
        Write-Host "无法启动进程。"
    }

    Write-Host "可使用 Stop-Process -Id <PID> 终止该进程,PID 可在 $pidFile 中找到。"
} -ArgumentList $exePath, $configPath, $outputLog, $errorLog, $pidFile

Write-Host "frpc.exe 的后台作业已启动,作业 ID:$($job.Id)"
Write-Host "可使用 'Receive-Job -Id $($job.Id)' 查看作业输出。"
Write-Host "可使用 'Stop-Job -Id $($job.Id)' 停止该作业。"
Write-Host "也可以使用 PID(存储在 $pidFile 中)执行 'Stop-Process -Id <PID>' 终止 frpc.exe 进程。"

frpc 设置为 Windows 服务

由于 frpc 无法直接作为服务启动,需要使用第三方工具,例如 SrvanyUI

使用工具 SrvanyUI

步骤 1:下载并解压工具

SrvanyUI 工具解压到指定目录,例如:

C:\Program Files (x86)\SrvanyUI

步骤 2:创建服务

  1. 打开 srvanyui.exe
  2. 选择 文件增加,创建一项新服务,服务类型选择 自建服务

步骤 3:启动服务

系统会提示是否立即启动服务,选择 立即启动,或稍后通过项目管理器启动。

步骤 4:管理服务

服务创建成功后,可以在 服务管理器 中像管理其他服务一样管理 frpc 服务。

管理 frpc 服务

使用 PowerShell 管理 frpc 服务:

# 查看服务状态
Get-Service -Name frpc

# 启动服务
Start-Service -Name frpc

# 重启服务
Restart-Service -Name frpc

# 停止服务
Stop-Service -Name frpc

# 移除服务
Remove-Service -Name frpc

注意

  • 路径替换:请将示例中的路径 C:\PathTo.../path/to/... 替换为您实际的文件路径。
  • 权限:确保所有操作均在具有足够权限的用户下进行,以避免权限不足导致的配置失败。

免责声明

本文档仅供参考,请根据实际需求和环境进行调整。使用 FRP 及相关工具时,请确保遵守相关法律法规和安全规范。