C hex转str str转hex

hex to str

/ hex to str
void hex_str(unsigned char *inchar, unsigned int len, unsigned char *outtxt)
{
  unsigned char hbit, lbit;
  unsigned int i;
  for (i = 0; i < len; i++)
  {
    hbit = (*(inchar + i) & 0xf0) >> 4;
    lbit = *(inchar + i) & 0x0f;

    if (hbit > 9)
      outtxt[2 * i] = 'A' + hbit - 10;
    else
      outtxt[2 * i] = '0' + hbit;

    if (lbit > 9)
      outtxt[2 * i + 1] = 'A' + lbit - 10;
    else
      outtxt[2 * i + 1] = '0' + lbit;
  }
  outtxt[2 * i] = 0;
}

str to hex

/ str to hex
unsigned int str_hex(unsigned char *str, unsigned char *hex)
{
  unsigned char ctmp, ctmp1, half;
  unsigned int num = 0;
  do
  {
    do
    {
      half = 0;
      ctmp = *str;
      if (!ctmp)
        break;
      str++;
    } while ((ctmp == 0x20) || (ctmp == 0x2c) || (ctmp == '\t'));
    if (!ctmp)
      break;
    if (ctmp >= 'a')
      ctmp = ctmp - 'a' + 10;
    else if (ctmp >= 'A')
      ctmp = ctmp - 'A' + 10;
    else
      ctmp = ctmp - '0';
    ctmp = ctmp << 4;
    half = 1;
    ctmp1 = *str;
    if (!ctmp1)
      break;
    str++;
    if ((ctmp1 == 0x20) || (ctmp1 == 0x2c) || (ctmp1 == '\t'))
    {
      ctmp = ctmp >> 4;
      ctmp1 = 0;
    }
    else if (ctmp1 >= 'a')
      ctmp1 = ctmp1 - 'a' + 10;
    else if (ctmp1 >= 'A')
      ctmp1 = ctmp1 - 'A' + 10;
    else
      ctmp1 = ctmp1 - '0';
    ctmp += ctmp1;
    *hex = ctmp;
    hex++;
    num++;
  } while (1);

  if (half)
  {
    ctmp = ctmp >> 4;
    *hex = ctmp;
    num++;
  }
  return (num);
}

引用

前端技巧

ios上点击元素出现黑框

  • 增加样式:
{
    outline:none;
    -webkit-tap-highlight-color:rgba(0,0,0,0);
}

Vuejs 内联背景图片

  • 如下般书写:
<div class="pic" :style="{ backgroundImage: `url(${require('../assets/bg.jpg')})`}"></div>

(为Docker daemon增加TSL)Protect the Docker daemon socket

在Docker daemon 宿主机上创建CA秘钥和公钥

查看docker官网文档

注意:请将下文中的所有$HOST替换为你docker daemon 所在宿主机的DNS名称

$ openssl genrsa -aes256 -out ca-key.pem 4096
Generating RSA private key, 4096 bit long modulus
............................................................................................................................................................................................++
........++
e is 65537 (0x10001)
Enter pass phrase for ca-key.pem:
Verifying - Enter pass phrase for ca-key.pem:

$ openssl req -new -x509 -days 365 -key ca-key.pem -sha256 -out ca.pem
Enter pass phrase for ca-key.pem:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:
State or Province Name (full name) [Some-State]:Queensland
Locality Name (eg, city) []:Brisbane
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Docker Inc
Organizational Unit Name (eg, section) []:Sales
Common Name (e.g. server FQDN or YOUR name) []:$HOST
Email Address []:Sven@home.org.au

现在我们已经有CA了,我们可以创建一个服务端秘钥和证书请求文件(CSR),确保上一步的Common Name和你的docker宿主机的DNS名称保持一致。

注意:请将下文中的所有$HOST替换为你docker daemon 所在宿主机的DNS名称

$ openssl genrsa -out server-key.pem 4096
Generating RSA private key, 4096 bit long modulus
.....................................................................++
.................................................................................................++
e is 65537 (0x10001)

$ openssl req -subj "/CN=$HOST" -sha256 -new -key server-key.pem -out server.csr

现在,我们可以用CA来签署证书了。这里我们可以填写IP地址或则DNS name,如,我们需要允许10.10.10.20和127.0.0.1连接:

