海运的博客

Windows空闲状态时自动睡眠Golang版

发布时间:December 5, 2018 // 分类: // No Comments

使用windows自带的当系统空闲一段时间后进入睡眠模式不太可靠,用golang写了个小工具定时使用getLastInputInfo获取上次鼠标和键盘的活动时间,超过一段时间未操作进入睡眠状态,当在全屏状态时不进入睡眠状态,如在播放视频时,全屏排除锁屏界面和win+d快捷键进入的桌面。

获取是否有程序在全屏使用的golang cgo实现,windows下编译运行需安装gcc,可安装TDM-GCC。

package main

/*
#include <windows.h>
#include <stdio.h>

int CheckFullscreen() {
  int bFullScreen = 0;
  HWND hWnd = GetForegroundWindow();
  RECT rcApp, rcDesk;
  GetWindowRect(GetDesktopWindow(), &rcDesk);

  if (hWnd!=GetDesktopWindow() && hWnd!=GetShellWindow()) {
    GetWindowRect(hWnd, &rcApp);
    if (rcApp.left<=rcDesk.left && rcApp.top<=rcDesk.top && rcApp.right>=rcDesk.right && rcApp.bottom>=rcDesk.bottom) {
      char wnd_name[256];
      char wnd_title[256];
      GetWindowText(hWnd,wnd_title,sizeof(wnd_title));
      GetClassName(hWnd, wnd_name, sizeof(wnd_name));
      printf("title: %s\n", wnd_title);
      printf("name: %s\n", wnd_name);

      if (strcmp(wnd_name, "Windows.UI.Core.CoreWindow") != 0 && strcmp(wnd_name, "WorkerW") != 0){
        bFullScreen =1;
      }
    }
  }
  return bFullScreen;
}


*/
import "C"

import (
  "flag"
  "fmt"
  "os/exec"
  "strings"
  "syscall"
  "time"
  "unsafe"
)

var (
  user32           = syscall.MustLoadDLL("user32.dll")
  kernel32         = syscall.MustLoadDLL("kernel32.dll")
  getLastInputInfo = user32.MustFindProc("GetLastInputInfo")
  getTickCount     = kernel32.MustFindProc("GetTickCount")
  lastInputInfo    struct {
    cbSize uint32
    dwTime uint32
  }
)

func IdleTime() uint32 {
  lastInputInfo.cbSize = uint32(unsafe.Sizeof(lastInputInfo))
  currentTickCount, _, _ := getTickCount.Call()
  r1, _, err := getLastInputInfo.Call(uintptr(unsafe.Pointer(&lastInputInfo)))
  if r1 == 0 {
    fmt.Println("error getting last input info: " + err.Error())
    return 0
  }
  return (uint32(currentTickCount) - lastInputInfo.dwTime)
}

func Execute(command string) (bool, string, error) {
  // splitting head => g++ parts => rest of the command
  parts := strings.Fields(command)
  head := parts[0]
  parts = parts[1:len(parts)]

  out, err := exec.Command(head, parts...).Output()
  if err != nil {
    return false, "", err
  }
  return true, string(out), nil
}

func sleepCommandLineImplementation(cmd string) {
  if cmd == "" {
    cmd = "C:\\Windows\\System32\\rundll32.exe powrprof.dll,SetSuspendState 0,1,0"
  }
  fmt.Println("Sleep implementation [windows], sleep command is [", cmd, "]")
  _, _, err := Execute(cmd)
  if err != nil {
    fmt.Println("Can't execute command [" + cmd + "] : " + err.Error())
  } else {
    fmt.Println("Command correctly executed")
  }
}

func sleepDLLImplementation() {
  var mod = syscall.NewLazyDLL("Powrprof.dll")
  var proc = mod.NewProc("SetSuspendState")

  // DLL API : public static extern bool SetSuspendState(bool hiberate, bool forceCritical, bool disableWakeEvent);
  // ex. : uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("Done Title"))),
  ret, _, _ := proc.Call(0,
  uintptr(0), // hibernate
  uintptr(1), // forceCritical
  uintptr(0)) // disableWakeEvent

  fmt.Println("Command executed, result code [" + fmt.Sprint(ret) + "]")
}

func main() {
  var timeout,interval int
  var execute string

  flag.IntVar(&timeout, "t", 15, "minute")
  flag.IntVar(&interval, "i", 60, "second")
  flag.StringVar(&execute, "c", "api", "api or cli")
  flag.Parse()
  fmt.Println("timeout", timeout, "minute")
  fmt.Println("interval", interval, "second")
  fmt.Println("execute", execute)
  //间隔不能太小,不然通过网络唤醒间隔时间内无操作会再次进入睡眠
  t := time.NewTicker(time.Duration(interval) * time.Second)
  for range t.C {
    idle := IdleTime()
    fmt.Println(time.Now().Format("2006-01-02 15:04:05") ,"idle time", idle)
    fmt.Println(time.Now().Format("2006-01-02 15:04:05") ,"full screen", C.CheckFullscreen())
    if idle > uint32(timeout)*60*1000 && C.CheckFullscreen() != 1 {
      if execute == "api"{
        sleepDLLImplementation()
      } else {
        sleepCommandLineImplementation("rundll32.exe powrprof.dll,SetSuspendState 0,1,0")
        //sleepCommandLineImplementation("")
      }
    }
  }
}

