# cpolar_script
**Repository Path**: jun-wan/cpolar_script
## Basic Information
- **Project Name**: cpolar_script
- **Description**: cpolar相关脚本及支持
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2025-10-19
- **Last Updated**: 2026-07-23
## Categories & Tags
**Categories**: Uncategorized
**Tags**: cpolar, Linux, JavaScript, Windows
## README
cpolar Script Toolkit
Windows / macOS / Linux 登录信息清理与 Termux 一键安装脚本
围绕 cpolar 的四个常用场景,提供中文交互、清晰步骤、自动服务管理和可直接在线执行的脚本。
> 本仓库当前重点维护四个脚本:Windows、macOS 与 Linux 端清理旧账户信息,以及 Android Termux 端安装并启动 cpolar。
## 目录
| 序号 | 脚本 | 适用平台 | 主要用途 | 在线执行 |
|------|------|----------|----------|----------|
| 1 | [clear-cpolar-auth.ps1](#1-windows-登录信息清理) | Windows PowerShell 5.1+ | 【windows】解决cpolar登录用户不一致问题(切换账号) | [查看命令](#windows-在线执行) |
| 2 | [clear-cpolar-auth.sh](#2-macos-登录信息清理) | macOS / Bash 3.2+ | 【macos】解决cpolar登录用户不一致问题(切换账号) | [查看命令](#macos-在线执行) |
| 3 | [clear-cpolar-auth.sh](#3-linux--nas-登录信息清理) | Linux / NAS / systemd | 【linux】解决cpolar登录用户不一致问题(切换账号) | [查看命令](#linux-在线执行) |
| 4 | [install.sh](#4-termux-一键安装) | Android / Termux | 安装 cpolar、启用服务并输出访问地址 | [查看命令](#termux-在线执行) |
---
## 1. Windows 登录信息清理
> **适用:Windows + cpolar Service** - 用于本地 `cpolar.yml` 记录的账号与当前登录用户不一致时,清空 `email` 和 `authtoken` 后重新登录。
脚本位置:[`cpolar_email_and_authtoken_clear/win/clear-cpolar-auth.ps1`](cpolar_email_and_authtoken_clear/win/clear-cpolar-auth.ps1)
### 功能
- 自动请求管理员权限。
- 检查 `%USERPROFILE%\.cpolar\cpolar.yml` 与 `cpolar` Windows 服务。
- 创建带时间戳的配置备份。
- 仅清空 `email` 和 `authtoken`,保留其他隧道配置。
- 使用 `sc.exe` 自动停止并重新启动 `cpolar` 服务。
- 显示中文 Banner、五步进度与最终结果摘要。
### 在线直接执行
```powershell
irm https://gitee.com/jun-wan/cpolar_script/raw/master/cpolar_email_and_authtoken_clear/win/clear-cpolar-auth.ps1 | iex
```
在普通 PowerShell 中执行时,Windows 会通过 UAC 打开管理员窗口;处理完成后按 Enter 关闭。若 PowerShell 已经以管理员身份运行,结果会直接显示在当前窗口。
---
## 2. macOS 登录信息清理
> **适用:macOS + Homebrew cpolar Service** - 用于当前用户 `cpolar.yml` 中的账号与实际登录用户不一致时,清空 `email` 和 `authtoken` 后重新登录。
脚本位置:[`cpolar_email_and_authtoken_clear/mac/clear-cpolar-auth.sh`](cpolar_email_and_authtoken_clear/mac/clear-cpolar-auth.sh)
### 路径说明
脚本使用当前用户的标准路径:
```text
$HOME/.cpolar/cpolar.yml
```
例如用户 `admin` 对应 `/Users/admin/.cpolar/cpolar.yml`。在 macOS 的 APFS 系统卷结构中,它可能显示为下面的物理路径,两者指向同一份文件:
```text
/System/Volumes/Data/Users/admin/.cpolar/cpolar.yml
```
### 使用前提
先按照 cpolar 的 Homebrew 方式完成安装并注册系统服务:
```bash
brew tap probezy/core && brew install cpolar
sudo cpolar service install
sudo cpolar service start
```
### 功能
- 自动识别 Intel 与 Apple Silicon 的 Homebrew 安装路径。
- 始终修改原登录用户的 `$HOME/.cpolar/cpolar.yml`,不会误改 `/var/root` 下的配置。
- 配置文件由 root 创建或普通用户不可写时,自动通过内部 `sudo` 完成读取、备份和写入。
- 创建带时间戳的配置备份。
- 仅清空 `email` 和 `authtoken`,保留其他隧道配置。
- 重复执行时识别字段已经为空,不重复写入配置文件。
- 使用官方 `cpolar service stop/start` 完成停止和启动,最后通过 launchd 确认运行状态。
- 操作失败时恢复配置备份并尝试重新启动服务。
- 显示中文 Banner、五步进度与最终结果摘要。
### 在线直接执行
```bash
curl -fsSL https://gitee.com/jun-wan/cpolar_script/raw/master/cpolar_email_and_authtoken_clear/mac/clear-cpolar-auth.sh | bash
```
脚本会在访问 root 所有的配置文件或管理 cpolar 系统服务时自行请求 `sudo` 权限。正常情况下不要在 `curl` 前添加 `sudo`。
### 权限与用户切换
推荐直接在需要清理配置的普通用户终端中执行在线命令。即使 `cpolar.yml` 属于 root,脚本也会自行调用 `sudo`,同时仍使用当前用户的 `$HOME` 定位配置。
如果当前位于 root Shell,需要切换到普通用户,请使用登录式切换,并将 `your_username` 替换为实际的 macOS 用户名:
```bash
su - your_username
```
不要使用不带 `-` 的 `su your_username`。它会继承 root 的当前目录;当该目录不允许目标用户访问时,会出现:
```text
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
```
如果已经处于 root Shell,也可以不切换用户,显式指定要处理的账号:
```bash
curl -fsSL https://gitee.com/jun-wan/cpolar_script/raw/master/cpolar_email_and_authtoken_clear/mac/clear-cpolar-auth.sh | CPOLAR_TARGET_USER=your_username bash
```
`sudo curl ... | bash` 不是正确的提权方式,因为它只让管道左侧的 `curl` 以 root 运行,右侧的 `bash` 仍使用原来的权限。确实需要让整个脚本由 sudo 执行时,应把 sudo 放在管道右侧,并指定目标用户:
```bash
curl -fsSL https://gitee.com/jun-wan/cpolar_script/raw/master/cpolar_email_and_authtoken_clear/mac/clear-cpolar-auth.sh | sudo CPOLAR_TARGET_USER=your_username bash
```
---
## 3. Linux / NAS 登录信息清理
> **适用:Linux / NAS + systemd** - 已在飞牛 NAS 的 `/usr/local/etc/cpolar/cpolar.yml` 与 `cpolar.service` 环境中验证基础清理方式。
脚本位置:[`cpolar_email_and_authtoken_clear/linux/clear-cpolar-auth.sh`](cpolar_email_and_authtoken_clear/linux/clear-cpolar-auth.sh)
### 功能
- 默认检查 `/usr/local/etc/cpolar/cpolar.yml` 与 `cpolar.service`。
- 创建带时间戳的配置备份。
- 删除顶层 `email:` 和 `authtoken:` 配置行,保留其他配置。
- 重复执行时识别旧账户配置行已经不存在,不重复修改配置。
- 使用 `systemctl stop/start` 自动重启并检查服务状态。
- 操作失败时恢复配置备份,并尝试重新启动 cpolar 服务。
- 不包含自删除逻辑,兼容 `curl ... | bash` 在线执行方式。
如其他 Linux 环境的路径或服务名称不同,可以通过环境变量覆盖:
```bash
CPOLAR_CONFIG_PATH=/自定义路径/cpolar.yml CPOLAR_SERVICE_NAME=cpolar bash clear-cpolar-auth.sh
```
### 在线直接执行
```bash
curl -fsSL https://gitee.com/jun-wan/cpolar_script/raw/master/cpolar_email_and_authtoken_clear/linux/clear-cpolar-auth.sh | bash
```
---
## 4. Termux 一键安装
> **适用:Android + Termux** - 自动添加 cpolar 软件源、安装 cpolar 与 termux-services,并启用 cpolar 服务。
脚本位置:[`termux_cpolar_install/install.sh`](termux_cpolar_install/install.sh)
### 功能
- 检查 Termux 环境与 `PREFIX`。
- 写入 `https://termux.cpolar.com` 软件源。
- 自动更新软件索引,失败时清理缓存并尝试切换官方 Termux 主源。
- 安装 `cpolar` 与 `termux-services`。
- 启用并启动 `cpolar` 服务。
- 仅输出物理网络接口对应的局域网 Web UI 地址。
- 显示中文 Banner、五步进度与安装结果摘要。
### 在线直接执行
```bash
curl -fsSL https://gitee.com/jun-wan/cpolar_script/raw/master/termux_cpolar_install/install.sh | bash
```
### 常用命令
```bash
sv status cpolar
sv restart cpolar
sv down cpolar
cpolar authtoken <你的Token>
```
Web UI:`http://127.0.0.1:9200`
---
## 项目结构
```text
cpolar_email_and_authtoken_clear/
├── win/
│ └── clear-cpolar-auth.ps1
├── mac/
│ └── clear-cpolar-auth.sh
└── linux/
└── clear-cpolar-auth.sh
termux_cpolar_install/
└── install.sh
```
## 使用提醒
- `irm ... | iex` 和 `curl ... | bash` 会直接执行远程脚本,建议先查看源码并确认链接属于本仓库。
- Windows、macOS 与 Linux 脚本会修改 cpolar 配置并重启服务,但都会在修改前创建备份。
- macOS 脚本不会安装 cpolar 或注册系统服务,请先完成 Homebrew 安装和 `cpolar service install`。
- Linux 脚本仅适用于使用 systemd 管理 cpolar 的环境;默认路径基于已验证的飞牛 NAS 安装方式。
- Termux 脚本会安装软件包并调整软件源,仅应在自己的设备或获得授权的环境中运行。
- 新文件在提交并推送到 Gitee `master` 分支后,对应 raw 链接才会生效。
## 参考
- [cpolar 官方网站](https://www.cpolar.com/)
- [probezy/homebrew-core](https://github.com/probezy/homebrew-core)
- [cpolar Android + Termux 教程](https://www.cpolar.com/blog/an-old-phone-turns-into-a-server-in-an-instant-building-a-typecho-blog-on-android)
- [termux-services](https://github.com/termux/termux-services)