Welcome to mirror list, hosted at ThFree Co, Russian Federation.

entity.go « entity « web - github.com/MHSanaei/3x-ui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 533fe97381e9f2271d130f7e279e068ef9bc88ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package entity

import (
	"crypto/tls"
	"net"
	"strings"
	"time"

	"x-ui/util/common"
)

type Msg struct {
	Success bool        `json:"success"`
	Msg     string      `json:"msg"`
	Obj     interface{} `json:"obj"`
}

type AllSetting struct {
	WebListen        string `json:"webListen" form:"webListen"`
	WebDomain        string `json:"webDomain" form:"webDomain"`
	WebPort          int    `json:"webPort" form:"webPort"`
	WebCertFile      string `json:"webCertFile" form:"webCertFile"`
	WebKeyFile       string `json:"webKeyFile" form:"webKeyFile"`
	WebBasePath      string `json:"webBasePath" form:"webBasePath"`
	SessionMaxAge    int    `json:"sessionMaxAge" form:"sessionMaxAge"`
	PageSize         int    `json:"pageSize" form:"pageSize"`
	ExpireDiff       int    `json:"expireDiff" form:"expireDiff"`
	TrafficDiff      int    `json:"trafficDiff" form:"trafficDiff"`
	RemarkModel      string `json:"remarkModel" form:"remarkModel"`
	TgBotEnable      bool   `json:"tgBotEnable" form:"tgBotEnable"`
	TgBotToken       string `json:"tgBotToken" form:"tgBotToken"`
	TgBotProxy       string `json:"tgBotProxy" form:"tgBotProxy"`
	TgBotChatId      string `json:"tgBotChatId" form:"tgBotChatId"`
	TgRunTime        string `json:"tgRunTime" form:"tgRunTime"`
	TgBotBackup      bool   `json:"tgBotBackup" form:"tgBotBackup"`
	TgBotLoginNotify bool   `json:"tgBotLoginNotify" form:"tgBotLoginNotify"`
	TgCpu            int    `json:"tgCpu" form:"tgCpu"`
	TgLang           string `json:"tgLang" form:"tgLang"`
	TimeLocation     string `json:"timeLocation" form:"timeLocation"`
	SecretEnable     bool   `json:"secretEnable" form:"secretEnable"`
	SubEnable        bool   `json:"subEnable" form:"subEnable"`
	SubListen        string `json:"subListen" form:"subListen"`
	SubPort          int    `json:"subPort" form:"subPort"`
	SubPath          string `json:"subPath" form:"subPath"`
	SubDomain        string `json:"subDomain" form:"subDomain"`
	SubCertFile      string `json:"subCertFile" form:"subCertFile"`
	SubKeyFile       string `json:"subKeyFile" form:"subKeyFile"`
	SubUpdates       int    `json:"subUpdates" form:"subUpdates"`
	SubEncrypt       bool   `json:"subEncrypt" form:"subEncrypt"`
	SubShowInfo      bool   `json:"subShowInfo" form:"subShowInfo"`
	SubURI           string `json:"subURI" form:"subURI"`
	SubJsonPath      string `json:"subJsonPath" form:"subJsonPath"`
	SubJsonURI       string `json:"subJsonURI" form:"subJsonURI"`
	SubJsonFragment  string `json:"subJsonFragment" form:"subJsonFragment"`
	SubJsonMux       string `json:"subJsonMux" form:"subJsonMux"`
	SubJsonRules     string `json:"subJsonRules" form:"subJsonRules"`
	Datepicker       string `json:"datepicker" form:"datepicker"`
}

func (s *AllSetting) CheckValid() error {
	if s.WebListen != "" {
		ip := net.ParseIP(s.WebListen)
		if ip == nil {
			return common.NewError("web listen is not valid ip:", s.WebListen)
		}
	}

	if s.SubListen != "" {
		ip := net.ParseIP(s.SubListen)
		if ip == nil {
			return common.NewError("Sub listen is not valid ip:", s.SubListen)
		}
	}

	if s.WebPort <= 0 || s.WebPort > 65535 {
		return common.NewError("web port is not a valid port:", s.WebPort)
	}

	if s.SubPort <= 0 || s.SubPort > 65535 {
		return common.NewError("Sub port is not a valid port:", s.SubPort)
	}

	if (s.SubPort == s.WebPort) && (s.WebListen == s.SubListen) {
		return common.NewError("Sub and Web could not use same ip:port, ", s.SubListen, ":", s.SubPort, " & ", s.WebListen, ":", s.WebPort)
	}

	if s.WebCertFile != "" || s.WebKeyFile != "" {
		_, err := tls.LoadX509KeyPair(s.WebCertFile, s.WebKeyFile)
		if err != nil {
			return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.WebCertFile, s.WebKeyFile, err)
		}
	}

	if s.SubCertFile != "" || s.SubKeyFile != "" {
		_, err := tls.LoadX509KeyPair(s.SubCertFile, s.SubKeyFile)
		if err != nil {
			return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.SubCertFile, s.SubKeyFile, err)
		}
	}

	if !strings.HasPrefix(s.WebBasePath, "/") {
		s.WebBasePath = "/" + s.WebBasePath
	}
	if !strings.HasSuffix(s.WebBasePath, "/") {
		s.WebBasePath += "/"
	}
	if !strings.HasPrefix(s.SubPath, "/") {
		s.SubPath = "/" + s.SubPath
	}
	if !strings.HasSuffix(s.SubPath, "/") {
		s.SubPath += "/"
	}

	if !strings.HasPrefix(s.SubJsonPath, "/") {
		s.SubJsonPath = "/" + s.SubJsonPath
	}
	if !strings.HasSuffix(s.SubJsonPath, "/") {
		s.SubJsonPath += "/"
	}

	_, err := time.LoadLocation(s.TimeLocation)
	if err != nil {
		return common.NewError("time location not exist:", s.TimeLocation)
	}

	return nil
}