nhd-seeding.user.js

這是一個用於NexusHD的Greasemonkey User Script,用於在torrents.php頁面標示出哪些種子是已在做種的。因為自己經常忘記NHD上的某個種子是否已經下載過,每次都要點開details.php頁面看看有沒有自己,所以想到了寫個腳本搞定這個問題。每次訪問userdetails.php頁面的時候,都會自動更新做種列表,然後在torrents.php里修改正在做種種子的背景色以示區別。使用這個腳本的時候請把uid改成自己的。

// ==UserScript==
// @name           nhd-seeding
// @namespace      http://watashi.ws/
// @include        http://www.nexushd.org/userdetails.php*
// @include        http://www.nexushd.org/torrents.php*
// @require        http://code.jquery.com/jquery-1.7.1.min.js
// ==/UserScript==

var prefix = 'http://www.nexushd.org/';
var uid = '13416';  // watashi

var re = /id=(\d+)/;

function extractId(s) {
    return re.exec(s)[1];
}

var action = /.*\/(.*?)\./.exec(document.location.toString())[1];
var list = GM_getValue('seeding', '').split(';');

if (action == 'userdetails') {
    var url = prefix + 'getusertorrentlistajax.php?userid=' + uid + '&type=seeding';
    $.get(url, function(data) {
        var table = $('a[href^=details\\.php]', data);
        list.length = 0;
        $.each(table, function(index, value) {
            list.push(extractId(value));
        });
        GM_setValue('seeding', list.join(';'));
    }, 'html');
} else if (action == 'torrents') {
    var table = $('table.torrents>tbody>tr').slice(1);
    table.each(function(index, value) {
        var id = extractId($('a[href^=details\\.php]:first', value).attr('href'));
        if (list.indexOf(id) != -1) {
            $(value).addClass('my-seeding');
        }
    });
    // $('.my-seeding').css('color', 'white');
    $('.my-seeding img.download').css('opacity', '.1');
    $('.my-seeding').css('background-color', 'rgba(0, 0, 0, 0.2)');
}

測試效果如圖:

passgen.pl

這個腳本誕生於CSDN程序員改密碼節當晚,本人也不幸躺槍,暴露了一個比較重要的常用密碼。當然隨後事件的發展告訴我們,不在不同網站用不同密碼是不行的。為每個網站想一個密碼是很頭疼的事,所以一個比較簡單的做法是用hash(主密碼+網站名)。於是寫了一個腳本,利用一個密钥,加上操作系統用戶密碼作為主密碼,再加上網站名,生成一個唯一而又安全(但絕對記不住)的密碼。

#!/usr/bin/perl

use strict;
use Authen::PAM;
use Digest::SHA1 'sha1_base64';
use Term::ReadPassword;

my $key = '== Any Random String as Secret Key ==';
@_ = getpwuid($<);
my $user = $_[0];
my $home = $_[7];
my $pass = '';
my $domain = '';

sub my_conv_func {
    my @res;
    while (@_) {
        my $msg_type = shift;
        my $msg = shift;
        my $ans = undef;
        $ans = $pass if $msg_type == 1;
        push @res, (0, $ans);
    }
    push @res, PAM_SUCCESS();
    return @res;
}

while (1) {
    $pass = read_password("Password for $user: ");
    my $pamh = new Authen::PAM("login", $user, \&my_conv_func);
    my $errnum = $pamh->pam_authenticate;
    if ($errnum == PAM_SUCCESS()) {
        last;
    } else {
        print $pamh->pam_strerror($errnum), "\n";
    }
}

sub gen {
    $domain = shift;
    my $sha1 = sha1_base64($key . $pass . $domain);
    $sha1 =~ s/[^\w]//g;
    return substr($sha1, 0, 12);
}

$| = 0;
open LOG, ">>$home/bin/passgen.log";
while (1) {
    print "\nDomain: ";
    chomp($_ = <STDIN>);
    last unless $_;
    lc;
    s/[^\w]//g;
    print "Domain='$_': ", gen($_);
    print LOG "Domain='$_': ", gen($_), "\n";
}
close LOG;

測試如下:

watashi@watashi-laptop:/tmp$ passgen.pl
Password for watashi:
Authentication failure
Password for watashi: 

Domain: example.com
Domain='examplecom': kEnZ2tf23jfm
Domain: twitter
Domain='twitter': mtaW5hV3jPqk
Domain:

對應的/home/watashi/bin/passgen.log內容如下:

Domain='examplecom': kEnZ2tf23jfm
Domain='twitter': mtaW5hV3jPqk

可以定時把passgen.log的內容轉移到值得信賴的地方。

cipher.sh

上面的passgen.pl是對網站名的單向加密,有時候我們還需要雙向加密,比如加密保存上面的passgen.log函數。於是寫了個腳本cipher.sh,其實就是簡單的gpg加base64。

    腳本有三個參數:

  • -d 表示解密,默認是加密;
  • -p passphrase 指定密碼,默認”123456789″;
  • -w wrap 加密時,可以指定每行的長度,默認為0,即不換行。

代碼和例子均在下面:

