Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dbe417f418 | ||
|
|
7566207b8f | ||
|
|
1b95dcde25 | ||
|
|
e7ac30ecb3 | ||
|
|
b3c15a59bf | ||
|
|
435784f863 | ||
|
|
97d2a45b3b | ||
|
|
e696090912 | ||
|
|
e974cfbff6 | ||
|
|
215a316363 | ||
|
|
195c8e661a | ||
|
|
14328fe0a3 | ||
|
|
b92b63331a | ||
|
|
d778c1f79e | ||
|
|
b93d7f1f8c | ||
|
|
9a074e7fd7 | ||
|
|
96a2344178 |
@@ -0,0 +1 @@
|
||||
custom: ["https://paypal.me/yisier"]
|
||||
@@ -1,6 +1,6 @@
|
||||
# NPS
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||
|
||||
@@ -12,11 +12,10 @@
|
||||
|
||||
---
|
||||
## 💰 赞助 / 推荐
|
||||
#### ☁️ [雨云 — 国内便宜稳定的云服务器,首月 5 折](https://www.rainyun.com/MjY0MzY1_)
|
||||
500M 带宽,高仿物理机,云服务器**15 元起**,通过此链接注册首月 5 折。
|
||||
#### 【腾讯云】[2核2G4M 服务器新客99元/年起](https://cloud.tencent.com/act/cps/redirect?redirect=6544&cps_key=de079542f18563fceb5c9b687dad2943&from=console)
|
||||
|
||||
#### 【NATNPS 云穿透】 — [无需自备服务器,注册即用,免费 3M 带宽 / 2 条隧道](https://natnps.com/register?utm_from=MQ==)
|
||||
|
||||
#### 🚀 [NATNPS 云穿透 — 免费 3M 带宽 / 2 条隧道](https://natnps.com/register?utm_from=MQ==)
|
||||
无需自备服务器,注册即用。免费版 3M 带宽 2 条隧道;6.8 元 12M 带宽 10 条隧道;13.8 元 24M 带宽 20 条隧道。
|
||||
|
||||
---
|
||||
## 🚀 快速开始
|
||||
@@ -46,6 +45,12 @@
|
||||
|
||||

|
||||
|
||||
|
||||
#### GUI
|
||||
|
||||

