移除tempfile的硬链接

本文提供了一个使用C语言实现的示例程序,演示如何创建符号链接并读取该链接的目标路径。通过symlink和readlink函数的应用,程序能够创建指向指定文件的符号链接,并输出该链接指向的实际文件名。

移除tempfile的硬链接


  
1 #include < sys / types.h >
2 #include < sys / stat.h >
3 #include < unistd.h >
4
5   int main( int argc, char ** argv)
6 {
7 char symname[] = { " mysym " };
8 char buf[ 80 ];
9
10 if (argc < 2 )
11 return 0 ;
12
13 if (symlink(argv,symname) ==- 1 )
14 {
15 printf( " symlink error!\n " );
16 exit( 1 );
17 }
18
19 if ( ! readlink(symname,buf, sizeof (buf)))
20 {
21 printf( " readlink error!\n " );
22 exit( 1 );
23 }
24 printf( " name of symbol link: %s.\n " ,buf);
25
26 exit( 0 );
27 }
1.“https://github.com/spacemeowx2/socat-windows/releases”显示404 找不到啊 2.WARNING: Enabling and disabling experimental features do not take effect until next start of PowerShell. WARNING: Enabling and disabling experimental features do not take effect until next start of PowerShell. [PS7 21:31:00] C:\Users\Administrator\Desktop [master] > function Install-Socat { >> param( >> [Parameter(Mandatory = $false)] >> [string]$InstallDrive = "E:", >> >> [Parameter(Mandatory = $false)] >> [string]$InstallDir = "Program Files\Socat" >> ) >> >> # 强制使用TLS 1.2 >> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 >> >> # 1. 规范化路径 >> $fullPath = [System.IO.Path]::GetFullPath("$InstallDrive\$InstallDir").TrimEnd('\') >> Write-Host "安装目录: $fullPath" -ForegroundColor Cyan >> >> # 2. 管理员权限检查 >> if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { >> Write-Warning "需要管理员权限安装程序" >> return $null >> } >> >> # 3. 检查现有安装 >> $existingPath = (Get-Command socat -ErrorAction SilentlyContinue | >> Select-Object -ExpandProperty Source -First 1) ?? $null >> >> if ($existingPath) { >> Write-Host "已安装于: $existingPath" -ForegroundColor Green >> return $existingPath >> } >> >> # 4. 创建安装目录 >> try { >> if (-not (Test-Path $fullPath)) { >> $null = New-Item -Path $fullPath -ItemType Directory -Force >> Write-Host "创建目录成功" -ForegroundColor Green >> } >> } catch { >> Write-Error "创建目录失败: $($_.Exception.Message)" >> return $null >> } >> >> # 5. 本地镜像解决方案 - 硬编码base64编码的socat.exe >> $tempFile = Join-Path $env:TEMP "socat.exe" >> $socatBase64 = @" >> TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA6AAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAADk4Gd4kF9sXpBfbF6QX2xe8d4kXJxf" >> ... [完整的base64编码字符串 - 由于长度限制此处省略] ... >> AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA >> "@ >> >> # 尝试从本地镜像安装 >> try { >> Write-Host "尝试从本地镜像安装..." -ForegroundColor Cyan >> $bytes = [Convert]::FromBase64String($socatBase64) >> [IO.File]::WriteAllBytes($tempFile, $bytes) >> >> # 验证文件 >> if ((Get-Item $tempFile).Length -gt 500KB) { >> Write-Host "本地镜像验证成功" -ForegroundColor Green >> Copy-Item -Path $tempFile -Destination (Join-Path $fullPath "socat.exe") -Force >> $socatExe = Join-Path $fullPath "socat.exe" >> goto EnvironmentSetup >> } >> } catch { >> Write-Warning "本地镜像安装失败: $($_.Exception.Message)" >> } >> >> # 6. 增强的网络下载方案 >> $tempFile = Join-Path $env:TEMP "socat-win64.zip" >> $downloadUrls = @( >> "https://cdn.jsdelivr.net/gh/spacemeowx2/socat-windows@latest/socat_x64.zip", >> "https://raw.fastgit.org/spacemeowx2/socat-windows/releases/latest/download/socat_x64.zip", >> "https://ghproxy.com/https://github.com/spacemeowx2/socat-windows/releases/latest/download/socat_x64.zip" >> ) >> >> :downloadLoop foreach ($url in $downloadUrls) { >> try { >> Write-Host "尝试下载: $url" -ForegroundColor Cyan >> >> # 方法1:使用.Net HttpClient(支持现代TLS) >> try { >> Write-Host "使用HttpClient下载..." -ForegroundColor DarkYellow >> $handler = [System.Net.Http.HttpClientHandler]::new() >> $client = [System.Net.Http.HttpClient]::new($handler) >> $client.Timeout = [System.TimeSpan]::FromSeconds(30) >> $client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0") >> >> $response = $client.GetAsync($url).Result >> if (-not $response.IsSuccessStatusCode) { >> throw "HTTP错误: $($response.StatusCode)" >> } >> >> $bytes = $response.Content.ReadAsByteArrayAsync().Result >> [System.IO.File]::WriteAllBytes($tempFile, $bytes) >> >> # 验证文件 >> if ((Get-Item $tempFile).Length -lt 500KB) { >> throw "文件过小" >> } >> >> Write-Host "下载成功!" -ForegroundColor Green >> break downloadLoop >> } catch { >> Write-Warning "HttpClient失败: $($_.Exception.Message)" >> } >> >> # 方法2:使用WebClient(兼容模式) >> try { >> Write-Host "尝试WebClient下载..." -ForegroundColor Yellow >> $wc = New-Object System.Net.WebClient >> $wc.Headers.Add('User-Agent', 'Mozilla/5.0') >> $wc.DownloadFile($url, $tempFile) >> >> # 验证文件 >> if ((Get-Item $tempFile).Length -lt 500KB) { >> throw "文件过小" >> } >> >> Write-Host "备用方法成功!" -ForegroundColor Green >> break downloadLoop >> } catch { >> Write-Warning "WebClient失败: $($_.Exception.Message)" >> } >> } catch { >> Write-Warning "下载失败: $($_.Exception.Message)" >> Remove-Item $tempFile -ErrorAction SilentlyContinue >> continue >> } >> } >> >> if (-not (Test-Path $tempFile)) { >> Write-Error "所有下载源均失败" >> >> # 提供手动安装指南 >> Write-Host @" >> 手动安装步骤: >> 1. 从以下链接下载最新版socat: >> https://github.com/spacemeowx2/socat-windows/releases >> 2. 解压到目录: $fullPath >> 3. 添加PATH环境变量: >> [Environment]::SetEnvironmentVariable('Path', `$env:Path + `";$fullPath`", 'Machine') >> 4. 重启PowerShell会话 >> "@ -ForegroundColor Yellow >> >> try { >> Start-Process "https://github.com/spacemeowx2/socat-windows/releases" >> } catch { >> Write-Warning "无法打开浏览器" >> } >> >> return $null >> } >> >> # 7. 解压文件 >> try { >> Expand-Archive -Path $tempFile -DestinationPath $fullPath -Force >> $socatExe = Get-ChildItem -Path $fullPath -Filter "socat.exe" -Recurse | >> Select-Object -First 1 -ExpandProperty FullName >> >> if (-not $socatExe) { >> throw "未找到socat.exe" >> } >> } catch { >> Write-Error "解压失败: $($_.Exception.Message)" >> return $null >> } finally { >> Remove-Item $tempFile -ErrorAction SilentlyContinue >> } >> >> # 8. 环境变量设置 >> :EnvironmentSetup >> $currentPath = [Environment]::GetEnvironmentVariable("Path", "Machine") >> if ($currentPath -notlike "*$fullPath*") { >> [Environment]::SetEnvironmentVariable("Path", "$currentPath;$fullPath", "Machine") >> Write-Host "已添加PATH环境变量" -ForegroundColor Green >> } >> >> $env:Path = [Environment]::GetEnvironmentVariable("Path", "Machine") >> >> # 9. 验证安装 >> if (Test-Path $socatExe) { >> try { >> $versionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($socatExe) >> Write-Host "socat 安装成功! 版本: $($versionInfo.FileVersion)" -ForegroundColor Green >> return $socatExe >> } catch { >> Write-Host "socat 安装成功: $socatExe" -ForegroundColor Green >> return $socatExe >> } >> } else { >> Write-Error "安装后文件验证失败" >> return $null >> } >> } [PS7 21:31:47] C:\Users\Administrator\Desktop [master] > function New-TcpTunnel { >> param( >> [Parameter(Mandatory=$true)] >> [ValidateRange(1, 65535)] >> [int]$LocalPort, >> >> [Parameter(Mandatory=$true)] >> [string]$RemoteHost, >> >> [Parameter(Mandatory=$true)] >> [ValidateRange(1, 65535)] >> [int]$RemotePort, >> >> [string]$SocatPath, >> [int]$TimeoutSeconds = 15, >> [switch]$Background >> ) >> >> # 1. 获取socat路径 >> if (-not $SocatPath -or -not (Test-Path $SocatPath)) { >> $SocatPath = Install-Socat >> if (-not $SocatPath) { >> throw "socat安装失败,请检查错误日志" >> } >> } >> >> # 2. 创建日志文件 >> $logDir = Join-Path $env:ProgramData "SocatLogs" >> if (-not (Test-Path $logDir)) { >> $null = New-Item -Path $logDir -ItemType Directory -Force >> } >> >> $logTime = (Get-Date).ToString("yyyyMMdd-HHmmss") >> $stdOutFile = Join-Path $logDir "socat-$LocalPort-$logTime-out.log" >> $stdErrFile = Join-Path $logDir "socat-$LocalPort-$logTime-err.log" >> >> # 3. 启动socat进程 >> $processArgs = @{ >> FilePath = $SocatPath >> ArgumentList = "TCP-LISTEN:$LocalPort,reuseaddr,fork TCP:$($RemoteHost):$RemotePort" >> NoNewWindow = $true >> RedirectStandardOutput = $stdOutFile >> RedirectStandardError = $stdErrFile >> PassThru = $true >> } >> >> if ($Background) { >> $processArgs.WindowStyle = 'Hidden' >> } >> >> try { >> $process = Start-Process @processArgs >> Write-Host "socat进程已启动 (PID: $($process.Id))" -ForegroundColor Cyan >> } catch { >> throw "启动socat失败: $($_.Exception.Message)" >> } >> >> # 4. 验证端口激活 >> $portActive = $false >> $startTime = Get-Date >> $maxWaitTime = New-TimeSpan -Seconds $TimeoutSeconds >> >> while (-not $portActive -and ((Get-Date) - $startTime) -lt $maxWaitTime) { >> Start-Sleep -Milliseconds 500 >> >> # 使用NetTCPIP模块检查端口 >> try { >> $portActive = [bool](Get-NetTCPConnection -LocalPort $LocalPort -ErrorAction SilentlyContinue) >> } catch { >> # 备用TCP客户端检查 >> try { >> $socket = New-Object System.Net.Sockets.TcpClient >> $socket.Connect("localhost", $LocalPort) >> $portActive = $socket.Connected >> $socket.Close() >> } catch {} >> } >> >> # 检查进程状态 >> if ($process.HasExited) { >> $exitCode = $process.ExitCode >> $errorLog = if (Test-Path $stdErrFile) { Get-Content $stdErrFile -Raw } else { "无错误日志" } >> throw "socat进程已退出 (代码: $exitCode)。错误信息: $errorLog" >> } >> } >> >> if (-not $portActive) { >> $errorLog = if (Test-Path $stdErrFile) { Get-Content $stdErrFile -Raw } else { "无错误日志" } >> throw "隧道创建失败,端口 $LocalPort 未激活。错误信息: $errorLog" >> } >> >> # 5. 返回隧道信息 >> return [PSCustomObject]@{ >> Process = $process >> Port = $LocalPort >> LogDir = $logDir >> Status = "Active" >> } >> } [PS7 21:32:29] C:\Users\Administrator\Desktop [master] > # 1. 强制使用本地镜像安装socat [PS7 21:32:36] C:\Users\Administrator\Desktop [master] > $socatPath = Install-Socat -InstallDrive "D:" -InstallDir "NetTools" 安装目录: D:\NetTools 尝试从本地镜像安装... WARNING: 本地镜像安装失败: Exception calling "FromBase64String" with "1" argument(s): "The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters." 尝试下载: https://cdn.jsdelivr.net/gh/spacemeowx2/socat-windows@latest/socat_x64.zip 使用HttpClient下载... WARNING: HttpClient失败: HTTP错误: NotFound 尝试WebClient下载... WARNING: WebClient失败: Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (404) Not Found." 尝试下载: https://raw.fastgit.org/spacemeowx2/socat-windows/releases/latest/download/socat_x64.zip 使用HttpClient下载... WARNING: HttpClient失败: HTTP错误: 尝试WebClient下载... WARNING: WebClient失败: Exception calling "DownloadFile" with "2" argument(s): "不知道这样的主机。 (raw.fastgit.org:443)" 尝试下载: https://ghproxy.com/https://github.com/spacemeowx2/socat-windows/releases/latest/download/socat_x64.zip 使用HttpClient下载... WARNING: HttpClient失败: HTTP错误: 尝试WebClient下载... WARNING: WebClient失败: Exception calling "DownloadFile" with "2" argument(s): "由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 [::ffff:59.24.3.174]:443 (ghproxy.com:443)" Install-Socat: 所有下载源均失败 手动安装步骤: 1. 从以下链接下载最新版socat: https://github.com/spacemeowx2/socat-windows/releases 2. 解压到目录: D:\NetTools 3. 添加PATH环境变量: [Environment]::SetEnvironmentVariable('Path', $env:Path + ";D:\NetTools", 'Machine') 4. 重启PowerShell会话 [PS7 21:33:24] C:\Users\Administrator\Desktop [master] > [PS7 21:33:24] C:\Users\Administrator\Desktop [master] > # 2. 创建后台HTTP隧道 [PS7 21:33:25] C:\Users\Administrator\Desktop [master] > $tunnel = New-TcpTunnel -LocalPort 8080 -RemoteHost "example.com" -RemotePort 80 -Background 安装目录: E:\Program Files\Socat 尝试从本地镜像安装... WARNING: 本地镜像安装失败: Exception calling "FromBase64String" with "1" argument(s): "The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters." 尝试下载: https://cdn.jsdelivr.net/gh/spacemeowx2/socat-windows@latest/socat_x64.zip 使用HttpClient下载... WARNING: HttpClient失败: HTTP错误: NotFound 尝试WebClient下载... WARNING: WebClient失败: Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (404) Not Found." 尝试下载: https://raw.fastgit.org/spacemeowx2/socat-windows/releases/latest/download/socat_x64.zip 使用HttpClient下载... WARNING: HttpClient失败: HTTP错误: 尝试WebClient下载... WARNING: WebClient失败: Exception calling "DownloadFile" with "2" argument(s): "不知道这样的主机。 (raw.fastgit.org:443)" 尝试下载: https://ghproxy.com/https://github.com/spacemeowx2/socat-windows/releases/latest/download/socat_x64.zip 使用HttpClient下载... WARNING: HttpClient失败: 文件过小 尝试WebClient下载... WARNING: WebClient失败: 文件过小 Install-Socat: Line | 21 | $SocatPath = Install-Socat | ~~~~~~~~~~~~~ | 解压失败: Start-Process: Line | 52 | $process = Start-Process @processArgs | ~~~~~~~~~~~~~~~~~~~~~~~~~~ | Parameters "-NoNewWindow" and "-WindowStyle" cannot be specified at the same time. socat进程已启动 (PID: ) Exception: Line | 89 | throw "隧道创建失败,端口 $LocalPort 未激活。错误信息: $errorLog" | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 隧道创建失败,端口 8080 未激活。错误信息: 无错误日志 [PS7 21:34:00] C:\Users\Administrator\Desktop [master] > [PS7 21:34:00] C:\Users\Administrator\Desktop [master] > # 3. 测试隧道 [PS7 21:34:00] C:\Users\Administrator\Desktop [master] > curl "http://localhost:$($tunnel.Port)" -UseBasicParsing Enter proxy password for user 'seBasicParsing': [PS7 21:34:04] C:\Users\Administrator\Desktop [master] > [PS7 21:34:05] C:\Users\Administrator\Desktop [master] > # 4. 停止隧道 [PS7 21:34:05] C:\Users\Administrator\Desktop [master] > $tunnel.Process | Stop-Process -Force Stop-Process: Cannot bind argument to parameter 'InputObject' because it is null. [PS7 21:34:05] C:\Users\Administrator\Desktop [master] > function New-NativeTunnel { >> param( >> [int]$LocalPort, >> [string]$RemoteHost, >> [int]$RemotePort >> ) >> >> $ruleName = "PortForward_${LocalPort}_to_${RemoteHost}_${RemotePort}" >> >> # 添加端口转发 >> netsh interface portproxy add v4tov4 listenport=$LocalPort listenaddress=0.0.0.0 connectport=$RemotePort connectaddress=$RemoteHost >> >> # 添加防火墙规则 >> netsh advfirewall firewall add rule name=$ruleName dir=in action=allow protocol=TCP localport=$LocalPort >> >> return [PSCustomObject]@{ >> LocalPort = $LocalPort >> RemoteHost = $RemoteHost >> RuleName = $ruleName >> } >> } [PS7 21:34:05] C:\Users\Administrator\Desktop [master] > [PS7 21:34:05] C:\Users\Administrator\Desktop [master] > # 使用示例 [PS7 21:34:05] C:\Users\Administrator\Desktop [master] > $tunnel = New-NativeTunnel -LocalPort 8080 -RemoteHost "example.com" -RemotePort 80 [PS7 21:34:05] C:\Users\Administrator\Desktop [master] > [PS7 21:34:05] C:\Users\Administrator\Desktop [master] >
09-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值