2026-02-17 13:06:23 +08:00
|
|
|
|
package srvconfig
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
"net"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"os"
|
|
|
|
|
|
"regexp"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"xiawan/wx/clientsdk/baseinfo"
|
|
|
|
|
|
"xiawan/wx/clientsdk/baseutils"
|
2026-02-26 10:44:13 +08:00
|
|
|
|
"xiawan/wx/clientsdk/mmtls"
|
2026-02-17 13:06:23 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/gogf/gf/database/gredis"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// GlobalSetting 全局设置
|
|
|
|
|
|
var GlobalSetting Setting
|
|
|
|
|
|
|
|
|
|
|
|
// TaskExecWaitTimes 任务执行间隔时间 500毫秒
|
|
|
|
|
|
var TaskExecWaitTimes = uint32(500)
|
|
|
|
|
|
|
2026-02-26 10:44:13 +08:00
|
|
|
|
// ProxyConfig 代理配置
|
|
|
|
|
|
type ProxyConfig struct {
|
|
|
|
|
|
// 代理失效后是否允许直连(false=不允许,连接失败;true=允许,降级为直连)
|
|
|
|
|
|
AllowDirectOnProxyFail bool `json:"allowDirectOnProxyFail"`
|
|
|
|
|
|
// 长连接超时时间(秒)
|
|
|
|
|
|
LongConnTimeout int `json:"longConnTimeout"`
|
|
|
|
|
|
LongConnReadTimeout int `json:"longConnReadTimeout"`
|
|
|
|
|
|
// 长连接建立时的重试次数
|
|
|
|
|
|
LongConnRetryTimes int `json:"longConnRetryTimes"`
|
|
|
|
|
|
// 长连接建立时的重试间隔(毫秒)
|
|
|
|
|
|
LongConnRetryInterval int `json:"longConnRetryInterval"`
|
|
|
|
|
|
// 短连接超时时间(秒)
|
|
|
|
|
|
ShortConnTimeout int `json:"shortConnTimeout"`
|
|
|
|
|
|
// 长连接断开后最大重试恢复次数
|
|
|
|
|
|
MaxLongRetryTimes int `json:"maxLongRetryTimes"`
|
|
|
|
|
|
// 长连接断开后重试间隔(秒)
|
|
|
|
|
|
LongRetryInterval int `json:"longRetryInterval"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-17 13:06:23 +08:00
|
|
|
|
// Setting 设置
|
|
|
|
|
|
type Setting struct {
|
|
|
|
|
|
Debug bool `json:"debug"`
|
|
|
|
|
|
Host string `json:"host"`
|
|
|
|
|
|
Port string `json:"port"`
|
|
|
|
|
|
ApiVersion string `json:"apiVersion"`
|
|
|
|
|
|
OpenClearMsg bool `json:"openClearMsg"`
|
|
|
|
|
|
SycnTargetIpUrl string `json:"sycnTargetIpUrl"` // 同步外网IP地址的url
|
|
|
|
|
|
|
|
|
|
|
|
WorkerPoolSize uint32 `json:"workerpoolsize"`
|
|
|
|
|
|
MaxWorkerTaskLen uint32 `json:"maxworkertasklen"`
|
|
|
|
|
|
WebDomain string `json:"webdomain"`
|
|
|
|
|
|
WebDomainNum string `json:"webdomainnum"`
|
|
|
|
|
|
SyncOnlineCount string `json:"syncOnlineCount"`
|
|
|
|
|
|
WebTaskName string `json:"webtaskname"`
|
|
|
|
|
|
WebTaskAppNumber string `json:"webtaskappnumber"`
|
|
|
|
|
|
//Redis
|
|
|
|
|
|
RedisConfig gredis.Config `json:"redisConfig"`
|
|
|
|
|
|
//消息同步 是否按微信id 发布消息
|
|
|
|
|
|
NewsSynWxId bool `json:"newsSynWxId"`
|
|
|
|
|
|
Dt bool `json:"dt"`
|
|
|
|
|
|
MysqlConnectStr string `json:"mySqlConnectStr"`
|
|
|
|
|
|
InitializeMySQLTables bool `json:"initializeMySQLTables"`
|
|
|
|
|
|
UserInputKey string `json:"userInputKey"`
|
|
|
|
|
|
Version string `json:"version"`
|
|
|
|
|
|
HttpSyncMsg bool `json:"httpSyncMsg"`
|
|
|
|
|
|
IsInitOld bool `json:"isInitOld"`
|
|
|
|
|
|
WsSyncMsg bool `json:"wsSyncMsg"`
|
|
|
|
|
|
SyncHisyory bool `json:"syncHisyory"`
|
|
|
|
|
|
LoginBeforeRequestUrl string `json:"loginBeforeRequestUrl"` // 登录前会请求该 url(二维码和唤醒)
|
|
|
|
|
|
// 当前服务器外网IP地址
|
|
|
|
|
|
TargetIp string `json:"targetIp"`
|
|
|
|
|
|
// 当前 HTTP-Api 服务的本地地址 基址
|
|
|
|
|
|
LocalApiUrl string
|
|
|
|
|
|
// 推广关注自己的微信公众号(新用户首次登录时关注)
|
|
|
|
|
|
GhWxid string `json:"ghWxid"`
|
|
|
|
|
|
// 管理接口授权 ADMIN_KEY
|
|
|
|
|
|
AdminKey string `json:"adminKey"`
|
|
|
|
|
|
// 是否开启 mac2ipad
|
|
|
|
|
|
Mac2Ipad bool `json:"mac2ipad"`
|
|
|
|
|
|
// 是否开启 car2ipad
|
|
|
|
|
|
Car2Ipad bool `json:"car2ipad"`
|
2026-02-26 10:44:13 +08:00
|
|
|
|
// 代理配置
|
|
|
|
|
|
ProxyConfig ProxyConfig `json:"proxyConfig"`
|
2026-02-17 13:06:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// getExternal 请求获取外网ip
|
|
|
|
|
|
func getExternal() string {
|
|
|
|
|
|
resp, err := http.Get(GlobalSetting.SycnTargetIpUrl)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fmt.Printf("获取 ip 失败 -> 退出程序!---->")
|
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
|
// return fmt.Sprintf("%s:%s", GlobalSetting.Host, GlobalSetting.Port)
|
|
|
|
|
|
}
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
content, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
|
|
if content == nil {
|
|
|
|
|
|
fmt.Printf("获取 ip 失败 -> 退出程序!---->")
|
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
|
// return fmt.Sprintf("%s:%s", GlobalSetting.Host, GlobalSetting.Port)
|
|
|
|
|
|
}
|
|
|
|
|
|
// 校验一下是不是 ip
|
|
|
|
|
|
if net.ParseIP(string(content)) == nil {
|
|
|
|
|
|
fmt.Printf("获取 ip 失败 -> 退出程序!---->")
|
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
|
}
|
|
|
|
|
|
fmt.Printf("获取 ip 成功 -> %s ----> %s\n", content, GlobalSetting.SycnTargetIpUrl)
|
|
|
|
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s:%s", content, GlobalSetting.Port)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func isValidWeChatID(wxid string) bool {
|
|
|
|
|
|
if strings.TrimSpace(wxid) == "" {
|
|
|
|
|
|
GlobalSetting.GhWxid = ""
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
regex := `^[a-zA-Z][a-zA-Z0-9_-]{5,19}$`
|
|
|
|
|
|
r := regexp.MustCompile(regex)
|
|
|
|
|
|
return r.MatchString(wxid)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func ConfigSetUp() {
|
|
|
|
|
|
tmpHomeDir, err := os.Getwd()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
baseutils.PrintLog(err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
baseinfo.HomeDIR = tmpHomeDir
|
|
|
|
|
|
|
|
|
|
|
|
// 读取配置文件
|
|
|
|
|
|
configData, err := baseutils.ReadFile(baseinfo.HomeDIR + "/assets/setting.json")
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
err = json.Unmarshal(configData, &GlobalSetting)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if len(strings.TrimSpace(GlobalSetting.AdminKey)) == 0 {
|
|
|
|
|
|
// 默认授权admin_key
|
|
|
|
|
|
GlobalSetting.AdminKey = "df704bd7012548049e68db25aff0507f"
|
|
|
|
|
|
}
|
|
|
|
|
|
fmt.Println("======== ADMIN_KEY ===", GlobalSetting.AdminKey, "========")
|
|
|
|
|
|
|
|
|
|
|
|
if !isValidWeChatID(GlobalSetting.GhWxid) {
|
|
|
|
|
|
fmt.Println("引流公众号(assets/setting.json ghWxid)不合法:", GlobalSetting.GhWxid, ";已重设为空")
|
|
|
|
|
|
GlobalSetting.GhWxid = ""
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 确保API版本格式正确,以斜杠开头
|
|
|
|
|
|
if GlobalSetting.ApiVersion != "" && !strings.HasPrefix(GlobalSetting.ApiVersion, "/") {
|
|
|
|
|
|
GlobalSetting.ApiVersion = "/" + GlobalSetting.ApiVersion
|
|
|
|
|
|
}
|
|
|
|
|
|
if GlobalSetting.ApiVersion == "/" {
|
|
|
|
|
|
GlobalSetting.ApiVersion = ""
|
|
|
|
|
|
}
|
|
|
|
|
|
// 如果ApiVersion不为空且不以斜杠结尾,则添加斜杠
|
|
|
|
|
|
if GlobalSetting.ApiVersion != "" && !strings.HasSuffix(GlobalSetting.ApiVersion, "/") {
|
|
|
|
|
|
GlobalSetting.ApiVersion += "/"
|
|
|
|
|
|
}
|
|
|
|
|
|
GlobalSetting.LocalApiUrl = fmt.Sprintf("http://127.0.0.1:%s%s", GlobalSetting.Port, GlobalSetting.ApiVersion)
|
|
|
|
|
|
|
|
|
|
|
|
// 获取当前服务器外网IP地址
|
|
|
|
|
|
// 如果没有指定 TargetIp, 同时 SycnTargetIpUrl 不为空, 则请求 SycnTargetIpUrl 获取外网 IP
|
|
|
|
|
|
if GlobalSetting.TargetIp == "" && GlobalSetting.SycnTargetIpUrl != "" {
|
|
|
|
|
|
GlobalSetting.TargetIp = getExternal()
|
|
|
|
|
|
}
|
2026-02-26 10:44:13 +08:00
|
|
|
|
|
|
|
|
|
|
// 设置代理配置默认值
|
|
|
|
|
|
if GlobalSetting.ProxyConfig.LongConnTimeout <= 0 {
|
|
|
|
|
|
GlobalSetting.ProxyConfig.LongConnTimeout = 15
|
|
|
|
|
|
}
|
|
|
|
|
|
if GlobalSetting.ProxyConfig.LongConnReadTimeout <= 0 {
|
|
|
|
|
|
readTimeout := GlobalSetting.ProxyConfig.LongConnTimeout
|
|
|
|
|
|
if readTimeout < 210 {
|
|
|
|
|
|
readTimeout = 210
|
|
|
|
|
|
}
|
|
|
|
|
|
GlobalSetting.ProxyConfig.LongConnReadTimeout = readTimeout
|
|
|
|
|
|
}
|
|
|
|
|
|
if GlobalSetting.ProxyConfig.LongConnRetryTimes <= 0 {
|
|
|
|
|
|
GlobalSetting.ProxyConfig.LongConnRetryTimes = 30
|
|
|
|
|
|
}
|
|
|
|
|
|
if GlobalSetting.ProxyConfig.LongConnRetryInterval <= 0 {
|
|
|
|
|
|
GlobalSetting.ProxyConfig.LongConnRetryInterval = 500
|
|
|
|
|
|
}
|
|
|
|
|
|
if GlobalSetting.ProxyConfig.ShortConnTimeout <= 0 {
|
|
|
|
|
|
GlobalSetting.ProxyConfig.ShortConnTimeout = 15
|
|
|
|
|
|
}
|
|
|
|
|
|
if GlobalSetting.ProxyConfig.MaxLongRetryTimes <= 0 {
|
|
|
|
|
|
GlobalSetting.ProxyConfig.MaxLongRetryTimes = 10
|
|
|
|
|
|
}
|
|
|
|
|
|
if GlobalSetting.ProxyConfig.LongRetryInterval <= 0 {
|
|
|
|
|
|
GlobalSetting.ProxyConfig.LongRetryInterval = 60
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 同步代理配置到 mmtls 全局配置
|
|
|
|
|
|
mmtls.GlobalProxyConfig.LongConnTimeout = GlobalSetting.ProxyConfig.LongConnTimeout
|
|
|
|
|
|
mmtls.GlobalProxyConfig.LongConnReadTimeout = GlobalSetting.ProxyConfig.LongConnReadTimeout
|
|
|
|
|
|
mmtls.GlobalProxyConfig.LongConnRetryTimes = GlobalSetting.ProxyConfig.LongConnRetryTimes
|
|
|
|
|
|
mmtls.GlobalProxyConfig.LongConnRetryInterval = GlobalSetting.ProxyConfig.LongConnRetryInterval
|
|
|
|
|
|
mmtls.GlobalProxyConfig.ShortConnTimeout = GlobalSetting.ProxyConfig.ShortConnTimeout
|
|
|
|
|
|
mmtls.GlobalProxyConfig.AllowDirectOnProxyFail = GlobalSetting.ProxyConfig.AllowDirectOnProxyFail
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Printf("======== 代理配置已同步 ========\n")
|
|
|
|
|
|
fmt.Printf("代理失效后允许直连: %v\n", GlobalSetting.ProxyConfig.AllowDirectOnProxyFail)
|
|
|
|
|
|
fmt.Printf("长连接超时: %d秒\n", GlobalSetting.ProxyConfig.LongConnTimeout)
|
|
|
|
|
|
fmt.Printf("长连接重试次数: %d次\n", GlobalSetting.ProxyConfig.LongConnRetryTimes)
|
|
|
|
|
|
fmt.Printf("长连接重试间隔: %dms\n", GlobalSetting.ProxyConfig.LongConnRetryInterval)
|
|
|
|
|
|
fmt.Printf("短连接超时: %d秒\n", GlobalSetting.ProxyConfig.ShortConnTimeout)
|
|
|
|
|
|
fmt.Printf("========================\n")
|
2026-02-17 13:06:23 +08:00
|
|
|
|
}
|