2020年2月

frp管理端截图

因为家里有台蜗牛星际安装了WIN10作为下载机同时用hyper-v 和 docker 跑了一些服务,不在家的时候用vnc远程连接操作或者用vsc的远程开发连到hyper-v上的Centos6.5
下面介绍一下穿透内网vnc远程桌面内网机器的过程

家里用的是移动宽带,没有外网IP
不在家的话只好用内网穿透

不过前提条件是需要有一台有固定IP的VPS

内网是:WIN10 (客户端)
外网中转VPS (服务器端)
操作系统:CENTOS6.5
固定IP:123.123.123.123

我用的是
https://github.com/fatedier/frp
下载对应的文件,分别是
WIN10用 frp_0.31.2_windows_amd64.zip
CENTOS6.5用 frp_0.31.2_linux_arm64.tar.gz

服务器端

文件放在这里 /data/services/frp/
修改配置文件 frps.ini

[common]
bind_addr = 0.0.0.0
bind_port = 7100
# 设置管理后台的用户名和密码
dashboard_user = king
dashboard_pwd = king
dashboard_port = 7700

后执行测试一下
# frps -c frps.ini
通过浏览器访问
http://123.123.123.123:7700
这里要注意vps要配置一下防火墙放开 7100和7700两个端口
centos 就是
# vi /etc/sysconfig/iptables
添加
-A INPUT -m state --state NEW -m tcp -p tcp --dport 7100 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 7700 -j ACCEPT
保存后重启防火墙
# service iptables restart
当然你的VPS是在腾讯云这些云平台的话还要注意一下安全组的设置

客户端

下载的文件放在 c:/frp
修改frpc.ini

[common]
server_addr = 123.123.123.123 #这里要和上面配置的服务器端口一致
server_port = 7100

[vnc] #名称自定义
type = tcp #vnc走的是tcp
local_ip = 127.0.0.1 #本地IP
local_port = 5900 #vnc默认的端口
remote_port = 6000 #转发端口

然后执行
frpc.exe -c frpc.ini

然后手机断开WIFI在4G下用VNC连接 123.123.123.123:6000 测试一下

WIN10开机启动 frpc

用这个工具 https://github.com/kohsuke/winsw
下载对应平台的执行文件,我这里是
WinSW.NET461.exe
把执行文件改名为 winsw.exe 放到 c:/frp
添加新增一个配置文件 winsw.xml 放到 c:/frp

<service>
    <id>frp</id>
    <name>frp</name>
    <description>frp remote control</description>
    <executable>frpc</executable>
    <arguments>-c frpc.ini</arguments>
    <logmode>reset</logmode>
</service>

然后以管理员的身份cmd,执行如下命令
winsw install
winsw start
搞定!

CENTOS6.5开机启动 frps

添加文件 /data/services/frp/frps.sh
nohup /data/services/frp/frps -c /data/services/frp/frps.ini >> /data/services/frp/frps.log 2>&1 &
添加文件 /etc/init.d/frps

#!/bin/bash
#
# Comments to support chkconfig
# chkconfig: - 98 02
# description: your_prog_name service script
#
# Source function library.
. /etc/init.d/functions

### Default variables
prog_name="frps"
prog_path="/data/services/frp/frps.sh"
pidfile="/var/run/${prog_name}.pid"
prog_demon="frps"
options=""

# Check if requirements are met
[ -x "${prog_path}" ] || exit 1

RETVAL=0

start(){
  echo -n $"Starting $prog_name: "
  daemon $prog_path $options
  RETVAL=$?
  PID=$(pidof ${prog_demon})
  [ ! -z "${PID}" ] && echo ${PID} > ${pidfile}
  echo
  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog_name
  return $RETVAL
}

stop(){
  echo -n $"Shutting down $prog_name: "
  killproc -p ${pidfile}
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog_name
  return $RETVAL
}

restart() {
  stop
  start
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    restart
    ;;
  status)
    status $prog_demon
    RETVAL=$?
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart|status}"
    RETVAL=1
esac

exit $RETVAL

执行以下命令
# chkconfig frps on 设置开机运行
# service frps start 启动
# service frps status 查看状态
# service frps stop 关闭

