树莓派自动连接WiFi网络

# /boot/wpa_supplicant.conf
country=CN
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
 
network={
    ssid="WiFi-A"
    psk="12345678"
    priority=1
}

network={
    ssid="WiFi-B"
    psk="12345678"
    priority=2
}

在树莓派的SD卡上新建文件 wpa_supplicant.conf 添加以上内容到新的配置文件中,修改WiFi账号和密码。

priority是优先级

阿里云OSS表单上传签名

/**
 *  阿里云 OSS 密钥
 */
define('AccessKeyId', 'ID');
define('AccessKeySecret', 'SS');
define('OssHost', 'https://bucket.oss-cn-beijing.aliyuncs.com');
define('UploadPrefix', 'files/');

function ossUploadSign($expire = 60) {
    $sign = [];
    $sign['accessid'] = AccessKeyId;
    $sign['host'] = OssHost;
    $sign['expire'] = time() + $expire;
    $sign['policy'] = base64_encode(json_encode(['expiration' => date('Y-m-d\TH:i:s\Z', $sign['expire']), 'conditions' => [['content-length-range', 0, 100 * 1024 * 1024], ['starts-with', '$key', UploadPrefix]]]));
    $sign['signature'] = base64_encode(hash_hmac('sha1', $sign['policy'], AccessKeySecret, true));
    $sign['dir'] = UploadPrefix;
    return json_encode($sign);
}

echo ossUploadSign();

PHP批量重命名

function AutoRenameFolder($dir) {
    foreach (glob($dir . '/*') as $k => $v) {
        if (is_file($v)) {
            rename($v, sprintf('%s/%s.%s', dirname($v), substr(1001 + $k, 1), strtolower(pathinfo($v, PATHINFO_EXTENSION))));
        }
        if (is_dir($v)) {
            AutoRenameFolder($v);
        }
    }
}

function main() {
    $path = __DIR__;
    AutoRenameFolder($path);
}

main();

自定义树莓派系统镜像

wget https://downloads.raspberrypi.org/raspbian_lite_latest

unzip raspbian_lite_latest

fdisk -lu 2018-11-13-raspbian-stretch-lite.img

mount -o loop,offset=4194304 2018-11-13-raspbian-stretch-lite.img /mnt/

touch /mnt/ssh

umount /mnt/

gzip 2018-11-13-raspbian-stretch-lite.img

FRP服务端和客户端配置

一、服务端配置

# frps.ini
[common]
bind_port = 7000
token = 12345678

二、客户端配置

1. 客户端提供服务方

# frpc-01.ini
[common]
server_addr = 1.2.3.4
server_port = 7000
token = 12345678

[service_name]
type = stcp
sk = abcdefg
local_ip = 127.0.0.1
local_port = 22
use_encryption = false
use_compression = true

2. 客户端连接方

# frpc-02.ini
[common]
server_addr = 1.2.3.4
server_port = 7000
token = 12345678

[client_name]
role = visitor
type = stcp
server_name = service_name
sk = abcdefg
bind_addr = 127.0.0.1
bind_port = 2201
use_encryption = false
use_compression = true

PHP获取所有汉字的Unicode编码

$start = hexdec('4e00');
$end = hexdec('9fa5');

$s = [];

while(True) {
    $k = dechex($start);
    if ($start > $end) {
        break;
    }
    $s[] = $k . ' ' . json_decode('["\u'.$k.'"]')[0];
    $start++;
}

file_put_contents('u.txt', implode("\n", $s));

Go 开发环境配置

一、安装 govendor 工具

go get -u github.com/kardianos/govendor

二、创建工程

cd $GOPATH/src
mkdir demo
cd demo
govendor init

三、安装依赖

govendor fetch github.com/go-sql-driver/mysql@=v1.4.1
govendor fetch github.com/gin-gonic/gin@=v1.3.0
govendor fetch github.com/gomodule/redigo/redis@=v1.7.0

四、开发

package main

import (
	"database/sql"
	"log"

	"github.com/gin-gonic/gin"
	_ "github.com/go-sql-driver/mysql"
)

var Database *sql.DB

func main() {
	DatabaseInit()
	defer Database.Close()
	gin.SetMode(gin.ReleaseMode)
	r := gin.Default()
	r.GET("/", IndexAction)
	r.GET("/user/:name", UserAction)
	r.Run()
}

func DatabaseInit() {
	var err error
	Database, err = sql.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/wordpress")
	if err != nil {
		log.Fatalln(err)
	}
	Database.SetMaxIdleConns(500)
	Database.SetMaxOpenConns(500)
	err = Database.Ping()
	if err != nil {
		log.Fatalln(err)
	}
}

func IndexAction(c *gin.Context) {
	c.JSON(200, gin.H{"message": "index"})
}

func UserAction(c *gin.Context) {
	var ID int
	err := Database.QueryRow("SELECT ID FROM wp_users WHERE user_login = ?", c.Param("name")).Scan(&ID)
	if err == nil {
		c.String(200, "Hello %d", ID)
	} else {
		c.String(200, "Hello nobody")
	}
}

五、gin-app

gin-app-0.1

php上传文件到七牛云


define('ApiUrl', 'https://up-z1.qiniup.com');
define('AccessKey', 'AK');
define('SecretKey', 'SK');
define('Bucket', 'bucket');

main();

function main() {
    Upload('01.zip', '03.zip');
}

function Upload($file, $path) {

    $data = [
        'token' => getSign($path),
        'file' => new CURLFile(realpath($file)),
        'key' => $path
    ];

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, ApiUrl);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    $data = curl_exec($curl);
    $error = curl_error($curl);
    curl_close($curl);

    return $data;
}

function Encode($s) {
    return str_replace(['+', '/'], ['-', '_'], base64_encode($s));
}

function getSign($path) {
    $toSign = Encode(json_encode(['scope' => Bucket . ':' . $path, 'deadline' => time() + 1200]));
    $sign = Encode(hash_hmac('sha1', $toSign, SecretKey, true));
    return sprintf('%s:%s:%s', AccessKey, $sign, $toSign);
}

上传文件到腾讯云COS


define('SecretId', 'SecretId');
define('SecretKey', 'SecretKey');
define('BucketURL', 'https://user-id.cos.ap-city.myqcloud.com');

main();

function main(){
    cosUpload('01.txt', '/02.txt');
}

function cosUpload($file, $path) {

    $url = BucketURL . $path;
    $data = file_get_contents($file);
    $header = array(
        'Authorization: ' . RequestSign('put', $path),
        'Date: ' . gmdate('D, d M Y H:i:s T')
    );

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    $data = curl_exec($curl);
    $error = curl_error($curl);
    curl_close($curl);

    return $data;
}

function RequestSign($method, $path) {
    $signTime = (string)(time() - 60) . ';' . (string)(time() + 1200);
    $host = parse_url(BucketURL);
    $httpString = sprintf("%s\n%s\n\nhost=%s\n", strtolower($method), $path, $host['host']);
    $stringToSign = sprintf("sha1\n%s\n%s\n", $signTime, sha1($httpString));
    $signKey = hash_hmac('sha1', $signTime, SecretKey);
    $signature = hash_hmac('sha1', $stringToSign, $signKey);
    return sprintf('q-sign-algorithm=sha1&q-ak=%s&q-sign-time=%s&q-key-time=%s&q-header-list=host&q-url-param-list=&q-signature=%s', SecretId, $signTime, $signTime, $signature);
}