Windows 系统自带功能实现端口转发的完整指南

概述 Windows 系统提供了多种内置工具用于实现端口转发和网络流量重定向,无需安装第三方软件。本文详细介绍各种方法的原理、优缺点和实际应用。 方法一:netsh portproxy(最简便) 原理 netsh 是 Windows 的网络配置命令行工具,其 portproxy 功能可以实现 IPv4/IPv6 端口转发。 基础用法 添加端口转发规则 netsh interface portproxy add v4tov4 listenport=<本地端口> listenaddress=<本地地址> connectport=<远程端口> connectaddress=<远程地址> 参数说明: listenport:本地监听的端口 listenaddress:本地监听的 IP 地址(0.0.0.0 表示所有接口) connectport:转发目标的端口 connectaddress:转发目标的 IP 地址 实例 例1:转发本地 8080 到 localhost:3000 netsh interface portproxy add v4tov4 listenport=8080 listenaddress=0.0.0.0 connectport=3000 connectaddress=127.0.0.1 例2:转发本地 80 到远程服务器 80 netsh interface portproxy add v4tov4 listenport=80 listenaddress=0.0.0.0 connectport=80 connectaddress=192.168.1.100 管理规则 查看所有转发规则 netsh interface portproxy show all 查看 IPv4 转发规则 netsh interface portproxy show v4tov4 删除特定规则 netsh interface portproxy delete v4tov4 listenport=8080 listenaddress=0.0.0.0 删除所有规则 netsh interface portproxy delete v4tov4 关键点 ⚠️ 需要管理员权限 运行 支持 IPv4 转 IPv4、IPv4 转 IPv6、IPv6 转 IPv4、IPv6 转 IPv6 规则在系统重启后仍然生效 无需额外配置防火墙(通常) 方法二:Windows 防火墙入站规则 原理 通过防火墙规则结合程序代理实现转发。 ...

2026-09-14 · 4 分钟 · czyt

一些使用go-fyne的笔记

界面交互 中文字体设置 查阅相关资料,有下面几种解决方案,下面来依次说明。 环境变量 可以通过指定 FYNE_FONT 环境变量来使用替代字体 使用字体bundle 安装fyne工具,使用下面的命令: go install fyne.io/fyne/v2/cmd/fyne@latest 准备好要使用的字体,我们这里使用miSans ,使用下面命令 fyne bundle MiSans-Normal.ttf > bundle.go 然后创建一个theme package tinytheme import ( "fyne.io/fyne/v2" "fyne.io/fyne/v2/theme" "fyneHello/fontRes" "image/color" ) type ChineseTheme struct{} var _ fyne.Theme = (*ChineseTheme)(nil) func (m *ChineseTheme) Font(s fyne.TextStyle) fyne.Resource { return fontRes.ResourceMiSansTTF } func (*ChineseTheme) Color(n fyne.ThemeColorName, v fyne.ThemeVariant) color.Color { return theme.DefaultTheme().Color(n, v) } func (*ChineseTheme) Icon(n fyne.ThemeIconName) fyne.Resource { return theme.DefaultTheme().Icon(n) } func (*ChineseTheme) Size(n fyne.ThemeSizeName) float32 { return theme.DefaultTheme().Size(n) } 在程序里面使用字体主题 ...

2024-01-11 · 11 分钟 · czyt

一些windows定制的相关资源

脚本 移除Edge @echo off reg delete "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Microsoft Edge" /v "NoRemove" /f >NUL 2>nul reg delete "HKLM\SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\ClientState\{56EB18F8-B008-4CBD-B6D2-8C97FE7E9062}" /v "experiment_control_labels" /f >NUL 2>nul reg add "HKLM\SOFTWARE\WOW6432Node\Microsoft\EdgeUpdateDev" /v "AllowUninstall" /t REG_DWORD /d 1 /f >NUL 2>nul for /D %%I in ("%ProgramFiles(x86)%\Microsoft\Edge\Application\*") do ( if exist "%%I\Installer\setup.exe" ( echo Uninstalling Edge Chromium pushd "%%I\Installer" setup.exe --uninstall --msedge --force-uninstall --system-level --delete-profile popd ) ) msiexec /X{2BFF39DC-EFF0-355C-80CD-41D847013784} /qn /norestart set "value=MicrosoftEdgeAutoLaunch_.*" set "path=HKCU\Software\Microsoft\Windows\CurrentVersion\Run" for /f "tokens=1,*" %%A in ('reg query "%path%" /f "Microsoft" ^| findstr /R "%value%"') do ( reg delete "%path%" /v "%%A" /f ) del /q /f "%userprofile%\Desktop\Microsoft Edge.lnk" >NUL 2>nul 注册表 安全 智能应用助理 打开 ...