$ echo subjectAltName = DNS:$HOST,IP:10.10.10.20,IP:127.0.0.1 >> extfile.cnf
$ echo extendedKeyUsage = serverAuth >> extfile.cnf # 使其职能用于服务端

现在我们使用CA生成签名的服务端证书:

$ openssl x509 -req -days 365 -sha256 -in server.csr -CA ca.pem -CAkey ca-key.pem \
  -CAcreateserial -out server-cert.pem -extfile extfile.cnf
Signature ok
subject=/CN=your.host.com
Getting CA Private Key
Enter pass phrase for ca-key.pem:

同样的我们生成客户端用户的私钥和证书请求文件,并使用CA生成签名证书:

$ openssl genrsa -out key.pem 4096
Generating RSA private key, 4096 bit long modulus
.........................................................++
................++
e is 65537 (0x10001)

$ openssl req -subj '/CN=client' -new -key key.pem -out client.csr

使该key可用作客服端验证,加入以下扩展配置信息。

$ echo extendedKeyUsage = clientAuth >> extfile.cnf

最后使用CA生成客户端签名的证书

$ openssl x509 -req -days 365 -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem \
  -CAcreateserial -out cert.pem -extfile extfile.cnf
Signature ok
subject=/CN=client
Getting CA Private Key
Enter pass phrase for ca-key.pem:

现在我们已经有客户端证书(cert.pem)和服务端证书(server-cert.pem)了,可以把证书请求文件删除:

$ rm -v client.csr server.csr

为了证书安全,修改证书的访问权限如下:

$ chmod -v 0400 ca-key.pem key.pem server-key.pem # 只对自己只读
$ chmod -v 0444 ca.pem server-cert.pem cert.pem # 任何人只读

使Docker daemon只接受被我们的CA验证通过的请求

  • 使用--tlsverify启动Docker daemon
$ dockerd --tlsverify --tlscacert=ca.pem --tlscert=server-cert.pem --tlskey=server-key.pem \
  -H=0.0.0.0:2376
  • 使用/etc/docker/daemon.json启动Docker daemon
$ sudo vi /etc/docker/daemon.json
{
  "tlsverify": true,
  "tlscert": "/var/docker/server-cert.pem",
  "tlskey": "/var/docker/server-key.pem",
  "tlscacert": "/var/docker/ca.pem",
  "hosts": [
    "tcp:/0.0.0.0:2376",
    "unix://var/run/docker.sock"
  ]
}
systemctl stop docker # on centos
systemctl start docker

使用Docker 客户端链接

  • 使用CLI访问
$ docker --tlsverify --tlscacert=ca.pem --tlscert=cert.pem --tlskey=key.pem \
  -H=$HOST:2376 version
  • 使用默认加证书的方式
$ mkdir -pv ~/.docker
$ cp -v {ca,cert,key}.pem ~/.docker

$ export DOCKER_HOST=tcp:/$HOST:2376 DOCKER_TLS_VERIFY=1

WordPress

wordpress docker compose file

---
wordpress:
    image: wordpress
    deploy:
      replicas: 3
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
    ports:
      - 80:80
    environment:
      WORDPRESS_DB_PASSWORD: password #your password
    volumes:
      - type: bind
        source: /usr/local/docker/wordpress/www/html # change to your host machine path
        target: /var/www/html
  mysql:
    image: mysql:5.7
    deploy:
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
    environment:
      MYSQL_ROOT_PASSWORD: password #your password
    volumes:
      - type: bind
        source: /var/lib/mysql # change to your host machine path
        target: /var/lib/mysql
  adminer:
    image: adminer
    deploy:
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
    ports:
      - 8080:8080
  portainer:
    image: portainer/portainer
    deploy:
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
    ports:
      - 9000:9000
    volumes:
      - type: bind
        source: /var/run/docker.sock
        target: /var/run/docker.sock
      - type: bind
        source: /var/lib/portainer/data # change to your host machine path
        target: /data

418-479-6751

npm

  • when you use the sudo install and got an error like so:
    node lib/install.js
    
    ⚠ EACCES: permission denied, mkdir '/usr/local/lib/node_modules/@rokid/homebase-ops-tool/node_modules/jpegtran-bin/vendor'
    ⚠ jpegtran pre-build test failed
    ℹ compiling from source
    

    just add these params follow the command

    --unsafe-perm=true --allow-root

or use this command below:

npm config set unsafe-perm=true