EdmondFrank's 时光足迹

この先は暗い夜道だけかもしれない それでも信じて進むんだ。星がその道を少しでも照らしてくれるのを。
或许前路永夜,即便如此我也要前进,因为星光即使微弱也会我为照亮前途。
——《四月は君の嘘》

Xlib编程之-创建全屏透明窗口

Xlib编程之-创建全屏透明窗口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 //source file : win.c
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
 int main(int argc, char* argv[])
 {
    Display* display = XOpenDisplay(NULL);

    XVisualInfo vinfo;
    XMatchVisualInfo(display, DefaultScreen(display), 32, TrueColor, &vinfo);

    XSetWindowAttributes attr;//窗口属性设置
    attr.colormap = XCreateColormap(display, DefaultRootWindow(display), vinfo.visual, AllocNone);
    attr.border_pixel = 0;
    attr.background_pixel = 0;

    //取输出设备的长宽像素
    int screen = DefaultScreen(display);
    int height = DisplayHeight(display,screen);
    int width = DisplayWidth(display,screen);

    //开始创建窗口
    Window win = XCreateWindow(display, DefaultRootWindow(display), 0, 0,width, height , 0, vinfo.depth, InputOutput, vinfo.visual,  CWColormap | CWBorderPixel | CWBackPixel, &attr);
    XSelectInput(display, win, StructureNotifyMask);
    //GC gc = XCreateGC(display, win, 0, 0);

    /*
    Atom wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", 0);
    XSetWMProtocols(display, win, &wm_delete_window, 1);
    */
    XMapWindow(display, win);
    XMoveWindow(display,win,0,0);

    XFlush(display);//刷新输出设备
    int keep_running = 1;
    XEvent event;

    //窗口事件监听尝试
    while (keep_running) {
        XNextEvent(display, &event);
        switch(event.type) {
            case ClientMessage:
                if (event.xclient.message_type == XInternAtom(display, "WM_PROTOCOLS", 1) && (Atom)event.xclient.data.l[0] == XInternAtom(display,"WM_DELETE_WINDOW",1))
                    keep_running = 0;
                break;
            default:
                break;
        }
    }

    //结束销毁
    XDestroyWindow(display, win);
    XCloseDisplay(display);
    return 0;
 }

编译

gcc -o win ./win.c -L/usr/lib -lX11

That is All

2016-10-12:天翼校园网客户端登录协议更新

天翼校园网客户端登录协议更新

PS:此登录协议仅供参考,未必适合所有高校. 本项目源码不遵循任何协议,各位可以随意下载及修改,但请勿用作非法 or 商业用途!

主要变化

原登录协议 步骤为:challange -> 获取rescode -> login,现登录协议取消了rescode认证,因此可以省略challange可以直接login

登录协议主要源码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#coding:utf-8
require 'json'
require 'net/http'
require 'uri'
require 'digest/md5'
body = File.read("./config.json")
config = JSON.parse(body)
username = config["username"].to_s
password = config["password"].to_s
clientip = config["clientip"].to_s
nasip = config["nasip"].to_s
mac = config["mac"].to_s
iswifi="1050"
def get_timestamp()
  return Time.now.to_i.to_s+'444'
end
def get_md5(str)
  return  Digest::MD5.hexdigest(str+'Eshore!@#').upcase
end

def active(username,clientip,nasip,mac)
  timestamp = get_timestamp
  authenticator= get_md5(clientip+nasip+mac+timestamp)
  puts username,clientip,nasip,mac,timestamp,authenticator
  url="http://enet.10000.gd.cn:8001/hbservice/client/active?username=#{username}&clientip=#{clientip}&nasip=#{nasip}&mac=#{mac}&timestamp=#{timestamp}&authenticator=#{authenticator}"
  uri = URI(url)
  resp = Net::HTTP.get(uri)
  #resp = `curl -A "" -H "Accept:" -H "Host:enet.10000.gd.cn:8001" #{url}`
  result = JSON.parse(resp)
  puts result
  return result["rescode"].to_s
end

def login(username,password,clientip,nasip,mac,iswifi)
  url = "http://enet.10000.gd.cn:10001/client/login"
  timestamp =  get_timestamp
  authenticator=get_md5(clientip+nasip+mac+timestamp)
  postdata={}
  postdata["username"]=username
  postdata["password"]=password
  postdata["clientip"]=clientip
  postdata["nasip"]=nasip
  postdata["mac"]=mac
  postdata["timestamp"]=timestamp
  postdata["authenticator"]=authenticator
  postdata["iswifi"]=iswifi
  data=postdata.to_json

  headers = {"Content-Type" => "application/json"}
  headers["Host"]="enet.10000.gd.cn:10001"
  headers["Content-Length"]=data.bytesize.to_s
  headers["Expect"]="100-continue"
  headers["Connection"]="Keep-Alive"
  puts `curl -d '#{data}' -A "" -H "Accept:" -H "Content-Type:application/json" -H "Host:enet.10000.gd.cn:10001" -H "Content-Length:#{data.bytesize.to_s}" -H "Expect:100-continue" -H "Connection:Keep-Alive" -X POST #{url}`
end
code = active(username,clientip,nasip,mac)
if code != "0"
  login(username,password,clientip,nasip,mac,iswifi)
end

注:源码中才用了curl发送post包,使用前需先确认系统中是否用curl工具.若是改回Ruby的net/http发包请去掉[Expect]=“100-continue"头

目前仅提供登录协议供大家参考,有能力者可自行实现完整程序及Gui界面,完整的程序(Cli版本/Gui版本),博主也会后续在我的Github上发布

从零开始的Rust-Enumerate方法

从零开始的Rust学习之旅-Enumerate方法

Enumerate方法 当你需要记录你已经循环了多少次了的时候,你可以使用 .enumerate()函数。

对范围(On ranges):

1
2
3
4
for(i,j) in (5..10).enumerate()  
{
  println!("i  =   {}  and j   =   {}", i,    j);
}

输出:
i = 0 and j = 5
i = 1 and j = 6
i = 2 and j = 7
i = 3 and j = 8
i = 4 and j = 9
别忘了在范围外面加上括号

对迭代器(On iterators):

1
2
3
4
let lines = "hello\nworld".lines();
for (linenumber, line) in lines.enumerate() {
  println!("{}:{}", linenumber, line);
}

输出:
0:hello
1:world

简单cat的实现-1.0

简单cat的实现-1.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
void vis(FILE *fp);
int strip = 0;
void main(int argc,char *argv[])
{
    int i = 0;
    FILE *fp;
    while(argc > 1 && argv[1][0] =='-'){
        switch(argv[1][1]){
        case 's':
            strip = 1;
            break;
        default:
            fprintf(stderr,"%s:unknown arg %s\n",argv[0],argv[1]);
            exit(1);
        }
        argc--;
        argv++;
    }

    if(argc == 1)
    vis(stdin);
    else
    for(i=1;i<argc;i++)
    if((fp=fopen(argv[i],"r")) == NULL){
        fprintf(stderr,"%s:can't open %s\n",argv[0],argv[i]);
        exit(1);
    }else{
        vis(fp);
        fclose(fp);
    }
    exit(0);
}
void vis(FILE *fp){
    int c;
    while((c=getc(fp))!=EOF)
    if(isascii(c) && (isprint(c) || c=='\n' || c=='\t' || c==' '))
    putchar(c);
    else if(!strip)
    printf("\\%03o",c);
}