Compare commits

..
178 Commits
Author SHA1 Message Date
zhengkunwang223 53756c382d feat: 取消网站 ipv6 的默认设置 (#1099) 2023-05-22 11:44:49 +08:00
zhengkunwang223 8533a71670 fix: 解决首页控制台报错的问题 (#1087) 2023-05-21 22:16:53 +08:00
zhengkunwang223 6b3c06a057 fix: 解决伪静态页面光标丢失的问题 (#1086) 2023-05-21 22:16:50 +08:00
zhengkunwang223 4fd87568cd fix: 解决搜索子目录模糊查询失败的问题 (#1085)
Refs https://github.com/1Panel-dev/1Panel/issues/1049
2023-05-21 22:16:44 +08:00
zhengkunwang223 56f8caf561 fix: 解决打开编辑文件页面确认按钮可能看不到的问题 (#1084)
Refs https://github.com/1Panel-dev/1Panel/issues/1042
2023-05-21 22:16:40 +08:00
zhengkunwang223 7d2f7a8d3b fix: 解决反向代理 1Panel 并启用 auth_basic 的时候无法登录的问题 (#1083)
Refs https://github.com/1Panel-dev/1Panel/issues/1082
2023-05-21 22:16:36 +08:00
xin.yang 01a9589be9 修复首页推荐应用处在窄窗口时显示异常的问题 2023-05-21 22:15:45 +08:00
zhengkunwang223 72eb02fd39 feat: 升级 distribution 版本到 v2.8.2 (#1067) 2023-05-21 22:15:16 +08:00
ssongliu c1332235a0 fix: 右侧抽屉宽度调整 (#1047) 2023-05-15 02:36:47 +00:00
ssongliu 212c8634ea fix: 容器配置界面样式调整 (#1045) 2023-05-15 02:22:47 +00:00
zhengkunwang223 b966ab3e11 fix: 解决创建数据库超时问题 (#1037) 2023-05-14 01:34:31 +00:00
zhengkunwang223 25c2246782 fix: 解决默认网站开启 https 443 端口没有设置 default_server 的问题 (#1034) 2023-05-14 01:04:31 +00:00
ssongliu 2ae1db4730 fix: 解决 ssl 启用和端口修改跳转问题 (#1030) 2023-05-13 12:20:30 +00:00
ssongliu 43652b2a54 fix: 解决数据库上传备份恢复失败的问题 (#1027) 2023-05-13 07:32:28 +00:00
ssongliu 574a504ec3 fix: 增加初始化逻辑 (#1026) 2023-05-13 04:20:29 +00:00
zhengkunwang223 52a8331c78 fix: 解决网站设置 https 情况下,设置默认网站失败的问题 (#1023) 2023-05-12 15:08:28 +00:00
ssongliu afa9eecf35 fix: 补全 swagger 文档 (#1022) 2023-05-12 22:30:50 +08:00
ssongliu 46e13d754f fix: 解决计划任务未勾选同步到本地造成的备份恢复问题 (#1021) 2023-05-12 22:30:26 +08:00
zhengkunwang223 0157326d61 fix: 限制应用失败情况下启动应用 (#1020) 2023-05-12 22:30:02 +08:00
ssongliu 7d08875f95 fix: 解决数据库用户指定多个 ip 无效的问题 (#1019) 2023-05-12 17:48:31 +08:00
zhengkunwang223 bd2a49e299 fix: 解决网站设置切换到 HTTPS 控制台报错的问题 (#1016)
fix https://github.com/1Panel-dev/1Panel/issues/672
2023-05-12 07:08:23 +00:00
ssongliu 06f1d03b93 fix: 端口校验正则表达式修改 (#1009) 2023-05-12 03:36:23 +00:00
Mystery0 M 4c39955f2f fix: 获取网站日志时,先检查一下日志文件大小,超过10M时直接报错 (#1011)
#### What this PR does / why we need it?

修复网站日志文件较大时,在界面上查看日志会导致系统短时间卡住的问题

相关Issue https://github.com/1Panel-dev/1Panel/issues/495
该提交可优化这个Issue的问题

#### Summary of your change

在获取文件内容之前,通过os.Stat获取文件的信息,并对文件大小进行判断,如果过大(目前是>10MB),则抛出错误中止流程

#### Please indicate you've done the following:

- [ ] Made sure tests are passing and test coverage is added if needed.
- [x] Made sure commit message follow the rule of [Conventional Commits specification](https://www.conventionalcommits.org/).
- [ ] Considered the docs impact and opened a new docs issue or PR with docs changes if needed.
2023-05-12 02:54:23 +00:00
maninhill 56f92310f8 Update README.md (#1005) 2023-05-11 23:27:01 +08:00
maninhill cb47806fd6 Update README.md 2023-05-11 21:28:57 +08:00
maninhill 16ae193e9f Update README.md 2023-05-11 15:33:17 +08:00
fossabot 8770939328 Add license scan report and status
Signed off by: fossabot <badges@fossa.com>
2023-05-11 15:33:17 +08:00
ssongliu d5bff6473e fix: 解决安全入口跳转失败的问题 (#995) 2023-05-10 15:28:18 +00:00
ssongliu a1c0408aae fix: 解决安全入口情况下升级后页面无法打开的问题 (#994) 2023-05-10 15:16:48 +00:00
ssongliu ab523b6879 fix: 解决密码过期后重定向到登录页的问题 (#990) 2023-05-10 09:12:18 +00:00
zhengkunwang223 740b4c52d2 fix: 修复已安装页面 php 应用丢失的问题 (#989) 2023-05-10 08:52:16 +00:00
ssongliu dbb94942df fix: 镜像导出增加默认值 (#988) 2023-05-10 08:16:15 +00:00
zhengkunwang223 1e4ea2f8c3 feat: 文件操作增加名称显示 (#985)
resolve  https://github.com/1Panel-dev/1Panel/issues/975
2023-05-10 07:02:15 +00:00
ssongliu 8083837333 fix: 容器监控适配部分单核系统 (#984) 2023-05-10 06:48:15 +00:00
ssongliu dd89933613 fix: ntp 依赖修改 (#983) 2023-05-10 06:36:15 +00:00
ssongliu 5101dace59 fix: 优化抽屉中操作对象的名称显示 (#982) 2023-05-10 06:34:19 +00:00
zhengkunwang223 81c56a740c feat: 限制 PHP 应用升级 (#981) 2023-05-10 06:32:23 +00:00
zhengkunwang223 27cc3bcccd fix: 解决运行环境版本与已安装应用版本不一致的问题 (#980)
Closes https://github.com/1Panel-dev/1Panel/issues/978
2023-05-10 06:20:16 +00:00
ssongliu 8ddaa26a57 fix: 解决本地备份账号更新失败的问题 (#977)
Closes #966
2023-05-10 06:08:14 +00:00
ssongliu 3a848428c3 fix: 忽略概览页异常数据 (#971) 2023-05-10 02:52:14 +00:00
ssongliu 4c18f3aa1d fix: 解决验证码加载失败的问题 (#970) 2023-05-10 02:50:18 +00:00
maninhill be6278c0b7 Update README.md 2023-05-09 23:50:17 +08:00
maninhill d5e08d5db2 Update README.md 2023-05-09 23:49:24 +08:00
zhengkunwang223 2d321b4a79 feat: app.yaml 增加 username 和 password 参数 (#960) 2023-05-09 09:31:44 +00:00
zhengkunwang223 fcd764d521 feat: 应用商店同步改为异步操作 (#956) 2023-05-09 07:47:43 +00:00
ssongliu 7d4a8782d8 fix: 解决登录情况下,输入任意路由跳转到安全入口的问题 (#954) 2023-05-09 07:45:47 +00:00
zhengkunwang223 eea28e8481 feat: 升级badger版本到v4 (#951) 2023-05-09 07:23:43 +00:00
FoXiMao 71d679d426 Fix maccms rewrite configuration file error (#948)
修复maccms rewrite 配置文件错误导致登陆时提示ERR_TOO_MANY_REDIRECTS 重定向次数过多的问题

#### What this PR does / why we need it?

#### Summary of your change

#### Please indicate you've done the following:

- [x] Made sure tests are passing and test coverage is added if needed.
- [x] Made sure commit message follow the rule of [Conventional Commits specification](https://www.conventionalcommits.org/).
- [ ] Considered the docs impact and opened a new docs issue or PR with docs changes if needed.
2023-05-09 07:21:47 +00:00
zhengkunwang223 2b23523bec fix: 修复文件列表总共条数显示问题 (#952) 2023-05-09 07:17:47 +00:00
wangdan-fit2cloud 149c26728c fix: 夜间模式ui优化(https://github.com/1Panel-dev/1Panel/issues/938) (#953)
#### What this PR does / why we need it?

#### Summary of your change

#### Please indicate you've done the following:

- [ ] Made sure tests are passing and test coverage is added if needed.
- [ ] Made sure commit message follow the rule of [Conventional Commits specification](https://www.conventionalcommits.org/).
- [ ] Considered the docs impact and opened a new docs issue or PR with docs changes if needed.
2023-05-09 07:15:46 +00:00
zhengkunwang223 8c5c2440fe fix: 解决定时任务停止网站失败的问题 (#933) 2023-05-08 09:03:41 +00:00
ssongliu f22caed20c fix: 提示信息国际化修改 (#931) 2023-05-08 08:49:41 +00:00
zhengkunwang223 ed3e7e7c26 fix: 解决停止一键部署网站失败的问题 (#930) 2023-05-08 08:39:41 +00:00
zhengkunwang223 54bc33a1a2 fix: 解决删除 Openresty 之后网站列表页面报错的问题 (#926) 2023-05-08 07:11:39 +00:00
ssongliu 900e141297 feat: 增加 iptables 设置修改提示信息 (#925) 2023-05-08 06:57:39 +00:00
zhengkunwang223 ca1eca476c feat: 解决时区获取不到导致的网站自动停止的问题 (#921) 2023-05-08 05:01:39 +00:00
ssongliu c4d2593a42 feat: 更新 ip 地址库 (#922) 2023-05-08 04:55:39 +00:00
zhengkunwang223 932811c167 feat: 文件搜索框样式调整 (#919) 2023-05-08 03:23:37 +00:00
zhengkunwang223 95ad101a08 fix: 解决同名反向代理失败导致文件被删除的问题 (#910) 2023-05-06 11:13:34 +00:00
zhengkunwang223 a0c329019f fix: 解决应用列表页面搜索之后点击安装没响应的问题 (#909) 2023-05-06 09:57:34 +00:00
ssongliu 8a92913230 fix: 解决计划任务部分脚本执行没有输出的问题 (#908) 2023-05-06 09:23:34 +00:00
ssongliu 5a8deddc63 feat: ssl 设置增加重启提示信息 (#905) 2023-05-06 08:51:34 +00:00
ssongliu 2a1e60da44 fix: 创建编排时,默认显示编辑模式 (#904) 2023-05-06 08:49:38 +00:00
ssongliu 6ca3deeac1 fix: 优化编排删除提示信息 (#902) 2023-05-06 08:09:33 +00:00
zhengkunwang223 1b2edbb79a feat: 非静态网站和运行环境网站隐藏网站目录权限设置 (#901) 2023-05-06 08:07:38 +00:00
zhengkunwang223 49c9929a53 feat: 网站域名跳转增加端口 (#900) 2023-05-06 07:49:33 +00:00
zhengkunwang223 8ce73a38dd feat: 网站新增域名,自动放开相应端口 (#899) 2023-05-06 07:27:34 +00:00
zhengkunwang223 28ebf7a0cc fix: 解决文件搜索不存在的资源后,点击下面存在的目录也会提示资源不存在的问题 (#896) 2023-05-06 06:49:33 +00:00
ssongliu 4f168656fc fix: mfa 启用时,增加时间同步提示信息 (#895) 2023-05-06 06:27:33 +00:00
zhengkunwang223 7f1f758e60 feat: 网站列表增加网站目录跳转功能 (#892) 2023-05-06 04:05:33 +00:00
maninhill f8ab71953d chore: remove console.log 2023-05-06 11:22:24 +08:00
ssongliu eb50ee1c03 fix: 数据库创建重名用户问题修改 (#887) 2023-05-06 02:15:32 +00:00
ssongliu a184cb9bc4 fix: 证书创建校验修改 (#884) 2023-05-05 10:31:30 +00:00
zhengkunwang223 0b50a10fb8 feat: 搜索子目录增加提示 (#883) 2023-05-05 09:35:30 +00:00
zhengkunwang223 9bafdc1b0b 增加安全问题联系人 (#882) 2023-05-05 09:33:34 +00:00
ssongliu 63af54353b feat: 监控设置改到【主机-监控】菜单内 (#881) 2023-05-05 09:31:31 +00:00
ssongliu b67739af13 feat: 面板设置统一改为抽屉实现 (#880) 2023-05-05 07:54:52 +00:00
zhengkunwang223 40633619ca fix: 解决解压 mac 压缩文件失败的问题 (#879) 2023-05-05 07:50:52 +00:00
zhengkunwang223 e672d1d896 fix: 解决反向代理网站停止不生效的问题 (#878) 2023-05-05 07:02:54 +00:00
zhengkunwang223 c69d162f5a fix: 修改用户名长度限制 (#877) 2023-05-05 06:42:52 +00:00
ssongliu 2fd97b1753 fix: 解决列表查询输入框刷新的问题 (#876) 2023-05-05 06:16:52 +00:00
zhengkunwang223 5f893929da fix: 解决反向代理停用无法删除的问题 (#875) 2023-05-05 06:14:56 +00:00
ssongliu 1c06b7b608 fix: 删除初始化用户界面及接口 (#874) 2023-05-05 03:56:52 +00:00
ssongliu 3ac64d65b6 fix: 修改初始化错误返回 (#873) 2023-05-05 03:40:51 +00:00
ssongliu f4f83283d3 fix: 容器创建端口校验修改 (#872) 2023-05-05 10:17:28 +07:00
zhengkunwang223 aed4a55655 feat: 升级 gin 版本到 v1.9.0 (#871) 2023-05-05 02:56:56 +00:00
ssongliu 5f55a56a9e fix: 指定 IP 输入增加提示信息 (#867) 2023-05-04 11:12:52 +00:00
ssongliu e95e79b572 fix: 限制描述长度 (#866) 2023-05-04 09:44:41 +00:00
ssongliu 15c0d0074b fix: 安全入口逻辑调整 (#863) 2023-05-04 08:40:37 +00:00
zhengkunwang223 f4716cb62f feat: 创建网站支持 ipv6 (#861) 2023-05-04 06:28:38 +00:00
ssongliu 1f2dfce7d1 fix: 初始化密码时,对密码进行加密操作 (#860) 2023-05-04 06:14:38 +00:00
ssongliu c679631440 fix: 时区问题修改 (#855) 2023-05-04 04:00:37 +00:00
zhengkunwang223 c62fd4841a feat: 增加已安装应用操作的执行时间 (#854) 2023-05-04 03:58:41 +00:00
ssongliu a073113817 fix: redis 按钮位置调整 (#856) 2023-05-04 03:56:37 +00:00
ssongliu 09d462b829 feat: 验证码输入判断逻辑修改 (#842) 2023-04-30 15:58:24 +00:00
ssongliu 379b171f0a fix: 解决容器创建批量端口失败的问题 (#830) 2023-04-28 14:46:20 +00:00
ssongliu c424e22924 fix: 两步验证样式调整 (#827) 2023-04-28 07:58:19 +00:00
zhengkunwang223 18e171446e feat: 解决文件上传成功后再次点击“确认”会重新上传一遍的问题 (#826) 2023-04-28 07:50:18 +00:00
zhengkunwang223 c0a1555c73 style: 网站列表增加 WAF 入口 (#825) 2023-04-28 07:18:19 +00:00
ssongliu 3b0c844f33 fix: 解决回车导致页面刷新的问题 (#824) 2023-04-28 07:16:22 +00:00
ssongliu 566a4f3568 fix: 面板设置证书改为抽屉实现 (#823) 2023-04-28 06:48:19 +00:00
ssongliu 2c8b19bff2 feat: 两步验证增加手动输入选项 (#822) 2023-04-28 03:42:16 +00:00
ssongliu 292dca6419 fix: 安全入口逻辑调整 (#821) 2023-04-27 14:44:16 +00:00
zhengkunwang223 ebe0f98209 feat: 文件搜索增加搜索子目录功能 (#819) 2023-04-27 21:31:52 +08:00
zhengkunwang223 eba1e5495f style: 运行环境页面增加提示 (#820) 2023-04-27 12:52:20 +00:00
ssongliu 0ebd04f012 feat: 1pctl 支持 mfa、ssl 和安全入口关闭 (#818) 2023-04-27 11:06:15 +00:00
maninhill c5e8a3fa04 Update README.md (#817)
#### What this PR does / why we need it?

#### Summary of your change

#### Please indicate you've done the following:

- [ ] Made sure tests are passing and test coverage is added if needed.
- [ ] Made sure commit message follow the rule of [Conventional Commits specification](https://www.conventionalcommits.org/).
- [ ] Considered the docs impact and opened a new docs issue or PR with docs changes if needed.
2023-04-27 09:36:15 +00:00
maninhill 42b76fd82d Update README.md 2023-04-27 17:21:58 +08:00
zhengkunwang223 bbf610569d fix: 解决本地应用升级失败的问题 (#813) 2023-04-27 07:30:15 +00:00
ssongliu eeb5fa81a1 feat: 安全登录界面样式优化 (#814) 2023-04-27 07:18:16 +00:00
zhengkunwang223 f65ca6678f style: 网站-密码访问 名称 改为 用户名 (#812) 2023-04-27 06:02:15 +00:00
zhengkunwang223 680f48dcba feat: 文件权限增加修改子文件功能 (#811) 2023-04-27 05:54:15 +00:00
ssongliu 8947e00302 fix: 面板设置样式调整 (#810) 2023-04-27 04:46:14 +00:00
ssongliu b42cf32326 feat: 编排删除增加删除文件勾选 (#808) 2023-04-27 04:44:18 +00:00
ssongliu 5b68332b9a fix: compose 验证、提交按钮合并 (#806) 2023-04-27 02:24:14 +00:00
zhengkunwang223 d7f53862e9 fix: 解决文件编辑之后大小没有改变的 BUG (#804) 2023-04-26 14:30:14 +00:00
zhengkunwang223 7887bf96de feat: 文件管理增加用户/用户组设置 (#803) 2023-04-26 14:18:16 +00:00
zhengkunwang223 db2aa35b2f fix: 解决文件复制到原路径导致文件内容清空的 BUG (#798) 2023-04-26 12:30:14 +00:00
ssongliu 936b0e59ab fix: 修复概览页流量显示问题 (#797) 2023-04-26 10:56:14 +00:00
zhengkunwang223 6e3923d0da feat: 优化文件复制粘贴逻辑 (#796) 2023-04-26 09:58:12 +00:00
ssongliu 4377575206 feat: 存储卷创建支持 nfs (#795) 2023-04-26 09:26:12 +00:00
ssongliu 0c09b12680 fix: 编排创建样式调整、日志改为异步加载 (#794) 2023-04-26 09:00:20 +00:00
zhengkunwang223 8a32d8032f feat: 修改反向代理网站创建逻辑 (#793) 2023-04-26 08:06:12 +00:00
zhengkunwang223 9f12a91f4a feat: 页脚增加论坛、文档入口 (#792) 2023-04-26 07:36:11 +00:00
ssongliu dd0ca4bcaf feat: 创建容器时,端口支持多种形式 (#790) 2023-04-26 07:18:13 +00:00
ssongliu 14cc97eb44 fix: 证书校验规则修改 (#788) 2023-04-26 00:04:10 +00:00
ssongliu 2d31c5b005 feat: 容器增加资源使用、端口显示 (#786) 2023-04-25 14:06:17 +00:00
zhengkunwang223 05e7506f61 feat: 1pctl version 增加 mode 参数 (#784) 2023-04-25 13:28:17 +00:00
zhengkunwang223 37806f1113 feat: 网站增加密码访问功能 (#782) 2023-04-25 10:10:17 +00:00
ssongliu 79622f324b feat: 1pctl user-info 返回增加 protocol 信息 (#781) 2023-04-25 10:02:17 +00:00
ssongliu 34e84081e3 feat: 增加系统 ssl 设置功能 (#780) 2023-04-25 06:34:16 +00:00
ssongliu edf07be281 fix: 解决容器终端断开后未退出进程的问题 (#779) 2023-04-25 06:32:16 +00:00
zhengkunwang223 1208514f37 style: 文件列表样式修改 (#775) 2023-04-24 14:10:14 +00:00
你的明明呐丶andblank bfd857ec4b feat: 增加复制路径功能 (#749)
Co-authored-by: blank <admin@zym88.cn>
2023-04-24 18:21:44 +08:00
zhengkunwang223 9435290bb6 feat: 修改 api 文档 2023-04-24 17:53:05 +08:00
zhengkunwang223 acf64dcf25 feat: 反向代理增加编辑源文功能 2023-04-24 17:53:05 +08:00
zhengkunwang223 2dd88364f8 feat: 反向代理增加编辑功能 2023-04-24 17:53:05 +08:00
zhengkunwang223 d900b52a50 feat: 网站增加创建反向代理功能 2023-04-24 17:53:05 +08:00
ssongliu bd8d96be4d fix: 安全入口长度限制改为 6-10 位 2023-04-24 14:30:54 +08:00
ssongliu 17dd07fe32 feat: 概览页增加未启用安全入口提示信息 2023-04-24 14:30:54 +08:00
ssongliu a06e5f28b3 feat: 增加单独的安全入口接口,防止泄漏 2023-04-24 14:30:54 +08:00
ssongliu d5f400670c feat: 增加系统安全入口功能 2023-04-24 14:30:54 +08:00
ssongliu 5222388ba1 fix: 解决中文插件下 ufw 防火墙获取端口列表失败的问题 (#746) 2023-04-21 08:45:22 +00:00
ssongliu 549ccbe6a0 fix: 解决 ufw 防火墙版本加载失败问题 (#742) 2023-04-21 04:28:11 +00:00
邓小明 7fd6afb17f 为了安全,使面板首页禁止搜索引擎收录 (#721) 2023-04-21 10:28:59 +08:00
zhengkunwang223 2ff4da5e46 feat: 静态网站默认页增加禁止搜索引擎收录 (#741) 2023-04-21 02:26:11 +00:00
ssongliu 0f6e98be20 fix: 验证码错误后清空验证码输入框 (#740) 2023-04-21 02:08:10 +00:00
ssongliu 4399ffa9a4 fix: 延长备份下载超时时间 (#739) 2023-04-20 15:16:18 +00:00
ssongliu 44a1d9d16c fix: 防火墙端口添加支持输入网段 (#738) 2023-04-20 15:10:17 +00:00
ssongliu 565fd1c605 fix: ufw 增加中文适配 2023-04-20 22:34:50 +08:00
ssongliu 09ac40846f fix: ufw 防火墙增加 sudo 判断 (#733) 2023-04-20 10:44:17 +00:00
zhengkunwang223 a0b820649e fix: 解决网站设置用户/组报错的问题 (#710) 2023-04-19 03:17:00 +00:00
zhengkunwang223 6cee4bfe7c feat: 修改伪静态配置存储目录 (#703) 2023-04-18 15:27:00 +00:00
zhengkunwang223 ad0c859b54 fix: 解决网站目录重复的问题 (#695) 2023-04-18 11:19:00 +00:00
zhengkunwang223 d660b7d315 feat: 网站增加设置目录运行用户和用户组的功能 (#694) 2023-04-18 10:46:58 +00:00
wangdan-fit2cloud 2dbc7f28fd style: 修改网站目录样式 (#693)
#### What this PR does / why we need it?

#### Summary of your change

#### Please indicate you've done the following:

- [ ] Made sure tests are passing and test coverage is added if needed.
- [ ] Made sure commit message follow the rule of [Conventional Commits specification](https://www.conventionalcommits.org/).
- [ ] Considered the docs impact and opened a new docs issue or PR with docs changes if needed.
2023-04-18 10:24:59 +00:00
ssongliu e224bc4b24 fix: 延长快照超时时间 (#691) 2023-04-18 09:32:59 +00:00
zhengkunwang223 9c52977825 style: 修改网站目录提示信息 (#688) 2023-04-18 08:02:58 +00:00
zhengkunwang223 cb151dc985 feat: mysql 和 redis 增加连接信息提示 (#687) 2023-04-18 07:30:57 +00:00
zhengkunwang223 24b9f8f705 fix: 解决网站过期时间设置为永久之后再次打开过期时间错误的问题 (#685) 2023-04-18 07:26:57 +00:00
zhengkunwang223 5592063e69 feat: 删除提示页面增加换行 (#684) 2023-04-18 07:25:00 +00:00
zhengkunwang223 7b19aab305 删除无用文件 (#677) 2023-04-17 10:32:36 +00:00
ssongliu 5c50695bdc fix: 登录页样式修改 (#676) 2023-04-17 10:30:40 +00:00
zhengkunwang223 1086597e3a feat: 增加配置网站运行目录功能 (#675) 2023-04-17 08:54:34 +00:00
ssongliu 2944ea508e fix: 容器创建增加镜像存在判断 (#674) 2023-04-17 08:08:28 +00:00
ssongliu 5e887bd00c feat: 备份账号删除时,查询使用情况 (#670) 2023-04-17 08:06:28 +00:00
zhengkunwang223 76b3cf4d2b feat: 运行环境支持手动输入 PHP 扩展 (#668) 2023-04-17 06:04:22 +00:00
ssongliu 4a9895218e fix: 命令执行增加超时时间 (#667) 2023-04-17 06:03:01 +00:00
ssongliu 222593ea69 fix: 页脚特殊符号修改 (#665) 2023-04-17 02:22:14 +00:00
ssongliu 05b7fd1f63 fix: 解决表单回车导致页面刷新的问题 (#664) 2023-04-17 02:20:17 +00:00
ssongliu 256c04f3b8 fix: 修改页脚错误 (#655) 2023-04-15 01:08:10 +00:00
zhengkunwang223 02577ef746 feat: OpenResty 升级提示转到创建网站处 (#647) 2023-04-14 09:18:48 +00:00
zhengkunwang223 059c3a0b80 feat: 创建 PHP 运行环境同步修改index目录文件和文件夹权限 (#644) 2023-04-14 09:16:53 +00:00
ssongliu 5ce1b1591a fix: 防火墙禁 ping 增加判断条件,不存在文件则隐藏 (#643) 2023-04-14 09:14:57 +00:00
zhengkunwang223 6595ad6228 feat: 增加伪静态设置 (#640) 2023-04-14 08:01:06 +00:00
ssongliu e7608673d7 feat: 打开防火墙时,自动放开应用以及 80 443 22 端口 (#637) 2023-04-14 07:10:46 +00:00
ssongliu 4c8fc1defa fix: 防火墙空状态页面样式优化 (#625) 2023-04-14 07:08:51 +00:00
ssongliu 975663f0ff feat: 增加自动更新检测 (#621) 2023-04-14 07:06:55 +00:00
297 changed files with 13558 additions and 6060 deletions
+10 -8
View File
@@ -6,6 +6,7 @@
<a href="https://app.codacy.com/gh/1Panel-dev/1Panel?utm_source=github.com&utm_medium=referral&utm_content=1Panel-dev/1Panel&utm_campaign=Badge_Grade_Dashboard"><img src="https://app.codacy.com/project/badge/Grade/da67574fd82b473992781d1386b937ef" alt="Codacy"></a>
<a href="https://github.com/1Panel-dev/1Panel/releases"><img src="https://img.shields.io/github/v/release/1Panel-dev/1Panel" alt="GitHub release"></a>
<a href="https://github.com/1Panel-dev/1Panel"><img src="https://img.shields.io/github/stars/1Panel-dev/1Panel?color=%231890FF&style=flat-square" alt="Stars"></a>
<a href="https://app.fossa.com/projects/git%2Bgithub.com%2F1Panel-dev%2F1Panel?ref=badge_shield"><img src="https://app.fossa.com/api/projects/git%2Bgithub.com%2F1Panel-dev%2F1Panel.svg?type=shield" alt="FOSSA Status"></a>
</p>
------------------------------
@@ -13,9 +14,9 @@
1Panel 是一个现代化、开源的 Linux 服务器运维管理面板。1Panel 的功能和优势包括:
- **快速建站**:深度集成 Wordpress 和 [Halo](https://github.com/halo-dev/halo/),域名绑定、SSL 证书配置等一键搞定;
- **高效管理**:通过 Web 端轻松管理 Linux 服务器,包括应用管理、主机监控、文件管理、数据库管理、容器管理等;
- **安全可靠**:最小漏洞暴露面,提供防火墙和安全审计等功能;
- **一键备份**:支持一键备份和恢复,备份数据云端存储,永不丢失。
- **高效管理**:通过 Web 端轻松管理 Linux 服务器,包括主机监控、文件管理、数据库管理、容器管理等;
- **安全可靠**:基于容器来管理和部署应用,最小漏洞暴露面,提供防火墙和日志审计等功能;
- **一键备份**:支持一键备份和恢复,备份数据到各类云端存储,永不丢失。
## UI 展示
@@ -41,12 +42,9 @@ curl -sSL https://resource.fit2cloud.com/1panel/package/quick_start.sh -o quick_
- [在线文档](https://1panel.cn/docs/)
- [教学视频](https://space.bilibili.com/510493147/channel/collectiondetail?sid=1199760)
- [社区论坛](https://bbs.fit2cloud.com/c/1p/7)
## 社区
如果您在使用过程中有任何疑问或对建议,欢迎提交 GitHub Issue 或加入到我们微信交流群进行交流沟通。
**微信交流群**
**加入微信交流群**
<img src="https://1panel.cn/img/wechat-group.jpg" width="156" height="156"/>
@@ -61,6 +59,10 @@ curl -sSL https://resource.fit2cloud.com/1panel/package/quick_start.sh -o quick_
[![Star History Chart](https://api.star-history.com/svg?repos=1Panel-dev/1Panel&type=Date)](https://star-history.com/#1Panel-dev/1Panel&Date)
## FOSSA Status
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2F1Panel-dev%2F1Panel.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2F1Panel-dev%2F1Panel?ref=badge_large)
## License
Copyright (c) 2014-2023 [FIT2CLOUD 飞致云](https://fit2cloud.com/), All rights reserved.
+2
View File
@@ -3,6 +3,7 @@
如果您发现安全问题,请直接联系我们:
- wanghe@fit2cloud.com
- zhengkun@fit2cloud.com
- support@fit2cloud.com
- 400-052-0755
@@ -13,6 +14,7 @@
All security bugs should be reported to the contact as below:
- wanghe@fit2cloud.com
- zhengkun@fit2cloud.com
- support@fit2cloud.com
- 400-052-0755
+9 -8
View File
@@ -38,14 +38,15 @@ func (b *BaseApi) SearchApp(c *gin.Context) {
// @Router /apps/sync [post]
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFuntions":[],"formatZH":"应用商店同步","formatEN":"App store synchronization"}
func (b *BaseApi) SyncApp(c *gin.Context) {
appService.SyncAppListFromLocal()
global.LOG.Infof("sync app list start ...")
if err := appService.SyncAppListFromRemote(); err != nil {
global.LOG.Errorf("sync app list error [%s]", err.Error())
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
global.LOG.Infof("sync app list success!")
go appService.SyncAppListFromLocal()
go func() {
global.LOG.Infof("sync app list start ...")
if err := appService.SyncAppListFromRemote(); err != nil {
global.LOG.Errorf("sync app list error [%s]", err.Error())
} else {
global.LOG.Infof("sync app list success!")
}
}()
helper.SuccessWithData(c, "")
}
+5 -68
View File
@@ -1,8 +1,6 @@
package v1
import (
"errors"
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/app/model"
@@ -28,7 +26,7 @@ func (b *BaseApi) Login(c *gin.Context) {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
if req.AuthMethod != "jwt" {
if req.AuthMethod != "jwt" && !req.IgnoreCaptcha {
if err := captcha.VerifyCode(req.CaptchaID, req.Captcha); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
@@ -102,71 +100,10 @@ func (b *BaseApi) Captcha(c *gin.Context) {
// @Summary Load safety status
// @Description 获取系统安全登录状态
// @Success 200
// @Failure 402
// @Router /auth/status [get]
func (b *BaseApi) GetSafetyStatus(c *gin.Context) {
if err := authService.SafetyStatus(c); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrUnSafety, constant.ErrTypeNotSafety, err)
return
}
helper.SuccessWithData(c, nil)
}
func (b *BaseApi) SafeEntrance(c *gin.Context) {
code, exist := c.Params.Get("code")
if !exist {
helper.ErrorWithDetail(c, constant.CodeErrUnSafety, constant.ErrTypeNotSafety, errors.New("missing code"))
return
}
ok, err := authService.VerifyCode(code)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrUnSafety, constant.ErrTypeNotSafety, errors.New("missing code"))
return
}
if !ok {
helper.ErrorWithDetail(c, constant.CodeErrUnSafety, constant.ErrTypeNotSafety, errors.New("missing code"))
return
}
if err := authService.SafeEntrance(c, code); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrUnSafety, constant.ErrTypeNotSafety, errors.New("missing code"))
return
}
helper.SuccessWithData(c, nil)
}
// @Tags Auth
// @Summary Check is First login
// @Description 判断是否为首次登录
// @Success 200
// @Router /auth/status [get]
func (b *BaseApi) CheckIsFirstLogin(c *gin.Context) {
helper.SuccessWithData(c, authService.CheckIsFirst())
}
// @Tags Auth
// @Summary Init user
// @Description 初始化用户
// @Accept json
// @Param request body dto.InitUser true "request"
// @Success 200
// @Router /auth/init [post]
func (b *BaseApi) InitUserInfo(c *gin.Context) {
var req dto.InitUser
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
if err := global.VALID.Struct(req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
if err := authService.InitUser(c, req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, nil)
// @Router /auth/issafety [get]
func (b *BaseApi) CheckIsSafety(c *gin.Context) {
code := c.DefaultQuery("code", "")
helper.SuccessWithData(c, authService.CheckIsSafety(code))
}
// @Tags Auth
+13 -3
View File
@@ -2,6 +2,8 @@ package v1
import (
"encoding/base64"
"fmt"
"path"
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
"github.com/1Panel-dev/1Panel/backend/app/dto"
@@ -104,9 +106,9 @@ func (b *BaseApi) ListBuckets(c *gin.Context) {
// @Success 200
// @Security ApiKeyAuth
// @Router /settings/backup/del [post]
// @x-panel-log {"bodyKeys":["ids"],"paramKeys":[],"BeforeFuntions":[{"input_colume":"id","input_value":"ids","isList":true,"db":"backup_accounts","output_colume":"type","output_value":"types"}],"formatZH":"删除备份账号 [types]","formatEN":"delete backup account [types]"}
// @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFuntions":[{"input_colume":"id","input_value":"id","isList":true,"db":"backup_accounts","output_colume":"type","output_value":"types"}],"formatZH":"删除备份账号 [types]","formatEN":"delete backup account [types]"}
func (b *BaseApi) DeleteBackup(c *gin.Context) {
var req dto.BatchDeleteReq
var req dto.OperateByID
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
@@ -116,7 +118,7 @@ func (b *BaseApi) DeleteBackup(c *gin.Context) {
return
}
if err := backupService.BatchDelete(req.Ids); err != nil {
if err := backupService.Delete(req.ID); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
@@ -356,6 +358,14 @@ func (b *BaseApi) Recover(c *gin.Context) {
return
}
if req.Source != "LOCAL" {
downloadPath, err := backupService.DownloadRecord(dto.DownloadRecord{Source: req.Source, FileDir: path.Dir(req.File), FileName: path.Base(req.File)})
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, fmt.Errorf("download file failed, err: %v", err))
return
}
req.File = downloadPath
}
switch req.Type {
case "mysql":
if err := backupService.MysqlRecover(req); err != nil {
+4 -2
View File
@@ -7,6 +7,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/gin-gonic/gin"
)
@@ -77,8 +78,9 @@ func (b *BaseApi) SearchJobRecords(c *gin.Context) {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
req.StartTime = req.StartTime.Add(8 * time.Hour)
req.EndTime = req.EndTime.Add(8 * time.Hour)
loc, _ := time.LoadLocation(common.LoadTimeZone())
req.StartTime = req.StartTime.In(loc)
req.EndTime = req.EndTime.In(loc)
total, list, err := cronjobService.SearchRecords(req)
if err != nil {
+23 -2
View File
@@ -186,7 +186,29 @@ func (b *BaseApi) ChangeFileMode(c *gin.Context) {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, nil)
helper.SuccessWithOutData(c)
}
// @Tags File
// @Summary Change file owner
// @Description 修改文件用户/组
// @Accept json
// @Param request body request.FileRoleUpdate true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /files/owner [post]
// @x-panel-log {"bodyKeys":["path","user","group"],"paramKeys":[],"BeforeFuntions":[],"formatZH":"修改用户/组 [paths] => [user]/[group]","formatEN":"Change owner [paths] => [user]/[group]"}
func (b *BaseApi) ChangeFileOwner(c *gin.Context) {
var req request.FileRoleUpdate
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
if err := fileService.ChangeOwner(req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithOutData(c)
}
// @Tags File
@@ -498,7 +520,6 @@ func (b *BaseApi) Size(c *gin.Context) {
// @Success 200 {string} content
// @Security ApiKeyAuth
// @Router /files/loadfile [post]
// @x-panel-log {"bodyKeys":["path"],"paramKeys":[],"BeforeFuntions":[],"formatZH":"读取文件 [path]","formatEN":"Read file [path]"}
func (b *BaseApi) LoadFromFile(c *gin.Context) {
var req dto.FilePath
if err := c.ShouldBindJSON(&req); err != nil {
+2 -2
View File
@@ -138,7 +138,7 @@ func (b *BaseApi) OperateIPRule(c *gin.Context) {
// @Param request body dto.BatchRuleOperate true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /hosts/firewall/ip [post]
// @Router /hosts/firewall/batch [post]
func (b *BaseApi) BatchOperateRule(c *gin.Context) {
var req dto.BatchRuleOperate
if err := c.ShouldBindJSON(&req); err != nil {
@@ -188,7 +188,7 @@ func (b *BaseApi) UpdatePortRule(c *gin.Context) {
// @Param request body dto.AddrRuleUpdate true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /hosts/firewall/update/ip [post]
// @Router /hosts/firewall/update/addr [post]
func (b *BaseApi) UpdateAddrRule(c *gin.Context) {
var req dto.AddrRuleUpdate
if err := c.ShouldBindJSON(&req); err != nil {
+4 -2
View File
@@ -9,6 +9,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/gin-gonic/gin"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/net"
@@ -24,8 +25,9 @@ func (b *BaseApi) LoadMonitor(c *gin.Context) {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
req.StartTime = req.StartTime.Add(8 * time.Hour)
req.EndTime = req.EndTime.Add(8 * time.Hour)
loc, _ := time.LoadLocation(common.LoadTimeZone())
req.StartTime = req.StartTime.In(loc)
req.EndTime = req.EndTime.In(loc)
var backdatas []dto.MonitorData
if req.Param == "all" || req.Param == "cpu" || req.Param == "memory" || req.Param == "load" {
+43 -1
View File
@@ -92,6 +92,48 @@ func (b *BaseApi) UpdatePassword(c *gin.Context) {
helper.SuccessWithData(c, nil)
}
// @Tags System Setting
// @Summary Update system ssl
// @Description 修改系统 ssl 登录
// @Accept json
// @Param request body dto.SSLUpdate true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /settings/ssl/update [post]
// @x-panel-log {"bodyKeys":["ssl"],"paramKeys":[],"BeforeFuntions":[],"formatZH":"修改系统 ssl => [ssl]","formatEN":"update system ssl => [ssl]"}
func (b *BaseApi) UpdateSSL(c *gin.Context) {
var req dto.SSLUpdate
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
if err := global.VALID.Struct(req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
if err := settingService.UpdateSSL(c, req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, nil)
}
// @Tags System Setting
// @Summary Load system cert info
// @Description 获取证书信息
// @Success 200 {object} dto.SettingInfo
// @Security ApiKeyAuth
// @Router /settings/ssl/info [get]
func (b *BaseApi) LoadFromCert(c *gin.Context) {
info, err := settingService.LoadFromCert()
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, info)
}
// @Tags System Setting
// @Summary Update system port
// @Description 更新系统端口
@@ -154,7 +196,7 @@ func (b *BaseApi) HandlePasswordExpired(c *gin.Context) {
// @Router /settings/time/sync [post]
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFuntions":[],"formatZH":"系统时间同步","formatEN":"sync system time"}
func (b *BaseApi) SyncTime(c *gin.Context) {
ntime, err := ntp.Getremotetime()
ntime, err := ntp.GetRemoteTime()
if err != nil {
helper.SuccessWithData(c, time.Now().Format("2006-01-02 15:04:05 MST -0700"))
return
+15 -3
View File
@@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"os/exec"
"strconv"
"time"
@@ -107,14 +108,15 @@ func (b *BaseApi) RedisWsSsh(c *gin.Context) {
return
}
defer wsConn.Close()
commands := fmt.Sprintf("docker exec -it %s redis-cli", redisConf.ContainerName)
commands := "redis-cli"
if len(redisConf.Requirepass) != 0 {
commands = fmt.Sprintf("docker exec -it %s redis-cli -a %s --no-auth-warning", redisConf.ContainerName, redisConf.Requirepass)
commands = fmt.Sprintf("redis-cli -a %s --no-auth-warning", redisConf.Requirepass)
}
slave, err := terminal.NewCommand(commands)
slave, err := terminal.NewCommand(fmt.Sprintf("docker exec -it %s %s", redisConf.ContainerName, commands))
if wshandleError(wsConn, err) {
return
}
defer killBash(redisConf.ContainerName, commands)
defer slave.Close()
tty, err := terminal.NewLocalWsSession(cols, rows, wsConn, slave)
@@ -177,6 +179,7 @@ func (b *BaseApi) ContainerWsSsh(c *gin.Context) {
if wshandleError(wsConn, err) {
return
}
defer killBash(containerID, command)
defer slave.Close()
tty, err := terminal.NewLocalWsSession(cols, rows, wsConn, slave)
@@ -216,6 +219,15 @@ func wshandleError(ws *websocket.Conn, err error) bool {
return false
}
func killBash(containerID, comm string) {
sudo := ""
if cmd.HasNoPasswordSudo() {
sudo = "sudo"
}
command := exec.Command("sh", "-c", fmt.Sprintf("%s kill -9 $(docker top %s -eo pid,command | grep '%s' | awk '{print $1}')", sudo, containerID, comm))
_, _ = command.CombinedOutput()
}
var upGrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024 * 1024 * 10,
+198
View File
@@ -560,3 +560,201 @@ func (b *BaseApi) UpdatePHPFile(c *gin.Context) {
}
helper.SuccessWithData(c, nil)
}
// @Tags Website
// @Summary Get rewrite conf
// @Description 获取伪静态配置
// @Accept json
// @Param request body request.NginxRewriteReq true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /websites/rewrite [post]
func (b *BaseApi) GetRewriteConfig(c *gin.Context) {
var req request.NginxRewriteReq
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
res, err := websiteService.GetRewriteConfig(req)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, res)
}
// @Tags Website
// @Summary Update rewrite conf
// @Description 更新伪静态配置
// @Accept json
// @Param request body request.NginxRewriteUpdate true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /websites/rewrite/update [post]
// @x-panel-log {"bodyKeys":["websiteID"],"paramKeys":[],"BeforeFuntions":[{"input_colume":"id","input_value":"websiteID","isList":false,"db":"websites","output_colume":"primary_domain","output_value":"domain"}],"formatZH":"伪静态配置修改 [domain]","formatEN":"Nginx conf rewrite update [domain]"}
func (b *BaseApi) UpdateRewriteConfig(c *gin.Context) {
var req request.NginxRewriteUpdate
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
if err := websiteService.UpdateRewriteConfig(req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, nil)
}
// @Tags Website
// @Summary Update Site Dir
// @Description 更新网站目录
// @Accept json
// @Param request body request.WebsiteUpdateDir true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /websites/dir/update [post]
// @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFuntions":[{"input_colume":"id","input_value":"id","isList":false,"db":"websites","output_colume":"primary_domain","output_value":"domain"}],"formatZH":"更新网站 [domain] 目录","formatEN":"Update domain [domain] dir"}
func (b *BaseApi) UpdateSiteDir(c *gin.Context) {
var req request.WebsiteUpdateDir
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
if err := websiteService.UpdateSiteDir(req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithOutData(c)
}
// @Tags Website
// @Summary Update Site Dir permission
// @Description 更新网站目录权限
// @Accept json
// @Param request body request.WebsiteUpdateDirPermission true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /websites/dir/permission [post]
// @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFuntions":[{"input_colume":"id","input_value":"id","isList":false,"db":"websites","output_colume":"primary_domain","output_value":"domain"}],"formatZH":"更新网站 [domain] 目录权限","formatEN":"Update domain [domain] dir permission"}
func (b *BaseApi) UpdateSiteDirPermission(c *gin.Context) {
var req request.WebsiteUpdateDirPermission
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
if err := websiteService.UpdateSitePermission(req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithOutData(c)
}
// @Tags Website
// @Summary Get proxy conf
// @Description 获取反向代理配置
// @Accept json
// @Param request body request.WebsiteProxyReq true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /websites/proxies [post]
func (b *BaseApi) GetProxyConfig(c *gin.Context) {
var req request.WebsiteProxyReq
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
res, err := websiteService.GetProxies(req.ID)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, res)
}
// @Tags Website
// @Summary Update proxy conf
// @Description 修改反向代理配置
// @Accept json
// @Param request body request.WebsiteProxyConfig true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /websites/proxies/update [post]
// @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFuntions":[{"input_colume":"id","input_value":"id","isList":false,"db":"websites","output_colume":"primary_domain","output_value":"domain"}],"formatZH":"修改网站 [domain] 反向代理配置 ","formatEN":"Update domain [domain] proxy config"}
func (b *BaseApi) UpdateProxyConfig(c *gin.Context) {
var req request.WebsiteProxyConfig
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
err := websiteService.OperateProxy(req)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithOutData(c)
}
// @Tags Website
// @Summary Update proxy file
// @Description 更新反向代理文件
// @Accept json
// @Param request body request.NginxProxyUpdate true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /websites/proxy/file [post]
// @x-panel-log {"bodyKeys":["websiteID"],"paramKeys":[],"BeforeFuntions":[{"input_colume":"id","input_value":"websiteID","isList":false,"db":"websites","output_colume":"primary_domain","output_value":"domain"}],"formatZH":"更新反向代理文件 [domain]","formatEN":"Nginx conf proxy file update [domain]"}
func (b *BaseApi) UpdateProxyConfigFile(c *gin.Context) {
var req request.NginxProxyUpdate
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
if err := websiteService.UpdateProxyFile(req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithOutData(c)
}
// @Tags Website
// @Summary Get AuthBasic conf
// @Description 获取密码访问配置
// @Accept json
// @Param request body request.NginxAuthReq true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /websites/auths [post]
func (b *BaseApi) GetAuthConfig(c *gin.Context) {
var req request.NginxAuthReq
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
res, err := websiteService.GetAuthBasics(req)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, res)
}
// @Tags Website
// @Summary Get AuthBasic conf
// @Description 更新密码访问配置
// @Accept json
// @Param request body request.NginxAuthUpdate true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /websites/auths/update [post]
func (b *BaseApi) UpdateAuthConfig(c *gin.Context) {
var req request.NginxAuthUpdate
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
if err := websiteService.UpdateAuthBasic(req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithOutData(c)
}
+6 -10
View File
@@ -17,11 +17,12 @@ type MfaCredential struct {
}
type Login struct {
Name string `json:"name"`
Password string `json:"password"`
Captcha string `json:"captcha"`
CaptchaID string `json:"captchaID"`
AuthMethod string `json:"authMethod"`
Name string `json:"name"`
Password string `json:"password"`
IgnoreCaptcha bool `json:"ignoreCaptcha"`
Captcha string `json:"captcha"`
CaptchaID string `json:"captchaID"`
AuthMethod string `json:"authMethod"`
}
type MFALogin struct {
@@ -30,8 +31,3 @@ type MFALogin struct {
Code string `json:"code"`
AuthMethod string `json:"authMethod"`
}
type InitUser struct {
Name string `json:"name" validate:"required"`
Password string `json:"password" validate:"required"`
}
+1
View File
@@ -36,6 +36,7 @@ type CommonBackup struct {
DetailName string `json:"detailName"`
}
type CommonRecover struct {
Source string `json:"source" validate:"required,oneof=OSS S3 SFTP MINIO LOCAL COS KODO"`
Type string `json:"type" validate:"required,oneof=app mysql redis website"`
Name string `json:"name"`
DetailName string `json:"detailName"`
+1 -1
View File
@@ -12,7 +12,7 @@ type PageInfo struct {
type UpdateDescription struct {
ID uint `json:"id" validate:"required"`
Description string `json:"description"`
Description string `json:"description" validate:"max=256"`
}
type OperationWithName struct {
+9 -2
View File
@@ -22,6 +22,10 @@ type ContainerInfo struct {
State string `json:"state"`
RunTime string `json:"runTime"`
CPUPercent float64 `json:"cpuPercent"`
MemoryPercent float64 `json:"memoryPercent"`
Ports []string `json:"ports"`
IsFromApp bool `json:"isFromApp"`
IsFromCompose bool `json:"isFromCompose"`
}
@@ -59,8 +63,10 @@ type VolumeHelper struct {
Mode string `json:"mode"`
}
type PortHelper struct {
ContainerPort int `json:"containerPort"`
HostPort int `json:"hostPort"`
HostIP string `json:"hostIP"`
HostPort string `json:"hostPort"`
ContainerPort string `json:"containerPort"`
Protocol string `json:"protocol"`
}
type ContainerLog struct {
@@ -140,6 +146,7 @@ type ComposeOperation struct {
Name string `json:"name" validate:"required"`
Path string `json:"path" validate:"required"`
Operation string `json:"operation" validate:"required,oneof=start stop down"`
WithFile bool `json:"withFile"`
}
type ComposeUpdate struct {
Name string `json:"name" validate:"required"`
+14 -5
View File
@@ -28,13 +28,20 @@ type NginxParam struct {
Params []string
}
type NginxAuth struct {
Username string `json:"username"`
Remark string `json:"remark"`
}
type NginxKey string
const (
Index NginxKey = "index"
LimitConn NginxKey = "limit-conn"
SSL NginxKey = "ssl"
HttpPer NginxKey = "http-per"
Index NginxKey = "index"
LimitConn NginxKey = "limit-conn"
SSL NginxKey = "ssl"
CACHE NginxKey = "cache"
HttpPer NginxKey = "http-per"
ProxyCache NginxKey = "proxy-cache"
)
var ScopeKeyMap = map[NginxKey][]string{
@@ -46,5 +53,7 @@ var ScopeKeyMap = map[NginxKey][]string{
var StaticFileKeyMap = map[NginxKey]struct {
}{
SSL: {},
SSL: {},
CACHE: {},
ProxyCache: {},
}
+8
View File
@@ -22,6 +22,7 @@ type FileCreate struct {
IsLink bool `json:"isLink"`
IsSymlink bool `json:"isSymlink"`
LinkPath string `json:"linkPath"`
Sub bool `json:"sub"`
}
type FileDelete struct {
@@ -88,3 +89,10 @@ type DirSizeReq struct {
type FileProcessReq struct {
Key string `json:"key"`
}
type FileRoleUpdate struct {
Path string `json:"path" validate:"required"`
User string `json:"user" validate:"required"`
Group string `json:"group" validate:"required"`
Sub bool `json:"sub" validate:"required"`
}
+29
View File
@@ -19,3 +19,32 @@ type NginxConfigUpdate struct {
WebsiteID uint `json:"websiteId" validate:"required"`
Params interface{} `json:"params"`
}
type NginxRewriteReq struct {
WebsiteID uint `json:"websiteId" validate:"required"`
Name string `json:"name" validate:"required"`
}
type NginxRewriteUpdate struct {
WebsiteID uint `json:"websiteId" validate:"required"`
Name string `json:"name" validate:"required"`
Content string `json:"content" validate:"required"`
}
type NginxProxyUpdate struct {
WebsiteID uint `json:"websiteID" validate:"required"`
Content string `json:"content" validate:"required"`
Name string `json:"name" validate:"required"`
}
type NginxAuthUpdate struct {
WebsiteID uint `json:"websiteID" validate:"required"`
Operate string `json:"operate" validate:"required"`
Username string `json:"username" validate:"required"`
Password string `json:"password" validate:"required"`
Remark string `json:"remark"`
}
type NginxAuthReq struct {
WebsiteID uint `json:"websiteID" validate:"required"`
}
+32
View File
@@ -145,3 +145,35 @@ type WebsitePHPFileUpdate struct {
Type string `json:"type" validate:"required"`
Content string `json:"content" validate:"required"`
}
type WebsiteUpdateDir struct {
ID uint `json:"id" validate:"required"`
SiteDir string `json:"siteDir" validate:"required"`
}
type WebsiteUpdateDirPermission struct {
ID uint `json:"id" validate:"required"`
User string `json:"user" validate:"required"`
Group string `json:"group" validate:"required"`
}
type WebsiteProxyConfig struct {
ID uint `json:"id" validate:"required"`
Operate string `json:"operate" validate:"required"`
Enable bool `json:"enable" validate:"required"`
Cache bool `json:"cache" validate:"required"`
CacheTime int `json:"cacheTime" validate:"required"`
CacheUnit string `json:"cacheUnit" validate:"required"`
Name string `json:"name" validate:"required"`
Modifier string `json:"modifier" validate:"required"`
Match string `json:"match" validate:"required"`
ProxyPass string `json:"proxyPass" validate:"required"`
ProxyHost string `json:"proxyHost" validate:"required"`
Content string `json:"content"`
FilePath string `json:"filePath"`
Replaces map[string]string `json:"replaces"`
}
type WebsiteProxyReq struct {
ID uint `json:"id" validate:"required"`
}
+1
View File
@@ -59,6 +59,7 @@ type AppInstalledDTO struct {
type DatabaseConn struct {
Password string `json:"password"`
ServiceName string `json:"serviceName"`
Port int64 `json:"port"`
}
type AppService struct {
+7
View File
@@ -1,5 +1,7 @@
package response
import "github.com/1Panel-dev/1Panel/backend/app/dto"
type NginxStatus struct {
Active string `json:"active"`
Accepts string `json:"accepts"`
@@ -14,3 +16,8 @@ type NginxParam struct {
Name string `json:"name"`
Params []string `json:"params"`
}
type NginxAuthRes struct {
Enable bool `json:"enable"`
Items []dto.NginxAuth `json:"items"`
}
+4
View File
@@ -47,3 +47,7 @@ type WebsiteLog struct {
type PHPConfig struct {
Params map[string]string `json:"params"`
}
type NginxRewriteRes struct {
Content string `json:"content"`
}
+22 -3
View File
@@ -16,6 +16,8 @@ type SettingInfo struct {
Language string `json:"language"`
ServerPort string `json:"serverPort"`
SSL string `json:"ssl"`
SSLType string `json:"sslType"`
SecurityEntrance string `json:"securityEntrance"`
ExpirationDays string `json:"expirationDays"`
ExpirationTime string `json:"expirationTime"`
@@ -39,6 +41,23 @@ type SettingUpdate struct {
Value string `json:"value"`
}
type SSLUpdate struct {
SSLType string `json:"sslType"`
Domain string `json:"domain"`
SSL string `json:"ssl" validate:"required,oneof=enable disable"`
Cert string `json:"cert"`
Key string `json:"key"`
SSLID uint `json:"sslID"`
}
type SSLInfo struct {
Domain string `json:"domain"`
Timeout string `json:"timeout"`
RootPath string `json:"rootPath"`
Cert string `json:"cert"`
Key string `json:"key"`
SSLID uint `json:"sslID"`
}
type PasswordUpdate struct {
OldPassword string `json:"oldPassword" validate:"required"`
NewPassword string `json:"newPassword" validate:"required"`
@@ -50,7 +69,7 @@ type PortUpdate struct {
type SnapshotCreate struct {
From string `json:"from" validate:"required,oneof=OSS S3 SFTP MINIO COS KODO"`
Description string `json:"description"`
Description string `json:"description" validate:"max=256"`
}
type SnapshotRecover struct {
IsNew bool `json:"isNew"`
@@ -60,12 +79,12 @@ type SnapshotRecover struct {
type SnapshotImport struct {
From string `json:"from"`
Names []string `json:"names"`
Description string `json:"description"`
Description string `json:"description" validate:"max=256"`
}
type SnapshotInfo struct {
ID uint `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Description string `json:"description" validate:"max=256"`
From string `json:"from"`
Status string `json:"status"`
Message string `json:"message"`
+27 -19
View File
@@ -4,25 +4,33 @@ import "time"
type Website struct {
BaseModel
Protocol string `gorm:"type:varchar(64);not null" json:"protocol"`
PrimaryDomain string `gorm:"type:varchar(128);not null" json:"primaryDomain"`
Type string `gorm:"type:varchar(64);not null" json:"type"`
Alias string `gorm:"type:varchar(128);not null" json:"alias"`
Remark string `gorm:"type:longtext;" json:"remark"`
Status string `gorm:"type:varchar(64);not null" json:"status"`
HttpConfig string `gorm:"type:varchar(64);not null" json:"httpConfig"`
ExpireDate time.Time `json:"expireDate"`
AppInstallID uint `gorm:"type:integer" json:"appInstallId"`
WebsiteGroupID uint `gorm:"type:integer" json:"webSiteGroupId"`
WebsiteSSLID uint `gorm:"type:integer" json:"webSiteSSLId"`
Proxy string `gorm:"type:varchar(128);not null" json:"proxy"`
ProxyType string `gorm:"type:varchar;" json:"proxyType"`
ErrorLog bool `json:"errorLog"`
AccessLog bool `json:"accessLog"`
DefaultServer bool `json:"defaultServer"`
RuntimeID uint `gorm:"type:integer" json:"runtimeID"`
Domains []WebsiteDomain `json:"domains" gorm:"-:migration"`
WebsiteSSL WebsiteSSL `json:"webSiteSSL" gorm:"-:migration"`
Protocol string `gorm:"type:varchar;not null" json:"protocol"`
PrimaryDomain string `gorm:"type:varchar;not null" json:"primaryDomain"`
Type string `gorm:"type:varchar;not null" json:"type"`
Alias string `gorm:"type:varchar;not null" json:"alias"`
Remark string `gorm:"type:longtext;" json:"remark"`
Status string `gorm:"type:varchar;not null" json:"status"`
HttpConfig string `gorm:"type:varchar;not null" json:"httpConfig"`
ExpireDate time.Time `json:"expireDate"`
Proxy string `gorm:"type:varchar;" json:"proxy"`
ProxyType string `gorm:"type:varchar;" json:"proxyType"`
SiteDir string `gorm:"type:varchar;" json:"siteDir"`
ErrorLog bool `json:"errorLog"`
AccessLog bool `json:"accessLog"`
DefaultServer bool `json:"defaultServer"`
Rewrite string `gorm:"type:varchar" json:"rewrite"`
WebsiteGroupID uint `gorm:"type:integer" json:"webSiteGroupId"`
WebsiteSSLID uint `gorm:"type:integer" json:"webSiteSSLId"`
RuntimeID uint `gorm:"type:integer" json:"runtimeID"`
AppInstallID uint `gorm:"type:integer" json:"appInstallId"`
User string `gorm:"type:varchar;" json:"user"`
Group string `gorm:"type:varchar;" json:"group"`
Domains []WebsiteDomain `json:"domains" gorm:"-:migration"`
WebsiteSSL WebsiteSSL `json:"webSiteSSL" gorm:"-:migration"`
}
func (w Website) TableName() string {
+7
View File
@@ -20,6 +20,7 @@ type ICronjobRepo interface {
Page(limit, offset int, opts ...DBOption) (int64, []model.Cronjob, error)
Create(cronjob *model.Cronjob) error
WithByJobID(id int) DBOption
WithByBackupID(id uint) DBOption
WithByRecordDropID(id int) DBOption
Save(id uint, cronjob model.Cronjob) error
Update(id uint, vars map[string]interface{}) error
@@ -114,6 +115,12 @@ func (c *CronjobRepo) WithByJobID(id int) DBOption {
}
}
func (c *CronjobRepo) WithByBackupID(id uint) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("target_dir_id = ?", id)
}
}
func (c *CronjobRepo) WithByRecordDropID(id int) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("id < ?", id)
-7
View File
@@ -343,13 +343,6 @@ func (a AppService) Install(ctx context.Context, req request.AppInstallCreate) (
}
go upApp(appInstall)
go updateToolApp(appInstall)
ports := []int{appInstall.HttpPort}
if appInstall.HttpsPort > 0 {
ports = append(ports, appInstall.HttpsPort)
}
go func() {
_ = OperateFirewallPort(nil, ports)
}()
return
}
+1
View File
@@ -141,6 +141,7 @@ func (a *AppInstallService) LoadConnInfo(key string) (response.DatabaseConn, err
}
data.Password = app.Password
data.ServiceName = app.ServiceName
data.Port = app.Port
return data, nil
}
+7
View File
@@ -226,6 +226,10 @@ func upgradeInstall(installId uint, detailId uint) error {
}
detailDir := path.Join(constant.ResourceDir, "apps", install.App.Key, "versions", detail.Version)
if install.App.Resource == constant.AppResourceLocal {
detailDir = path.Join(constant.ResourceDir, "localApps", strings.TrimPrefix(install.App.Key, "local"), "versions", detail.Version)
}
cmd := exec.Command("/bin/bash", "-c", fmt.Sprintf("cp -rf %s/* %s", detailDir, install.GetPath()))
stdout, err := cmd.CombinedOutput()
if err != nil {
@@ -566,6 +570,9 @@ func getAppFromRepo(downloadPath, version string) error {
func handleInstalled(appInstallList []model.AppInstall, updated bool) ([]response.AppInstalledDTO, error) {
var res []response.AppInstalledDTO
for _, installed := range appInstallList {
if updated && installed.App.Type == "php" {
continue
}
installDTO := response.AppInstalledDTO{
AppInstall: installed,
}
+7 -72
View File
@@ -1,9 +1,7 @@
package service
import (
"fmt"
"strconv"
"time"
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/constant"
@@ -19,11 +17,8 @@ import (
type AuthService struct{}
type IAuthService interface {
SafetyStatus(c *gin.Context) error
CheckIsFirst() bool
InitUser(c *gin.Context, req dto.InitUser) error
CheckIsSafety(code string) bool
VerifyCode(code string) (bool, error)
SafeEntrance(c *gin.Context, code string) error
Login(c *gin.Context, info dto.Login) (*dto.UserLoginInfo, error)
LogOut(c *gin.Context) error
MFALogin(c *gin.Context, info dto.MFALogin) (*dto.UserLoginInfo, error)
@@ -33,22 +28,6 @@ func NewIAuthService() IAuthService {
return &AuthService{}
}
func (u *AuthService) SafeEntrance(c *gin.Context, code string) error {
codeWithMD5 := encrypt.Md5(code)
cookieValue, _ := encrypt.StringEncrypt(codeWithMD5)
c.SetCookie(codeWithMD5, cookieValue, 604800, "", "", false, false)
expiredSetting, err := settingRepo.Get(settingRepo.WithByKey("ExpirationDays"))
if err != nil {
return err
}
timeout, _ := strconv.Atoi(expiredSetting.Value)
if err := settingRepo.Update("ExpirationTime", time.Now().AddDate(0, 0, timeout).Format("2006-01-02 15:04:05")); err != nil {
return err
}
return nil
}
func (u *AuthService) Login(c *gin.Context, info dto.Login) (*dto.UserLoginInfo, error) {
nameSetting, err := settingRepo.Get(settingRepo.WithByKey("UserName"))
if err != nil {
@@ -164,57 +143,13 @@ func (u *AuthService) VerifyCode(code string) (bool, error) {
return setting.Value == code, nil
}
func (u *AuthService) SafetyStatus(c *gin.Context) error {
setting, err := settingRepo.Get(settingRepo.WithByKey("SecurityEntrance"))
func (u *AuthService) CheckIsSafety(code string) bool {
status, err := settingRepo.Get(settingRepo.WithByKey("SecurityEntrance"))
if err != nil {
return err
return false
}
codeWithEcrypt, err := c.Cookie(encrypt.Md5(setting.Value))
if err != nil {
return err
if len(status.Value) == 0 {
return true
}
code, err := encrypt.StringDecrypt(codeWithEcrypt)
if err != nil {
return err
}
if code != encrypt.Md5(setting.Value) {
return errors.New("code not match")
}
return nil
}
func (u *AuthService) CheckIsFirst() bool {
user, _ := settingRepo.Get(settingRepo.WithByKey("UserName"))
pass, _ := settingRepo.Get(settingRepo.WithByKey("Password"))
return len(user.Value) == 0 || len(pass.Value) == 0
}
func (u *AuthService) InitUser(c *gin.Context, req dto.InitUser) error {
user, _ := settingRepo.Get(settingRepo.WithByKey("UserName"))
pass, _ := settingRepo.Get(settingRepo.WithByKey("Password"))
if len(user.Value) == 0 || len(pass.Value) == 0 {
newPass, err := encrypt.StringEncrypt(req.Password)
if err != nil {
return err
}
if err := settingRepo.Update("UserName", req.Name); err != nil {
return err
}
if err := settingRepo.Update("Password", newPass); err != nil {
return err
}
expiredSetting, err := settingRepo.Get(settingRepo.WithByKey("ExpirationDays"))
if err != nil {
return err
}
timeout, _ := strconv.Atoi(expiredSetting.Value)
if timeout != 0 {
if err := settingRepo.Update("ExpirationTime", time.Now().AddDate(0, 0, timeout).Format("2006-01-02 15:04:05")); err != nil {
return err
}
}
return nil
}
return fmt.Errorf("can't init user because user %s is in system", user.Value)
return status.Value == code
}
+45 -24
View File
@@ -5,14 +5,16 @@ import (
"encoding/json"
"fmt"
"os"
"path"
"strings"
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/buserr"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/cloud_storage"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
fileUtils "github.com/1Panel-dev/1Panel/backend/utils/files"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
)
@@ -26,7 +28,7 @@ type IBackupService interface {
Create(backupDto dto.BackupOperate) error
GetBuckets(backupDto dto.ForBuckets) ([]interface{}, error)
Update(ireq dto.BackupOperate) error
BatchDelete(ids []uint) error
Delete(id uint) error
BatchDeleteRecord(ids []uint) error
NewClient(backup *model.BackupAccount) (cloud_storage.CloudStorageClient, error)
@@ -108,15 +110,15 @@ func (u *BackupService) DownloadRecord(info dto.DownloadRecord) (string, error)
if err != nil {
return "", fmt.Errorf("new cloud storage client failed, err: %v", err)
}
tempPath := fmt.Sprintf("%sdownload%s", constant.DataDir, info.FileDir)
if _, err := os.Stat(tempPath); err != nil && os.IsNotExist(err) {
if err = os.MkdirAll(tempPath, os.ModePerm); err != nil {
global.LOG.Errorf("mkdir %s failed, err: %v", tempPath, err)
targetPath := fmt.Sprintf("%s/download/%s/%s", constant.DataDir, info.FileDir, info.FileName)
if _, err := os.Stat(path.Dir(targetPath)); err != nil && os.IsNotExist(err) {
if err = os.MkdirAll(path.Dir(targetPath), os.ModePerm); err != nil {
global.LOG.Errorf("mkdir %s failed, err: %v", path.Dir(targetPath), err)
}
}
targetPath := tempPath + info.FileName
if _, err = os.Stat(targetPath); err != nil && os.IsNotExist(err) {
isOK, err := backClient.Download(info.FileName, targetPath)
srcPath := fmt.Sprintf("%s/%s", info.FileDir, info.FileName)
if exist, _ := backClient.Exist(srcPath); exist {
isOK, err := backClient.Download(srcPath, targetPath)
if !isOK {
return "", fmt.Errorf("cloud storage download failed, err: %v", err)
}
@@ -159,8 +161,12 @@ func (u *BackupService) GetBuckets(backupDto dto.ForBuckets) ([]interface{}, err
return client.ListBuckets()
}
func (u *BackupService) BatchDelete(ids []uint) error {
return backupRepo.Delete(commonRepo.WithIdsIn(ids))
func (u *BackupService) Delete(id uint) error {
cronjobs, _ := cronjobRepo.List(cronjobRepo.WithByBackupID(id))
if len(cronjobs) != 0 {
return buserr.New(constant.ErrBackupInUsed)
}
return backupRepo.Delete(commonRepo.WithByID(id))
}
func (u *BackupService) BatchDeleteRecord(ids []uint) error {
@@ -174,7 +180,7 @@ func (u *BackupService) BatchDeleteRecord(ids []uint) error {
global.LOG.Errorf("remove file %s failed, err: %v", record.FileDir+record.FileName, err)
}
} else {
backupAccount, err := backupRepo.Get(commonRepo.WithByName(record.Source))
backupAccount, err := backupRepo.Get(commonRepo.WithByType(record.Source))
if err != nil {
return err
}
@@ -218,7 +224,7 @@ func (u *BackupService) Update(req dto.BackupOperate) error {
if strings.HasSuffix(dirStr, "/") {
dirStr = dirStr[:strings.LastIndex(dirStr, "/")]
}
if err := updateBackupDir(dirStr, oldDir); err != nil {
if err := copyDir(oldDir, dirStr); err != nil {
_ = backupRepo.Update(req.ID, (map[string]interface{}{"vars": oldVars}))
return err
}
@@ -304,18 +310,33 @@ func loadLocalDir() (string, error) {
return "", fmt.Errorf("error type dir: %T", varMap["dir"])
}
func updateBackupDir(dir, oldDir string) error {
if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
if err = os.MkdirAll(dir, os.ModePerm); err != nil {
return err
func copyDir(src, dst string) error {
srcInfo, err := os.Stat(src)
if err != nil {
return err
}
if err = os.MkdirAll(dst, srcInfo.Mode()); err != nil {
return err
}
files, err := os.ReadDir(src)
if err != nil {
return err
}
fileOP := fileUtils.NewFileOp()
for _, file := range files {
srcPath := fmt.Sprintf("%s/%s", src, file.Name())
dstPath := fmt.Sprintf("%s/%s", dst, file.Name())
if file.IsDir() {
if err = copyDir(srcPath, dstPath); err != nil {
global.LOG.Errorf("copy dir %s to %s failed, err: %v", srcPath, dstPath, err)
}
} else {
if err := fileOP.CopyFile(srcPath, dst); err != nil {
global.LOG.Errorf("copy file %s to %s failed, err: %v", srcPath, dstPath, err)
}
}
}
if strings.HasSuffix(oldDir, "/") {
oldDir = oldDir[:strings.LastIndex(oldDir, "/")]
}
stdout, err := cmd.Execf("cp -r %s/* %s", oldDir, dir)
if err != nil {
return errors.New(string(stdout))
}
return nil
}
+166 -50
View File
@@ -10,6 +10,7 @@ import (
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/1Panel-dev/1Panel/backend/app/dto"
@@ -97,27 +98,45 @@ func (u *ContainerService) Page(req dto.PageContainer) (int64, interface{}, erro
records = list[start:end]
}
var wg sync.WaitGroup
wg.Add(len(records))
for _, container := range records {
IsFromCompose := false
if _, ok := container.Labels[composeProjectLabel]; ok {
IsFromCompose = true
}
IsFromApp := false
if created, ok := container.Labels[composeCreatedBy]; ok && created == "Apps" {
IsFromApp = true
}
backDatas = append(backDatas, dto.ContainerInfo{
ContainerID: container.ID,
CreateTime: time.Unix(container.Created, 0).Format("2006-01-02 15:04:05"),
Name: container.Names[0][1:],
ImageId: strings.Split(container.ImageID, ":")[1],
ImageName: container.Image,
State: container.State,
RunTime: container.Status,
IsFromApp: IsFromApp,
IsFromCompose: IsFromCompose,
})
go func(item types.Container) {
IsFromCompose := false
if _, ok := item.Labels[composeProjectLabel]; ok {
IsFromCompose = true
}
IsFromApp := false
if created, ok := item.Labels[composeCreatedBy]; ok && created == "Apps" {
IsFromApp = true
}
var ports []string
for _, port := range item.Ports {
if port.IP == "::" || port.PublicPort == 0 {
continue
}
ports = append(ports, fmt.Sprintf("%v:%v/%s", port.PublicPort, port.PrivatePort, port.Type))
}
cpu, mem := loadCpuAndMem(client, item.ID)
backDatas = append(backDatas, dto.ContainerInfo{
ContainerID: item.ID,
CreateTime: time.Unix(item.Created, 0).Format("2006-01-02 15:04:05"),
Name: item.Names[0][1:],
ImageId: strings.Split(item.ImageID, ":")[1],
ImageName: item.Image,
State: item.State,
RunTime: item.Status,
CPUPercent: cpu,
MemoryPercent: mem,
Ports: ports,
IsFromApp: IsFromApp,
IsFromCompose: IsFromCompose,
})
wg.Done()
}(container)
}
wg.Wait()
return int64(total), backDatas, nil
}
@@ -147,24 +166,27 @@ func (u *ContainerService) Inspect(req dto.InspectReq) (string, error) {
}
func (u *ContainerService) ContainerCreate(req dto.ContainerCreate) error {
if len(req.ExposedPorts) != 0 {
for _, port := range req.ExposedPorts {
if common.ScanPort(port.HostPort) {
return buserr.WithDetail(constant.ErrPortInUsed, port.HostPort, nil)
}
}
portMap, err := checkPortStats(req.ExposedPorts)
if err != nil {
return err
}
client, err := docker.NewDockerClient()
if err != nil {
return err
}
exposeds := make(nat.PortSet)
for port := range portMap {
exposeds[port] = struct{}{}
}
config := &container.Config{
Image: req.Image,
Cmd: req.Cmd,
Env: req.Env,
Labels: stringsToMap(req.Labels),
Tty: true,
OpenStdin: true,
Image: req.Image,
Cmd: req.Cmd,
Env: req.Env,
Labels: stringsToMap(req.Labels),
Tty: true,
OpenStdin: true,
ExposedPorts: exposeds,
}
hostConf := &container.HostConfig{
AutoRemove: req.AutoRemove,
@@ -181,11 +203,7 @@ func (u *ContainerService) ContainerCreate(req dto.ContainerCreate) error {
hostConf.Memory = req.Memory
}
if len(req.ExposedPorts) != 0 {
hostConf.PortBindings = make(nat.PortMap)
for _, port := range req.ExposedPorts {
bindItem := nat.PortBinding{HostPort: strconv.Itoa(port.HostPort)}
hostConf.PortBindings[nat.Port(fmt.Sprintf("%d/tcp", port.ContainerPort))] = []nat.PortBinding{bindItem}
}
hostConf.PortBindings = portMap
}
if len(req.Volumes) != 0 {
config.Volumes = make(map[string]struct{})
@@ -198,8 +216,10 @@ func (u *ContainerService) ContainerCreate(req dto.ContainerCreate) error {
global.LOG.Infof("new container info %s has been made, now start to create", req.Name)
ctx := context.Background()
if err := pullImages(ctx, client, req.Image); err != nil {
return err
if !checkImageExist(client, req.Image) {
if err := pullImages(ctx, client, req.Image); err != nil {
return err
}
}
container, err := client.ContainerCreate(ctx, config, hostConf, &network.NetworkingConfig{}, &v1.Platform{}, req.Name)
if err != nil {
@@ -268,16 +288,16 @@ func (u *ContainerService) ContainerStats(id string) (*dto.ContainterStats, erro
body, err := io.ReadAll(res.Body)
if err != nil {
res.Body.Close()
return nil, err
}
res.Body.Close()
var stats *types.StatsJSON
if err := json.Unmarshal(body, &stats); err != nil {
return nil, err
}
var data dto.ContainterStats
previousCPU := stats.PreCPUStats.CPUUsage.TotalUsage
previousSystem := stats.PreCPUStats.SystemUsage
data.CPUPercent = calculateCPUPercentUnix(previousCPU, previousSystem, stats)
data.CPUPercent = calculateCPUPercentUnix(stats)
data.IORead, data.IOWrite = calculateBlockIO(stats.BlkioStats)
data.Memory = float64(stats.MemoryStats.Usage) / 1024 / 1024
if cache, ok := stats.MemoryStats.Stats["cache"]; ok {
@@ -292,25 +312,36 @@ func (u *ContainerService) ContainerStats(id string) (*dto.ContainterStats, erro
func stringsToMap(list []string) map[string]string {
var lableMap = make(map[string]string)
for _, label := range list {
sps := strings.Split(label, "=")
if len(sps) > 1 {
if strings.Contains(label, "=") {
sps := strings.SplitN(label, "=", 2)
lableMap[sps[0]] = sps[1]
}
}
return lableMap
}
func calculateCPUPercentUnix(previousCPU, previousSystem uint64, v *types.StatsJSON) float64 {
var (
cpuPercent = 0.0
cpuDelta = float64(v.CPUStats.CPUUsage.TotalUsage) - float64(previousCPU)
systemDelta = float64(v.CPUStats.SystemUsage) - float64(previousSystem)
)
func calculateCPUPercentUnix(stats *types.StatsJSON) float64 {
cpuPercent := 0.0
cpuDelta := float64(stats.CPUStats.CPUUsage.TotalUsage) - float64(stats.PreCPUStats.CPUUsage.TotalUsage)
systemDelta := float64(stats.CPUStats.SystemUsage) - float64(stats.PreCPUStats.SystemUsage)
if systemDelta > 0.0 && cpuDelta > 0.0 {
cpuPercent = (cpuDelta / systemDelta) * float64(len(v.CPUStats.CPUUsage.PercpuUsage)) * 100.0
cpuPercent = (cpuDelta / systemDelta) * 100.0
if len(stats.CPUStats.CPUUsage.PercpuUsage) != 0 {
cpuPercent = cpuPercent * float64(len(stats.CPUStats.CPUUsage.PercpuUsage))
}
}
return cpuPercent
}
func calculateMemPercentUnix(memStats types.MemoryStats) float64 {
memPercent := 0.0
memUsage := float64(memStats.Usage - memStats.Stats["cache"])
memLimit := float64(memStats.Limit)
if memUsage > 0.0 && memLimit > 0.0 {
memPercent = (memUsage / memLimit) * 100.0
}
return memPercent
}
func calculateBlockIO(blkio types.BlkioStats) (blkRead float64, blkWrite float64) {
for _, bioEntry := range blkio.IoServiceBytesRecursive {
switch strings.ToLower(bioEntry.Op) {
@@ -332,6 +363,23 @@ func calculateNetwork(network map[string]types.NetworkStats) (float64, float64)
return rx, tx
}
func checkImageExist(client *client.Client, image string) bool {
images, err := client.ImageList(context.Background(), types.ImageListOptions{})
if err != nil {
fmt.Println(err)
return false
}
for _, img := range images {
for _, tag := range img.RepoTags {
if tag == image || tag == image+":latest" {
return true
}
}
}
return false
}
func pullImages(ctx context.Context, client *client.Client, image string) error {
out, err := client.ImagePull(ctx, image, types.ImagePullOptions{})
if err != nil {
@@ -344,3 +392,71 @@ func pullImages(ctx context.Context, client *client.Client, image string) error
}
return nil
}
func loadCpuAndMem(client *client.Client, container string) (float64, float64) {
res, err := client.ContainerStats(context.Background(), container, false)
if err != nil {
return 0, 0
}
body, err := io.ReadAll(res.Body)
if err != nil {
res.Body.Close()
return 0, 0
}
res.Body.Close()
var stats *types.StatsJSON
if err := json.Unmarshal(body, &stats); err != nil {
return 0, 0
}
CPUPercent := calculateCPUPercentUnix(stats)
MemPercent := calculateMemPercentUnix(stats.MemoryStats)
return CPUPercent, MemPercent
}
func checkPortStats(ports []dto.PortHelper) (nat.PortMap, error) {
portMap := make(nat.PortMap)
if len(ports) == 0 {
return portMap, nil
}
for _, port := range ports {
if strings.Contains(port.ContainerPort, "-") {
if !strings.Contains(port.HostPort, "-") {
return portMap, buserr.New(constant.ErrPortRules)
}
hostStart, _ := strconv.Atoi(strings.Split(port.HostPort, "-")[0])
hostEnd, _ := strconv.Atoi(strings.Split(port.HostPort, "-")[1])
containerStart, _ := strconv.Atoi(strings.Split(port.ContainerPort, "-")[0])
containerEnd, _ := strconv.Atoi(strings.Split(port.ContainerPort, "-")[1])
if (hostEnd-hostStart) <= 0 || (containerEnd-containerStart) <= 0 {
return portMap, buserr.New(constant.ErrPortRules)
}
if (containerEnd - containerStart) != (hostEnd - hostStart) {
return portMap, buserr.New(constant.ErrPortRules)
}
for i := 0; i <= hostEnd-hostStart; i++ {
bindItem := nat.PortBinding{HostPort: strconv.Itoa(hostStart + i), HostIP: port.HostIP}
portMap[nat.Port(fmt.Sprintf("%d/%s", containerStart+i, port.Protocol))] = []nat.PortBinding{bindItem}
}
for i := hostStart; i <= hostEnd; i++ {
if common.ScanPort(i) {
return portMap, buserr.WithDetail(constant.ErrPortInUsed, i, nil)
}
}
} else {
portItem := 0
if strings.Contains(port.HostPort, "-") {
portItem, _ = strconv.Atoi(strings.Split(port.HostPort, "-")[0])
} else {
portItem, _ = strconv.Atoi(port.HostPort)
}
if common.ScanPort(portItem) {
return portMap, buserr.WithDetail(constant.ErrPortInUsed, portItem, nil)
}
bindItem := nat.PortBinding{HostPort: strconv.Itoa(portItem), HostIP: port.HostIP}
portMap[nat.Port(fmt.Sprintf("%s/%s", port.ContainerPort, port.Protocol))] = []nat.PortBinding{bindItem}
}
}
return portMap, nil
}
+9 -13
View File
@@ -4,6 +4,7 @@ import (
"bufio"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path"
@@ -154,9 +155,10 @@ func (u *ContainerService) CreateCompose(req dto.ComposeCreate) (string, error)
go func() {
defer file.Close()
cmd := exec.Command("docker-compose", "-f", req.Path, "up", "-d")
stdout, err := cmd.CombinedOutput()
_, _ = file.Write(stdout)
if err != nil {
multiWriter := io.MultiWriter(os.Stdout, file)
cmd.Stdout = multiWriter
cmd.Stderr = multiWriter
if err := cmd.Run(); err != nil {
global.LOG.Errorf("docker-compose up %s failed, err: %v", req.Name, err)
_, _ = compose.Down(req.Path)
_, _ = file.WriteString("docker-compose up failed!")
@@ -180,7 +182,9 @@ func (u *ContainerService) ComposeOperation(req dto.ComposeOperation) error {
global.LOG.Infof("docker-compose %s %s successful", req.Operation, req.Name)
if req.Operation == "down" {
_ = composeRepo.DeleteRecord(commonRepo.WithByName(req.Name))
_ = os.RemoveAll(strings.ReplaceAll(req.Path, "/docker-compose.yml", ""))
if req.WithFile {
_ = os.RemoveAll(path.Dir(req.Path))
}
}
return nil
@@ -211,15 +215,7 @@ func (u *ContainerService) ComposeUpdate(req dto.ComposeUpdate) error {
}
func (u *ContainerService) loadPath(req *dto.ComposeCreate) error {
if req.From == "template" {
template, err := composeRepo.Get(commonRepo.WithByID(req.Template))
if err != nil {
return err
}
req.From = "edit"
req.File = template.Content
}
if req.From == "edit" {
if req.From == "template" || req.From == "edit" {
dir := fmt.Sprintf("%s/docker/compose/%s", constant.DataDir, req.Name)
if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
if err = os.MkdirAll(dir, os.ModePerm); err != nil {
+4 -4
View File
@@ -29,7 +29,7 @@ func (u *CronjobService) HandleJob(cronjob *model.Cronjob) {
if len(cronjob.Script) == 0 {
return
}
stdout, errExec := cmd.Exec(cronjob.Script)
stdout, errExec := cmd.ExecCronjobWithTimeOut(cronjob.Script, 5*time.Minute)
if errExec != nil {
err = errExec
}
@@ -48,7 +48,7 @@ func (u *CronjobService) HandleJob(cronjob *model.Cronjob) {
if len(cronjob.URL) == 0 {
return
}
stdout, errCurl := cmd.Exec("curl " + cronjob.URL)
stdout, errCurl := cmd.ExecWithTimeOut("curl "+cronjob.URL, 5*time.Minute)
if err != nil {
err = errCurl
}
@@ -170,7 +170,7 @@ func handleTar(sourceDir, targetDir, name, exclusionRules string) error {
commands := fmt.Sprintf("tar zcvf %s %s %s", targetDir+"/"+name, excludeRules, path)
global.LOG.Debug(commands)
stdout, err := cmd.Exec(commands)
stdout, err := cmd.ExecWithTimeOut(commands, 5*time.Minute)
if err != nil {
global.LOG.Errorf("do handle tar failed, stdout: %s, err: %v", stdout, err)
return errors.New(stdout)
@@ -187,7 +187,7 @@ func handleUnTar(sourceFile, targetDir string) error {
commands := fmt.Sprintf("tar zxvfC %s %s", sourceFile, targetDir)
global.LOG.Debug(commands)
stdout, err := cmd.Exec(commands)
stdout, err := cmd.ExecWithTimeOut(commands, 5*time.Minute)
if err != nil {
global.LOG.Errorf("do handle untar failed, stdout: %s, err: %v", stdout, err)
return errors.New(stdout)
+72 -33
View File
@@ -14,6 +14,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/app/repo"
"github.com/1Panel-dev/1Panel/backend/buserr"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
@@ -99,22 +100,7 @@ func (u *MysqlService) Create(ctx context.Context, req dto.MysqlDBCreate) (*mode
}
return nil, err
}
tmpPermission := req.Permission
if err := excSQL(app.ContainerName, app.Password, fmt.Sprintf("create user '%s'@'%s' identified by '%s';", req.Username, tmpPermission, req.Password)); err != nil {
_ = excSQL(app.ContainerName, app.Password, fmt.Sprintf("drop database `%s`", req.Name))
if strings.Contains(err.Error(), "ERROR 1396") {
return nil, buserr.New(constant.ErrUserIsExist)
}
return nil, err
}
grantStr := fmt.Sprintf("grant all privileges on `%s`.* to '%s'@'%s'", req.Name, req.Username, tmpPermission)
if req.Name == "*" {
grantStr = fmt.Sprintf("grant all privileges on *.* to '%s'@'%s'", mysql.Username, tmpPermission)
}
if app.Version == "5.7.39" {
grantStr = fmt.Sprintf("%s identified by '%s' with grant option;", grantStr, req.Password)
}
if err := excSQL(app.ContainerName, app.Password, grantStr); err != nil {
if err := u.createUser(app, req); err != nil {
return nil, err
}
@@ -271,6 +257,9 @@ func (u *MysqlService) ChangeAccess(info dto.ChangeDBInfo) error {
if err != nil {
return err
}
if info.Value == mysql.Permission {
return nil
}
}
app, err := appInstallRepo.LoadBaseInfo("mysql", "")
if err != nil {
@@ -284,24 +273,30 @@ func (u *MysqlService) ChangeAccess(info dto.ChangeDBInfo) error {
}
if info.Value != mysql.Permission {
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("drop user if exists '%s'@'%s'", mysql.Username, mysql.Permission)); err != nil {
return err
var userlist []string
if strings.Contains(mysql.Permission, ",") {
userlist = strings.Split(mysql.Permission, ",")
} else {
userlist = append(userlist, mysql.Permission)
}
for _, user := range userlist {
if len(user) != 0 {
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("drop user if exists '%s'@'%s'", mysql.Username, user)); err != nil {
return err
}
}
}
if info.ID == 0 {
return nil
}
}
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("create user if not exists '%s'@'%s' identified by '%s';", mysql.Username, info.Value, mysql.Password)); err != nil {
return err
}
grantStr := fmt.Sprintf("grant all privileges on `%s`.* to '%s'@'%s'", mysql.Name, mysql.Username, info.Value)
if mysql.Name == "*" {
grantStr = fmt.Sprintf("grant all privileges on *.* to '%s'@'%s'", mysql.Username, info.Value)
}
if app.Version == "5.7.39" {
grantStr = fmt.Sprintf("%s identified by '%s' with grant option;", grantStr, mysql.Password)
}
if err := excuteSql(app.ContainerName, app.Password, grantStr); err != nil {
if err := u.createUser(app, dto.MysqlDBCreate{
Username: mysql.Username,
Name: mysql.Name,
Permission: info.Value,
Password: mysql.Password,
}); err != nil {
return err
}
if err := excuteSql(app.ContainerName, app.Password, "flush privileges"); err != nil {
@@ -475,6 +470,50 @@ func (u *MysqlService) LoadStatus() (*dto.MysqlStatus, error) {
return &info, nil
}
func (u *MysqlService) createUser(app *repo.RootInfo, req dto.MysqlDBCreate) error {
var userlist []string
if strings.Contains(req.Permission, ",") {
ips := strings.Split(req.Permission, ",")
for _, ip := range ips {
if len(ip) != 0 {
userlist = append(userlist, fmt.Sprintf("'%s'@'%s'", req.Username, ip))
}
}
} else {
userlist = append(userlist, fmt.Sprintf("'%s'@'%s'", req.Username, req.Permission))
}
for _, user := range userlist {
if err := excSQL(app.ContainerName, app.Password, fmt.Sprintf("create user %s identified by '%s';", user, req.Password)); err != nil {
handleCreateError(req.Name, userlist, app)
if strings.Contains(err.Error(), "ERROR 1396") {
return buserr.New(constant.ErrUserIsExist)
}
return err
}
grantStr := fmt.Sprintf("grant all privileges on `%s`.* to %s", req.Name, user)
if req.Name == "*" {
grantStr = fmt.Sprintf("grant all privileges on *.* to %s", user)
}
if app.Version == "5.7.39" {
grantStr = fmt.Sprintf("%s identified by '%s' with grant option;", grantStr, req.Password)
}
if err := excSQL(app.ContainerName, app.Password, grantStr); err != nil {
handleCreateError(req.Name, userlist, app)
return err
}
}
return nil
}
func handleCreateError(dbName string, userlist []string, app *repo.RootInfo) {
_ = excSQL(app.ContainerName, app.Password, fmt.Sprintf("drop database `%s`", dbName))
for _, user := range userlist {
if err := excSQL(app.ContainerName, app.Password, fmt.Sprintf("drop user if exists %s", user)); err != nil {
global.LOG.Errorf("drop user failed, err: %v", err)
}
}
}
func excuteSqlForMaps(containerName, password, command string) (map[string]string, error) {
cmd := exec.Command("docker", "exec", containerName, "mysql", "-uroot", "-p"+password, "-e", command)
stdout, err := cmd.CombinedOutput()
@@ -515,15 +554,15 @@ func excuteSql(containerName, password, command string) error {
}
func excSQL(containerName, password, command string) error {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "docker", "exec", containerName, "mysql", "-uroot", "-p"+password, "-e", command)
err := cmd.Run()
stdout, err := cmd.CombinedOutput()
if ctx.Err() == context.DeadlineExceeded {
return buserr.WithDetail(constant.ErrExecTimeOut, containerName, nil)
}
if err != nil {
stdStr := strings.ReplaceAll(err.Error(), "mysql: [Warning] Using a password on the command line interface can be insecure.\n", "")
stdStr := strings.ReplaceAll(string(stdout), "mysql: [Warning] Using a password on the command line interface can be insecure.\n", "")
if err != nil || strings.HasPrefix(string(stdStr), "ERROR ") {
return errors.New(stdStr)
}
return nil
+11 -1
View File
@@ -39,6 +39,7 @@ type IFileService interface {
ChangeName(req request.FileRename) error
Wget(w request.FileWget) (string, error)
MvFile(m request.FileMove) error
ChangeOwner(req request.FileRoleUpdate) error
}
func NewIFileService() IFileService {
@@ -159,7 +160,16 @@ func (f *FileService) BatchDelete(op request.FileBatchDelete) error {
func (f *FileService) ChangeMode(op request.FileCreate) error {
fo := files.NewFileOp()
return fo.Chmod(op.Path, fs.FileMode(op.Mode))
if op.Sub {
return fo.ChmodR(op.Path, op.Mode)
} else {
return fo.Chmod(op.Path, fs.FileMode(op.Mode))
}
}
func (f *FileService) ChangeOwner(req request.FileRoleUpdate) error {
fo := files.NewFileOp()
return fo.ChownR(req.Path, req.User, req.Group, req.Sub)
}
func (f *FileService) Compress(c request.FileCompress) error {
+54 -21
View File
@@ -36,10 +36,14 @@ func NewIFirewallService() IFirewallService {
func (u *FirewallService) LoadBaseInfo() (dto.FirewallBaseInfo, error) {
var baseInfo dto.FirewallBaseInfo
baseInfo.PingStatus = u.pingStatus()
baseInfo.Status = "not running"
baseInfo.Version = "-"
baseInfo.Name = "-"
client, err := firewall.NewFirewallClient()
if err != nil {
if err.Error() == "no such type" {
return dto.FirewallBaseInfo{Name: "-", Version: "-", Status: "not running", PingStatus: "Disable"}, nil
return baseInfo, nil
}
return baseInfo, err
}
@@ -48,12 +52,7 @@ func (u *FirewallService) LoadBaseInfo() (dto.FirewallBaseInfo, error) {
if err != nil {
return baseInfo, err
}
baseInfo.PingStatus, err = u.PingStatus()
if err != nil {
return baseInfo, err
}
if baseInfo.Status == "not running" {
baseInfo.Version = "-"
return baseInfo, err
}
baseInfo.Version, err = client.Version()
@@ -142,14 +141,10 @@ func (u *FirewallService) OperateFirewall(operation string) error {
if err := client.Start(); err != nil {
return err
}
serverPort, err := settingRepo.Get(settingRepo.WithByKey("ServerPort"))
if err != nil {
if err := u.addPortsBeforeStart(client); err != nil {
_ = client.Stop()
return err
}
if err := client.Port(fireClient.FireInfo{Port: serverPort.Value, Protocol: "tcp", Strategy: "accept"}, "add"); err != nil {
return err
}
_ = client.Reload()
_, _ = cmd.Exec("systemctl restart docker")
return nil
case "stop":
@@ -159,9 +154,9 @@ func (u *FirewallService) OperateFirewall(operation string) error {
_, _ = cmd.Exec("systemctl restart docker")
return nil
case "disablePing":
return u.UpdatePingStatus("0")
return u.updatePingStatus("0")
case "enablePing":
return u.UpdatePingStatus("1")
return u.updatePingStatus("1")
}
return fmt.Errorf("not support such operation: %s", operation)
}
@@ -369,15 +364,22 @@ func (u *FirewallService) loadPortByApp() []portOfApp {
return datas
}
func (u *FirewallService) PingStatus() (string, error) {
stdout, _ := cmd.Exec("sudo cat /etc/sysctl.conf | grep net/ipv4/icmp_echo_ignore_all= ")
if stdout == "net/ipv4/icmp_echo_ignore_all=1\n" {
return constant.StatusEnable, nil
func (u *FirewallService) pingStatus() string {
if _, err := os.Stat("/etc/sysctl.conf"); err != nil {
return constant.StatusNone
}
return constant.StatusDisable, nil
commond := "cat /etc/sysctl.conf | grep net/ipv4/icmp_echo_ignore_all= "
if cmd.HasNoPasswordSudo() {
commond = "sudo cat /etc/sysctl.conf | grep net/ipv4/icmp_echo_ignore_all= "
}
stdout, _ := cmd.Exec(commond)
if stdout == "net/ipv4/icmp_echo_ignore_all=1\n" {
return constant.StatusEnable
}
return constant.StatusDisable
}
func (u *FirewallService) UpdatePingStatus(enabel string) error {
func (u *FirewallService) updatePingStatus(enabel string) error {
lineBytes, err := os.ReadFile(confPath)
if err != nil {
return err
@@ -406,10 +408,41 @@ func (u *FirewallService) UpdatePingStatus(enabel string) error {
return err
}
stdout, err := cmd.Exec("sudo sysctl -p")
commond := "sysctl -p"
if cmd.HasNoPasswordSudo() {
commond = "sudo sysctl -p"
}
stdout, err := cmd.Exec(commond)
if err != nil {
return fmt.Errorf("update ping status failed, err: %v", stdout)
}
return nil
}
func (u *FirewallService) addPortsBeforeStart(client firewall.FirewallClient) error {
serverPort, err := settingRepo.Get(settingRepo.WithByKey("ServerPort"))
if err != nil {
return err
}
if err := client.Port(fireClient.FireInfo{Port: serverPort.Value, Protocol: "tcp", Strategy: "accept"}, "add"); err != nil {
return err
}
if err := client.Port(fireClient.FireInfo{Port: "22", Protocol: "tcp", Strategy: "accept"}, "add"); err != nil {
return err
}
if err := client.Port(fireClient.FireInfo{Port: "80", Protocol: "tcp", Strategy: "accept"}, "add"); err != nil {
return err
}
if err := client.Port(fireClient.FireInfo{Port: "443", Protocol: "tcp", Strategy: "accept"}, "add"); err != nil {
return err
}
apps := u.loadPortByApp()
for _, app := range apps {
if err := client.Port(fireClient.FireInfo{Port: app.HttpPort, Protocol: "tcp", Strategy: "accept"}, "add"); err != nil {
return err
}
}
return client.Reload()
}
+4
View File
@@ -161,6 +161,10 @@ func getNginxParamsFromStaticFile(scope dto.NginxKey, newParams []dto.NginxParam
switch scope {
case dto.SSL:
newConfig = parser.NewStringParser(string(nginx_conf.SSL)).Parse()
case dto.CACHE:
newConfig = parser.NewStringParser(string(nginx_conf.Cache)).Parse()
case dto.ProxyCache:
newConfig = parser.NewStringParser(string(nginx_conf.ProxyCache)).Parse()
}
for _, dir := range newConfig.GetDirectives() {
addParam := dto.NginxParam{
+12 -1
View File
@@ -8,7 +8,9 @@ import (
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/docker"
"github.com/1Panel-dev/1Panel/backend/utils/files"
"github.com/docker/cli/cli/command"
"github.com/subosito/gotenv"
"os"
"path"
"strings"
)
@@ -87,7 +89,16 @@ func getComposeService(name, runtimeDir string, composeFile, env []byte, skipNor
if err != nil {
return nil, err
}
composeService, err := docker.NewComposeService()
logPath := path.Join(runtimeDir, "build.log")
fileOp := files.NewFileOp()
if fileOp.Stat(logPath) {
_ = fileOp.DeleteFile(logPath)
}
file, err := os.Create(logPath)
if err != nil {
return nil, err
}
composeService, err := docker.NewComposeService(command.WithOutputStream(file))
if err != nil {
return nil, err
}
+196
View File
@@ -1,8 +1,14 @@
package service
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/1Panel-dev/1Panel/backend/app/dto"
@@ -12,6 +18,8 @@ import (
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/encrypt"
"github.com/1Panel-dev/1Panel/backend/utils/files"
"github.com/1Panel-dev/1Panel/backend/utils/ssl"
"github.com/gin-gonic/gin"
)
@@ -22,6 +30,8 @@ type ISettingService interface {
Update(key, value string) error
UpdatePassword(c *gin.Context, old, new string) error
UpdatePort(port uint) error
UpdateSSL(c *gin.Context, req dto.SSLUpdate) error
LoadFromCert() (*dto.SSLInfo, error)
HandlePasswordExpired(c *gin.Context, old, new string) error
}
@@ -90,6 +100,135 @@ func (u *SettingService) UpdatePort(port uint) error {
return nil
}
func (u *SettingService) UpdateSSL(c *gin.Context, req dto.SSLUpdate) error {
secretDir := global.CONF.System.BaseDir + "/1panel/secret/"
if req.SSL == "disable" {
if err := settingRepo.Update("SSL", "disable"); err != nil {
return err
}
if err := settingRepo.Update("SSLType", "self"); err != nil {
return err
}
_ = os.Remove(secretDir + "server.crt")
_ = os.Remove(secretDir + "server.key")
go func() {
_, err := cmd.Exec("systemctl restart 1panel.service")
if err != nil {
global.LOG.Errorf("restart system failed, err: %v", err)
}
}()
return nil
}
if _, err := os.Stat(secretDir); err != nil && os.IsNotExist(err) {
if err = os.MkdirAll(secretDir, os.ModePerm); err != nil {
return err
}
}
if err := settingRepo.Update("SSLType", req.SSLType); err != nil {
return err
}
if req.SSLType == "self" {
if len(req.Domain) == 0 {
return fmt.Errorf("load domain failed")
}
if err := ssl.GenerateSSL(req.Domain); err != nil {
return err
}
}
if req.SSLType == "select" {
sslInfo, err := websiteSSLRepo.GetFirst(commonRepo.WithByID(req.SSLID))
if err != nil {
return err
}
req.Cert = sslInfo.Pem
req.Key = sslInfo.PrivateKey
req.SSLType = "import"
if err := settingRepo.Update("SSLID", strconv.Itoa(int(req.SSLID))); err != nil {
return err
}
}
if req.SSLType == "import" {
cert, err := os.OpenFile(secretDir+"server.crt.tmp", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer cert.Close()
if _, err := cert.WriteString(req.Cert); err != nil {
return err
}
key, err := os.OpenFile(secretDir+"server.key.tmp", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
if _, err := key.WriteString(req.Key); err != nil {
return err
}
defer key.Close()
}
if err := checkCertValid(req.Domain); err != nil {
return err
}
fileOp := files.NewFileOp()
if err := fileOp.Rename(secretDir+"server.crt.tmp", secretDir+"server.crt"); err != nil {
return err
}
if err := fileOp.Rename(secretDir+"server.key.tmp", secretDir+"server.key"); err != nil {
return err
}
if err := settingRepo.Update("SSL", req.SSL); err != nil {
return err
}
go func() {
_, err := cmd.Exec("systemctl restart 1panel.service")
if err != nil {
global.LOG.Errorf("restart system failed, err: %v", err)
}
}()
return nil
}
func (u *SettingService) LoadFromCert() (*dto.SSLInfo, error) {
ssl, err := settingRepo.Get(settingRepo.WithByKey("SSL"))
if err != nil {
return nil, err
}
if ssl.Value == "disable" {
return &dto.SSLInfo{}, nil
}
sslType, err := settingRepo.Get(settingRepo.WithByKey("SSLType"))
if err != nil {
return nil, err
}
data, err := loadInfoFromCert()
if err != nil {
return nil, err
}
switch sslType.Value {
case "import":
if _, err := os.Stat(global.CONF.System.BaseDir + "/1panel/secret/server.crt"); err != nil {
return nil, fmt.Errorf("load server.crt file failed, err: %v", err)
}
certFile, _ := os.ReadFile(global.CONF.System.BaseDir + "/1panel/secret/server.crt")
data.Cert = string(certFile)
if _, err := os.Stat(global.CONF.System.BaseDir + "/1panel/secret/server.key"); err != nil {
return nil, fmt.Errorf("load server.key file failed, err: %v", err)
}
keyFile, _ := os.ReadFile(global.CONF.System.BaseDir + "/1panel/secret/server.key")
data.Key = string(keyFile)
case "select":
sslID, err := settingRepo.Get(settingRepo.WithByKey("SSLID"))
if err != nil {
return nil, err
}
id, _ := strconv.Atoi(sslID.Value)
data.SSLID = uint(id)
}
return data, nil
}
func (u *SettingService) HandlePasswordExpired(c *gin.Context, old, new string) error {
setting, err := settingRepo.Get(settingRepo.WithByKey("Password"))
if err != nil {
@@ -128,3 +267,60 @@ func (u *SettingService) UpdatePassword(c *gin.Context, old, new string) error {
_ = global.SESSION.Clean()
return nil
}
func loadInfoFromCert() (*dto.SSLInfo, error) {
var info dto.SSLInfo
certFile := global.CONF.System.BaseDir + "/1panel/secret/server.crt"
if _, err := os.Stat(certFile); err != nil {
return &info, err
}
certData, err := os.ReadFile(certFile)
if err != nil {
return &info, err
}
certBlock, _ := pem.Decode(certData)
if certBlock == nil {
return &info, err
}
certObj, err := x509.ParseCertificate(certBlock.Bytes)
if err != nil {
return &info, err
}
var domains []string
if len(certObj.IPAddresses) != 0 {
for _, ip := range certObj.IPAddresses {
domains = append(domains, ip.String())
}
}
if len(certObj.DNSNames) != 0 {
domains = append(domains, certObj.DNSNames...)
}
return &dto.SSLInfo{
Domain: strings.Join(domains, ","),
Timeout: certObj.NotAfter.Format("2006-01-02 15:04:05"),
RootPath: global.CONF.System.BaseDir + "/1panel/secret/server.crt",
}, nil
}
func checkCertValid(domain string) error {
certificate, err := os.ReadFile(global.CONF.System.BaseDir + "/1panel/secret/server.crt.tmp")
if err != nil {
return err
}
key, err := os.ReadFile(global.CONF.System.BaseDir + "/1panel/secret/server.key.tmp")
if err != nil {
return err
}
if _, err = tls.X509KeyPair(certificate, key); err != nil {
return err
}
certBlock, _ := pem.Decode(certificate)
if certBlock == nil {
return err
}
if _, err := x509.ParseCertificate(certBlock.Bytes); err != nil {
return err
}
return nil
}
+2 -2
View File
@@ -854,7 +854,7 @@ func (u *SnapshotService) handleTar(sourceDir, targetDir, name, exclusionRules s
commands := fmt.Sprintf("tar --warning=no-file-changed -zcf %s %s -C %s .", targetDir+"/"+name, exStr, sourceDir)
global.LOG.Debug(commands)
stdout, err := cmd.Exec(commands)
stdout, err := cmd.ExecWithTimeOut(commands, 30*time.Minute)
if err != nil {
global.LOG.Errorf("do handle tar failed, stdout: %s, err: %v", stdout, err)
return errors.New(stdout)
@@ -871,7 +871,7 @@ func (u *SnapshotService) handleUnTar(sourceDir, targetDir string) error {
commands := fmt.Sprintf("tar -zxf %s -C %s .", sourceDir, targetDir)
global.LOG.Debug(commands)
stdout, err := cmd.Exec(commands)
stdout, err := cmd.ExecWithTimeOut(commands, 30*time.Minute)
if err != nil {
global.LOG.Errorf("do handle untar failed, stdout: %s, err: %v", stdout, err)
return errors.New(stdout)
+1 -1
View File
@@ -148,7 +148,7 @@ func (u *UpgradeService) Upgrade(req dto.Upgrade) error {
go writeLogs(req.Version)
_ = settingRepo.Update("SystemVersion", req.Version)
_ = settingRepo.Update("SystemStatus", "Free")
_, _ = cmd.Exec("systemctl daemon-reload && systemctl restart 1panel.service")
_, _ = cmd.ExecWithTimeOut("systemctl daemon-reload && systemctl restart 1panel.service", 1*time.Minute)
}()
return nil
}
+616 -13
View File
@@ -2,17 +2,26 @@ package service
import (
"bufio"
"bytes"
"context"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/nginx"
"github.com/1Panel-dev/1Panel/backend/utils/nginx/components"
"github.com/1Panel-dev/1Panel/backend/utils/nginx/parser"
"github.com/1Panel-dev/1Panel/cmd/server/nginx_conf"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
"os"
"path"
"reflect"
"regexp"
"strconv"
"strings"
"time"
@@ -57,6 +66,15 @@ type IWebsiteService interface {
GetPHPConfig(id uint) (*response.PHPConfig, error)
UpdatePHPConfig(req request.WebsitePHPConfigUpdate) error
UpdatePHPConfigFile(req request.WebsitePHPFileUpdate) error
GetRewriteConfig(req request.NginxRewriteReq) (*response.NginxRewriteRes, error)
UpdateRewriteConfig(req request.NginxRewriteUpdate) error
UpdateSiteDir(req request.WebsiteUpdateDir) error
UpdateSitePermission(req request.WebsiteUpdateDirPermission) error
OperateProxy(req request.WebsiteProxyConfig) (err error)
GetProxies(id uint) (res []request.WebsiteProxyConfig, err error)
UpdateProxyFile(req request.NginxProxyUpdate) (err error)
GetAuthBasics(req request.NginxAuthReq) (res response.NginxAuthRes, err error)
UpdateAuthBasic(req request.NginxAuthUpdate) (err error)
}
func NewIWebsiteService() IWebsiteService {
@@ -68,6 +86,13 @@ func (w WebsiteService) PageWebsite(req request.WebsiteSearch) (int64, []respons
websiteDTOs []response.WebsiteDTO
opts []repo.DBOption
)
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return 0, nil, nil
}
return 0, nil, err
}
opts = append(opts, commonRepo.WithOrderBy("created_at desc"))
if req.Name != "" {
opts = append(opts, websiteRepo.WithDomainLike(req.Name))
@@ -98,10 +123,12 @@ func (w WebsiteService) PageWebsite(req request.WebsiteSearch) (int64, []respons
}
runtimeName = runtime.Name
}
sitePath := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name, "www", "sites", web.Alias)
websiteDTOs = append(websiteDTOs, response.WebsiteDTO{
Website: web,
AppName: appName,
RuntimeName: runtimeName,
SitePath: sitePath,
})
}
return total, websiteDTOs, nil
@@ -143,6 +170,7 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
WebsiteGroupID: create.WebsiteGroupID,
Protocol: constant.ProtocolHTTP,
Proxy: create.Proxy,
SiteDir: "/",
AccessLog: true,
ErrorLog: true,
}
@@ -385,10 +413,16 @@ func (w WebsiteService) DeleteWebsite(req request.WebsiteDelete) error {
}
func (w WebsiteService) CreateWebsiteDomain(create request.WebsiteDomainCreate) (model.WebsiteDomain, error) {
var domainModel model.WebsiteDomain
var ports []int
var domains []string
var (
domainModel model.WebsiteDomain
ports []int
domains []string
)
if create.Port != 80 {
if common.ScanPort(create.Port) {
return domainModel, buserr.WithDetail(constant.ErrPortInUsed, create.Port, nil)
}
}
website, err := websiteRepo.GetFirst(commonRepo.WithByID(create.WebsiteID))
if err != nil {
return domainModel, err
@@ -405,6 +439,11 @@ func (w WebsiteService) CreateWebsiteDomain(create request.WebsiteDomainCreate)
Port: create.Port,
WebsiteID: create.WebsiteID,
}
if create.Port != 80 {
go func() {
_ = OperateFirewallPort(nil, []int{create.Port})
}()
}
return domainModel, websiteDomainRepo.Create(context.TODO(), &domainModel)
}
@@ -435,7 +474,11 @@ func (w WebsiteService) DeleteWebsiteDomain(domainId uint) error {
domains = append(domains, webSiteDomain.Domain)
}
if len(ports) > 0 || len(domains) > 0 {
if err := deleteListenAndServerName(website, ports, domains); err != nil {
stringBinds := make([]string, len(ports))
for i := 0; i < len(ports); i++ {
stringBinds[i] = strconv.Itoa(ports[i])
}
if err := deleteListenAndServerName(website, stringBinds, domains); err != nil {
return err
}
}
@@ -582,14 +625,28 @@ func (w WebsiteService) OpWebsiteHTTPS(ctx context.Context, req request.WebsiteH
if !req.Enable {
website.Protocol = constant.ProtocolHTTP
website.WebsiteSSLID = 0
if err := deleteListenAndServerName(website, []int{443}, []string{}); err != nil {
if err := deleteListenAndServerName(website, []string{"443", "[::]:443"}, []string{}); err != nil {
return response.WebsiteHTTPS{}, err
}
nginxParams := getNginxParamsFromStaticFile(dto.SSL, nil)
nginxParams = append(nginxParams, dto.NginxParam{
Name: "if",
Params: []string{"($scheme", "=", "http)"},
})
nginxParams = append(nginxParams,
dto.NginxParam{
Name: "if",
Params: []string{"($scheme", "=", "http)"},
},
dto.NginxParam{
Name: "ssl_certificate",
},
dto.NginxParam{
Name: "ssl_certificate_key",
},
dto.NginxParam{
Name: "ssl_protocols",
},
dto.NginxParam{
Name: "ssl_ciphers",
},
)
if err := deleteNginxConfig(constant.NginxScopeServer, nginxParams, &website); err != nil {
return response.WebsiteHTTPS{}, err
}
@@ -797,7 +854,16 @@ func (w WebsiteService) OpWebsiteLog(req request.WebsiteLogReq) (*response.Websi
return res, nil
}
}
content, err := os.ReadFile(path.Join(sitePath, "log", req.LogType))
filePath := path.Join(sitePath, "log", req.LogType)
fileInfo, err := os.Stat(filePath)
if err != nil {
return nil, err
}
if fileInfo.Size() > 10<<20 {
return nil, buserr.New(constant.ErrFileToLarge)
}
fileInfo.Size()
content, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
@@ -846,7 +912,23 @@ func (w WebsiteService) OpWebsiteLog(req request.WebsiteLogReq) (*response.Websi
func (w WebsiteService) ChangeDefaultServer(id uint) error {
defaultWebsite, _ := websiteRepo.GetFirst(websiteRepo.WithDefaultServer())
if defaultWebsite.ID > 0 {
if err := updateNginxConfig(constant.NginxScopeServer, []dto.NginxParam{{Name: "listen", Params: []string{"80"}}}, &defaultWebsite); err != nil {
params, err := getNginxParamsByKeys(constant.NginxScopeServer, []string{"listen"}, &defaultWebsite)
if err != nil {
return err
}
var changeParams []dto.NginxParam
for _, param := range params {
paramLen := len(param.Params)
var newParam []string
if paramLen > 1 && param.Params[paramLen-1] == components.DefaultServer {
newParam = param.Params[:paramLen-1]
}
changeParams = append(changeParams, dto.NginxParam{
Name: param.Name,
Params: newParam,
})
}
if err := updateNginxConfig(constant.NginxScopeServer, changeParams, &defaultWebsite); err != nil {
return err
}
defaultWebsite.DefaultServer = false
@@ -859,7 +941,28 @@ func (w WebsiteService) ChangeDefaultServer(id uint) error {
if err != nil {
return err
}
if err := updateNginxConfig(constant.NginxScopeServer, []dto.NginxParam{{Name: "listen", Params: []string{"80", "default_server"}}}, &website); err != nil {
params, err := getNginxParamsByKeys(constant.NginxScopeServer, []string{"listen"}, &website)
if err != nil {
return err
}
var changeParams []dto.NginxParam
for _, param := range params {
paramLen := len(param.Params)
bind := param.Params[0]
var newParam []string
if bind == "80" || bind == "443" || bind == "[::]:80" || bind == "[::]:443" {
if param.Params[paramLen-1] == components.DefaultServer {
newParam = param.Params
} else {
newParam = append(param.Params, components.DefaultServer)
}
}
changeParams = append(changeParams, dto.NginxParam{
Name: param.Name,
Params: newParam,
})
}
if err := updateNginxConfig(constant.NginxScopeServer, changeParams, &website); err != nil {
return err
}
website.DefaultServer = true
@@ -981,3 +1084,503 @@ func (w WebsiteService) UpdatePHPConfigFile(req request.WebsitePHPFileUpdate) er
}
return nil
}
func (w WebsiteService) UpdateRewriteConfig(req request.NginxRewriteUpdate) error {
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
if err != nil {
return err
}
nginxFull, err := getNginxFull(&website)
if err != nil {
return err
}
includePath := fmt.Sprintf("/www/sites/%s/rewrite/%s.conf", website.Alias, website.PrimaryDomain)
absolutePath := path.Join(nginxFull.Install.GetPath(), includePath)
fileOp := files.NewFileOp()
var oldRewriteContent []byte
if !fileOp.Stat(path.Dir(absolutePath)) {
if err := fileOp.CreateDir(path.Dir(absolutePath), 0755); err != nil {
return err
}
}
if !fileOp.Stat(absolutePath) {
if err := fileOp.CreateFile(absolutePath); err != nil {
return err
}
} else {
oldRewriteContent, err = fileOp.GetContent(absolutePath)
if err != nil {
return err
}
}
if err := fileOp.WriteFile(absolutePath, strings.NewReader(req.Content), 0755); err != nil {
return err
}
if err := updateNginxConfig(constant.NginxScopeServer, []dto.NginxParam{{Name: "include", Params: []string{includePath}}}, &website); err != nil {
_ = fileOp.WriteFile(absolutePath, bytes.NewReader(oldRewriteContent), 0755)
return err
}
website.Rewrite = req.Name
return websiteRepo.Save(context.Background(), &website)
}
func (w WebsiteService) GetRewriteConfig(req request.NginxRewriteReq) (*response.NginxRewriteRes, error) {
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
if err != nil {
return nil, err
}
var contentByte []byte
if req.Name == "current" {
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
if err != nil {
return nil, err
}
rewriteConfPath := path.Join(nginxInstall.GetPath(), "www", "sites", website.Alias, "rewrite", fmt.Sprintf("%s.conf", website.PrimaryDomain))
fileOp := files.NewFileOp()
if fileOp.Stat(rewriteConfPath) {
contentByte, err = fileOp.GetContent(rewriteConfPath)
if err != nil {
return nil, err
}
}
} else {
rewriteFile := fmt.Sprintf("rewrite/%s.conf", strings.ToLower(req.Name))
contentByte, err = nginx_conf.Rewrites.ReadFile(rewriteFile)
if err != nil {
return nil, err
}
}
return &response.NginxRewriteRes{
Content: string(contentByte),
}, err
}
func (w WebsiteService) UpdateSiteDir(req request.WebsiteUpdateDir) error {
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
if err != nil {
return err
}
runDir := req.SiteDir
siteDir := path.Join("/www/sites", website.Alias, "index")
if req.SiteDir != "/" {
siteDir = fmt.Sprintf("%s/%s", siteDir, req.SiteDir)
}
if err := updateNginxConfig(constant.NginxScopeServer, []dto.NginxParam{{Name: "root", Params: []string{siteDir}}}, &website); err != nil {
return err
}
website.SiteDir = runDir
return websiteRepo.Save(context.Background(), &website)
}
func (w WebsiteService) UpdateSitePermission(req request.WebsiteUpdateDirPermission) error {
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
if err != nil {
return err
}
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
if err != nil {
return err
}
absoluteIndexPath := path.Join(nginxInstall.GetPath(), "www", "sites", website.Alias, "index")
if website.SiteDir != "/" {
absoluteIndexPath = path.Join(absoluteIndexPath, website.SiteDir)
}
chownCmd := fmt.Sprintf("chown -R %s:%s %s", req.User, req.Group, absoluteIndexPath)
if cmd.HasNoPasswordSudo() {
chownCmd = fmt.Sprintf("sudo %s", chownCmd)
}
if out, err := cmd.ExecWithTimeOut(chownCmd, 1*time.Second); err != nil {
if out != "" {
return errors.New(out)
}
return err
}
website.User = req.User
website.Group = req.Group
return websiteRepo.Save(context.Background(), &website)
}
func (w WebsiteService) OperateProxy(req request.WebsiteProxyConfig) (err error) {
var (
website model.Website
params []response.NginxParam
nginxInstall model.AppInstall
par *parser.Parser
oldContent []byte
)
website, err = websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
if err != nil {
return
}
params, err = getNginxParamsByKeys(constant.NginxScopeHttp, []string{"proxy_cache"}, &website)
if err != nil {
return
}
nginxInstall, err = getAppInstallByKey(constant.AppOpenresty)
if err != nil {
return
}
fileOp := files.NewFileOp()
if len(params) == 0 || len(params[0].Params) == 0 {
commonDir := path.Join(nginxInstall.GetPath(), "www", "common", "proxy")
proxyTempPath := path.Join(commonDir, "proxy_temp_dir")
if !fileOp.Stat(proxyTempPath) {
_ = fileOp.CreateDir(proxyTempPath, 0755)
}
proxyCacheDir := path.Join(commonDir, "proxy_temp_dir")
if !fileOp.Stat(proxyCacheDir) {
_ = fileOp.CreateDir(proxyCacheDir, 0755)
}
nginxParams := getNginxParamsFromStaticFile(dto.CACHE, nil)
if err = updateNginxConfig(constant.NginxScopeHttp, nginxParams, &website); err != nil {
return
}
}
includeDir := path.Join(nginxInstall.GetPath(), "www", "sites", website.Alias, "proxy")
if !fileOp.Stat(includeDir) {
_ = fileOp.CreateDir(includeDir, 0755)
}
fileName := fmt.Sprintf("%s.conf", req.Name)
includePath := path.Join(includeDir, fileName)
backName := fmt.Sprintf("%s.bak", req.Name)
backPath := path.Join(includeDir, backName)
if req.Operate == "create" && (fileOp.Stat(includePath) || fileOp.Stat(backPath)) {
err = buserr.New(constant.ErrNameIsExist)
return
}
defer func() {
if err != nil {
switch req.Operate {
case "create":
_ = fileOp.DeleteFile(includePath)
case "edit":
_ = fileOp.WriteFile(includePath, bytes.NewReader(oldContent), 0755)
}
}
}()
var config *components.Config
switch req.Operate {
case "create":
config = parser.NewStringParser(string(nginx_conf.Proxy)).Parse()
case "edit":
par, err = parser.NewParser(includePath)
if err != nil {
return
}
config = par.Parse()
oldContent, err = fileOp.GetContent(includePath)
if err != nil {
return
}
case "delete":
_ = fileOp.DeleteFile(includePath)
_ = fileOp.DeleteFile(backPath)
return updateNginxConfig(constant.NginxScopeServer, nil, &website)
case "disable":
_ = fileOp.Rename(includePath, backPath)
return updateNginxConfig(constant.NginxScopeServer, nil, &website)
case "enable":
_ = fileOp.Rename(backPath, includePath)
return updateNginxConfig(constant.NginxScopeServer, nil, &website)
}
config.FilePath = includePath
directives := config.Directives
location, ok := directives[0].(*components.Location)
if !ok {
err = errors.New("error")
return
}
location.UpdateDirective("proxy_pass", []string{req.ProxyPass})
location.UpdateDirective("proxy_set_header", []string{"Host", req.ProxyHost})
location.ChangePath(req.Modifier, req.Match)
if req.Cache {
location.AddCache(req.CacheTime, req.CacheUnit)
} else {
location.RemoveCache()
}
if len(req.Replaces) > 0 {
location.AddSubFilter(req.Replaces)
} else {
location.RemoveSubFilter()
}
if err = nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
return buserr.WithErr(constant.ErrUpdateBuWebsite, err)
}
nginxInclude := fmt.Sprintf("/www/sites/%s/proxy/*.conf", website.Alias)
if err = updateNginxConfig(constant.NginxScopeServer, []dto.NginxParam{{Name: "include", Params: []string{nginxInclude}}}, &website); err != nil {
return
}
return
}
func (w WebsiteService) GetProxies(id uint) (res []request.WebsiteProxyConfig, err error) {
var (
website model.Website
nginxInstall model.AppInstall
fileList response.FileInfo
)
website, err = websiteRepo.GetFirst(commonRepo.WithByID(id))
if err != nil {
return
}
nginxInstall, err = getAppInstallByKey(constant.AppOpenresty)
if err != nil {
return
}
includeDir := path.Join(nginxInstall.GetPath(), "www", "sites", website.Alias, "proxy")
fileOp := files.NewFileOp()
if !fileOp.Stat(includeDir) {
return
}
fileList, err = NewIFileService().GetFileList(request.FileOption{FileOption: files.FileOption{Path: includeDir, Expand: true, Page: 1, PageSize: 100}})
if len(fileList.Items) == 0 {
return
}
var (
content []byte
config *components.Config
)
for _, configFile := range fileList.Items {
proxyConfig := request.WebsiteProxyConfig{
ID: website.ID,
}
parts := strings.Split(configFile.Name, ".")
proxyConfig.Name = parts[0]
if parts[1] == "conf" {
proxyConfig.Enable = true
} else {
proxyConfig.Enable = false
}
proxyConfig.FilePath = configFile.Path
content, err = fileOp.GetContent(configFile.Path)
if err != nil {
return
}
proxyConfig.Content = string(content)
config = parser.NewStringParser(string(content)).Parse()
directives := config.GetDirectives()
location, ok := directives[0].(*components.Location)
if !ok {
err = errors.New("error")
return
}
proxyConfig.ProxyPass = location.ProxyPass
proxyConfig.Cache = location.Cache
if location.CacheTime > 0 {
proxyConfig.CacheTime = location.CacheTime
proxyConfig.CacheUnit = location.CacheUint
}
proxyConfig.Match = location.Match
proxyConfig.Modifier = location.Modifier
proxyConfig.ProxyHost = location.Host
proxyConfig.Replaces = location.Replaces
res = append(res, proxyConfig)
}
return
}
func (w WebsiteService) UpdateProxyFile(req request.NginxProxyUpdate) (err error) {
var (
website model.Website
nginxFull dto.NginxFull
oldRewriteContent []byte
)
website, err = websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
if err != nil {
return err
}
nginxFull, err = getNginxFull(&website)
if err != nil {
return err
}
includePath := fmt.Sprintf("/www/sites/%s/proxy/%s.conf", website.Alias, req.Name)
absolutePath := path.Join(nginxFull.Install.GetPath(), includePath)
fileOp := files.NewFileOp()
oldRewriteContent, err = fileOp.GetContent(absolutePath)
if err != nil {
return err
}
if err = fileOp.WriteFile(absolutePath, strings.NewReader(req.Content), 0755); err != nil {
return err
}
defer func() {
if err != nil {
_ = fileOp.WriteFile(absolutePath, bytes.NewReader(oldRewriteContent), 0755)
}
}()
return updateNginxConfig(constant.NginxScopeServer, nil, &website)
}
func (w WebsiteService) UpdateAuthBasic(req request.NginxAuthUpdate) (err error) {
var (
website model.Website
nginxInstall model.AppInstall
params []dto.NginxParam
authContent []byte
authArray []string
)
website, err = websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
if err != nil {
return err
}
nginxInstall, err = getAppInstallByKey(constant.AppOpenresty)
if err != nil {
return
}
authPath := fmt.Sprintf("/www/sites/%s/auth_basic/auth.pass", website.Alias)
absoluteAuthPath := path.Join(nginxInstall.GetPath(), authPath)
fileOp := files.NewFileOp()
if !fileOp.Stat(path.Dir(absoluteAuthPath)) {
_ = fileOp.CreateDir(path.Dir(absoluteAuthPath), 0755)
}
if !fileOp.Stat(absoluteAuthPath) {
_ = fileOp.CreateFile(absoluteAuthPath)
}
defer func() {
if err != nil {
switch req.Operate {
case "create":
}
}
}()
params = append(params, dto.NginxParam{Name: "auth_basic", Params: []string{`"Authentication"`}})
params = append(params, dto.NginxParam{Name: "auth_basic_user_file", Params: []string{authPath}})
authContent, err = fileOp.GetContent(absoluteAuthPath)
if err != nil {
return
}
authArray = strings.Split(string(authContent), "\n")
switch req.Operate {
case "disable":
return deleteNginxConfig(constant.NginxScopeServer, params, &website)
case "enable":
return updateNginxConfig(constant.NginxScopeServer, params, &website)
case "create":
for _, line := range authArray {
authParams := strings.Split(line, ":")
username := authParams[0]
if username == req.Username {
err = buserr.New(constant.ErrUsernameIsExist)
return
}
}
var passwdHash []byte
passwdHash, err = bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
if err != nil {
return
}
line := fmt.Sprintf("%s:%s\n", req.Username, passwdHash)
if req.Remark != "" {
line = fmt.Sprintf("%s:%s:%s\n", req.Username, passwdHash, req.Remark)
}
authArray = append(authArray, line)
case "edit":
userExist := false
for index, line := range authArray {
authParams := strings.Split(line, ":")
username := authParams[0]
if username == req.Username {
userExist = true
var passwdHash []byte
passwdHash, err = bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
if err != nil {
return
}
userPasswd := fmt.Sprintf("%s:%s\n", req.Username, passwdHash)
if req.Remark != "" {
userPasswd = fmt.Sprintf("%s:%s:%s\n", req.Username, passwdHash, req.Remark)
}
authArray[index] = userPasswd
}
}
if !userExist {
err = buserr.New(constant.ErrUsernameIsNotExist)
return
}
case "delete":
deleteIndex := -1
for index, line := range authArray {
authParams := strings.Split(line, ":")
username := authParams[0]
if username == req.Username {
deleteIndex = index
}
}
if deleteIndex < 0 {
return
}
authArray = append(authArray[:deleteIndex], authArray[deleteIndex+1:]...)
}
var passFile *os.File
passFile, err = os.Create(absoluteAuthPath)
if err != nil {
return
}
defer passFile.Close()
writer := bufio.NewWriter(passFile)
for _, line := range authArray {
_, err = writer.WriteString(line + "\n")
if err != nil {
return
}
}
err = writer.Flush()
if err != nil {
return
}
return
}
func (w WebsiteService) GetAuthBasics(req request.NginxAuthReq) (res response.NginxAuthRes, err error) {
var (
website model.Website
nginxInstall model.AppInstall
authContent []byte
nginxParams []response.NginxParam
)
website, err = websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
if err != nil {
return
}
nginxInstall, err = getAppInstallByKey(constant.AppOpenresty)
if err != nil {
return
}
authPath := fmt.Sprintf("/www/sites/%s/auth_basic/auth.pass", website.Alias)
absoluteAuthPath := path.Join(nginxInstall.GetPath(), authPath)
fileOp := files.NewFileOp()
if !fileOp.Stat(absoluteAuthPath) {
return
}
nginxParams, err = getNginxParamsByKeys(constant.NginxScopeServer, []string{"auth_basic"}, &website)
if err != nil {
return
}
res.Enable = len(nginxParams[0].Params) > 0
authContent, err = fileOp.GetContent(absoluteAuthPath)
authArray := strings.Split(string(authContent), "\n")
for _, line := range authArray {
if line == "" {
continue
}
params := strings.Split(line, ":")
auth := dto.NginxAuth{
Username: params[0],
}
if len(params) == 3 {
auth.Remark = params[2]
}
res.Items = append(res.Items, auth)
}
return
}
+92 -22
View File
@@ -2,6 +2,9 @@ package service
import (
"fmt"
"github.com/1Panel-dev/1Panel/backend/buserr"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/utils/nginx/components"
"path"
"strconv"
"strings"
@@ -58,8 +61,6 @@ func createIndexFile(website *model.Website, runtime *model.Runtime) error {
if runtime.Type == constant.RuntimePHP {
indexPath = path.Join(indexFolder, "index.php")
indexContent = string(nginx_conf.IndexPHP)
} else {
return nil
}
}
@@ -74,12 +75,51 @@ func createIndexFile(website *model.Website, runtime *model.Runtime) error {
return err
}
}
if website.Type == constant.Runtime && runtime.Resource == constant.ResourceAppstore {
if err := chownRootDir(indexFolder); err != nil {
return err
}
}
if err := fileOp.WriteFile(indexPath, strings.NewReader(indexContent), 0755); err != nil {
return err
}
return nil
}
func createProxyFile(website *model.Website, runtime *model.Runtime) error {
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
if err != nil {
return err
}
proxyFolder := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name, "www", "sites", website.Alias, "proxy")
filePath := path.Join(proxyFolder, "root.conf")
fileOp := files.NewFileOp()
if !fileOp.Stat(proxyFolder) {
if err := fileOp.CreateDir(proxyFolder, 0755); err != nil {
return err
}
}
if !fileOp.Stat(filePath) {
if err := fileOp.CreateFile(filePath); err != nil {
return err
}
}
config := parser.NewStringParser(string(nginx_conf.Proxy)).Parse()
config.FilePath = filePath
directives := config.Directives
location, ok := directives[0].(*components.Location)
if !ok {
return errors.New("error")
}
location.ChangePath("^~", "/")
location.UpdateDirective("proxy_pass", []string{website.Proxy})
location.UpdateDirective("proxy_set_header", []string{"Host", "$host"})
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
return buserr.WithErr(constant.ErrUpdateBuWebsite, err)
}
return nil
}
func createWebsiteFolder(nginxInstall model.AppInstall, website *model.Website, runtime *model.Runtime) error {
nginxFolder := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name)
siteFolder := path.Join(nginxFolder, "www", "sites", website.Alias)
@@ -97,19 +137,21 @@ func createWebsiteFolder(nginxInstall model.AppInstall, website *model.Website,
if err := fileOp.CreateFile(path.Join(siteFolder, "log", "error.log")); err != nil {
return err
}
if err := fileOp.CreateDir(path.Join(siteFolder, "index"), 0755); err != nil {
if err := fileOp.CreateDir(path.Join(siteFolder, "index"), 0775); err != nil {
return err
}
if err := fileOp.CreateDir(path.Join(siteFolder, "ssl"), 0755); err != nil {
return err
}
if website.Type == constant.Runtime && runtime.Type == constant.RuntimePHP && runtime.Resource == constant.ResourceLocal {
phpPoolDir := path.Join(siteFolder, "php-pool")
if err := fileOp.CreateDir(phpPoolDir, 0755); err != nil {
return err
}
if err := fileOp.CreateFile(path.Join(phpPoolDir, "php-fpm.sock")); err != nil {
return err
if website.Type == constant.Runtime {
if runtime.Type == constant.RuntimePHP && runtime.Resource == constant.ResourceLocal {
phpPoolDir := path.Join(siteFolder, "php-pool")
if err := fileOp.CreateDir(phpPoolDir, 0755); err != nil {
return err
}
if err := fileOp.CreateFile(path.Join(phpPoolDir, "php-fpm.sock")); err != nil {
return err
}
}
}
if website.Type == constant.Static || website.Type == constant.Runtime {
@@ -117,6 +159,11 @@ func createWebsiteFolder(nginxInstall model.AppInstall, website *model.Website,
return err
}
}
if website.Type == constant.Proxy {
if err := createProxyFile(website, runtime); err != nil {
return err
}
}
}
return fileOp.CopyDir(path.Join(nginxFolder, "www", "common", "waf", "rules"), path.Join(siteFolder, "waf"))
}
@@ -161,9 +208,9 @@ func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain, a
server.UpdateRootProxy([]string{proxy})
case constant.Static:
server.UpdateRoot(rootIndex)
//server.UpdateRootLocation()
case constant.Proxy:
server.UpdateRootProxy([]string{website.Proxy})
nginxInclude := fmt.Sprintf("/www/sites/%s/proxy/*.conf", website.Alias)
server.UpdateDirective("include", []string{nginxInclude})
case constant.Runtime:
if runtime.Resource == constant.ResourceLocal {
switch runtime.Type {
@@ -186,7 +233,6 @@ func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain, a
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
return err
}
if err := opNginx(nginxInstall.ContainerName, constant.NginxCheck); err != nil {
_ = deleteWebsiteFolder(nginxInstall, website)
return err
@@ -255,7 +301,7 @@ func addListenAndServerName(website model.Website, ports []int, domains []string
return nginxCheckAndReload(nginxConfig.OldContent, nginxConfig.FilePath, nginxFull.Install.ContainerName)
}
func deleteListenAndServerName(website model.Website, ports []int, domains []string) error {
func deleteListenAndServerName(website model.Website, binds []string, domains []string) error {
nginxFull, err := getNginxFull(&website)
if err != nil {
return nil
@@ -263,8 +309,8 @@ func deleteListenAndServerName(website model.Website, ports []int, domains []str
nginxConfig := nginxFull.SiteConfig
config := nginxFull.SiteConfig.Config
server := config.FindServers()[0]
for _, port := range ports {
server.DeleteListen(strconv.Itoa(port))
for _, bind := range binds {
server.DeleteListen(bind)
}
for _, domain := range domains {
server.DeleteServerName(domain)
@@ -325,11 +371,12 @@ func applySSL(website model.Website, websiteSSL model.WebsiteSSL, req request.We
}
config := nginxFull.SiteConfig.Config
server := config.FindServers()[0]
server.UpdateListen("443", false, "ssl")
server.UpdateListen("443", website.DefaultServer, "ssl")
switch req.HttpConfig {
case constant.HTTPSOnly:
server.RemoveListenByBind("80")
server.RemoveListenByBind("[::]:80")
server.RemoveDirective("if", []string{"($scheme"})
case constant.HTTPToHTTPS:
server.UpdateListen("80", website.DefaultServer)
@@ -447,16 +494,32 @@ func opWebsite(website *model.Website, operate string) error {
}
server := servers[0]
if operate == constant.StopWeb {
if website.Type == constant.Deployment || website.Type == constant.Static || website.Type == constant.Proxy {
server.RemoveDirective("location", []string{"", "/"})
}
if website.Type == constant.Runtime {
proxyInclude := fmt.Sprintf("/www/sites/%s/proxy/*.conf", website.Alias)
server.RemoveDirective("include", []string{proxyInclude})
rewriteInclude := fmt.Sprintf("/www/sites/%s/rewrite/%s.conf", website.Alias, website.Alias)
server.RemoveDirective("include", []string{rewriteInclude})
switch website.Type {
case constant.Deployment:
server.RemoveDirective("location", []string{"/"})
case constant.Runtime:
server.RemoveDirective("location", []string{"~", "[^/]\\.php(/|$)"})
}
server.UpdateRoot("/usr/share/nginx/html/stop")
website.Status = constant.WebStopped
}
if operate == constant.StartWeb {
proxyInclude := fmt.Sprintf("/www/sites/%s/proxy/*.conf", website.Alias)
absoluteIncludeDir := path.Join(nginxInstall.Install.GetPath(), fmt.Sprintf("/www/sites/%s/proxy", website.Alias))
if files.NewFileOp().Stat(absoluteIncludeDir) {
server.UpdateDirective("include", []string{proxyInclude})
}
server.UpdateDirective("include", []string{proxyInclude})
rewriteInclude := fmt.Sprintf("/www/sites/%s/rewrite/%s.conf", website.Alias, website.Alias)
absoluteRewritePath := path.Join(nginxInstall.Install.GetPath(), rewriteInclude)
if files.NewFileOp().Stat(absoluteRewritePath) {
server.UpdateDirective("include", []string{rewriteInclude})
}
switch website.Type {
case constant.Deployment:
server.RemoveDirective("root", nil)
@@ -471,7 +534,6 @@ func opWebsite(website *model.Website, operate string) error {
server.UpdateRootLocation()
case constant.Proxy:
server.RemoveDirective("root", nil)
server.UpdateRootProxy([]string{website.Proxy})
case constant.Runtime:
rootIndex := path.Join("/www/sites", website.Alias, "index")
server.UpdateRoot(rootIndex)
@@ -505,3 +567,11 @@ func checkIsLinkApp(website model.Website) bool {
}
return false
}
func chownRootDir(path string) error {
_, err := cmd.ExecWithTimeOut(fmt.Sprintf("chown -R 1000:1000 %s", path), 1*time.Second)
if err != nil {
return err
}
return nil
}
+19 -14
View File
@@ -1,18 +1,23 @@
package configs
type System struct {
Port string `mapstructure:"port"`
DbFile string `mapstructure:"db_file"`
DbPath string `mapstructure:"db_path"`
LogPath string `mapstructure:"log_path"`
DataDir string `mapstructure:"data_dir"`
TmpDir string `mapstructure:"tmp_dir"`
Cache string `mapstructure:"cache"`
Backup string `mapstructure:"backup"`
EncryptKey string `mapstructure:"encrypt_key"`
BaseDir string `mapstructure:"base_dir"`
Mode string `mapstructure:"mode"`
RepoUrl string `mapstructure:"repo_url"`
Version string `mapstructure:"version"`
IsDemo bool `mapstructure:"is_demo"`
Port string `mapstructure:"port"`
SSL string `mapstructure:"ssl"`
DbFile string `mapstructure:"db_file"`
DbPath string `mapstructure:"db_path"`
LogPath string `mapstructure:"log_path"`
DataDir string `mapstructure:"data_dir"`
TmpDir string `mapstructure:"tmp_dir"`
Cache string `mapstructure:"cache"`
Backup string `mapstructure:"backup"`
EncryptKey string `mapstructure:"encrypt_key"`
BaseDir string `mapstructure:"base_dir"`
Mode string `mapstructure:"mode"`
RepoUrl string `mapstructure:"repo_url"`
Version string `mapstructure:"version"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
Entrance string `mapstructure:"entrance"`
IsDemo bool `mapstructure:"is_demo"`
ChangeUserInfo bool `mapstructure:"change_user_info"`
}
+12 -4
View File
@@ -46,6 +46,7 @@ var (
ErrTypeNotSafety = "ErrNotSafety"
ErrNameIsExist = "ErrNameIsExist"
ErrDemoEnvironment = "ErrDemoEnvironment"
ErrInitUser = "ErrInitUser"
)
// app
@@ -65,10 +66,12 @@ var (
// website
var (
ErrDomainIsExist = "ErrDomainIsExist"
ErrAliasIsExist = "ErrAliasIsExist"
ErrAppDelete = "ErrAppDelete"
ErrGroupIsUsed = "ErrGroupIsUsed"
ErrDomainIsExist = "ErrDomainIsExist"
ErrAliasIsExist = "ErrAliasIsExist"
ErrAppDelete = "ErrAppDelete"
ErrGroupIsUsed = "ErrGroupIsUsed"
ErrUsernameIsExist = "ErrUsernameIsExist"
ErrUsernameIsNotExist = "ErrUsernameIsNotExist"
)
// ssl
@@ -104,6 +107,7 @@ var (
var (
ErrInUsed = "ErrInUsed"
ErrObjectInUsed = "ErrObjectInUsed"
ErrPortRules = "ErrPortRules"
)
// runtime
@@ -114,3 +118,7 @@ var (
ErrImageExist = "ErrImageExist"
ErrDelWithWebsite = "ErrDelWithWebsite"
)
var (
ErrBackupInUsed = "ErrBackupInUsed"
)
+3 -2
View File
@@ -5,8 +5,9 @@ const (
NginxScopeHttp = "http"
NginxScopeOut = "out"
NginxReload = "reload"
NginxCheck = "check"
NginxReload = "reload"
NginxCheck = "check"
NginxRestart = "restart"
ConfigNew = "add"
ConfigUpdate = "update"
+1 -1
View File
@@ -5,7 +5,7 @@ const (
SessionName = "psession"
AuthMethodJWT = "jwt"
JWTHeaderName = "Authorization"
JWTHeaderName = "PanelAuthorization"
JWTBufferTime = 3600
JWTIssuer = "1Panel"
+1
View File
@@ -9,4 +9,5 @@ const (
StatusUploading = "Uploading"
StatusEnable = "Enable"
StatusDisable = "Disable"
StatusNone = "None"
)
+5 -7
View File
@@ -9,22 +9,20 @@ import (
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/cron/job"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/robfig/cron/v3"
)
func Run() {
nyc, _ := time.LoadLocation("Asia/Shanghai")
nyc, _ := time.LoadLocation(common.LoadTimeZone())
Cron := cron.New(cron.WithLocation(nyc), cron.WithChain(cron.Recover(cron.DefaultLogger)), cron.WithChain(cron.DelayIfStillRunning(cron.DefaultLogger)))
_, err := Cron.AddJob("@every 5m", job.NewMonitorJob())
if err != nil {
if _, err := Cron.AddJob("@every 5m", job.NewMonitorJob()); err != nil {
global.LOG.Errorf("can not add monitor corn job: %s", err.Error())
}
_, err = Cron.AddJob("@daily", job.NewWebsiteJob())
if err != nil {
if _, err := Cron.AddJob("@daily", job.NewWebsiteJob()); err != nil {
global.LOG.Errorf("can not add website corn job: %s", err.Error())
}
_, err = Cron.AddJob("@daily", job.NewSSLJob())
if err != nil {
if _, err := Cron.AddJob("@daily", job.NewSSLJob()); err != nil {
global.LOG.Errorf("can not add ssl corn job: %s", err.Error())
}
Cron.Start()
+3 -1
View File
@@ -4,6 +4,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/repo"
"github.com/1Panel-dev/1Panel/backend/app/service"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"time"
)
@@ -18,13 +19,14 @@ func (ssl *ssl) Run() {
sslRepo := repo.NewISSLRepo()
sslService := service.NewIWebsiteSSLService()
sslList, _ := sslRepo.List()
nyc, _ := time.LoadLocation(common.LoadTimeZone())
global.LOG.Info("The scheduled certificate update task is currently in progress ...")
now := time.Now().Add(10 * time.Second)
for _, s := range sslList {
if !s.AutoRenew || s.Provider == "manual" || s.Provider == "dnsManual" {
continue
}
expireDate := s.ExpireDate.In(time.Now().Location())
expireDate := s.ExpireDate.In(nyc)
sub := expireDate.Sub(now)
if sub.Hours() < 720 {
global.LOG.Errorf("Update the SSL certificate for the [%s] domain", s.PrimaryDomain)
+15 -7
View File
@@ -7,6 +7,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/service"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"sync"
"time"
)
@@ -18,9 +19,10 @@ func NewWebsiteJob() *website {
}
func (w *website) Run() {
nyc, _ := time.LoadLocation(common.LoadTimeZone())
websites, _ := repo.NewIWebsiteRepo().List()
global.LOG.Info("website cron job start...")
now := time.Now().Add(10 * time.Second)
global.LOG.Info("Website scheduled task in progress ...")
now := time.Now().Add(10 * time.Minute)
if len(websites) > 0 {
neverExpireDate, _ := time.Parse(constant.DateLayout, constant.DefaultDate)
var wg sync.WaitGroup
@@ -28,27 +30,33 @@ func (w *website) Run() {
if site.Status != constant.WebRunning || neverExpireDate.Equal(site.ExpireDate) {
continue
}
expireDate, _ := time.ParseInLocation(constant.DateTimeLayout, site.ExpireDate.String(), time.Now().Location())
expireDate, err := time.ParseInLocation(constant.DateLayout, site.ExpireDate.Format(constant.DateLayout), nyc)
if err != nil {
global.LOG.Errorf("time parse err %v", err)
continue
}
if expireDate.Before(now) {
wg.Add(1)
go func(ws model.Website) {
stopWebsite(ws.ID, &wg)
stopWebsite(ws.ID, ws.PrimaryDomain, &wg)
}(site)
}
}
wg.Wait()
}
global.LOG.Info("website cron job end...")
global.LOG.Info("Website scheduled task has completed")
}
func stopWebsite(websiteId uint, wg *sync.WaitGroup) {
func stopWebsite(websiteId uint, websiteName string, wg *sync.WaitGroup) {
websiteService := service.NewIWebsiteService()
req := request.WebsiteOp{
ID: websiteId,
Operate: constant.StopWeb,
}
if err := websiteService.OpWebsite(req); err != nil {
global.LOG.Errorf("stop website err: %s", err.Error())
global.LOG.Errorf("Website [%s] seop failed err %v", websiteName, err)
} else {
global.LOG.Infof("Website [%s] stopped successfully", websiteName)
}
wg.Done()
}
+8 -1
View File
@@ -17,6 +17,7 @@ ErrRepoNotValid: "Remote repository verification failed!"
ErrNameIsExist: "Name is already exist"
ErrDemoEnvironment: "Demo server, prohibit this operation!"
ErrCmdTimeout: "Command execution timed out!"
ErrInitUser: "The user initialization has been completed by the system. Please do not repeat the operation!"
#app
ErrPortInUsed: "{{ .detail }} port already in use"
@@ -43,6 +44,8 @@ ErrDomainIsExist: "Domain is already exist"
ErrAliasIsExist: "Alias is already exist"
ErrAppDelete: 'Other Website use this App'
ErrGroupIsUsed: 'The group is in use and cannot be deleted'
ErrUsernameIsExist: 'Username is already exist'
ErrUsernameIsNotExist: 'Username is not exist'
#ssl
ErrSSLCannotDelete: "The certificate is being used by the website and cannot be removed"
@@ -61,10 +64,14 @@ ErrTypeOfRedis: "The recovery file type does not match the current persistence m
#container
ErrInUsed: "{{ .detail }} is in use and cannot be deleted"
ErrObjectInUsed: "This object is in use and cannot be deleted"
ErrPortRules: "Invalid port specification"
#runtime
ErrDirNotFound: "The build folder does not exist! Please check file integrity!"
ErrFileNotExist: "{{ .detail }} file does not exist! Please check source file integrity!"
ErrImageBuildErr: "Image build failed"
ErrImageExist: "Image is already exist!"
ErrDelWithWebsite: "The operating environment has been associated with a website and cannot be deleted"
ErrDelWithWebsite: "The operating environment has been associated with a website and cannot be deleted"
#setting
ErrBackupInUsed: "The backup account is already being used in a cronjob and cannot be deleted."
+8 -1
View File
@@ -17,6 +17,7 @@ ErrRepoNotValid: "远程仓库校验失败!"
ErrNameIsExist: "名称已存在"
ErrDemoEnvironment: "演示服务器,禁止此操作!"
ErrCmdTimeout: "命令执行超时!"
ErrInitUser: "系统已完成用户初始化,请勿重复操作!"
#app
ErrPortInUsed: "{{ .detail }} 端口已被占用!"
@@ -43,6 +44,8 @@ ErrDomainIsExist: "域名已存在"
ErrAliasIsExist: "代号已存在"
ErrAppDelete: '其他网站使用此应用,无法删除'
ErrGroupIsUsed: '分组正在使用中,无法删除'
ErrUsernameIsExist: '用户名已存在'
ErrUsernameIsNotExist: '用户不存在'
#ssl
ErrSSLCannotDelete: "证书正在被网站使用,无法删除"
@@ -61,10 +64,14 @@ ErrTypeOfRedis: "恢复文件类型与当前持久化方式不符,请修改后
#container
ErrInUsed: "{{ .detail }} 正被使用,无法删除"
ErrObjectInUsed: "该对象正被使用,无法删除"
ErrPortRules: "无效的端口规格"
#runtime
ErrDirNotFound: "build 文件夹不存在!请检查文件完整性!"
ErrFileNotExist: "{{ .detail }} 文件不存在!请检查源文件完整性!"
ErrImageBuildErr: "镜像 build 失败"
ErrImageExist: "镜像已存在!"
ErrDelWithWebsite: "运行环境已经关联网站,无法删除"
ErrDelWithWebsite: "运行环境已经关联网站,无法删除"
#setting
ErrBackupInUsed: "该备份账号已在计划任务中使用,无法删除"
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"fmt"
"time"
"github.com/dgraph-io/badger/v3"
"github.com/dgraph-io/badger/v4"
"github.com/pkg/errors"
)
+1 -1
View File
@@ -5,7 +5,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/init/cache/badger_db"
"github.com/dgraph-io/badger/v3"
"github.com/dgraph-io/badger/v4"
)
func Init() {
+28 -1
View File
@@ -3,6 +3,9 @@ package hook
import (
"github.com/1Panel-dev/1Panel/backend/app/repo"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/encrypt"
)
func Init() {
@@ -17,11 +20,35 @@ func Init() {
global.LOG.Errorf("load service encrypt key from setting failed, err: %v", err)
}
global.CONF.System.EncryptKey = enptrySetting.Value
sslSetting, err := settingRepo.Get(settingRepo.WithByKey("SSL"))
if err != nil {
global.LOG.Errorf("load service ssl from setting failed, err: %v", err)
}
global.CONF.System.SSL = sslSetting.Value
if _, err := settingRepo.Get(settingRepo.WithByKey("SystemStatus")); err != nil {
_ = settingRepo.Create("SystemStatus", "Free")
}
if err := settingRepo.Update("SystemStatus", "Free"); err != nil {
global.LOG.Fatalf("init service before start failed, err: %v", "Free")
global.LOG.Fatalf("init service before start failed, err: %v", err)
}
if global.CONF.System.ChangeUserInfo {
if err := settingRepo.Update("UserName", common.RandStrAndNum(10)); err != nil {
global.LOG.Fatalf("init username before start failed, err: %v", err)
}
pass, _ := encrypt.StringEncrypt(common.RandStrAndNum(10))
if err := settingRepo.Update("Password", pass); err != nil {
global.LOG.Fatalf("init password before start failed, err: %v", err)
}
if err := settingRepo.Update("SecurityEntrance", common.RandStrAndNum(10)); err != nil {
global.LOG.Fatalf("init entrance before start failed, err: %v", err)
}
if cmd.HasNoPasswordSudo() {
_, _ = cmd.Exec("sudo sed -i '/CHANGE_USER_INFO=true/d' /usr/local/bin/1pctl")
} else {
_, _ = cmd.Exec("sed -i '/CHANGE_USER_INFO=true/d' /usr/local/bin/1pctl")
}
}
}
+1
View File
@@ -25,6 +25,7 @@ func Init() {
migrations.UpdateTableApp,
migrations.UpdateTableHost,
migrations.UpdateTableWebsite,
migrations.AddEntranceAndSSL,
})
if err := m.Migrate(); err != nil {
global.LOG.Error(err)
+36 -5
View File
@@ -8,6 +8,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/encrypt"
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
@@ -61,10 +62,13 @@ var AddTableSetting = &gormigrate.Migration{
if err := tx.AutoMigrate(&model.Setting{}); err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "UserName", Value: ""}).Error; err != nil {
encryptKey := common.RandStr(16)
if err := tx.Create(&model.Setting{Key: "UserName", Value: global.CONF.System.Username}).Error; err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "Password", Value: ""}).Error; err != nil {
global.CONF.System.EncryptKey = encryptKey
pass, _ := encrypt.StringEncrypt(global.CONF.System.Password)
if err := tx.Create(&model.Setting{Key: "Password", Value: pass}).Error; err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "Email", Value: ""}).Error; err != nil {
@@ -91,13 +95,13 @@ var AddTableSetting = &gormigrate.Migration{
if err := tx.Create(&model.Setting{Key: "ServerPort", Value: global.CONF.System.Port}).Error; err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "SecurityEntrance", Value: "onepanel"}).Error; err != nil {
if err := tx.Create(&model.Setting{Key: "SecurityEntrance", Value: global.CONF.System.Entrance}).Error; err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "JWTSigningKey", Value: common.RandStr(16)}).Error; err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "EncryptKey", Value: common.RandStr(16)}).Error; err != nil {
if err := tx.Create(&model.Setting{Key: "EncryptKey", Value: encryptKey}).Error; err != nil {
return err
}
@@ -276,8 +280,35 @@ var UpdateTableHost = &gormigrate.Migration{
}
var UpdateTableWebsite = &gormigrate.Migration{
ID: "20230406-update-table-website",
ID: "20230418-update-table-website",
Migrate: func(tx *gorm.DB) error {
if err := tx.AutoMigrate(&model.Website{}); err != nil {
return err
}
if err := tx.Model(&model.Website{}).Where("1 = 1").Update("site_dir", "/").Error; err != nil {
return err
}
return nil
},
}
var AddEntranceAndSSL = &gormigrate.Migration{
ID: "20230414-add-entrance-and-ssl",
Migrate: func(tx *gorm.DB) error {
if err := tx.Model(&model.Setting{}).
Where("key = ? AND value = ?", "SecurityEntrance", "onepanel").
Updates(map[string]interface{}{"value": ""}).Error; err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "SSLType", Value: "self"}).Error; err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "SSLID", Value: "0"}).Error; err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "SSL", Value: "disable"}).Error; err != nil {
return err
}
return tx.AutoMigrate(&model.Website{})
},
}
+2 -17
View File
@@ -1,11 +1,11 @@
package router
import (
"github.com/gin-contrib/gzip"
"html/template"
"net/http"
v1 "github.com/1Panel-dev/1Panel/backend/app/api/v1"
"github.com/gin-contrib/gzip"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/i18n"
"github.com/1Panel-dev/1Panel/backend/middleware"
@@ -39,46 +39,31 @@ func setWebStatic(rootRouter *gin.Engine) {
func Routers() *gin.Engine {
Router := gin.Default()
docs.SwaggerInfo.BasePath = "/api/v1"
Router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
Router.Use(middleware.OperationLog())
// Router.Use(middleware.CSRF())
// Router.Use(middleware.LoadCsrfToken())
if global.CONF.System.IsDemo {
Router.Use(middleware.DemoHandle())
}
Router.Use(gzip.Gzip(gzip.DefaultCompression))
setWebStatic(Router)
Router.Use(i18n.GinI18nLocalize())
Router.GET("/api/v1/info", v1.ApiGroupApp.BaseApi.GetSafetyStatus)
Router.GET("/api/v1/:code", v1.ApiGroupApp.BaseApi.SafeEntrance)
Router.SetFuncMap(template.FuncMap{
"Localize": ginI18n.GetMessage,
})
Router.Use(middleware.JwtAuth())
systemRouter := rou.RouterGroupApp
swaggerRouter := Router.Group("1panel")
docs.SwaggerInfo.BasePath = "/api/v1"
swaggerRouter.Use(middleware.JwtAuth()).Use(middleware.SessionAuth()).GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
PublicGroup := Router.Group("")
{
PublicGroup.GET("/health", func(c *gin.Context) {
c.JSON(200, "ok")
})
}
PrivateGroup := Router.Group("/api/v1")
PrivateGroup.Use(middleware.GlobalLoading())
//PrivateGroup.Use(middleware.SafetyAuth())
{
systemRouter.InitBaseRouter(PrivateGroup)
systemRouter.InitDashboardRouter(PrivateGroup)
+40 -13
View File
@@ -21,6 +21,7 @@ func Init() {
port := "9999"
mode := ""
version := "v1.0.0"
username, password, entrance := "", "", ""
fileOp := files.NewFileOp()
v := viper.NewWithOptions()
v.SetConfigType("yaml")
@@ -40,8 +41,11 @@ func Init() {
}
} else {
baseDir = loadParams("BASE_DIR")
port = loadParams("PANEL_PORT")
version = loadParams("ORIGINAL_INSTALLED_VERSION")
port = loadParams("ORIGINAL_PORT")
version = loadParams("ORIGINAL_VERSION")
username = loadParams("ORIGINAL_USERNAME")
password = loadParams("ORIGINAL_PASSWORD")
entrance = loadParams("ORIGINAL_ENTRANCE")
if strings.HasSuffix(baseDir, "/") {
baseDir = baseDir[:strings.LastIndex(baseDir, "/")]
@@ -60,14 +64,25 @@ func Init() {
if err := v.Unmarshal(&serverConfig); err != nil {
panic(err)
}
if mode == "dev" && fileOp.Stat("/opt/1panel/conf/app.yaml") && serverConfig.System.BaseDir != "" {
baseDir = serverConfig.System.BaseDir
}
if mode == "dev" && fileOp.Stat("/opt/1panel/conf/app.yaml") && serverConfig.System.Port != "" {
port = serverConfig.System.Port
}
if mode == "dev" && fileOp.Stat("/opt/1panel/conf/app.yaml") && serverConfig.System.Version != "" {
version = serverConfig.System.Version
if mode == "dev" && fileOp.Stat("/opt/1panel/conf/app.yaml") {
if serverConfig.System.BaseDir != "" {
baseDir = serverConfig.System.BaseDir
}
if serverConfig.System.Port != "" {
port = serverConfig.System.Port
}
if serverConfig.System.Version != "" {
version = serverConfig.System.Version
}
if serverConfig.System.Username != "" {
username = serverConfig.System.Username
}
if serverConfig.System.Password != "" {
password = serverConfig.System.Password
}
if serverConfig.System.Entrance != "" {
entrance = serverConfig.System.Entrance
}
}
global.CONF = serverConfig
@@ -81,6 +96,10 @@ func Init() {
global.CONF.System.TmpDir = global.CONF.System.DataDir + "/tmp"
global.CONF.System.Port = port
global.CONF.System.Version = version
global.CONF.System.Username = username
global.CONF.System.Password = password
global.CONF.System.Entrance = entrance
global.CONF.System.ChangeUserInfo = loadChange()
global.Viper = v
}
@@ -89,9 +108,17 @@ func loadParams(param string) string {
if err != nil {
panic(err)
}
baseDir := strings.ReplaceAll(stdout, "\n", "")
if len(baseDir) == 0 {
info := strings.ReplaceAll(stdout, "\n", "")
if len(info) == 0 || info == `""` {
panic(fmt.Sprintf("error `%s` find in /usr/bin/1pctl", param))
}
return baseDir
return info
}
func loadChange() bool {
stdout, err := cmd.Exec("grep '^CHANGE_USER_INFO=' /usr/bin/1pctl | cut -d'=' -f2")
if err != nil {
return false
}
return stdout == "true\n"
}
+3 -1
View File
@@ -7,6 +7,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
"github.com/1Panel-dev/1Panel/backend/app/repo"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/gin-gonic/gin"
)
@@ -29,7 +30,8 @@ func PasswordExpired() gin.HandlerFunc {
helper.ErrorWithDetail(c, constant.CodePasswordExpired, constant.ErrTypePasswordExpired, err)
return
}
expiredTime, err := time.Parse("2006-01-02 15:04:05", extime.Value)
loc, _ := time.LoadLocation(common.LoadTimeZone())
expiredTime, err := time.ParseInLocation("2006-01-02 15:04:05", extime.Value, loc)
if err != nil {
helper.ErrorWithDetail(c, constant.CodePasswordExpired, constant.ErrTypePasswordExpired, err)
return
-18
View File
@@ -1,18 +0,0 @@
package middleware
import (
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
"github.com/1Panel-dev/1Panel/backend/app/service"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/gin-gonic/gin"
)
func SafetyAuth() gin.HandlerFunc {
return func(c *gin.Context) {
if err := service.NewIAuthService().SafetyStatus(c); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrUnSafety, constant.ErrTypeNotSafety, nil)
return
}
c.Next()
}
}
+1 -2
View File
@@ -14,8 +14,7 @@ func (s *BaseRouter) InitBaseRouter(Router *gin.RouterGroup) {
baseRouter.GET("/captcha", baseApi.Captcha)
baseRouter.POST("/mfalogin", baseApi.MFALogin)
baseRouter.POST("/login", baseApi.Login)
baseRouter.GET("/status", baseApi.CheckIsFirstLogin)
baseRouter.POST("/init", baseApi.InitUserInfo)
baseRouter.GET("/issafety", baseApi.CheckIsSafety)
baseRouter.POST("/logout", baseApi.LogOut)
baseRouter.GET("/demo", baseApi.CheckIsDemo)
}
+1
View File
@@ -21,6 +21,7 @@ func (f *FileRouter) InitFileRouter(Router *gin.RouterGroup) {
fileRouter.POST("/del", baseApi.DeleteFile)
fileRouter.POST("/batch/del", baseApi.BatchDeleteFile)
fileRouter.POST("/mode", baseApi.ChangeFileMode)
fileRouter.POST("/owner", baseApi.ChangeFileOwner)
fileRouter.POST("/compress", baseApi.CompressFile)
fileRouter.POST("/decompress", baseApi.DeCompressFile)
fileRouter.POST("/content", baseApi.GetContent)
+2
View File
@@ -23,6 +23,8 @@ func (s *SettingRouter) InitSettingRouter(Router *gin.RouterGroup) {
settingRouter.GET("/search/available", baseApi.GetSystemAvailable)
settingRouter.POST("/update", baseApi.UpdateSetting)
settingRouter.POST("/port/update", baseApi.UpdatePort)
settingRouter.POST("/ssl/update", baseApi.UpdateSSL)
settingRouter.GET("/ssl/info", baseApi.LoadFromCert)
settingRouter.POST("/password/update", baseApi.UpdatePassword)
settingRouter.POST("/time/sync", baseApi.SyncTime)
settingRouter.POST("/monitor/clean", baseApi.CleanMonitor)
+13
View File
@@ -45,5 +45,18 @@ func (a *WebsiteRouter) InitWebsiteRouter(Router *gin.RouterGroup) {
groupRouter.GET("/php/config/:id", baseApi.GetWebsitePHPConfig)
groupRouter.POST("/php/config", baseApi.UpdateWebsitePHPConfig)
groupRouter.POST("/php/update", baseApi.UpdatePHPFile)
groupRouter.POST("/rewrite", baseApi.GetRewriteConfig)
groupRouter.POST("/rewrite/update", baseApi.UpdateRewriteConfig)
groupRouter.POST("/dir/update", baseApi.UpdateSiteDir)
groupRouter.POST("/dir/permission", baseApi.UpdateSiteDirPermission)
groupRouter.POST("/proxies", baseApi.GetProxyConfig)
groupRouter.POST("/proxies/update", baseApi.UpdateProxyConfig)
groupRouter.POST("/proxies/file", baseApi.UpdateProxyConfigFile)
groupRouter.POST("/auths", baseApi.GetAuthConfig)
groupRouter.POST("/auths/update", baseApi.UpdateAuthConfig)
}
}
+38 -15
View File
@@ -1,8 +1,11 @@
package server
import (
"crypto/tls"
"encoding/gob"
"fmt"
"net/http"
"os"
"time"
"github.com/1Panel-dev/1Panel/backend/init/app"
@@ -43,22 +46,42 @@ func Start() {
rootRouter := router.Routers()
address := fmt.Sprintf(":%s", global.CONF.System.Port)
s := initServer(address, rootRouter)
global.LOG.Infof("server run success on %s", global.CONF.System.Port)
if err := s.ListenAndServe(); err != nil {
global.LOG.Error(err)
panic(err)
}
}
type server interface {
ListenAndServe() error
}
func initServer(address string, router *gin.Engine) server {
s := endless.NewServer(address, router)
s := endless.NewServer(address, rootRouter)
s.ReadHeaderTimeout = 20 * time.Second
s.WriteTimeout = 60 * time.Second
s.MaxHeaderBytes = 1 << 20
return s
if global.CONF.System.SSL == "disable" {
global.LOG.Infof("server run success on %s with http", global.CONF.System.Port)
if err := s.ListenAndServe(); err != nil {
global.LOG.Error(err)
panic(err)
}
} else {
certificate, err := os.ReadFile(global.CONF.System.BaseDir + "/1panel/secret/server.crt")
if err != nil {
panic(err)
}
key, err := os.ReadFile(global.CONF.System.BaseDir + "/1panel/secret/server.key")
if err != nil {
panic(err)
}
cert, err := tls.X509KeyPair(certificate, key)
if err != nil {
panic(err)
}
s := &http.Server{
Addr: address,
Handler: rootRouter,
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
},
}
global.LOG.Infof("server run success on %s with https", global.CONF.System.Port)
if err := s.ListenAndServeTLS("", ""); err != nil {
global.LOG.Error(err)
panic(err)
}
}
}
+4 -3
View File
@@ -67,10 +67,11 @@ func (cos cosClient) Exist(path string) (bool, error) {
if err != nil {
return false, err
}
if _, err := client.Object.IsExist(context.Background(), path); err != nil {
return true, err
exist, err := client.Object.IsExist(context.Background(), path)
if err != nil {
return false, err
}
return false, nil
return exist, nil
}
func (cos cosClient) Delete(path string) (bool, error) {
+2 -2
View File
@@ -59,9 +59,9 @@ func (kodo kodoClient) Exist(path string) (bool, error) {
return false, err
}
if _, err := kodo.client.Stat(bucket, path); err != nil {
return true, err
return false, err
}
return false, nil
return true, nil
}
func (kodo kodoClient) Delete(path string) (bool, error) {
+2 -2
View File
@@ -81,9 +81,9 @@ func (minIo minIoClient) Exist(path string) (bool, error) {
if _, ok := minIo.Vars["bucket"]; ok {
_, err := minIo.client.GetObject(context.Background(), minIo.Vars["bucket"].(string), path, minio.GetObjectOptions{})
if err != nil {
return true, err
return false, err
}
return false, nil
return true, nil
} else {
return false, constant.ErrInvalidParams
}
@@ -1,40 +0,0 @@
package client
import (
"encoding/json"
"fmt"
"testing"
"github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/init/db"
"github.com/1Panel-dev/1Panel/backend/init/log"
"github.com/1Panel-dev/1Panel/backend/init/viper"
)
func TestMinio(t *testing.T) {
viper.Init()
log.Init()
db.Init()
var backup model.BackupAccount
if err := global.DB.Where("id = ?", 3).First(&backup).Error; err != nil {
fmt.Println(err)
}
varMap := make(map[string]interface{})
if err := json.Unmarshal([]byte(backup.Vars), &varMap); err != nil {
fmt.Println(err)
}
varMap["type"] = backup.Type
varMap["bucket"] = backup.Bucket
switch backup.Type {
case constant.Sftp:
varMap["password"] = backup.Credential
case constant.OSS, constant.S3, constant.MinIo:
varMap["secretKey"] = backup.Credential
}
client, _ := NewMinIoClient(varMap)
_, _ = client.ListObjects("directory/directory-test-minio/")
}
+35 -2
View File
@@ -4,10 +4,11 @@ import (
"bytes"
"context"
"fmt"
"github.com/1Panel-dev/1Panel/backend/buserr"
"github.com/1Panel-dev/1Panel/backend/constant"
"os/exec"
"time"
"github.com/1Panel-dev/1Panel/backend/buserr"
"github.com/1Panel-dev/1Panel/backend/constant"
)
func Exec(cmdStr string) (string, error) {
@@ -66,6 +67,32 @@ func ExecWithTimeOut(cmdStr string, timeout time.Duration) (string, error) {
return stdout.String(), nil
}
func ExecCronjobWithTimeOut(cmdStr string, timeout time.Duration) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
cmd := exec.Command("bash", "-c", cmdStr)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if ctx.Err() == context.DeadlineExceeded {
return "", buserr.New(constant.ErrCmdTimeout)
}
errMsg := ""
if len(stderr.String()) != 0 {
errMsg = fmt.Sprintf("stderr:\n %s", stderr.String())
}
if len(stdout.String()) != 0 {
if len(errMsg) != 0 {
errMsg = fmt.Sprintf("%s \n\n; stdout:\n %s", errMsg, stdout.String())
} else {
errMsg = fmt.Sprintf("stdout\n: %s", stdout.String())
}
}
return errMsg, err
}
func Execf(cmdStr string, a ...interface{}) (string, error) {
cmd := exec.Command("bash", "-c", fmt.Sprintf(cmdStr, a...))
var stdout, stderr bytes.Buffer
@@ -88,3 +115,9 @@ func Execf(cmdStr string, a ...interface{}) (string, error) {
}
return stdout.String(), nil
}
func HasNoPasswordSudo() bool {
cmd2 := exec.Command("sudo", "-n", "ls")
err2 := cmd2.Run()
return err2 == nil
}
+19
View File
@@ -11,6 +11,7 @@ import (
"sort"
"strconv"
"strings"
"time"
)
func CompareVersion(version1 string, version2 string) bool {
@@ -82,6 +83,16 @@ func RandStr(n int) string {
return string(b)
}
func RandStrAndNum(n int) string {
mathRand.Seed(time.Now().UnixNano())
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
b := make([]byte, 10)
for i := range b {
b[i] = charset[mathRand.Int63()%int64(len(charset))]
}
return (string(b))
}
func ScanPort(port int) bool {
ln, err := net.Listen("tcp", ":"+strconv.Itoa(port))
if err != nil {
@@ -134,3 +145,11 @@ func LoadSizeUnit(value float64) string {
}
return fmt.Sprintf("%v", value)
}
func LoadTimeZone() string {
loc := time.Now().Location()
if _, err := time.LoadLocation(loc.String()); err != nil {
return "Asia/Shanghai"
}
return loc.String()
}
+46 -7
View File
@@ -6,6 +6,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"io"
"io/fs"
"net/http"
@@ -106,7 +107,7 @@ func (f FileOp) SaveFile(dst string, content string, mode fs.FileMode) error {
}
defer file.Close()
write := bufio.NewWriter(file)
_, _ = write.WriteString(string(content))
_, _ = write.WriteString(content)
write.Flush()
return nil
}
@@ -119,6 +120,37 @@ func (f FileOp) Chown(dst string, uid int, gid int) error {
return f.Fs.Chown(dst, uid, gid)
}
func (f FileOp) ChownR(dst string, uid string, gid string, sub bool) error {
cmdStr := fmt.Sprintf("chown %s:%s %s", uid, gid, dst)
if sub {
cmdStr = fmt.Sprintf("chown -R %s:%s %s", uid, gid, dst)
}
if cmd.HasNoPasswordSudo() {
cmdStr = fmt.Sprintf("sudo %s", cmdStr)
}
if msg, err := cmd.ExecWithTimeOut(cmdStr, 2*time.Second); err != nil {
if msg != "" {
return errors.New(msg)
}
return err
}
return nil
}
func (f FileOp) ChmodR(dst string, mode int64) error {
cmdStr := fmt.Sprintf("chmod -R %v %s", fmt.Sprintf("%04o", mode), dst)
if cmd.HasNoPasswordSudo() {
cmdStr = fmt.Sprintf("sudo %s", cmdStr)
}
if msg, err := cmd.ExecWithTimeOut(cmdStr, 2*time.Second); err != nil {
if msg != "" {
return errors.New(msg)
}
return err
}
return nil
}
func (f FileOp) Rename(oldName string, newName string) error {
return f.Fs.Rename(oldName, newName)
}
@@ -322,17 +354,21 @@ func (f FileOp) CopyFile(src, dst string) error {
}
defer srcFile.Close()
srcInfo, err := f.Fs.Stat(src)
if err != nil {
return err
}
dstPath := path.Join(dst, srcInfo.Name())
if src == dstPath {
return nil
}
err = f.Fs.MkdirAll(filepath.Dir(dst), 0666)
if err != nil {
return err
}
srcInfo, err := f.Fs.Stat(src)
if err != nil {
return err
}
dstFile, err := f.Fs.OpenFile(path.Join(dst, srcInfo.Name()), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0775)
dstFile, err := f.Fs.OpenFile(dstPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0775)
if err != nil {
return err
}
@@ -446,6 +482,9 @@ func (f FileOp) Decompress(srcFile string, dst string, cType CompressType) error
handler := func(ctx context.Context, archFile archiver.File) error {
info := archFile.FileInfo
if strings.HasPrefix(archFile.NameInArchive, "__MACOSX") {
return nil
}
filePath := filepath.Join(dst, archFile.NameInArchive)
if archFile.FileInfo.IsDir() {
if err := f.Fs.MkdirAll(filePath, info.Mode()); err != nil {
+46 -38
View File
@@ -1,14 +1,17 @@
package files
import (
"bufio"
"fmt"
"github.com/1Panel-dev/1Panel/backend/buserr"
"github.com/1Panel-dev/1Panel/backend/constant"
"io/fs"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/spf13/afero"
@@ -73,6 +76,8 @@ func NewFileInfo(op FileOption) (*FileInfo, error) {
Extension: filepath.Ext(info.Name()),
IsHidden: IsHidden(op.Path),
Mode: fmt.Sprintf("%04o", info.Mode().Perm()),
User: GetUsername(info.Sys().(*syscall.Stat_t).Uid),
Group: GetGroup(info.Sys().(*syscall.Stat_t).Gid),
MimeType: GetMimeType(op.Path),
}
if file.IsSymlink {
@@ -93,35 +98,38 @@ func NewFileInfo(op FileOption) (*FileInfo, error) {
return file, nil
}
func (f *FileInfo) search(dir, showHidden bool, af afero.Afero, search string, count int) ([]FileSearchInfo, error) {
var files []FileSearchInfo
if err := afero.Walk(af, f.Path, func(path string, info fs.FileInfo, err error) error {
if info != nil {
if dir && !info.IsDir() {
return nil
}
if !showHidden && IsHidden(info.Name()) {
return nil
}
lowerName := strings.ToLower(info.Name())
lowerSearch := strings.ToLower(search)
if strings.Contains(lowerName, lowerSearch) {
files = append(files, FileSearchInfo{
Path: path,
FileInfo: info,
})
if len(files) > count {
return nil
}
}
}
return nil
}); err != nil {
return nil, err
func (f *FileInfo) search(search string, count int) (files []FileSearchInfo, total int, err error) {
cmd := exec.Command("find", f.Path, "-name", fmt.Sprintf("*%s*", search))
output, err := cmd.StdoutPipe()
if err != nil {
return
}
return files, nil
if err = cmd.Start(); err != nil {
return
}
defer cmd.Wait()
defer cmd.Process.Kill()
scanner := bufio.NewScanner(output)
for scanner.Scan() {
line := scanner.Text()
info, err := os.Stat(line)
if err != nil {
continue
}
total++
if total > count {
continue
}
files = append(files, FileSearchInfo{
Path: line,
FileInfo: info,
})
}
if err = scanner.Err(); err != nil {
return
}
return
}
func (f *FileInfo) listChildren(dir, showHidden, containSub bool, search string, page, pageSize int) error {
@@ -129,10 +137,11 @@ func (f *FileInfo) listChildren(dir, showHidden, containSub bool, search string,
var (
files []FileSearchInfo
err error
total int
)
if search != "" && containSub {
files, err = f.search(dir, showHidden, *afs, search, page*pageSize)
files, total, err = f.search(search, page*pageSize)
if err != nil {
return err
}
@@ -149,17 +158,13 @@ func (f *FileInfo) listChildren(dir, showHidden, containSub bool, search string,
}
}
f.ItemTotal = 0
var items []*FileInfo
for _, df := range files {
if dir && !df.IsDir() {
continue
}
name := df.Name()
fPath := path.Join(df.Path, df.Name())
if search != "" {
if containSub {
fPath = df.Path
@@ -172,12 +177,10 @@ func (f *FileInfo) listChildren(dir, showHidden, containSub bool, search string,
}
}
}
if !showHidden && IsHidden(name) {
continue
}
f.ItemTotal++
isSymlink, isInvalidLink := false, false
if IsSymlink(df.Mode()) {
isSymlink = true
@@ -201,19 +204,24 @@ func (f *FileInfo) listChildren(dir, showHidden, containSub bool, search string,
Extension: filepath.Ext(name),
Path: fPath,
Mode: fmt.Sprintf("%04o", df.Mode().Perm()),
MimeType: GetMimeType(fPath),
User: GetUsername(df.Sys().(*syscall.Stat_t).Uid),
Group: GetGroup(df.Sys().(*syscall.Stat_t).Gid),
}
if isSymlink {
file.LinkPath = GetSymlink(fPath)
}
if df.Size() > 0 {
file.MimeType = GetMimeType(fPath)
}
if isInvalidLink {
file.Type = "invalid_link"
}
items = append(items, file)
}
if containSub {
f.ItemTotal = total
}
start := (page - 1) * pageSize
end := pageSize + start
var result []*FileInfo
+29 -3
View File
@@ -1,10 +1,12 @@
package files
import (
"github.com/gabriel-vasile/mimetype"
"github.com/spf13/afero"
"net/http"
"os"
"os/user"
"path/filepath"
"strconv"
"sync"
)
@@ -13,11 +15,19 @@ func IsSymlink(mode os.FileMode) bool {
}
func GetMimeType(path string) string {
mime, err := mimetype.DetectFile(path)
file, err := os.Open(path)
if err != nil {
return ""
}
return mime.String()
defer file.Close()
buffer := make([]byte, 512)
_, err = file.Read(buffer)
if err != nil {
return ""
}
mimeType := http.DetectContentType(buffer)
return mimeType
}
func GetSymlink(path string) string {
@@ -28,6 +38,22 @@ func GetSymlink(path string) string {
return linkPath
}
func GetUsername(uid uint32) string {
usr, err := user.LookupId(strconv.Itoa(int(uid)))
if err != nil {
return ""
}
return usr.Username
}
func GetGroup(gid uint32) string {
usr, err := user.LookupGroupId(strconv.Itoa(int(gid)))
if err != nil {
return ""
}
return usr.Name
}
func ScanDir(fs afero.Fs, path string, dirMap *sync.Map, wg *sync.WaitGroup) {
afs := &afero.Afero{Fs: fs}
files, _ := afs.ReadDir(path)
+26 -17
View File
@@ -7,10 +7,18 @@ import (
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
)
type Ufw struct{}
type Ufw struct {
CmdStr string
}
func NewUfw() (*Ufw, error) {
return &Ufw{}, nil
var ufw Ufw
if cmd.HasNoPasswordSudo() {
ufw.CmdStr = "sudo ufw"
} else {
ufw.CmdStr = "ufw"
}
return &ufw, nil
}
func (f *Ufw) Name() string {
@@ -18,18 +26,19 @@ func (f *Ufw) Name() string {
}
func (f *Ufw) Status() (string, error) {
stdout, err := cmd.Exec("sudo ufw status | grep Status")
if err != nil {
return "", fmt.Errorf("load the firewall status failed, err: %s", stdout)
}
stdout, _ := cmd.Execf("%s status | grep Status", f.CmdStr)
if stdout == "Status: active\n" {
return "running", nil
}
stdout1, _ := cmd.Execf("%s status | grep 状态", f.CmdStr)
if stdout1 == "状态: 激活\n" {
return "running", nil
}
return "not running", nil
}
func (f *Ufw) Version() (string, error) {
stdout, err := cmd.Exec("sudo ufw version | grep ufw")
stdout, err := cmd.Execf("%s version | grep ufw", f.CmdStr)
if err != nil {
return "", fmt.Errorf("load the firewall status failed, err: %s", stdout)
}
@@ -38,7 +47,7 @@ func (f *Ufw) Version() (string, error) {
}
func (f *Ufw) Start() error {
stdout, err := cmd.Exec("echo y | sudo ufw enable")
stdout, err := cmd.Execf("echo y | %s enable", f.CmdStr)
if err != nil {
return fmt.Errorf("enable the firewall failed, err: %s", stdout)
}
@@ -46,7 +55,7 @@ func (f *Ufw) Start() error {
}
func (f *Ufw) Stop() error {
stdout, err := cmd.Exec("sudo ufw disable")
stdout, err := cmd.Execf("%s disable", f.CmdStr)
if err != nil {
return fmt.Errorf("stop the firewall failed, err: %s", stdout)
}
@@ -58,7 +67,7 @@ func (f *Ufw) Reload() error {
}
func (f *Ufw) ListPort() ([]FireInfo, error) {
stdout, err := cmd.Exec("sudo ufw status verbose")
stdout, err := cmd.Execf("%s status verbose", f.CmdStr)
if err != nil {
return nil, err
}
@@ -66,7 +75,7 @@ func (f *Ufw) ListPort() ([]FireInfo, error) {
var datas []FireInfo
isStart := false
for _, line := range portInfos {
if strings.HasPrefix(line, "--") {
if strings.HasPrefix(line, "-") {
isStart = true
continue
}
@@ -83,7 +92,7 @@ func (f *Ufw) ListPort() ([]FireInfo, error) {
}
func (f *Ufw) ListAddress() ([]FireInfo, error) {
stdout, err := cmd.Exec("sudo ufw status verbose")
stdout, err := cmd.Execf("%s status verbose", f.CmdStr)
if err != nil {
return nil, err
}
@@ -91,7 +100,7 @@ func (f *Ufw) ListAddress() ([]FireInfo, error) {
var datas []FireInfo
isStart := false
for _, line := range portInfos {
if strings.HasPrefix(line, "--") {
if strings.HasPrefix(line, "-") {
isStart = true
continue
}
@@ -123,9 +132,9 @@ func (f *Ufw) Port(port FireInfo, operation string) error {
return fmt.Errorf("unsupport strategy %s", port.Strategy)
}
command := fmt.Sprintf("sudo ufw %s %s", port.Strategy, port.Port)
command := fmt.Sprintf("%s %s %s", f.CmdStr, port.Strategy, port.Port)
if operation == "remove" {
command = fmt.Sprintf("sudo ufw delete %s %s", port.Strategy, port.Port)
command = fmt.Sprintf("%s delete %s %s", f.CmdStr, port.Strategy, port.Port)
}
if len(port.Protocol) != 0 {
command += fmt.Sprintf("/%s", port.Protocol)
@@ -147,9 +156,9 @@ func (f *Ufw) RichRules(rule FireInfo, operation string) error {
return fmt.Errorf("unsupport strategy %s", rule.Strategy)
}
ruleStr := fmt.Sprintf("sudo ufw %s ", rule.Strategy)
ruleStr := fmt.Sprintf("%s %s ", f.CmdStr, rule.Strategy)
if operation == "remove" {
ruleStr = fmt.Sprintf("sudo ufw delete %s ", rule.Strategy)
ruleStr = fmt.Sprintf("%s delete %s ", f.CmdStr, rule.Strategy)
}
if len(rule.Protocol) != 0 {
ruleStr += fmt.Sprintf("proto %s ", rule.Protocol)
-35
View File
@@ -62,41 +62,6 @@ func (b *Block) UpdateDirective(key string, params []string) {
b.Directives = directives
}
//func (b *Block) UpdateDirectiveBySecondKey(name string, key string, directive Directive) {
//
// directives := b.GetDirectives()
//
// index := -1
// for i, dir := range directives {
// if dir.GetName() == name && dir.GetParameters()[0] == key {
// index = i
// break
// }
// }
// if index > -1 {
// directives[index] = &directive
// } else {
// directives = append(directives, &directive)
// }
// b.Directives = directives
//}
//func (b *Block) RemoveDirectives(names []string) {
// nameMaps := make(map[string]struct{}, len(names))
// for _, name := range names {
// nameMaps[name] = struct{}{}
// }
// directives := b.GetDirectives()
// var newDirectives []IDirective
// for _, dir := range directives {
// if _, ok := nameMaps[dir.GetName()]; ok {
// continue
// }
// newDirectives = append(newDirectives, dir)
// }
// b.Directives = newDirectives
//}
func (b *Block) RemoveDirective(key string, params []string) {
directives := b.GetDirectives()
var newDirectives []IDirective
+2
View File
@@ -45,6 +45,8 @@ var repeatKeys = map[string]struct {
"if": {},
"proxy_set_header": {},
"location": {},
"include": {},
"sub_filter": {},
}
func IsRepeatKey(key string) bool {
+229 -21
View File
@@ -1,32 +1,240 @@
package components
import (
"fmt"
"regexp"
"strconv"
"strings"
)
type Location struct {
*Directive
Modifier string
Match string
Modifier string
Match string
Cache bool
ProxyPass string
Host string
CacheTime int
CacheUint string
Comment string
Directives []IDirective
Line int
Parameters []string
Replaces map[string]string
}
func NewLocation(directive *Directive) *Location {
func NewLocation(directive IDirective) *Location {
location := &Location{
Modifier: "",
Match: "",
Directive: directive,
Modifier: "",
Match: "",
}
if directive.GetBlock() != nil {
directive.Comment = directive.GetBlock().GetComment()
}
if len(directive.Parameters) == 0 {
directives := make([]IDirective, 0)
if len(directive.GetParameters()) == 0 {
panic("no enough parameter for location")
}
if len(directive.Parameters) == 1 {
location.Match = directive.Parameters[0]
return location
} else if len(directive.Parameters) == 2 {
location.Match = directive.Parameters[1]
location.Modifier = directive.Parameters[0]
return location
for _, dir := range directive.GetBlock().GetDirectives() {
directives = append(directives, dir)
params := dir.GetParameters()
switch dir.GetName() {
case "proxy_pass":
location.ProxyPass = params[0]
case "proxy_set_header":
if params[0] == "Host" {
location.Host = params[1]
}
case "proxy_cache":
location.Cache = true
case "if":
if params[0] == "(" && params[1] == "$uri" && params[2] == "~*" {
dirs := dir.GetBlock().GetDirectives()
for _, di := range dirs {
if di.GetName() == "expires" {
re := regexp.MustCompile(`^(\d+)(\w+)$`)
matches := re.FindStringSubmatch(di.GetParameters()[0])
if matches == nil {
continue
}
cacheTime, err := strconv.Atoi(matches[1])
if err != nil {
continue
}
unit := matches[2]
location.CacheUint = unit
location.CacheTime = cacheTime
}
}
}
case "sub_filter":
if location.Replaces == nil {
location.Replaces = make(map[string]string, 0)
}
location.Replaces[strings.Trim(params[0], "\"")] = strings.Trim(params[1], "\"")
}
}
return nil
params := directive.GetParameters()
if len(params) == 1 {
location.Match = params[0]
} else if len(params) == 2 {
location.Match = params[1]
location.Modifier = params[0]
}
location.Parameters = directive.GetParameters()
location.Line = directive.GetLine()
location.Comment = directive.GetComment()
location.Directives = directives
return location
}
func (l *Location) GetName() string {
return "location"
}
func (l *Location) GetParameters() []string {
return l.Parameters
}
func (l *Location) GetBlock() IBlock {
return l
}
func (l *Location) GetComment() string {
return l.Comment
}
func (l *Location) GetLine() int {
return l.Line
}
func (l *Location) GetDirectives() []IDirective {
return l.Directives
}
func (l *Location) FindDirectives(directiveName string) []IDirective {
directives := make([]IDirective, 0)
for _, directive := range l.Directives {
if directive.GetName() == directiveName {
directives = append(directives, directive)
}
if directive.GetBlock() != nil {
directives = append(directives, directive.GetBlock().FindDirectives(directiveName)...)
}
}
return directives
}
func (l *Location) UpdateDirective(key string, params []string) {
if key == "" || len(params) == 0 {
return
}
directives := l.Directives
index := -1
for i, dir := range directives {
if dir.GetName() == key {
if IsRepeatKey(key) {
oldParams := dir.GetParameters()
if !(len(oldParams) > 0 && oldParams[0] == params[0]) {
continue
}
}
index = i
break
}
}
newDirective := &Directive{
Name: key,
Parameters: params,
}
if index > -1 {
directives[index] = newDirective
} else {
directives = append(directives, newDirective)
}
l.Directives = directives
}
func (l *Location) RemoveDirective(key string, params []string) {
directives := l.Directives
var newDirectives []IDirective
for _, dir := range directives {
if dir.GetName() == key {
if len(params) > 0 {
oldParams := dir.GetParameters()
if oldParams[0] == params[0] {
continue
}
} else {
continue
}
}
newDirectives = append(newDirectives, dir)
}
l.Directives = newDirectives
}
func (l *Location) ChangePath(Modifier string, Match string) {
if Match != "" && Modifier != "" {
l.Parameters = []string{Modifier, Match}
}
if Match != "" && Modifier == "" {
l.Parameters = []string{Match}
}
l.Modifier = Modifier
l.Match = Match
}
func (l *Location) AddCache(cacheTime int, cacheUint string) {
l.RemoveDirective("add_header", []string{"Cache-Control", "no-cache"})
l.RemoveDirective("if", []string{"(", "$uri", "~*", `"\.(gif|png|jpg|css|js|woff|woff2)$"`, ")"})
directives := l.GetDirectives()
newDir := &Directive{
Name: "if",
Parameters: []string{"(", "$uri", "~*", `"\.(gif|png|jpg|css|js|woff|woff2)$"`, ")"},
Block: &Block{},
}
block := &Block{}
block.Directives = append(block.Directives, &Directive{
Name: "expires",
Parameters: []string{strconv.Itoa(cacheTime) + cacheUint},
})
newDir.Block = block
directives = append(directives, newDir)
l.Directives = directives
l.UpdateDirective("proxy_ignore_headers", []string{"Set-Cookie", "Cache-Control", "expires"})
l.UpdateDirective("proxy_cache", []string{"proxy_cache_panel"})
l.UpdateDirective("proxy_cache_key", []string{"$host$uri$is_args$args"})
l.UpdateDirective("proxy_cache_valid", []string{"200", "304", "301", "302", "10m"})
l.Cache = true
l.CacheTime = cacheTime
l.CacheUint = cacheUint
}
func (l *Location) RemoveCache() {
l.RemoveDirective("if", []string{"(", "$uri", "~*", `"\.(gif|png|jpg|css|js|woff|woff2)$"`, ")"})
l.RemoveDirective("proxy_ignore_headers", []string{"Set-Cookie"})
l.RemoveDirective("proxy_cache", []string{"proxy_cache_panel"})
l.RemoveDirective("proxy_cache_key", []string{"$host$uri$is_args$args"})
l.RemoveDirective("proxy_cache_valid", []string{"200"})
l.UpdateDirective("add_header", []string{"Cache-Control", "no-cache"})
l.CacheTime = 0
l.CacheUint = ""
l.Cache = false
}
func (l *Location) AddSubFilter(subFilters map[string]string) {
l.RemoveDirective("sub_filter", []string{})
l.Replaces = subFilters
for k, v := range subFilters {
l.UpdateDirective("sub_filter", []string{fmt.Sprintf(`"%s"`, k), fmt.Sprintf(`"%s"`, v)})
}
l.UpdateDirective("proxy_set_header", []string{"Accept-Encoding", `""`})
l.UpdateDirective("sub_filter_once", []string{"off"})
}
func (l *Location) RemoveSubFilter() {
l.RemoveDirective("sub_filter", []string{})
l.RemoveDirective("proxy_set_header", []string{"Accept-Encoding", `""`})
l.RemoveDirective("sub_filter_once", []string{"off"})
l.Replaces = nil
}
+23 -22
View File
@@ -65,7 +65,19 @@ func (s *Server) FindDirectives(directiveName string) []IDirective {
directives = append(directives, directive.GetBlock().FindDirectives(directiveName)...)
}
}
if directiveName == "listen" {
for _, listen := range s.Listens {
params := []string{listen.Bind}
params = append(params, listen.Parameters...)
if listen.DefaultServer != "" {
params = append(params, DefaultServer)
}
directives = append(directives, &Directive{
Name: "listen",
Parameters: params,
})
}
}
return directives
}
@@ -75,14 +87,12 @@ func (s *Server) UpdateDirective(key string, params []string) {
}
if key == "listen" {
defaultServer := false
if len(params) > 1 && params[1] == "default_server" {
paramLen := len(params)
if paramLen > 0 && params[paramLen-1] == "default_server" {
params = params[:paramLen-1]
defaultServer = true
}
if len(params) > 2 {
s.UpdateListen(params[0], defaultServer, params[2:]...)
} else {
s.UpdateListen(params[0], defaultServer)
}
s.UpdateListen(params[0], defaultServer, params[1:]...)
return
}
@@ -116,22 +126,14 @@ func (s *Server) RemoveDirective(key string, params []string) {
directives := s.Directives
var newDirectives []IDirective
for _, dir := range directives {
if key == "location" {
if location, ok := dir.(*Location); ok {
if len(params) == 2 && location.Match == params[1] && location.Modifier == params[0] {
continue
}
}
} else {
if dir.GetName() == key {
if len(params) > 0 {
oldParams := dir.GetParameters()
if oldParams[0] == params[0] {
continue
}
} else {
if dir.GetName() == key {
if len(params) > 0 {
oldParams := dir.GetParameters()
if oldParams[0] == params[0] {
continue
}
} else {
continue
}
}
newDirectives = append(newDirectives, dir)
@@ -319,5 +321,4 @@ func (s *Server) AddHTTP2HTTPS() {
})
newDir.Block = block
s.UpdateDirectiveBySecondKey("if", "($scheme", newDir)
}
@@ -52,8 +52,8 @@ func (sl *ServerListen) GetBlock() IBlock {
func (sl *ServerListen) GetParameters() []string {
params := []string{sl.Bind}
params = append(params, sl.DefaultServer)
params = append(params, sl.Parameters...)
params = append(params, sl.DefaultServer)
return params
}
-1
View File
@@ -74,7 +74,6 @@ func DumpBlock(b components.IBlock, style *Style, startLine int) string {
}
directives := b.GetDirectives()
for i, directive := range directives {
if directive.GetLine() > line {
-1
View File
@@ -84,7 +84,6 @@ func (p *Parser) Parse() *components.Config {
}
func (p *Parser) parseBlock() *components.Block {
context := &components.Block{
Comment: "",
Directives: make([]components.IDirective, 0),
+4 -4
View File
@@ -7,7 +7,7 @@ import (
"runtime"
"time"
"github.com/gogf/gf/os/gproc"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
)
const ntpEpochOffset = 2208988800
@@ -30,7 +30,7 @@ type packet struct {
TxTimeFrac uint32
}
func Getremotetime() (time.Time, error) {
func GetRemoteTime() (time.Time, error) {
conn, err := net.Dial("udp", "pool.ntp.org:123")
if err != nil {
return time.Time{}, fmt.Errorf("failed to connect: %v", err)
@@ -62,10 +62,10 @@ func Getremotetime() (time.Time, error) {
func UpdateSystemDate(dateTime string) error {
system := runtime.GOOS
if system == "linux" {
if _, err := gproc.ShellExec(`date -s "` + dateTime + `"`); err != nil {
if _, err := cmd.Execf(`date -s "` + dateTime + `"`); err != nil {
return fmt.Errorf("update system date failed, err: %v", err)
}
return nil
}
return fmt.Errorf("The current system architecture does not support synchronization")
return fmt.Errorf("the current system architecture %v does not support synchronization", system)
}
+112
View File
@@ -0,0 +1,112 @@
package ssl
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"net"
"os"
"time"
"github.com/1Panel-dev/1Panel/backend/global"
)
func GenerateSSL(domain string) error {
rootPrivateKey, _ := rsa.GenerateKey(rand.Reader, 2048)
ipItem := net.ParseIP(domain)
isIP := false
if len(ipItem) != 0 {
isIP = true
}
rootTemplate := x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "1Panel Root CA"},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
BasicConstraintsValid: true,
IsCA: true,
KeyUsage: x509.KeyUsageCertSign,
}
if isIP {
rootTemplate.IPAddresses = []net.IP{ipItem}
} else {
rootTemplate.DNSNames = []string{domain}
}
rootCertBytes, _ := x509.CreateCertificate(rand.Reader, &rootTemplate, &rootTemplate, &rootPrivateKey.PublicKey, rootPrivateKey)
rootCertBlock := &pem.Block{
Type: "CERTIFICATE",
Bytes: rootCertBytes,
}
interPrivateKey, _ := rsa.GenerateKey(rand.Reader, 2048)
interTemplate := x509.Certificate{
SerialNumber: big.NewInt(2),
Subject: pkix.Name{CommonName: "1Panel Intermediate CA"},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
BasicConstraintsValid: true,
IsCA: true,
KeyUsage: x509.KeyUsageCertSign,
}
if isIP {
interTemplate.IPAddresses = []net.IP{ipItem}
} else {
interTemplate.DNSNames = []string{domain}
}
interCertBytes, _ := x509.CreateCertificate(rand.Reader, &interTemplate, &rootTemplate, &interPrivateKey.PublicKey, rootPrivateKey)
interCertBlock := &pem.Block{
Type: "CERTIFICATE",
Bytes: interCertBytes,
}
clientPrivateKey, _ := rsa.GenerateKey(rand.Reader, 2048)
clientTemplate := x509.Certificate{
SerialNumber: big.NewInt(3),
Subject: pkix.Name{CommonName: domain},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
KeyUsage: x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
}
if isIP {
clientTemplate.IPAddresses = []net.IP{ipItem}
} else {
clientTemplate.DNSNames = []string{domain}
}
clientCertBytes, _ := x509.CreateCertificate(rand.Reader, &clientTemplate, &interTemplate, &clientPrivateKey.PublicKey, interPrivateKey)
clientCertBlock := &pem.Block{
Type: "CERTIFICATE",
Bytes: clientCertBytes,
}
pemBytes := []byte{}
pemBytes = append(pemBytes, pem.EncodeToMemory(clientCertBlock)...)
pemBytes = append(pemBytes, pem.EncodeToMemory(interCertBlock)...)
pemBytes = append(pemBytes, pem.EncodeToMemory(rootCertBlock)...)
certOut, err := os.OpenFile(global.CONF.System.BaseDir+"/1panel/secret/server.crt.tmp", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer certOut.Close()
if _, err := certOut.Write(pemBytes); err != nil {
return err
}
keyOut, err := os.OpenFile(global.CONF.System.BaseDir+"/1panel/secret/server.key.tmp", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer keyOut.Close()
if err := pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(clientPrivateKey)}); err != nil {
return err
}
return nil
}
+7 -24
View File
@@ -21,9 +21,8 @@ type LocalCommand struct {
closeSignal syscall.Signal
closeTimeout time.Duration
cmd *exec.Cmd
pty *os.File
ptyClosed chan struct{}
cmd *exec.Cmd
pty *os.File
}
func NewCommand(commands string) (*LocalCommand, error) {
@@ -33,15 +32,13 @@ func NewCommand(commands string) (*LocalCommand, error) {
if err != nil {
return nil, errors.Wrapf(err, "failed to start command")
}
ptyClosed := make(chan struct{})
lcmd := &LocalCommand{
closeSignal: DefaultCloseSignal,
closeTimeout: DefaultCloseTimeout,
cmd: cmd,
pty: pty,
ptyClosed: ptyClosed,
cmd: cmd,
pty: pty,
}
return lcmd, nil
@@ -57,16 +54,10 @@ func (lcmd *LocalCommand) Write(p []byte) (n int, err error) {
func (lcmd *LocalCommand) Close() error {
if lcmd.cmd != nil && lcmd.cmd.Process != nil {
_ = lcmd.cmd.Process.Signal(lcmd.closeSignal)
}
for {
select {
case <-lcmd.ptyClosed:
return nil
case <-lcmd.closeTimeoutC():
_ = lcmd.cmd.Process.Signal(syscall.SIGKILL)
}
_ = lcmd.cmd.Process.Kill()
}
_ = lcmd.pty.Close()
return nil
}
func (lcmd *LocalCommand) ResizeTerminal(width int, height int) error {
@@ -100,11 +91,3 @@ func (lcmd *LocalCommand) Wait(quitChan chan bool) {
setQuit(quitChan)
}
}
func (lcmd *LocalCommand) closeTimeoutC() <-chan time.Time {
if lcmd.closeTimeout >= 0 {
return time.After(lcmd.closeTimeout)
}
return make(chan time.Time)
}
+49
View File
@@ -0,0 +1,49 @@
package cmd
import (
"github.com/spf13/cobra"
)
func init() {
RootCmd.AddCommand(resetMFACmd)
RootCmd.AddCommand(resetSSLCmd)
RootCmd.AddCommand(resetEntranceCmd)
}
var resetMFACmd = &cobra.Command{
Use: "reset-mfa",
Short: "关闭 1Panel 两步验证",
RunE: func(cmd *cobra.Command, args []string) error {
db, err := loadDBConn()
if err != nil {
return err
}
return setSettingByKey(db, "MFAStatus", "disable")
},
}
var resetSSLCmd = &cobra.Command{
Use: "reset-https",
Short: "取消 1Panel https 方式登录",
RunE: func(cmd *cobra.Command, args []string) error {
db, err := loadDBConn()
if err != nil {
return err
}
return setSettingByKey(db, "SSL", "disable")
},
}
var resetEntranceCmd = &cobra.Command{
Use: "reset-entrance",
Short: "取消 1Panel 安全入口",
RunE: func(cmd *cobra.Command, args []string) error {
db, err := loadDBConn()
if err != nil {
return err
}
return setSettingByKey(db, "SecurityEntrance", "")
},
}
+28
View File
@@ -1,10 +1,14 @@
package cmd
import (
"fmt"
"strings"
"time"
"github.com/1Panel-dev/1Panel/backend/server"
cmdUtils "github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/spf13/cobra"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
@@ -28,8 +32,32 @@ type setting struct {
About string `json:"about" gorm:"type:longText"`
}
func loadDBConn() (*gorm.DB, error) {
stdout, err := cmdUtils.Exec("grep '^BASE_DIR=' /usr/bin/1pctl | cut -d'=' -f2")
if err != nil {
return nil, fmt.Errorf("handle load `BASE_DIR` failed, err: %v", err)
}
baseDir := strings.ReplaceAll(stdout, "\n", "")
if len(baseDir) == 0 {
return nil, fmt.Errorf("error `BASE_DIR` find in /usr/bin/1pctl \n")
}
if strings.HasSuffix(baseDir, "/") {
baseDir = baseDir[:strings.LastIndex(baseDir, "/")]
}
db, err := gorm.Open(sqlite.Open(baseDir+"/1panel/db/1Panel.db"), &gorm.Config{})
if err != nil {
return nil, fmt.Errorf("init my db conn failed, err: %v \n", err)
}
return db, nil
}
func getSettingByKey(db *gorm.DB, key string) string {
var setting setting
_ = db.Where("key = ?", key).First(&setting).Error
return setting.Value
}
func setSettingByKey(db *gorm.DB, key, value string) error {
return db.Model(&setting{}).Where("key = ?", key).Updates(map[string]interface{}{"value": value}).Error
}
+6 -18
View File
@@ -2,14 +2,10 @@ package cmd
import (
"fmt"
"strings"
"github.com/1Panel-dev/1Panel/backend/global"
cmdUtils "github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/utils/encrypt"
"github.com/spf13/cobra"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func init() {
@@ -20,25 +16,15 @@ var userinfoCmd = &cobra.Command{
Use: "user-info",
Short: "获取用户信息",
RunE: func(cmd *cobra.Command, args []string) error {
stdout, err := cmdUtils.Exec("grep '^BASE_DIR=' /usr/bin/1pctl | cut -d'=' -f2")
db, err := loadDBConn()
if err != nil {
panic(err)
}
baseDir := strings.ReplaceAll(stdout, "\n", "")
if len(baseDir) == 0 {
fmt.Printf("error `BASE_DIR` find in /usr/bin/1pctl \n")
}
if strings.HasSuffix(baseDir, "/") {
baseDir = baseDir[:strings.LastIndex(baseDir, "/")]
}
db, err := gorm.Open(sqlite.Open(baseDir+"/1panel/db/1Panel.db"), &gorm.Config{})
if err != nil {
fmt.Printf("init my db conn failed, err: %v \n", err)
return fmt.Errorf("init my db conn failed, err: %v \n", err)
}
user := getSettingByKey(db, "UserName")
password := getSettingByKey(db, "Password")
port := getSettingByKey(db, "ServerPort")
ssl := getSettingByKey(db, "SSL")
entrance := getSettingByKey(db, "SecurityEntrance")
enptrySetting := getSettingByKey(db, "EncryptKey")
p := ""
@@ -52,6 +38,8 @@ var userinfoCmd = &cobra.Command{
fmt.Printf("username: %s\n", user)
fmt.Printf("password: %s\n", p)
fmt.Printf("port: %s\n", port)
fmt.Printf("ssl: %s\n", ssl)
fmt.Printf("entrance: %s\n", entrance)
return nil
},
}
+12 -18
View File
@@ -2,12 +2,12 @@ package cmd
import (
"fmt"
"strings"
cmdUtils "github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/configs"
"github.com/1Panel-dev/1Panel/cmd/server/conf"
"gopkg.in/yaml.v3"
"github.com/spf13/cobra"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func init() {
@@ -18,27 +18,21 @@ var versionCmd = &cobra.Command{
Use: "version",
Short: "获取系统版本信息",
RunE: func(cmd *cobra.Command, args []string) error {
stdout, err := cmdUtils.Exec("grep '^BASE_DIR=' /usr/bin/1pctl | cut -d'=' -f2")
db, err := loadDBConn()
if err != nil {
panic(err)
}
baseDir := strings.ReplaceAll(stdout, "\n", "")
if len(baseDir) == 0 {
fmt.Printf("error `BASE_DIR` find in /usr/bin/1pctl \n")
}
if strings.HasSuffix(baseDir, "/") {
baseDir = baseDir[:strings.LastIndex(baseDir, "/")]
}
db, err := gorm.Open(sqlite.Open(baseDir+"/1panel/db/1Panel.db"), &gorm.Config{})
if err != nil {
fmt.Printf("init my db conn failed, err: %v \n", err)
return err
}
version := getSettingByKey(db, "SystemVersion")
appStoreVersion := getSettingByKey(db, "AppStoreVersion")
fmt.Printf("1panel version: %s\n", version)
fmt.Printf("appstore version: %s\n", appStoreVersion)
config := configs.ServerConfig{}
if err := yaml.Unmarshal(conf.AppYaml, &config); err != nil {
return fmt.Errorf("unmarshal conf.App.Yaml failed, errL %v", err)
} else {
fmt.Printf("mode: %s\n", config.System.Mode)
}
return nil
},
}
+2
View File
@@ -5,6 +5,8 @@ system:
repo_url: https://resource.fit2cloud.com/1panel/package
is_demo: false
port: 9999
username: admin
password: admin123
log:
level: debug
+1052 -222
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+12
View File
@@ -0,0 +1,12 @@
proxy_temp_path /www/common/proxy/proxy_temp_dir;
proxy_cache_path /www/common/proxy/proxy_cache_dir levels=1:2 keys_zone=proxy_cache_panel:20m inactive=1d max_size=5g;
client_body_buffer_size 512k;
proxy_connect_timeout 60;
proxy_read_timeout 60;
proxy_send_timeout 60;
proxy_buffer_size 32k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_cache proxy_cache_panel;
+1
View File
@@ -2,6 +2,7 @@
<html>
<head>
<meta charset="utf-8">
<meta name="robots" content="noindex,nofollow" />
<title>恭喜,站点创建成功!</title>
<style>
.container {
+13
View File
@@ -1,6 +1,7 @@
package nginx_conf
import (
"embed"
_ "embed"
)
@@ -15,3 +16,15 @@ var Index []byte
//go:embed index.php
var IndexPHP []byte
//go:embed rewrite/*
var Rewrites embed.FS
//go:embed cache.conf
var Cache []byte
//go:embed proxy.conf
var Proxy []byte
//go:embed proxy_cache.conf
var ProxyCache []byte
+12
View File
@@ -0,0 +1,12 @@
location ^~ /test {
proxy_pass http://1panel.cloud/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
add_header X-Cache $upstream_cache_status;
}

Some files were not shown because too many files have changed in this diff Show More