最近在自己家中的迷你主机上部署了一些自己用的服务,使用 frp 做内网穿透将迷你主机的服务映射到公网供自己使用。frp 支持 SSH 的端口复用,但需要使用到 Socat 这个工具。Socat 在 linux 和 mac 环境下都可以使用命令直接下载使用,但 windows 没有现成的库,网上找到的都是别人自己编译,也不知道靠不靠谱,所以决定使用 Github action 全自动,透明的编译一版 windows 能直接用的 Socat 。目前已经做好了,项目地址:https://github.com/raikiriww/socat_windows

项目介绍

整个项目目前(2024-03-03)只有一个 readme 和 Github action 的执行文件。具体为:

name: Cygwin Build

on:
  # Triggers the workflow on push or pull request events but only for the "main" branch
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  build:
    permissions: write-all
    runs-on: windows-latest

    steps:
    - uses: actions/checkout@v2

    - name: Install Cygwin
      run: |
        choco install cygwin
        choco install cyg-get
        cyg-get gcc-g++ gcc-core cygwin32-gcc-g++ cygwin32-gcc-core make gcc-fortran gcc-objc gcc-objc++ libkrb5-devel libkrb5_3 libreadline-devel libssl-devel libwrap-devel tcp_wrappers         

    - name: Configure Environment
      run: |
        echo "C:\tools\cygwin\bin" >> $GITHUB_PATH        

    - name: Download and Extract the archive
      run: |
        curl -O http://www.dest-unreach.org/socat/download/socat-1.8.0.0.tar.gz
        tar -xvzf socat-1.8.0.0.tar.gz        

    - name: Execute Build Script in Cygwin
      run: |
        C:\tools\cygwin\bin\bash -lc "cd /cygdrive/d/a/socat_windows/socat_windows/socat-1.8.0.0 && ./configure && make && make install"
                
    - name: Copy Cygwin DLLs
      run: |
        Copy-Item "C:\tools\cygwin\bin\cygcrypto-3.dll" -Destination "D:\a\socat_windows\socat_windows\socat-1.8.0.0"
        Copy-Item "C:\tools\cygwin\bin\cygwin1.dll" -Destination "D:\a\socat_windows\socat_windows\socat-1.8.0.0"
        Copy-Item "C:\tools\cygwin\bin\cygssl-3.dll" -Destination "D:\a\socat_windows\socat_windows\socat-1.8.0.0"
        Copy-Item "C:\tools\cygwin\bin\cygreadline7.dll" -Destination "D:\a\socat_windows\socat_windows\socat-1.8.0.0"
        Copy-Item "C:\tools\cygwin\bin\cygwrap-0.dll" -Destination "D:\a\socat_windows\socat_windows\socat-1.8.0.0"
        Copy-Item "C:\tools\cygwin\bin\cygncursesw-10.dll" -Destination "D:\a\socat_windows\socat_windows\socat-1.8.0.0"
        Copy-Item "C:\tools\cygwin\bin\cygz.dll" -Destination "D:\a\socat_windows\socat_windows\socat-1.8.0.0"        
      shell: pwsh

    - name: Upload Artifacts
      if: ${{  success() }}
      uses: actions/upload-artifact@v2
      with:
        name: socat-1.8.0.0
        path: socat-1.8.0.0/

    - name: Archive production artifacts
      if: ${{  success() }}
      run: 7z a socat-1.8.0.0.zip D:\a\socat_windows\socat_windows\socat-1.8.0.0

    - name: Upload Release
      uses: softprops/action-gh-release@v1
      if: ${{  success() }}
      with:
        tag_name: 1.8.0.0
        files: socat-1.8.0.0.zip

整个编译过程在 Github 的 windows runner 上执行。使用 Cygwin 作为编译工具。编译过程分为下面几步:

  1. 初始化代码仓库。这步会将仓库的所有文件下载到 runner 中。
  2. 安装 Cygwin 和依赖的库。
  3. 将 Cygwin 的 bin 目录加到环境变量中。
  4. 下载 Socat 的源码。
  5. 使用 Cygwin 编译。
  6. 拷贝依赖的 dll 文件到编译好的程序目录。
  7. 打包编译好的程序作为 Artifact 。
  8. 将打包好的压缩包作为 Release 发布。

使用方法

  1. 下载项目 Releases 页面的程序压缩包。解压到本地。
  2. 将解压好的文件夹路径添加到 windows 的 PATH 环境变量中。

打开终端输入 socat 即可使用。编译好的程序在我自己的电脑上(windows11 22H2 22621.2506)运行良好。