把下面脚本放到,路径自己决定
/data/services/godaddy/ddns.sh
然后 crontab -e 添加,频率自己修改
*/5 * * * * /data/services/godaddy/ddns.sh >> /data/services/godaddy/ddns.log

顺便找了几个获取公网IP的接口
http://ip-api.com/json/?lang=zh-CN

有频率限制,不能短时间内重复调用,应该是因为可以查任意IP归属地,所以限制了
http://ip.taobao.com/service/getIpInfo.php?ip=myip
这个也可以任意查任意IP归属地
http://ip-api.com/json/113.97.18.125?lang=zh-CN

http://www.taobao.com/help/getip.php

http://pv.sohu.com/cityjson

ddns.sh

# 你的域名,注意不包括主机名
domain="king.com"
# 主机名,支持泛域名哦也支持 @
name="*" 
# 登录 godaddy 查看
key="xxxxxxxxxxxxxxxx" 
# 登录 godaddy 查看
secret="xxxxxxxxxxx"

headers="Authorization: sso-key $key:$secret"
cur_dateTime="`date +%Y-%m-%d,%H:%M:%S`";
echo $cur_dateTime;
# echo $headers
result=$(curl -s -X GET -H "$headers" \
"https://api.godaddy.com/v1/domains/$domain/records/A/$name")
dnsIp=$(echo $result | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")
echo "dnsIp:" $dnsIp

# 获取你的公网IP
ret=$(curl -s GET "http://pv.sohu.com/cityjson")
currentIp=$(echo $ret | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")
echo "currentIp:" $currentIp
if [ "$dnsIp" != "$currentIp" ];
then
    echo "Ips are not equal"
    request='[{"data":"'$currentIp'","ttl":3600}]'
    echo $request
    nresult=$(curl -i -s -X PUT \
 -H "$headers" \
 -H "Content-Type: application/json" \
 -d $request "https://api.godaddy.com/v1/domains/$domain/records/A/$name")
    echo $nresult
fi  

用户文件多的话,例如头像,存储到CDN的时候也要考虑目录平均散列
比较常用是按日期,例如 /2020/02/
但是缺点也非常明显,分布不均
或者直接按userId取模,例如 userId % 1000 分1000个目录来存放,只要userId是连续的,文件就必然是平均分布到这1000个目录
可是,数量很大的时候,一级目录下每个目录的文件数量或者一级目录就会非常多。
那么可以按二级目录来存放

function buildPath($id, $l1num, $l2num)
{
    //(id %(二级目录数量*二级目录数量) / 二级目录数量 => 取整 一级目录
    //(id %(二级目录数量*二级目录数量)% 二级目录数量 => 二级目录
    $tmp = $id % ($l1num * $l2num);
    $l1 = floor($tmp / $l2num);
    $l2 = $tmp % $l2num;
    return ['l1' => $l1, 'l2' => $l2];
}

为了方便演示我把一级目录和二级目录数量设置得比较小的值
就算是

$l1num = 10;
$l2num = 100;
$fileNum = 1000000;

每个目录就1000个文件,这样足够了:)
测试

$l1num = 2; //一级目录数量
$l2num = 5; //二级目录数量
$fileNum = 1000000; //文件数量
$dir = [];
for($i = 0; $i < $l1num; $i++) {
    for ($j = 0; $j < $l2num; $j++) {
        $dir[$i][$j] = 0;
    }
}
for ($i = 1; $i <= $fileNum; $i++) {
    $rs = $this->buildPath($i, $l1num, $l2num);
    $dir[$rs['l1']][$rs['l2']]++;
}
print_r($dir);
Array
(
    [0] => Array
        (
            [0] => 100000
            [1] => 100000
            [2] => 100000
            [3] => 100000
            [4] => 100000
        )

    [1] => Array
        (
            [0] => 100000
            [1] => 100000
            [2] => 100000
            [3] => 100000
            [4] => 100000
        )

)

如果没有唯一id的话也可以使用文件md5后的hash截取部份字串来作来目录来存放,不过缺点就是目录数量就非常多,而且hash费cpu