如进入睡眠时为休眠,可关闭休眠:

powercfg -h off

参考:
https://bbs.csdn.net/topics/390838652
https://stackoverflow.com/questions/22949444/using-golang-to-get-windows-idle-time-getlastinputinfo-or-similar
https://github.com/SR-G/sleep-on-lan
https://www.cnblogs.com/yeshou/p/5197765.html

ROS无线中继配置

发布时间:December 1, 2018 // 分类: // No Comments

首先要禁用DHCP服务器,无线列表点击Setup Repeater,ssid输入要中继的ssid,下面填密码:
2018-12-01_172033.jpg

完成后出现两个无线网卡,wlan1为中继网卡,wlan2为无线ap网卡,可修改ssid。

2018-12-01_172108.jpg

N1盒子armbian编译内核开启iptables透明代理重定向REDIRECT模块

发布时间:November 17, 2018 // 分类: // 2 Comments

armbian默认内核开启REDIRECT涉及的选项较多,不能使用仅编译内核模块方式,可编译内核并替换。

首先开启Netfilter nf_tables support,然后选择nf_tables redirect support,路径见图片顶部。

2018-11-17_154225.png

开启ipv4 nf_tables support、ipv4 redirect support for nf_tables、redirect target support:

2018-11-17_153925.png

开启ipv6 nf_tables support和ipv6 redirect support for nf_tables:

2018-11-17_154122.png

ubuntu18.04编译使用transmission2.92跳过校验

发布时间:November 16, 2018 // 分类:PT // No Comments

安装编译环境及transmission依赖:

apt-get install ca-certificates libcurl4-openssl-dev libssl-dev pkg-config build-essential checkinstall libevent-dev intltool

下载编译transmission:

wget http://archive.ubuntu.com/ubuntu/pool/main/t/transmission/transmission_2.92.orig.tar.gz
tar zxvf transmission_2.92.orig.tar.gz 
cd transmission-2.92
wget http://archive.ubuntu.com/ubuntu/pool/main/t/transmission/transmission_2.92-3ubuntu2.debian.tar.xz
tar Jxvf transmission_2.92-3ubuntu2.debian.tar.xz 
#打openssl补丁,不然编译失败
patch -p 1 < debian/patches/f91cf5ad8c677b61ceb0bf5877b87f9e93256dd7.patch 
#patch -p 1 < debian/patches/8c8386a7f3f482a9c917f51d28e0042e55f56b3e.patch 
#patch -p 1 < debian/patches/transmission-fix-dns-rebinding-vuln.patch 
#transmission跳过校验patch
wget https://github.com/superlukia/transmission-2.92_skiphashcheck/commit/56e327d1dacb5b3453954b76a6d0edd30edb7a34.patch
patch -p 1 < 56e327d1dacb5b3453954b76a6d0edd30edb7a34.patch
./configure 
make 
#替换已安装的transmission-daemon
mv /usr/bin/transmission-daemon /usr/bin/transmission-daemon.bak
cp daemon/transmission-daemon /usr/bin/transmission-daemon

在WEB界面添加种子后校验时右键点击种子Ask tracker for more peers即可跳过校验。
另类方法:
https://www.jianshu.com/p/ab2df4282e59
参考:
https://github.com/superlukia/transmission-2.92_skiphashcheck

路由iptables允许转发动态IPV6地址指定后缀

发布时间:November 16, 2018 // 分类: // No Comments

使用前缀为内网机器分配公网IP地址,如果需要在外网通过IPV6访问内网机器资源,由于前缀不固定,后缀是通过mac生成的,可以用ip6tables指定后缀允许访问:

ip6tables -I FORWARD -i pppoe-wan -d ::74f2:eac0:59d4:25ed/::ffff:ffff:ffff:ffff -j ACCEPT

参考:
https://superuser.com/questions/1181445/allow-traffic-through-a-firewall-to-a-dynamic-ipv6-address

分类
最新文章
最近回复
  • 海运: 你是编译不成功呢?还是编译后不能运行呢?还是运行后不能访问web界面呢?
  • 白墨: 可能不清楚就是编译安装后启动后访问不了web界面
  • 白墨: 你好博主按照你的教程是编译安装不了的,在centos7版本下,我在开源镜像站下载centos7...
  • 感谢: 好的谢谢。
  • 海运: 可在github选择指定的branch或tag查看Makefile文件内核版本git指定bra...
  • 感谢: 请问一下“git checkout 20181012”这条命令是什么意思,没有google到。...
  • 海运: 抱歉,我没用蓝牙,现在N1在稳定挂PT中,没法折腾了。
  • hxhw: 大神能弄一個N1盒子開啟藍芽的教程不?我目前的藍芽識別不了似的(BD Address: 00:...
  • 海运: 抱歉,没用过wireguard,不了解具体情况。看错误提示是创建虚拟网卡的类型wireguar...
  • zhu tie: 本人小白我想在n1上装wireguard,安装后错误信息如下:wg-quick up wg0[...
归档
页面执行耗时:0.0683秒。-->