// toEnglishName searches the registry for an English name of a time zone // whose zone names are stdname and dstname and returns the English name. func toEnglishName(stdname, dstname string) (string, error) { var zones syscall.Handle p, _ := syscall.UTF16PtrFromString(`SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones`) if err := syscall.RegOpenKeyEx(syscall.HKEY_LOCAL_MACHINE, p, 0, syscall.KEY_READ, &zones); err != nil { return "", err } defer syscall.RegCloseKey(zones) var count uint32 if err := syscall.RegQueryInfoKey(zones, nil, nil, nil, &count, nil, nil, nil, nil, nil, nil, nil); err != nil { return "", err } var buf [50]uint16 // buf needs to be large enough to fit zone descriptions for i := uint32(0); i < count; i++ { n := uint32(len(buf)) if syscall.RegEnumKeyEx(zones, i, &buf[0], &n, nil, nil, nil, nil) != nil { continue } kname := syscall.UTF16ToString(buf[:]) matched, err := matchZoneKey(zones, kname, stdname, dstname) if err == nil && matched { return kname, nil } } return "", errors.New(`English name for time zone "` + stdname + `" not found in registry`) }
func getTuntapComponentId() (string, error) { adapters, err := registry.OpenKey(registry.LOCAL_MACHINE, ADAPTER_KEY, registry.READ) if err != nil { return "", err } var i uint32 for ; i < 1000; i++ { var name_length uint32 = TAPWIN32_MAX_REG_SIZE buf := make([]uint16, name_length) if err = syscall.RegEnumKeyEx( syscall.Handle(adapters), i, &buf[0], &name_length, nil, nil, nil, nil); err != nil { return "", err } key_name := syscall.UTF16ToString(buf[:]) adapter, err := registry.OpenKey(adapters, key_name, registry.READ) if err != nil { return "", err } name := syscall.StringToUTF16("ComponentId") name2 := syscall.StringToUTF16("NetCfgInstanceId") var valtype uint32 var component_id = make([]byte, TAPWIN32_MAX_REG_SIZE) var componentLen = uint32(len(component_id)) if err = syscall.RegQueryValueEx( syscall.Handle(adapter), &name[0], nil, &valtype, &component_id[0], &componentLen); err != nil { return "", err } if unicodeTostring(component_id) == TUNTAP_COMPONENT_ID { var valtype uint32 var netCfgInstanceId = make([]byte, TAPWIN32_MAX_REG_SIZE) var netCfgInstanceIdLen = uint32(len(netCfgInstanceId)) if err = syscall.RegQueryValueEx( syscall.Handle(adapter), &name2[0], nil, &valtype, &netCfgInstanceId[0], &netCfgInstanceIdLen); err != nil { return "", err } fmt.Println("Device:", unicodeTostring(netCfgInstanceId)) return unicodeTostring(netCfgInstanceId), nil } } return "", errors.New("not found component id") }
func QueryKey(handle syscall.Handle, mylist *[1 << 10]string) (csubkeys uint32) { var cvalues uint32 var achvalue []uint16 cbName := uint32(255) if syscall.RegQueryInfoKey(handle, nil, nil, nil, &csubkeys, nil, nil, &cvalues, nil, nil, nil, nil) != nil { return } //fmt.Println(csubkeys, cvalues, mylist) if csubkeys != 0 { var buf [1 << 10]uint16 for i := uint32(0); i < csubkeys; i = i + 1 { n := uint32(len(buf)) //fmt.Println(i, int(csubkeys), achkey) if syscall.RegEnumKeyEx(handle, i, &buf[0], &n, nil, nil, nil, nil) != nil { return } ext := syscall.UTF16ToString(buf[:]) //fmt.Println(i, ext, (*mylist)[i]) (*mylist)[i] = ext } } //fmt.Println(mylist) if cvalues != 0 { var j uint32 for j = 0; j < cvalues; j++ { if syscall.RegEnumKeyEx(handle, j, (*uint16)(unsafe.Pointer(&achvalue[0])), &cbName, nil, nil, nil, nil) != nil { return } } } return csubkeys }
func initMime() { var root syscall.Handle rootpathp, _ := syscall.UTF16PtrFromString(`\`) if syscall.RegOpenKeyEx(syscall.HKEY_CLASSES_ROOT, rootpathp, 0, syscall.KEY_READ, &root) != nil { return } defer syscall.RegCloseKey(root) var count uint32 if syscall.RegQueryInfoKey(root, nil, nil, nil, &count, nil, nil, nil, nil, nil, nil, nil) != nil { return } var buf [1 << 10]uint16 for i := uint32(0); i < count; i++ { n := uint32(len(buf)) if syscall.RegEnumKeyEx(root, i, &buf[0], &n, nil, nil, nil, nil) != nil { continue } ext := syscall.UTF16ToString(buf[:]) if len(ext) < 2 || ext[0] != '.' { // looking for extensions only continue } var h syscall.Handle extpathp, _ := syscall.UTF16PtrFromString(`\` + ext) if syscall.RegOpenKeyEx( syscall.HKEY_CLASSES_ROOT, extpathp, 0, syscall.KEY_READ, &h) != nil { continue } var typ uint32 n = uint32(len(buf) * 2) // api expects array of bytes, not uint16 contenttypep, _ := syscall.UTF16PtrFromString("Content Type") if syscall.RegQueryValueEx( h, contenttypep, nil, &typ, (*byte)(unsafe.Pointer(&buf[0])), &n) != nil { syscall.RegCloseKey(h) continue } syscall.RegCloseKey(h) if typ != syscall.REG_SZ { // null terminated strings only continue } mimeType := syscall.UTF16ToString(buf[:]) setExtensionType(ext, mimeType) } }
// ReadSubKeyNames returns the names of subkeys of key k. // The parameter n controls the number of returned names, // analogous to the way os.File.Readdirnames works. func (k Key) ReadSubKeyNames(n int) ([]string, error) { ki, err := k.Stat() if err != nil { return nil, err } names := make([]string, 0, ki.SubKeyCount) buf := make([]uint16, ki.MaxSubKeyLen+1) // extra room for terminating zero byte loopItems: for i := uint32(0); ; i++ { if n > 0 { if len(names) == n { return names, nil } } l := uint32(len(buf)) for { err := syscall.RegEnumKeyEx(syscall.Handle(k), i, &buf[0], &l, nil, nil, nil, nil) if err == nil { break } if err == syscall.ERROR_MORE_DATA { // Double buffer size and try again. l = uint32(2 * len(buf)) buf = make([]uint16, l) continue } if err == _ERROR_NO_MORE_ITEMS { break loopItems } return names, err } names = append(names, syscall.UTF16ToString(buf[:l])) } if n > len(names) { return names, io.EOF } return names, nil }
func queryAdapters(handle syscall.Handle) (*Interface, error) { var dev Interface var index uint32 for { var length uint32 = 72 adapter := make([]uint16, length) err := syscall.RegEnumKeyEx(handle, index, &adapter[0], &length, nil, nil, nil, nil) if err == NO_MORE_ITEMS { Log(Warning, "No more items in Windows Registry") break } index++ adapterID := string(utf16.Decode(adapter[0:length])) adapterID = removeZeroes(adapterID) path := fmt.Sprintf("%s\\%s\\Connection", NETWORK_KEY, adapterID) var iHandle syscall.Handle err = syscall.RegOpenKeyEx(syscall.HKEY_LOCAL_MACHINE, syscall.StringToUTF16Ptr(path), 0, syscall.KEY_READ, &iHandle) if err != nil { syscall.RegCloseKey(iHandle) continue } length = 1024 aName := make([]byte, length) err = syscall.RegQueryValueEx(iHandle, syscall.StringToUTF16Ptr("Name"), nil, nil, &aName[0], &length) if err != nil { syscall.RegCloseKey(iHandle) continue } aNameUtf16 := make([]uint16, length/2) for i := 0; i < int(length)-2; i += 2 { aNameUtf16[i/2] = binary.LittleEndian.Uint16(aName[i:]) } aNameUtf16[length/2-1] = 0 adapterName := string(utf16.Decode(aNameUtf16)) adapterName = removeZeroes(adapterName) Log(Warning, "AdapterName : %s, len : %d", adapterName, len(adapterName)) var isInUse = false for _, i := range UsedInterfaces { if i == adapterName { isInUse = true } } if isInUse { Log(Warning, "Adapter already in use. Skipping.") continue } UsedInterfaces = append(UsedInterfaces, adapterName) tapname := fmt.Sprintf("%s%s%s", USERMODE_DEVICE_DIR, adapterID, TAP_SUFFIX) dev.file, err = syscall.CreateFile(syscall.StringToUTF16Ptr(tapname), syscall.GENERIC_WRITE|syscall.GENERIC_READ, 0, nil, syscall.OPEN_EXISTING, syscall.FILE_ATTRIBUTE_SYSTEM|syscall.FILE_FLAG_OVERLAPPED, 0) if err != nil { syscall.CloseHandle(dev.Handle) continue } Log(Info, "Acquired control over TAP interface: %s", adapterName) dev.Name = adapterID dev.Interface = adapterName return &dev, nil } return nil, nil }