Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
luolongfei committed Aug 29, 2022
2 parents 8a54459 + 17d2323 commit 2ab9ba2
Show file tree
Hide file tree
Showing 91 changed files with 29,043 additions and 47 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#####################################################################

# .env 文件版本
ENV_FILE_VERSION='v2.5'
ENV_FILE_VERSION='v2.6'

###################### 账户配置 Account config #########################
# Freenom 账户 Freenom Account
Expand Down Expand Up @@ -138,3 +138,6 @@ CUSTOM_LANGUAGE=zh

# 送信时是否显示服务器信息 1:显示 0:不显示 Whether to display server information when sending messages 1: Display 0: Do not display
SHOW_SERVER_INFO=0

# 是否给敏感信息打码 1:启用 0:不启用 Whether to enable the obfuscation of sensitive information 1: Enabled 0: Not enabled
MOSAIC_SENSITIVE_INFO=0
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

此处包含了自脚本发布以来的所有更新日志。以前的日志只记录了比较大的变更,以后的日志会尽可能详尽一些。

#### [v0.5.1](https://github.com/luolongfei/freenom/releases/tag/v0.5.1) - 2022-08-27

- 支持一键部署至 Koyeb、Heroku 等平台,虽然 Heroku 马上要收费了,但 Koyeb 依然免费
- 优化在各种环境下的目录读写权限判断
- 支持给日志或者命令行输出内容中的敏感信息打马赛克,默认不启用

#### [v0.5](https://github.com/luolongfei/freenom/releases/tag/v0.5) - 2022-05-15

- 增加支持 华为云函数、Railway 等部署方式
Expand Down
29 changes: 29 additions & 0 deletions Dockerfile.koyeb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM php:8.1.9-fpm-alpine3.16

LABEL author="mybsdc <[email protected]>" \
maintainer="luolongfei <[email protected]>"

ENV TZ Asia/Shanghai
ENV IS_KOYEB 1

WORKDIR /app

COPY . ./

COPY ./koyeb/nginx.template.conf ./
COPY ./koyeb/web ./web/

RUN set -eux \
&& apk update \
&& apk add --no-cache tzdata bash nginx gettext \
&& mkdir -p /app/logs \
&& chmod -Rf 666 /app/logs

COPY ./koyeb/startup.sh /
RUN chmod +x /startup.sh

EXPOSE 80 443 2019

