//go:build windows package remote import ( "github.com/KarpelesLab/vncpasswd" "github.com/kardianos/service" "golang.org/x/sys/windows/registry" ) var VncService service.Service // ReadVncServerConfig 从注册表读取 TightVNC 服务端配置 func ReadVncServerConfig() (VncServerConfig, error) { config := VncServerConfig{ ExtraPorts: "", QueryTimeout: 0, QueryAcceptOnTimeout: 0, LocalInputPriorityTimeout: 0, LocalInputPriority: 0, BlockRemoteInput: 0, BlockLocalInput: 0, IpAccessControl: "", RfbPort: 0, HttpPort: 0, DisconnectAction: 0, AcceptRfbConnections: 0, UseVncAuthentication: 0, UseControlAuthentication: 0, RepeatControlAuthentication: 0, LoopbackOnly: 0, AcceptHttpConnections: 0, LogLevel: 0, EnableFileTransfers: 0, RemoveWallpaper: 0, UseD3D: 0, UseMirrorDriver: 0, EnableUrlParams: 0, Password: []byte{}, AlwaysShared: 0, NeverShared: 0, DisconnectClients: 0, PollingInterval: 0, AllowLoopback: 0, VideoRecognitionInterval: 0, GrabTransparentWindows: 0, SaveLogToAllUsersPath: 0, RunControlInterface: 0, IdleTimeout: 0, VideoClasses: "", VideoRects: "", ConnectToRdp: 0, } key, err := registry.OpenKey(registry.LOCAL_MACHINE, "SOFTWARE\\TightVNC\\Server", registry.ALL_ACCESS) if err != nil { return config, err } defer key.Close() config.ExtraPorts, _, _ = key.GetStringValue("ExtraPorts") config.QueryTimeout, _, _ = GetDwordValue(key, "QueryTimeout") config.QueryAcceptOnTimeout, _, _ = GetDwordValue(key, "QueryAcceptOnTimeout") config.LocalInputPriorityTimeout, _, _ = GetDwordValue(key, "LocalInputPriorityTimeout") config.LocalInputPriority, _, _ = GetDwordValue(key, "LocalInputPriority") config.BlockRemoteInput, _, _ = GetDwordValue(key, "BlockRemoteInput") config.BlockLocalInput, _, _ = GetDwordValue(key, "BlockLocalInput") config.IpAccessControl, _, _ = key.GetStringValue("IpAccessControl") config.RfbPort, _, _ = GetDwordValue(key, "RfbPort") config.HttpPort, _, _ = GetDwordValue(key, "HttpPort") config.DisconnectAction, _, _ = GetDwordValue(key, "DisconnectAction") config.AcceptRfbConnections, _, _ = GetDwordValue(key, "AcceptRfbConnections") config.UseVncAuthentication, _, _ = GetDwordValue(key, "UseVncAuthentication") config.UseControlAuthentication, _, _ = GetDwordValue(key, "UseControlAuthentication") config.RepeatControlAuthentication, _, _ = GetDwordValue(key, "RepeatControlAuthentication") config.LoopbackOnly, _, _ = GetDwordValue(key, "LoopbackOnly") config.AcceptHttpConnections, _, _ = GetDwordValue(key, "AcceptHttpConnections") config.EnableFileTransfers, _, _ = GetDwordValue(key, "EnableFileTransfers") config.RemoveWallpaper, _, _ = GetDwordValue(key, "RemoveWallpaper") config.UseD3D, _, _ = GetDwordValue(key, "UseD3D") config.UseMirrorDriver, _, _ = GetDwordValue(key, "UseMirrorDriver") config.EnableUrlParams, _, _ = GetDwordValue(key, "EnableUrlParams") config.Password, _, _ = key.GetBinaryValue("Password") config.AlwaysShared, _, _ = GetDwordValue(key, "AlwaysShared") config.NeverShared, _, _ = GetDwordValue(key, "NeverShared") config.DisconnectClients, _, _ = GetDwordValue(key, "DisconnectClients") config.PollingInterval, _, _ = GetDwordValue(key, "PollingInterval") config.AllowLoopback, _, _ = GetDwordValue(key, "AllowLoopback") config.VideoRecognitionInterval, _, _ = GetDwordValue(key, "VideoRecognitionInterval") config.GrabTransparentWindows, _, _ = GetDwordValue(key, "GrabTransparentWindows") config.SaveLogToAllUsersPath, _, _ = GetDwordValue(key, "SaveLogToAllUsersPath") config.RunControlInterface, _, _ = GetDwordValue(key, "RunControlInterface") config.IdleTimeout, _, _ = GetDwordValue(key, "IdleTimeout") config.VideoClasses, _, _ = key.GetStringValue("VideoClasses") config.VideoRects, _, _ = key.GetStringValue("VideoRects") config.ConnectToRdp, _, _ = GetDwordValue(key, "ExtraPorts") return config, nil } // GetDwordValue 读取 REG_DWORD 类型注册表值 func GetDwordValue(k registry.Key, name string) (val uint32, valtype uint32, err error) { val64 := uint64(0) val64, valtype, err = k.GetIntegerValue(name) val = uint32(val64) return } var vncSvcConfig = &service.Config{ Name: "tvnserver", DisplayName: "TightVNC Server", Description: "", Arguments: []string{"-service"}, } // InstallVncServer 安装VNC服务端 返回:结果,密码,错误 func InstallVncServer() (bool, string, error) { //安装服务 //导入注册表 //生成随机密码 return true, "12345678", nil } // VncInit 初始化 VNC 服务对象,VncInstall/VncStart 等操作前必须调用 func VncInit() error { var err error VncService, err = service.New(nil, vncSvcConfig) if err != nil { return err } return nil } // VncInstall 安装 TightVNC 服务 func VncInstall() error { err := VncService.Install() if err != nil { return err } return nil } // VncUninstall 停止并卸载 TightVNC 服务 func VncUninstall() error { _ = VncService.Stop() err := VncService.Uninstall() if err != nil { return err } return nil } // VncStart 启动 TightVNC 服务 func VncStart() error { err := VncService.Start() if err != nil { return err } return nil } // VncStop 停止 TightVNC 服务 func VncStop() error { err := VncService.Stop() if err != nil { return err } return nil } // VncRestart 重启 TightVNC 服务 func VncRestart() error { err := VncService.Stop() if err != nil { return err } err = VncService.Start() if err != nil { return err } return nil } // VncPasswordCrypt 加密 VNC 密码(TightVNC 注册表存储格式) func VncPasswordCrypt(password string) []byte { return vncpasswd.Crypt(password) } // VncPasswordDecrypt 解密 VNC 密码 func VncPasswordDecrypt(data []byte) string { return vncpasswd.Decrypt(data) }