一个通过公网简单方便而且相对安全的传输数据的方法
- 发布
- 更新
- 字数
- 456
- 阅读
- 1 分钟
有几个小文件想要通过公网进行传输,想了个小方法。思路如下:
配置一个 nginx,然后通过 http 身份验证进行认证。
先安装 htpasswd 工具
bash
apt install apache2-utils再创建密码文件
bash
htpasswd -c /etc/nginx/.htpasswd username按提示输入密码。则生成 .htpasswd 文件。
然后在 nginx 站点配置中加入:
bash
auth_basic "Restricted Access";
auth_basic_user_file /etc/nginx/.htpasswd;这就可以实现身份验证了。
不过可以更进一步,将需要传输的小文件进行加密,然后通过文本的方式进行传播。在通过 http 获取了加密文本之后,再按约定的密码解密就可以了。
先写一个用于文件加密的 /usr/local/etc/get_encoded_file_key.sh,如下:
bash
#!/bin/bash
# cert cer file
cert_file_cer="/etc/nginx/fullchain.cer"
# password
en_password="my_en_password"
encoded_string=$(base64 "$cert_file_cer");
encrypted_string=$(echo "$encoded_string" | openssl enc -aes-256-cbc -a -salt -pass pass:$en_password -pbkdf2)
echo "$encrypted_string"然后再修改 nginx 的站点配置如下:
bash
# 路径用随机字符串填充,越随机越长越好
location /1234567890qwertyuipasdfghjklzxcvbnm {
auth_basic "Restricted Access";
auth_basic_user_file /etc/nginx/.htpasswd;
content_by_lua '
local handle = io.popen("/usr/local/etc/get_encoded_file_key.sh")
local result = handle:read("*a")
handle:close()
ngx.say(result)
';
}然后重启 nginx。至此服务端就配置好了。
再从客户端写脚本获取。
bash
#!/bin/bash
# 定义获取的用户名和密码
htauth_user="username"
htauth_password="password"
url_cer="https://$htauth_user:$htauth_password@getinfo.mydomain.com/1234567890qwertyuipasdfghjklzxcvbnm"
de_password_cer="my_en_password";
de_wget="/usr/local/bin/wget"
de_openssl="/usr/bin/openssl"
de_base64="/usr/local/bin/base64"
current_path="/usr/local/etc"
$de_wget $url_key -O $current_path/encoded_file;
$de_openssl enc -aes-256-cbc -d -in $current_path/encoded_file -out $current_path/decrypted_file -pass pass:$de_password_cer -pbkdf2 -salt -a
cat $current_path/decrypted_file | $de_base64 -d > $cert_path/fullchain.cer
rm $current_path/encoded_file
rm $current_path/decrypted_file至此完成。
这个只是一个简单的思路,肯定有不严谨不安全的地方,不建议在生产环境中使用它。
评论
正在载入评论…