watashi@watashi-laptop:/tmp$ cipher.sh -p 'ppnn13%dkstFeb.1st'
cipher:
娉娉袅袅十三余,豆蔻梢头二月初
success
jA0EAwMC125frrUDV8JgyUVt6gKXEggNZ5nHHniJTnLQQcKf3EszkGx4LMFcvEoItfw1B8/nCyL44z6NXJ/ssUzQBeWOYzFQwJyTMXgd4eoclHdLTIY=
watashi@watashi-laptop:/tmp$ cipher.sh -d
digest:
jA0EAwMC125frrUDV8JgyUVt6gKXEggNZ5nHHniJTnLQQcKf3EszkGx4LMFcvEoItfw1B8/nCyL44z6NXJ/ssUzQBeWOYzFQwJyTMXgd4eoclHdLTIY=
gpg: CAST5 encrypted data
gpg: encrypted with 1 passphrase
gpg: decryption failed: Bad session key
fail

watashi@watashi-laptop:/tmp$ cipher.sh -dpppnn13%dkstFeb.1st
digest:
jA0EAwMC125frrUDV8JgyUVt6gKXEggNZ5nHHniJTnLQQcKf3EszkGx4LMFcvEoItfw1B8/nCyL44z6NXJ/ssUzQBeWOYzFQwJyTMXgd4eoclHdLTIY=
gpg: CAST5 encrypted data
gpg: encrypted with 1 passphrase
gpg: WARNING: message was not integrity protected
success
娉娉袅袅十三余,豆蔻梢头二月初
watashi@watashi-laptop:/tmp$ cipher.sh -w 72 < `which cipher.sh`
cipher:
success
jA0EAwMCgJ6Aj3zvQ5BgycC83heaAm4x9aOds3vwvPc+UH22Hv6IGkaCynZ9KUYEXPmhsKOB
sOFNDT0mHCR+FheDsoE+ul3l0HZ+yevS8TIqAGkxVulEFnrqAPo49WfOlpkdC5Edghh+DCXF
nLMvtMUaGRhE5Jtiplf2VX77jKCanS8+tQve93aG7H4sWmiXBA8cpJNj1wH9/yJhq2zt4eXF
dEPNtAcPQKu18v72F2GyiMOfNJtrcCz9elovsD8DrCoHnboHIhsmSaCAd8zGaSbyrjTNaq0C
zBzQop3w9w1RSt8l0TSV97y6kzJ0nYOwj/gHSNK5PYOBBnJKtvdGHeUcpGpR1v1kNkeSTbK7
UzAe0776LyA/vTQnJyxVx2FllbM3+MriChhYK9D1jy+w9L8ZpkSrxy5yv7ftPtx7bSVQTPn0
mgAXrF6Qzg3+JYzrKlbkAlNlomaaHi++HE3qapRIOA8x7c9MDXZAggJyfsr4EomYjlHwvUak
vkwtpspT3lEI7kxMzvjfRbKbQIM=
watashi@watashi-laptop:/tmp$ cipher.sh -w 72 < `which cipher.sh` | cipher.sh -p 123456789 -d
digest:
cipher:
success
gpg: CAST5 encrypted data
gpg: encrypted with 1 passphrase
gpg: WARNING: message was not integrity protected
success
optstring='dp:w:'
operation=cipher
passphrase=123456789
wrap=0

eval set -- `getopt $optstring "$@"`

while [ $# -gt 0 ]
do
    case $1 in
        -d)
            operation=digest
            shift
        ;;
        -p)
            passphrase=$2
            shift 2
        ;;
        -w)
            wrap=$2
            shift 2
        ;;
        --)
            shift
            break
        ;;
    esac
done

echo -e "\e[1;34m$operation:\e[0m" >&2
if [ "$operation" = "digest" ]
then
    output=`cat | base64 -i -d | gpg2 --batch --passphrase "$passphrase"`
else
    output=`cat | gpg2 --batch --passphrase "$passphrase" -c | base64 -w "$wrap"`
fi

result=$?

if [ $result -eq 0 ]
then
    echo -e "\e[1;32msuccess\e[0m" >&2
else
    echo -e "\e[1;31mfail\e[0m" >&2
fi
echo "$output"

exit $result
watashi@watashi-laptop:/tmp$

repack.sh

這個主要是處理某些糟糕物用的,為了節約硬盤資源,也為了方便mcomix之類的漫畫閱讀器,對原本rar或7z打包的圖片,將無損的bmp或png轉成jpg,再打包成zip,如果需要還可以給convert傳參數-resize 50%等縮小圖片。這個腳本經過大量實戰,對文件夾嵌套、文件名有空格和文件只讀等問題都已能正確處理了。

dir=${1%/*}
file=${1##*/}
ext=${file##*.}
shift
echo "$dir || $file || $ext || $*"

if [ "$dir" != "$file" ]
then
    cd "$dir"
fi

mkdir __temp__
cd __temp__
case $ext in
    7z)
        7z x "../$file"
        ;;
    rar)
        unrar x "../$file"
        ;;
    *)
        exit
        ;;
esac
# for i in `find . -regextype posix-extended -iregex '.*\.(bmp|png)'`
# do
# 	convert $* "$i" "$i.jpg"
# 	rm "$i"
# done
find . -regextype posix-extended -iregex '.*\.(bmp|png)' -print0 | xargs -0 -t -i convert $* '{}' '{}.jpg'
# find . -regextype posix-extended -iregex '.*\.(bmp|png)' -delete
yes | find . -regextype posix-extended -iregex '.*\.(bmp|png)|thumbs\.db' -delete
zip -r "$file.zip" .
mv "$file.zip" ..
cd ..
rm -rf __temp__
3 Responses to “四個腳本”
  1. Fly梦 says:

    表示繁体+斜体看起来相当吃力……

  2. owen_water says:

    ym

  3.  
Leave a Reply