# https://www.koyeb.com/docs/deploy-to-koyeb-button
# https://www.koyeb.com/docs/quickstart/deploy-a-docker-application
CMD ["/bin/bash", "-c", "/startup.sh"]
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Documentation: [English version](https://github.com/luolongfei/freenom/blob/main

[🚈 通过 Railway 部署](#-通过-Railway-部署)

[📦 通过 Koyeb 部署](#-通过-Koyeb-部署)(推荐没有自己服务器的用户使用此方案,一键部署)

[☁ 通过 各种云函数 部署](#-通过各种云函数部署)

[🚧 直接拉取源码部署](#-直接拉取源码部署)
Expand Down Expand Up @@ -79,7 +81,7 @@ Thanks for non-commercial open source development authorization by JetBrains.

无论是续期成败或者程序执行出错,都会收到脚本发出的通知。如果是续期成败相关的通知,通知会包括未续期域名的到期天数等内容。*此处展示的是通知邮件的内容。*

<a href="https://s4.ax1x.com/2022/02/26/bZr7WQ.png"><img src="https://s4.ax1x.com/2022/02/26/bZr7WQ.png" alt="邮件示例" border="0" width="670" height="540" /></a>
<a href="https://s4.ax1x.com/2022/02/26/bZr7WQ.png"><img src="https://s4.ax1x.com/2022/02/26/bZr7WQ.png" alt="邮件示例" border="0" width="95%" height="100%" /></a>

### 🎁 事前准备

Expand Down Expand Up @@ -410,8 +412,6 @@ systemctl restart docker

### 🧊 通过 Heroku 部署

~~*推荐没有自己服务器的用户使用此方案部署。此方案完全免费。*~~

**Heroku 将于 2022-11-28 停止提供免费服务,所以,忘掉本文吧。官方通告:[https://blog.heroku.com/next-chapter](https://blog.heroku.com/next-chapter)**

有关 【通过 Heroku 部署】 的具体操作步骤请参考 [此处](https://github.com/luolongfei/freenom/wiki/%E9%80%9A%E8%BF%87-Heroku-%E9%83%A8%E7%BD%B2)
Expand All @@ -427,6 +427,18 @@ systemctl restart docker

***

### 📦 通过 Koyeb 部署

*推荐没有自己服务器的用户使用此方案部署。此方案完全免费。*

有关 【通过 Koyeb 部署】 的具体操作步骤请参考 [此处](https://github.com/luolongfei/freenom/wiki/%E9%80%9A%E8%BF%87-Koyeb-%E9%83%A8%E7%BD%B2)

**在看完上行文档的具体内容,并且你确定你行后**,便可点击下方按钮,尝试一键部署:

[![Deploy to Koyeb](https://www.koyeb.com/static/images/deploy/button.svg)](https://app.koyeb.com/deploy?type=docker&name=freenom&ports=80;http;/&env[FF_TOKEN]=20190214&env[SHOW_SERVER_INFO]=1&env[MOSAIC_SENSITIVE_INFO]=1&env[FREENOM_USERNAME]=&env[FREENOM_PASSWORD]=&env[MULTIPLE_ACCOUNTS]=&env[TELEGRAM_CHAT_ID]=&env[TELEGRAM_BOT_TOKEN]=&env[TELEGRAM_BOT_ENABLE]=0&image=docker.io/luolongfei/freenom:koyeb)

***

### ☁ 通过各种云函数部署

所有云函数使用的是同一个压缩包,已做兼容处理,下载地址:
Expand Down
2 changes: 1 addition & 1 deletion app/Console/FreeNom.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class FreeNom extends Base
{
const VERSION = 'v0.5';
const VERSION = 'v0.5.1';

const TIMEOUT = 33;

Expand Down
7 changes: 7 additions & 0 deletions app/Console/Upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ public function friendlyDateFormat($date, $timezone = null)
public function handle()
{
try {
// 检查 Data 目录写权限
if (!is_writable(DATA_PATH)) {
system_log(lang('100135'));

return true;
}

if (!$this->needToUpgrade()) {
return true;
}
Expand Down
50 changes: 26 additions & 24 deletions app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,16 @@ function lang($key = '')
function system_log($content, array $response = [], $fileName = '')
{
try {
# 云函数只有 /tmp 目录可写
$path = IS_SCF ? '/tmp/' : sprintf('%s/logs/%s/', ROOT_PATH, date('Y-m'));
$file = $path . ($fileName ?: date('d')) . '.log';

if (!is_dir($path)) {
mkdir($path, 0777, true);
chmod($path, 0777);
}

$handle = fopen($file, 'a'); // 追加而非覆盖

if (!filesize($file)) {
chmod($file, 0666);
}

$msg = sprintf(
"[%s] %s %s\n",
date('Y-m-d H:i:s'),
is_string($content) ? $content : json_encode($content),
$response ? json_encode($response, JSON_UNESCAPED_UNICODE) : '');

// 在 Github Actions 上运行,过滤敏感信息
if (env('ON_GITHUB_ACTIONS')) {
$msg = preg_replace_callback('/(?P<secret>[\w-.]{1,4}?)(?=@[\w-.]+)/i', function ($m) {
// 过滤敏感信息
if ((int)env('MOSAIC_SENSITIVE_INFO') === 1) {
// 在 php 7.3 之前,连字符“-”在中括号中随便放,但在之后,只能放在开头或结尾或者转义后才能随便放
$msg = preg_replace_callback('/(?P<secret>[\w.-]{1,3}?)(?=@[\w.-]+)/ui', function ($m) {
return str_ireplace($m['secret'], str_repeat('*', strlen($m['secret'])), $m['secret']);
}, $msg);
}
Expand All @@ -100,8 +86,26 @@ function system_log($content, array $response = [], $fileName = '')
// 干掉着色标签
$msg = strip_tags($msg); // 不完整或者破损标签将导致更多的数据被删除

fwrite($handle, $msg);
fclose($handle);
// 写入日志文件
if (is_writable(ROOT_PATH)) {
$path = sprintf('%s/logs/%s/', ROOT_PATH, date('Y-m'));
$file = $path . ($fileName ?: date('d')) . '.log';

if (!is_dir($path)) {
mkdir($path, 0666, true); // 0666 所有用户可读写
}

$handle = fopen($file, 'a'); // 追加而非覆盖

if ($handle !== false) {
if (!filesize($file)) {
chmod($file, 0666);
}

fwrite($handle, $msg);
fclose($handle);
}
}

flush();
} catch (\Exception $e) {
Expand Down Expand Up @@ -238,11 +242,9 @@ function system_check()
throw new LlfException(34520006, ['7.3', PHP_VERSION]);
}

// 如果是在 云函数 或 Heroku 部署,则不需要检查这几项
if (IS_SCF || (int)env('IS_HEROKU') === 1) {
// 特殊环境无需检查这几项
if (IS_SCF || !is_writable(ROOT_PATH) || (int)env('IS_KOYEB') === 1 || (int)env('IS_HEROKU') === 1) {
system_log(lang('100009'));
system_log(lang('100010'));
system_log(lang('100011'));
} else {
if (!function_exists('putenv')) {
throw new LlfException(34520005);
Expand Down
7 changes: 3 additions & 4 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ plain='\033[0m'

# 生成配置文件
if [ ! -f /conf/.env ]; then
cp /app/.env.example /conf/.env
echo -e "[${green}Info${plain}] 已生成 .env 文件,请将 .env 文件中的配置项改为你自己的,然后重启容器(如果你是在第三方 Docker 环境中使用容器,例如 railway/heroku 等平台,可忽略本条提醒)"
cp /app/.env.example /conf/.env && echo -e "[${green}Info${plain}] 已生成 .env 文件,请将 .env 文件中的配置项改为你自己的,然后重启容器,如果当前环境非普通 VPS,可忽略此提示" || echo -e "[${yellow}Warn${plain}] 未能正常生成 .env 文件"
fi
if [ ! -f /app/.env ]; then
ln -s /conf/.env /app/.env
ln -s /conf/.env /app/.env || echo -e "[${yellow}Warn${plain}] 未能正常创建 .env 文件链接"
fi

# PHP 命令
PHP_COMMAND='php /app/run > /app/logs/freenom_cron.log 2>&1'
PHP_COMMAND='/usr/local/bin/php /app/run > /app/logs/freenom_cron.log 2>&1'

# 指定脚本执行时间
if [ -z "${RUN_AT}" ]; then
Expand Down
Binary file added koyeb/favicon.ico
Binary file not shown.
34 changes: 34 additions & 0 deletions koyeb/nginx.template.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
worker_processes auto;

error_log stderr;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
access_log /dev/stdout;
server_tokens off;

server {
listen 80;

root /app/web;

location / {
index index.php index.html index.htm;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /app/web/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
}
57 changes: 57 additions & 0 deletions koyeb/startup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env bash

#===================================================================#
# Author: luolongfei <[email protected]> #
# Intro: https://github.com/luolongfei/freenom #
#===================================================================#

set -e

# 自定义颜色变量
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'

echo -e "[${green}Info${plain}] 项目地址:https://github.com/luolongfei/freenom"
echo -e "[${green}Info${plain}] 洛阳亲友如相问,一片冰心在玉壶。 by luolongfei"

# PHP 命令
PHP_COMMAND='/usr/local/bin/php /app/run > /app/logs/freenom_cron.log 2>&1'

# 指定脚本执行时间
if [ -z "${RUN_AT}" ]; then
minute=$( shuf -i 0-59 -n 1 )
hour=$( shuf -i 6-23 -n 1 )
CRON_COMMAND="${minute} ${hour} * * * ${PHP_COMMAND}"
echo -e "[${green}Info${plain}] 已自动指定执行时间,续期任务将在北京时间每天 「${hour}:${minute}」 执行"
echo -e "[${green}Info${plain}] 在没有手动指定 RUN_AT 环境变量的情况下,每次重建容器,程序都会重新在 06 ~ 23 点全时段中自动随机指定一个执行时间,目的是防止很多人在同一个时间点执行任务导致 Freenom 无法稳定提供服务"
else
if [[ "${RUN_AT}" =~ ^([01][0-9]|2[0-3]|[0-9]):([0-5][0-9]|[0-9])$ ]]; then
minute=$( echo ${RUN_AT} | egrep -o '([0-5][0-9]|[0-9])$' )
hour=$( echo ${RUN_AT} | egrep -o '^([01][0-9]|2[0-3]|[0-9])' )
CRON_COMMAND="${minute} ${hour} * * * ${PHP_COMMAND}"
echo -e "[${green}Info${plain}] 你已指定执行时间,续期任务将在北京时间每天 「${hour}:${minute}」 执行"
elif [ "$(php /app/run -c=Cron -m=verify --cron_exp="${RUN_AT}")" -eq 1 ]; then
CRON_COMMAND="${RUN_AT} ${PHP_COMMAND}"
echo -e "[${green}Info${plain}] 你自定义的 Cron 表达式为「${RUN_AT}」,已通过正则验证"
else
echo -e "[${red}Error${plain}] RUN_AT 的值无效"
echo -e "${yellow}请输入一个有效的时间指令,其值可以为时分格式,如:11:24,也可以为 Cron 表达式,如:'24 11 * * *',甚至可以不输入,让程序自动生成,推荐采用自动生成的方式,不建议手动指定此环境变量"
exit 1
fi
fi

# 添加计划任务
sed -i '/freenom_cron/'d /etc/crontabs/root
echo -e "${CRON_COMMAND}" >> /etc/crontabs/root
echo -e "[${green}Info${plain}] 计划任务:${CRON_COMMAND}"

# 启动 Cron
/usr/sbin/crond

# nginx 配置
cp /app/nginx.template.conf /app/nginx.conf

# 启动 php-fpm 与 nginx
php-fpm -D -R; nginx -c /app/nginx.conf -g 'daemon off;'
5 changes: 5 additions & 0 deletions koyeb/web/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"sensiolabs/ansi-to-html": "^1.2"
}
}
Loading

0 comments on commit 2ab9ba2

Please sign in to comment.