|
||||
|
||||
|
||||
#### 命令行直接启动
|
||||
|
||||
```shell
|
||||
@@ -54,6 +59,7 @@ npc -server=ip:8025 -vkey=xxx -tls_enable=true # TLS 桥接
|
||||
npc -server=ip:8024 -vkey=vkey1,vkey2 # 多隧道
|
||||
```
|
||||
|
||||
|
||||
---
|
||||
|
||||
## ☕ 请作者喝杯咖啡
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#/bash/sh
|
||||
export VERSION=0.26.38
|
||||
export VERSION=0.26.39
|
||||
export GOPROXY=direct
|
||||
|
||||
sudo apt-get update
|
||||
|
||||
+1
-1
@@ -280,7 +280,7 @@ func printSlogan() {
|
||||
fmt.Printf("\033[32;0m# \033[31mNPS内网穿透客户端\033[0m #\n")
|
||||
fmt.Printf("\033[32;0m# #\n")
|
||||
fmt.Printf("\033[32;0m#\033[32m 地址:\033[31;0mhttps://github.com/yisier/nps\033[0m #\n")
|
||||
fmt.Printf("\033[32;0m#\033[32m 提示:\033[32;0m1、涉及到系统服务的需要以管理员身份运行\033[0m\033[32;0m #\n")
|
||||
fmt.Printf("\033[32;0m#\033[32m 提示:\033[32;0m1、涉及到系统服务的需要以管理员/root身份运行\033[0m\033[32;0m #\n")
|
||||
fmt.Printf("\033[32;0m#\033[32m \033[32;0m2、直接启动或[注册系统服务]需要使用[快捷启动命令]\033[0m\033[32;0m #\n")
|
||||
fmt.Printf("\033[32;0m#\033[32m \033[32;0m3、其他命令如卸载/启动/停止只需要输入[vkey]\033[0m\033[32;0m #\n")
|
||||
fmt.Printf("\033[32;0m###########################################################\n")
|
||||
|
||||
+120
-44
@@ -50,12 +50,6 @@ func main() {
|
||||
common.PrintVersion()
|
||||
return
|
||||
}
|
||||
if *serverCmd {
|
||||
_ = logs.SetLogger(logs.AdapterConsole, `{"level":7,"color":true}`)
|
||||
printSlogan()
|
||||
inputCmd()
|
||||
return
|
||||
}
|
||||
|
||||
var logPath string
|
||||
// *confPath why get null value ?
|
||||
@@ -73,6 +67,13 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
if *serverCmd {
|
||||
_ = logs.SetLogger(logs.AdapterConsole, `{"level":7,"color":true}`)
|
||||
printSlogan()
|
||||
inputCmd()
|
||||
return
|
||||
}
|
||||
|
||||
// auto-generate default config files if not exist
|
||||
initConfig(filepath.Join(common.GetRunPath(), "conf"))
|
||||
|
||||
@@ -198,7 +199,11 @@ func main() {
|
||||
}
|
||||
return
|
||||
case "update":
|
||||
install.UpdateNps()
|
||||
if updated, err := install.UpdateNps(); err != nil {
|
||||
logs.Error("更新失败:", err)
|
||||
} else if updated {
|
||||
restartNpsService(s)
|
||||
}
|
||||
return
|
||||
//default:
|
||||
// logs.Error("command is not support")
|
||||
@@ -210,35 +215,117 @@ func main() {
|
||||
}
|
||||
|
||||
func printSlogan() {
|
||||
confFile := ""
|
||||
for _, dir := range []string{common.GetAppPath(), common.GetRunPath()} {
|
||||
if p := filepath.Join(dir, "conf", "nps.conf"); common.FileExists(p) {
|
||||
confFile = p
|
||||
break
|
||||
}
|
||||
}
|
||||
configLoaded := false
|
||||
if confFile != "" {
|
||||
if err := beego.LoadAppConfig("ini", confFile); err == nil {
|
||||
configLoaded = true
|
||||
}
|
||||
}
|
||||
|
||||
green := color.New(color.FgGreen).SprintFunc()
|
||||
// 第一次输入,如果输入 1,2,3,4 则需要输入秘钥,否则
|
||||
w := color.Output
|
||||
|
||||
fmt.Printf("%s", green(""))
|
||||
fmt.Fprintf(w, "%s", green(""))
|
||||
|
||||
fmt.Printf("\033[32;0m欢迎使用 NPS 管理脚本,当前版本:v%s\n", version.VERSION)
|
||||
fmt.Printf("\033[0m") // 重置颜色
|
||||
fmt.Fprintf(w, "\033[32;0m###########################################################\n")
|
||||
fmt.Fprintf(w, "\033[32;0m# \033[31mNPS内网穿透服务端\033[0m #\n")
|
||||
fmt.Fprintf(w, "\033[32;0m# #\n")
|
||||
fmt.Fprintf(w, "\033[32;0m#\033[32m 地址:\033[31;0mhttps://github.com/yisier/nps\033[0m #\n")
|
||||
fmt.Fprintf(w, "\033[32;0m#\033[32m 提示:\033[32;0m1、涉及到系统服务的需要以管理员/root身份运行\033[0m #\n")
|
||||
fmt.Fprintf(w, "\033[32;0m#\033[32m \033[32;0m2、安装/卸载/更新请选择对应的菜单项\033[0m #\n")
|
||||
fmt.Fprintf(w, "\033[32;0m#\033[32m \033[32;0m3、启动/停止/重启操作的是系统服务\033[0m #\n")
|
||||
fmt.Fprintf(w, "\033[32;0m###########################################################\n")
|
||||
fmt.Fprintf(w, "\033[0m") // 重置颜色
|
||||
|
||||
fmt.Printf("\n")
|
||||
fmt.Fprintf(w, "\n")
|
||||
|
||||
fmt.Printf("\u001B[32m输入[1]\u001B[0m - 安装 NPS\n")
|
||||
fmt.Printf("\u001B[32m输入[2]\u001B[0m - 卸载 NPS\n")
|
||||
fmt.Printf("\u001B[32m输入[3]\u001B[0m - 更新 NPS\n")
|
||||
fmt.Printf("---------------------\n")
|
||||
fmt.Printf("\u001B[32m输入[4]\u001B[0m - 查看状态\n")
|
||||
fmt.Printf("---------------------\n")
|
||||
fmt.Printf("\u001B[32m输入[5]\u001B[0m - 启动 NPS\n")
|
||||
fmt.Printf("\u001B[32m输入[6]\u001B[0m - 停止 NPS\n")
|
||||
fmt.Printf("\u001B[32m输入[7]\u001B[0m - 重启 NPS\n")
|
||||
fmt.Printf("---------------------\n")
|
||||
fmt.Printf("\u001B[32m输入[0]\u001B[0m - 退出脚本\n")
|
||||
fmt.Printf("---------------------\n")
|
||||
fmt.Printf("\n")
|
||||
fmt.Fprintf(w, "\u001B[32m输入 [1]\u001B[0m - 安装 NPS\n")
|
||||
fmt.Fprintf(w, "\u001B[32m输入 [2]\u001B[0m - 卸载 NPS\n")
|
||||
fmt.Fprintf(w, "\u001B[32m输入 [3]\u001B[0m - 更新 NPS\n")
|
||||
fmt.Fprintf(w, "---------------------\n")
|
||||
fmt.Fprintf(w, "\u001B[32m输入 [4]\u001B[0m - 启动 NPS\n")
|
||||
fmt.Fprintf(w, "\u001B[32m输入 [5]\u001B[0m - 停止 NPS\n")
|
||||
fmt.Fprintf(w, "\u001B[32m输入 [6]\u001B[0m - 重启 NPS\n")
|
||||
fmt.Fprintf(w, "---------------------\n")
|
||||
fmt.Fprintf(w, "\u001B[32m输入 [0]\u001B[0m - 退出脚本\n")
|
||||
fmt.Fprintf(w, "---------------------\n")
|
||||
|
||||
status := npsServiceStatus()
|
||||
fmt.Fprintf(w, "\u001B[32m当前版本:v%s\u001B[0m\n", version.VERSION)
|
||||
fmt.Fprintf(w, "\u001B[32m服务状态:%s\u001B[0m\n", status)
|
||||
// 管理面板地址仅当服务已启动时显示
|
||||
if status == "运行中" {
|
||||
fmt.Fprintf(w, "\u001B[32m管理面板:%s\u001B[0m\n", npsPanelAddr(configLoaded))
|
||||
}
|
||||
fmt.Fprintf(w, "\n")
|
||||
}
|
||||
|
||||
// npsServiceStatus 返回 Nps 系统服务的运行状态描述。
|
||||
func npsServiceStatus() string {
|
||||
prg := &nps{exit: make(chan struct{})}
|
||||
s, err := service.New(prg, &service.Config{Name: "Nps"})
|
||||
if err != nil {
|
||||
return "未知"
|
||||
}
|
||||
st, err := s.Status()
|
||||
if err != nil {
|
||||
return "未安装"
|
||||
}
|
||||
switch st {
|
||||
case service.StatusRunning:
|
||||
return "运行中"
|
||||
case service.StatusStopped:
|
||||
return "已停止"
|
||||
}
|
||||
return "未知"
|
||||
}
|
||||
|
||||
// npsPanelAddr 返回管理面板的访问地址(基于当前配置)。
|
||||
// configLoaded 为 false 表示未找到配置文件,无法判断面板地址。
|
||||
func npsPanelAddr(configLoaded bool) string {
|
||||
if !configLoaded {
|
||||
return "未找到配置文件 conf/nps.conf"
|
||||
}
|
||||
port := strings.TrimSpace(beego.AppConfig.String("web_port"))
|
||||
if port == "" {
|
||||
return "未配置 web_port(管理面板已关闭)"
|
||||
}
|
||||
scheme := "http"
|
||||
if beego.AppConfig.DefaultBool("web_open_ssl", false) {
|
||||
scheme = "https"
|
||||
}
|
||||
host := strings.TrimSpace(beego.AppConfig.String("web_host"))
|
||||
if host == "" || host == "a.o.com" {
|
||||
host = "127.0.0.1"
|
||||
}
|
||||
base := strings.TrimSuffix(beego.AppConfig.String("web_base_url"), "/")
|
||||
return scheme + "://" + host + ":" + port + base
|
||||
}
|
||||
|
||||
// restartNpsService 在更新完成后自动重启已安装的 Nps 系统服务,使新版本生效。
|
||||
// 未安装为系统服务时不改动当前进程,仅提示手动重启。
|
||||
func restartNpsService(s service.Service) {
|
||||
if _, err := s.Status(); err != nil {
|
||||
fmt.Println("未检测到已安装的 Nps 服务,请手动重启 nps 使新版本生效")
|
||||
return
|
||||
}
|
||||
if err := service.Control(s, "restart"); err != nil {
|
||||
fmt.Println("Nps 服务自动重启失败,请手动重启:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("Nps 服务已自动重启,新版本已生效")
|
||||
}
|
||||
|
||||
func inputCmd() {
|
||||
var flag string
|
||||
fmt.Printf("请输入[0-7]:")
|
||||
fmt.Print("请输入[0-6]:")
|
||||
|
||||
stdin := bufio.NewReader(os.Stdin)
|
||||
_, err := fmt.Fscanln(stdin, &flag)
|
||||
@@ -348,24 +435,13 @@ func inputCmd() {
|
||||
}
|
||||
break
|
||||
case "3":
|
||||
install.UpdateNpsNew()
|
||||
if updated, err := install.UpdateNps(); err != nil {
|
||||
fmt.Println("更新失败:", err)
|
||||
} else if updated {
|
||||
restartNpsService(s)
|
||||
}
|
||||
break
|
||||
case "4":
|
||||
// 查看状态
|
||||
var statusMsg = ""
|
||||
status, err := s.Status()
|
||||
if err != nil {
|
||||
statusMsg = "\u001B[31m未运行\u001B[0m"
|
||||
} else {
|
||||
if status == 1 {
|
||||
statusMsg = "\u001B[32m运行中\u001B[0m"
|
||||
} else {
|
||||
statusMsg = "\u001B[31m未运行\u001B[0m"
|
||||
}
|
||||
}
|
||||
fmt.Println("NPS服务状态:" + statusMsg)
|
||||
break
|
||||
case "5":
|
||||
// 启动 NPS
|
||||
err := service.Control(s, "start")
|
||||
if err != nil {
|
||||
@@ -375,7 +451,7 @@ func inputCmd() {
|
||||
}
|
||||
|
||||
break
|
||||
case "6":
|
||||
case "5":
|
||||
// 停止 NPS
|
||||
err := service.Control(s, "stop")
|
||||
if err != nil {
|
||||
@@ -385,7 +461,7 @@ func inputCmd() {
|
||||
}
|
||||
|
||||
break
|
||||
case "7":
|
||||
case "6":
|
||||
// 重启 NPS
|
||||
err := service.Control(s, "restart")
|
||||
if err != nil {
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 32 KiB After Width: | Height: | Size: 43 KiB |
+10
-1
@@ -1,8 +1,17 @@
|
||||
# 更新日志
|
||||
- 2026-10-10 v0.26.39
|
||||
- 修复:
|
||||
- 同时开启 `web_base_url` 与 `open_captcha` 时,验证码图片路径未跟随 `web_base_url`,反向代理到子路径后无法登录 [#359](https://github.com/yisier/nps/pull/359)
|
||||
- Web 面板语言机制:浏览器语言按前缀归一化匹配(`zh*`→`zh-CN`、`en*`→`en-US`),匹配不到时回退默认 `en-US`;用户手动切换语言后长期持久化,不再因关闭浏览器而失效 [#356](https://github.com/yisier/nps/issues/356)
|
||||
- SOCKS5(私密代理)UDP ASSOCIATE 回复的中继地址 `BND.ADDR` 优先使用客户端实际连接到的服务端地址,避免外网 IP 探测被运营商 HTTP 劫持返回错误 IP 导致 UDP 不通;外网探测改为 HTTPS 端点并加超时与校验 [#355](https://github.com/yisier/nps/issues/355)
|
||||
- 开启 `web_open_ssl=true` 后以系统服务方式启动时 Web 面板无法访问:证书相对路径按进程工作目录解析失败,改为基于 nps 运行目录解析 `web_cert_file` / `web_key_file` [#353](https://github.com/yisier/nps/issues/353)
|
||||
- `nps` 服务端升级后自动重启服务 [#47](https://github.com/yisier/nps/issues/347)
|
||||
|
||||
- 2026-08-24 v0.26.38
|
||||
- 新增:
|
||||
- GUI 客户端(npc-gui)迁移到 Wails v3
|
||||
- GUI 客户端新增版本自动更新:启动时自动检查 GitHub 最新 release,支持一键下载并热替换当前可执行文件后自动重启
|
||||
- GUI 客户端新增版本自动更新:启动时自动检查 GitHub 最新 release,支持一键下载并热替换当前可执行文件后自动重启
|
||||

|
||||
- 修复:
|
||||
- 客户端内网地址为 192.169.x.x 等非标准私网段时,Web 面板客户端列表不显示本机 IP [#348](https://github.com/yisier/nps/issues/348)
|
||||
|
||||
|
||||
+47
-12
@@ -20,6 +20,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"ehang.io/nps/lib/crypt"
|
||||
)
|
||||
@@ -452,18 +453,41 @@ func GetExtFromPath(path string) string {
|
||||
|
||||
var externalIp string
|
||||
|
||||
// externalIpServices are queried in order until one returns a valid IP.
|
||||
// HTTPS plain-text echo services are preferred, because a plain HTTP endpoint
|
||||
// can be hijacked or transparently cached by some ISPs (see #355).
|
||||
var externalIpServices = []string{
|
||||
"https://ifconfig.me/ip",
|
||||
"https://ipinfo.io/ip",
|
||||
"http://v4.ipip.net",
|
||||
}
|
||||
|
||||
// GetExternalIp returns the public IP of this server by asking an external
|
||||
// "what is my IP" service. The result is cached only after a valid answer,
|
||||
// so a transient failure or a hijacked/empty response is not cached forever.
|
||||
func GetExternalIp() string {
|
||||
if externalIp != "" {
|
||||
return externalIp
|
||||
}
|
||||
resp, err := http.Get("http://myexternalip.com/raw")
|
||||
if err != nil {
|
||||
return ""
|
||||
client := &http.Client{Timeout: 3 * time.Second}
|
||||
for _, url := range externalIpServices {
|
||||
resp, err := client.Get(url)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
content, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 64))
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
continue
|
||||
}
|
||||
ip := strings.TrimSpace(string(content))
|
||||
if net.ParseIP(ip) == nil {
|
||||
continue
|
||||
}
|
||||
externalIp = ip
|
||||
return externalIp
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
content, _ := ioutil.ReadAll(resp.Body)
|
||||
externalIp = string(content)
|
||||
return externalIp
|
||||
return ""
|
||||
}
|
||||
|
||||
func GetIntranetIp() (error, string) {
|
||||
@@ -609,12 +633,23 @@ func IsPublicIP(IP net.IP) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func GetServerIpByClientIp(clientIp net.IP) string {
|
||||
if IsPublicIP(clientIp) {
|
||||
return GetExternalIp()
|
||||
// GetServerIpByClientIp returns an address the SOCKS5 client can use to reach
|
||||
// the server's UDP relay (the SOCKS5 BND.ADDR).
|
||||
//
|
||||
// - LAN client: the server's intranet IP.
|
||||
// - WAN client: the local IP the client actually connected to, which is
|
||||
// guaranteed to be reachable because the TCP control connection succeeded.
|
||||
// Only when that address is not public (the server sits behind NAT) do we
|
||||
// fall back to querying an external "what is my IP" service.
|
||||
func GetServerIpByClientIp(clientIp, localIp net.IP) string {
|
||||
if !IsPublicIP(clientIp) {
|
||||
_, ip := GetIntranetIp()
|
||||
return ip
|
||||
}
|
||||
_, ip := GetIntranetIp()
|
||||
return ip
|
||||
if IsPublicIP(localIp) {
|
||||
return localIp.String()
|
||||
}
|
||||
return GetExternalIp()
|
||||
}
|
||||
|
||||
func PrintVersion() {
|
||||
|
||||
+10
-24
@@ -136,43 +136,29 @@ RestartSec=120
|
||||
WantedBy=multi-user.target
|
||||
`
|
||||
|
||||
func UpdateNps() {
|
||||
destPath, err := downloadLatest("server")
|
||||
if err != nil {
|
||||
log.Println("下载更新失败:", err)
|
||||
return
|
||||
}
|
||||
//复制文件到对应目录
|
||||
if _, err := copyStaticFile(destPath, "nps"); err != nil {
|
||||
log.Println("替换服务端文件失败:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("Update completed, please restart")
|
||||
}
|
||||
|
||||
func UpdateNpsNew() {
|
||||
// UpdateNps 检查并替换服务端到最新版本。
|
||||
// 返回 updated=true 表示已用新版本替换当前二进制(调用方应重启服务使其生效);
|
||||
// updated=false 且 err=nil 表示当前已是最新版本,未做任何改动。
|
||||
func UpdateNps() (updated bool, err error) {
|
||||
latest, err := fetchLatestVersion()
|
||||
if err != nil {
|
||||
log.Println("获取最新版本失败:", err)
|
||||
return
|
||||
return false, err
|
||||
}
|
||||
fmt.Println("最新版本为:", latest)
|
||||
if compareVersion(version.VERSION, latest) >= 0 {
|
||||
fmt.Println("当前已是最新版本,无需更新")
|
||||
return
|
||||
return false, nil
|
||||
}
|
||||
tempDir := filepath.Join(common.GetAppPath(), "temp")
|
||||
destPath, err := downloadLatest2("server", tempDir)
|
||||
if err != nil {
|
||||
log.Println("下载更新失败:", err)
|
||||
return
|
||||
return false, err
|
||||
}
|
||||
//复制文件到对应目录
|
||||
if err := copyStaticFileReplaceNps(destPath, common.GetAppPath()); err != nil {
|
||||
log.Println("替换服务端文件失败:", err)
|
||||
return
|
||||
return false, err
|
||||
}
|
||||
fmt.Println("更新成功,请重启服务")
|
||||
fmt.Println("更新成功")
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func fetchLatestVersion() (string, error) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package version
|
||||
|
||||
const VERSION = "0.26.38"
|
||||
const VERSION = "0.26.39"
|
||||
|
||||
// Compulsory minimum version, Minimum downward compatibility to this version
|
||||
func GetVersion() string {
|
||||
|
||||
@@ -267,7 +267,12 @@ func (s *Sock5ModeServer) handleUDP(c net.Conn) {
|
||||
return
|
||||
}
|
||||
// reply the local addr
|
||||
s.sendUdpReply(c, reply, succeeded, common.GetServerIpByClientIp(c.RemoteAddr().(*net.TCPAddr).IP))
|
||||
// Use the address the client actually connected to as BND.ADDR whenever it
|
||||
// is public; only fall back to external IP detection when the server is
|
||||
// behind NAT (see #355).
|
||||
clientIp := c.RemoteAddr().(*net.TCPAddr).IP
|
||||
localIp := c.LocalAddr().(*net.TCPAddr).IP
|
||||
s.sendUdpReply(c, reply, succeeded, common.GetServerIpByClientIp(clientIp, localIp))
|
||||
defer reply.Close()
|
||||
// new a tunnel to client
|
||||
link := conn.NewLink("udp5", "", s.task.Client.Cnf.Crypt, s.task.Client.Cnf.Compress, c.RemoteAddr().String(), false, "")
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"ehang.io/nps/bridge"
|
||||
@@ -75,6 +76,15 @@ func (s *WebServer) Start() error {
|
||||
if beego.AppConfig.String("web_open_ssl") == "true" {
|
||||
keyPath := beego.AppConfig.String("web_key_file")
|
||||
certPath := beego.AppConfig.String("web_cert_file")
|
||||
// Resolve relative paths against the nps run path instead of the
|
||||
// process working directory. Otherwise the web panel fails to start
|
||||
// when nps runs from another directory (e.g. as a system service).
|
||||
if !filepath.IsAbs(certPath) {
|
||||
certPath = filepath.Join(common.GetRunPath(), certPath)
|
||||
}
|
||||
if !filepath.IsAbs(keyPath) {
|
||||
keyPath = filepath.Join(common.GetRunPath(), keyPath)
|
||||
}
|
||||
err = http.ServeTLS(l, beego.BeeApp.Handlers, certPath, keyPath)
|
||||
} else {
|
||||
err = http.Serve(l, beego.BeeApp.Handlers)
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"github.com/astaxie/beego/utils/captcha"
|
||||
"math/rand"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -27,10 +28,22 @@ type record struct {
|
||||
lastLoginTime time.Time
|
||||
}
|
||||
|
||||
func init() {
|
||||
// InitCaptcha registers the captcha image route and the create_captcha template
|
||||
// function. The image URL prefix must include web_base_url, otherwise the login
|
||||
// page would request the captcha from the site root and fail behind a reverse
|
||||
// proxy that only forwards the web_base_url sub path.
|
||||
//
|
||||
// It must be called after beego.LoadAppConfig, so it cannot live in init():
|
||||
// package init() runs before main() loads conf/nps.conf, where web_base_url
|
||||
// would still be empty.
|
||||
func InitCaptcha() {
|
||||
// use beego cache system store the captcha data
|
||||
store := cache.NewMemoryCache()
|
||||
cpt = captcha.NewWithFilter("/captcha/", store)
|
||||
base := strings.TrimSuffix(beego.AppConfig.String("web_base_url"), "/")
|
||||
if base != "" && !strings.HasPrefix(base, "/") {
|
||||
base = "/" + base
|
||||
}
|
||||
cpt = captcha.NewWithFilter(base+"/captcha/", store)
|
||||
}
|
||||
|
||||
func (self *LoginController) Index() {
|
||||
|
||||
@@ -6,6 +6,10 @@ import (
|
||||
)
|
||||
|
||||
func Init() {
|
||||
// The captcha image path and its filter are built from web_base_url, so it
|
||||
// has to be registered here (after the config is loaded) instead of in an
|
||||
// init() function.
|
||||
controllers.InitCaptcha()
|
||||
web_base_url := beego.AppConfig.String("web_base_url")
|
||||
if len(web_base_url) > 0 {
|
||||
ns := beego.NewNamespace(web_base_url,
|
||||
|
||||
@@ -56,6 +56,23 @@
|
||||
if (flag) { return {'value':(langobj[languages['current']] || langobj[languages['default']] || 'N/A')}}
|
||||
}
|
||||
|
||||
// Match a browser language (e.g. "zh", "zh-TW", "en-GB") to an available
|
||||
// menu language key (e.g. "zh-CN", "en-US"). Exact match wins, then the
|
||||
// primary subtag (before the first "-"). Returns null when nothing matches
|
||||
// so the caller can fall back to the default language.
|
||||
function normalizeLang (lang) {
|
||||
if (!lang) return null;
|
||||
var lower = ('' + lang).toLowerCase();
|
||||
var primary = lower.split('-')[0];
|
||||
var fallback = null;
|
||||
for (var key in languages['menu']) {
|
||||
var k = key.toLowerCase();
|
||||
if (k === lower) return key;
|
||||
if (fallback == null && k.split('-')[0] === primary) fallback = key;
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
$.fn.cloudLang = function () {
|
||||
$.ajax({
|
||||
type: 'GET',
|
||||
@@ -65,7 +82,7 @@
|
||||
languages['content'] = xml2json($(xml).children())['content'];
|
||||
languages['menu'] = languages['content']['languages'];
|
||||
languages['default'] = languages['content']['default'];
|
||||
languages['navigator'] = (getCookie ('lang') || navigator.language || navigator.browserLanguage);
|
||||
languages['navigator'] = (getCookie ('lang') || normalizeLang(navigator.language || navigator.browserLanguage) || languages['default']);
|
||||
for(var key in languages['menu']){
|
||||
$('#languagemenu').next().append('<li lang="' + key + '"><a><img src="' + window.nps.web_base_url + '/static/img/flag/' + key + '.png"> ' + languages['menu'][key] +'</a></li>');
|
||||
if ( key == languages['navigator'] ) languages['current'] = key;
|
||||
@@ -76,11 +93,11 @@
|
||||
});
|
||||
};
|
||||
|
||||
$.fn.setLang = function (dom) {
|
||||
$.fn.setLang = function (dom, persistLang) {
|
||||
languages['current'] = $('#languagemenu').attr('lang');
|
||||
if ( dom == '' ) {
|
||||
$('#languagemenu span').text(' ' + languages['menu'][languages['current']]);
|
||||
if (languages['current'] != getCookie('lang')) setCookie('lang', languages['current']);
|
||||
if (persistLang && languages['current'] != getCookie('lang')) setCookie('lang', languages['current'], 36500);
|
||||
if($("#table").length>0) $('#table').bootstrapTable('refreshOptions', { 'locale': languages['current']});
|
||||
}
|
||||
$.each($(dom + ' [langtag]'), function (i, item) {
|
||||
@@ -122,7 +139,7 @@ $(document).ready(function () {
|
||||
$('body').cloudLang();
|
||||
$('body').on('click','li[lang]',function(){
|
||||
$('#languagemenu').attr('lang',$(this).attr('lang'));
|
||||
$('body').setLang ('');
|
||||
$('body').setLang ('', true);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user