2023-09-20 · 1 分钟 · czyt

Avalonia使用备忘

安装 模板 可以安装一些Avalonia的dotnet模板 dotnet new install Templates.Prism.Avalonia dotnet new install Avalonia.Templates IDE插件 Rider里面安装插件 AvaloniaRider VisualStudio安装插件 Avalonia for Visual Studio Avalonia 中的一些特性 Binding $parent 您可以使用$parent符号绑定到目标在逻辑上的父级: <Button DockPanel.Dock="Bottom" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" x:CompileBindings="False" Command="{Binding $parent[Window].DataContext.AddItem}">Add Item </Button> <Border Tag="Hello World!"> <TextBlock Text="{Binding $parent.Tag}"/> </Border> 也可以通过在$parent符号添加索引器绑定到父控件的父控件: <Border Tag="Hello World!"> <Border> <TextBlock Text="{Binding $parent[1].Tag}"/> </Border> </Border> 索引器从0开始,因此$parent[0]等同于$parent。 还可以按类型绑定到祖先: <Border Tag="Hello World!"> <Decorator> <TextBlock Text="{Binding $parent[Border].Tag}"/> </Decorator> </Border> 最后,您可以组合索引器和类型: <Border Tag="Hello World!"> <Border> <Decorator> <TextBlock Text="{Binding $parent[Border;1].Tag}"/> </Decorator> </Border> </Border> 如果需要在祖先类型中包含XAML命名空间,一般使用:字符: ...

2023-07-31 · 8 分钟 · czyt

使用C#以编程方式切换Windows专注模式

缘起 最近需要以编程方式调用windows api实现windows10专注模式的切换,但是Google一圈,没有现成代码。找到的相关帖子要么是cpp的要么是rust的,而且是undocument的Windows api。 Csharp调用 以下是完整代码 public static class FocusAssistToogle { private const string NtdllDlDll = "ntdll.dll"; private const uint DataBufferSize = 4; private static readonly byte[] DisableDataBuf = { 0x00, 0x00, 0x00, 0x00 }; // 01仅优先通知 02 仅限闹钟 private static readonly byte[] EnableDataBuf = { 0x02, 0x00, 0x00, 0x00 }; [DllImport(NtdllDlDll, SetLastError = true)] private static extern int ZwUpdateWnfStateData( ref WnfSWnfStateName sWnfStateName, byte[] buffer, uint bufferSize, IntPtr previousStateData, IntPtr currentStateData, uint previousStateDataSize, uint currentStateDataSize); [StructLayout(LayoutKind.Sequential)] private struct WnfSWnfStateName { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] public uint[] Data; } private static WnfSWnfStateName _wnfSWnfShelQuietMomentShellModeChanged = new WnfSWnfStateName { Data = new uint[] { 0xa3bf5075, 0xd83063e } }; public static bool EnableFocusMode() { int result = ZwUpdateWnfStateData(ref _wnfSWnfShelQuietMomentShellModeChanged, EnableDataBuf, DataBufferSize, IntPtr.Zero, IntPtr.Zero, 0, 0); return result == 0; } public static bool DisableFocusMode() { int result = ZwUpdateWnfStateData(ref _wnfSWnfShelQuietMomentShellModeChanged, DisableDataBuf, DataBufferSize, IntPtr.Zero, IntPtr.Zero, 0, 0); return result == 0; } } 调用时,最好先调用DisableFoucusMode方法,再调用EnableFocusMode,参考StackOverflow上的说明 ...

2023-07-24 · 2 分钟 · czyt

浅谈windows默认Shell的替换

Windows XP时代 Xp时代提供的是通过注册表来自定义shell 设置所有用户的shell 注册表键HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\Shell 设置当前用户的shell注册表键 HKEY_Current_User\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\Shell 注册表键值类型 REG_SZ 值修改为你要自定义为shell的程序的完整路径。 在windows10下使用该技巧可能会出现黑屏的现象,参考stackoverflow的回答 Simply replacing the “explorer.exe” (HKLM\SOFTWARE\Microsoft\Window NT\Winlogon\Shell) with a custom app location provided a black screen. A much simpler way, and it works great, was to create a BATCH script to call the custom app through elevated powershell… powershell -nologo -noprofile -executionpolicy bypass -command "start-process -verb 'runas' -filepath <full path of custom app executable>" By replacing “explorer.exe” with this batch script I was able to successfully create a kiosk style lockdown under Windows 10 PRO with a non-UWP app. ...

2022-08-31 · 2 分钟 · czyt

Git小技巧

Windows下GIT的几个小技巧 记住git密码 使用下面命令可以设置记住git密码,但推荐使用ssh进行操作。 git config credential.helper store 设置换行符转换 在windows下开发时,迁出的代码是CRLF会导致编译的sh脚本不能正确执行: git config --global core.autocrlf false Git推送到多个服务器 要实现一次push到多个远程仓库 本机git仓库A https://aaaaa.git 要同步push的远程git仓库B https://bbbbb.git 通过git remote add添加 先使用git remote -v查看远程仓库的情况 ,然后添加一个git仓库 git remote add b https://bbbbb.git 再次查看远程仓库情况,如果需要push,则需要push两次 通过git remote set-url 添加 如果按上面添加过remote分支,需要先git remote rm b,使用下面命令添加即可。 git remote set-url --add a https://bbbbb.git 查看远程仓库情况,看看是否已经是两个push地址了 。这个只需push一次就行了 修改配置文件 打开 .git/config 找到 [remote “github”],添加对应的 url 即可,效果如下。这种方法其实和方法二是一样的。 [remote "a"] url = https://aaaaa.git fetch = +refs/heads/*:refs/remotes/a/* url = https://bbbbb.git 参考链接 ● 一个项目push到多个远程Git仓库 https://segmentfault.com/a/1190000011294144 ...

2022-06-17 · 2 分钟 · czyt

grpc-golang windows环境搭建说明

下载protoc,打开链接 下载后将对应的文件解压到gopath的bin目录。 下载protoc的golang插件。下载地址 链接 下载后放在protoc的同级目录(需要改扩展名为exe) 测试,定义一个Proto syntax = "proto3"; option go_package = ".;hello"; package main; message String { string value = 1; } 然后执行命令 protoc hello.proto --go_out=. ,大功告成,生成的文件内容如下: // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.24.0-devel // protoc v3.12.3 // source: hello.proto package hello import ( proto "github.com/golang/protobuf/proto" protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" ) const ( // Verify that this generated code is sufficiently up-to-date. _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) // Verify that runtime/protoimpl is sufficiently up-to-date. _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) // This is a compile-time assertion that a sufficiently up-to-date version // of the legacy proto package is being used. const _ = proto.ProtoPackageIsVersion4 type String struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Value string `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"` } func (x *String) Reset() { *x = String{} if protoimpl.UnsafeEnabled { mi := &file_hello_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *String) String() string { return protoimpl.X.MessageStringOf(x) } func (*String) ProtoMessage() {} func (x *String) ProtoReflect() protoreflect.Message { mi := &file_hello_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { ms.StoreMessageInfo(mi) } return ms } return mi.MessageOf(x) } // Deprecated: Use String.ProtoReflect.Descriptor instead. func (*String) Descriptor() ([]byte, []int) { return file_hello_proto_rawDescGZIP(), []int{0} } func (x *String) GetValue() string { if x != nil { return x.Value } return "" } var File_hello_proto protoreflect.FileDescriptor var file_hello_proto_rawDesc = []byte{ 0x0a, 0x0b, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x22, 0x1e, 0x0a, 0x06, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x42, 0x09, 0x5a, 0x07, 0x2e, 0x3b, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( file_hello_proto_rawDescOnce sync.Once file_hello_proto_rawDescData = file_hello_proto_rawDesc ) func file_hello_proto_rawDescGZIP() []byte { file_hello_proto_rawDescOnce.Do(func() { file_hello_proto_rawDescData = protoimpl.X.CompressGZIP(file_hello_proto_rawDescData) }) return file_hello_proto_rawDescData } var file_hello_proto_msgTypes = make([]protoimpl.MessageInfo, 1) var file_hello_proto_goTypes = []interface{}{ (*String)(nil), // 0: main.String } var file_hello_proto_depIdxs = []int32{ 0, // [0:0] is the sub-list for method output_type 0, // [0:0] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name 0, // [0:0] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name } func init() { file_hello_proto_init() } func file_hello_proto_init() { if File_hello_proto != nil { return } if !protoimpl.UnsafeEnabled { file_hello_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*String); i { case 0: return &v.state case 1: return &v.sizeCache case 2: return &v.unknownFields default: return nil } } } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_hello_proto_rawDesc, NumEnums: 0, NumMessages: 1, NumExtensions: 0, NumServices: 0, }, GoTypes: file_hello_proto_goTypes, DependencyIndexes: file_hello_proto_depIdxs, MessageInfos: file_hello_proto_msgTypes, }.Build() File_hello_proto = out.File file_hello_proto_rawDesc = nil file_hello_proto_goTypes = nil file_hello_proto_depIdxs = nil } 注意: ...

2022-02-28 · 3 分钟 · czyt

Rust安装及配置

下载rustup 从此处 下载,如果你需要安装vs的cpp生成工具,可以在这个页面 进行下载。 设置rustup镜像 字节提供的镜像 https://rsproxy.cn export RUSTUP_DIST_SERVER="https://rsproxy.cn" export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup" 设置两个环境变量即可。windows可以使用下面的命令进行设置: setx RUSTUP_DIST_SERVER "https://rsproxy.cn" setx RUSTUP_UPDATE_ROOT "https://rsproxy.cn/rustup" 中科大 设置环境变量 RUSTUP_DIST_SERVER (用于更新 toolchain) export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static 以及 RUSTUP_UPDATE_ROOT (用于更新 rustup) export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup 华中科技大学 **方法一:**在“系统-高级系统设置-环境变量” 中增加环境变量。 变量名为 RUSTUP_DIST_SERVER,值为https://mirrors.hust.edu.cn/rustup。 变量名为 RUSTUP_UPDATE_ROOT,值为https://mirrors.hust.edu.cn/rustup/rustup。 **方法二(推荐):**直接执行下面的Powershell脚本: [System.Environment]::SetEnvironmentVariable("RUSTUP_DIST_SERVER", "https://mirrors.hust.edu.cn/rustup", "User") [System.Environment]::SetEnvironmentVariable("RUSTUP_UPDATE_ROOT", "https://mirrors.hust.edu.cn/rustup/rustup", "User") 设置RUSTUP_HOME和CARGO_HOME可以实现自定义安装路径 对于使用buf的开发者,需要添加下面的内容: [registries.buf] index = "sparse+https://buf.build/gen/cargo/" credential-provider = "cargo:token" 然后登陆,token可以从这里 获取 cargo login --registry buf "Bearer {token}" 更多内容,请参考https://buf.build/docs/bsr/generated-sdks/cargo crates.io 镜像 编辑 ~/.cargo/config ,这里使用的是中科大的镜像。 cargo版本 1.39 中添加了对 .toml 扩展的支持,并且是首选形式。如果两个文件都存在,Cargo 将使用不带扩展名的文件。 [source.crates-io] replace-with = 'ustc' [source.ustc] registry = "git://mirrors.ustc.edu.cn/crates.io-index" 或者使用字节的,参考官网文档 ...

2022-02-28 · 9 分钟 · czyt