mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-01-11 08:12:46 +00:00
Refactor code and fix linter warnings (#3627)
Some checks are pending
Release 3X-UI / build (386) (push) Waiting to run
Release 3X-UI / build (amd64) (push) Waiting to run
Release 3X-UI / build (arm64) (push) Waiting to run
Release 3X-UI / build (armv5) (push) Waiting to run
Release 3X-UI / build (armv6) (push) Waiting to run
Release 3X-UI / build (armv7) (push) Waiting to run
Release 3X-UI / build (s390x) (push) Waiting to run
Release 3X-UI / Build for Windows (push) Waiting to run
Some checks are pending
Release 3X-UI / build (386) (push) Waiting to run
Release 3X-UI / build (amd64) (push) Waiting to run
Release 3X-UI / build (arm64) (push) Waiting to run
Release 3X-UI / build (armv5) (push) Waiting to run
Release 3X-UI / build (armv6) (push) Waiting to run
Release 3X-UI / build (armv7) (push) Waiting to run
Release 3X-UI / build (s390x) (push) Waiting to run
Release 3X-UI / Build for Windows (push) Waiting to run
* refactor: use any instead of empty interface * refactor: code cleanup
This commit is contained in:
parent
4800f8fb70
commit
6041d10e3d
16 changed files with 61 additions and 47 deletions
|
|
@ -4,6 +4,7 @@ import (
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"maps"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/mhsanaei/3x-ui/v2/database/model"
|
"github.com/mhsanaei/3x-ui/v2/database/model"
|
||||||
|
|
@ -197,9 +198,8 @@ func (s *SubJsonService) getConfig(inbound *model.Inbound, client model.Client,
|
||||||
|
|
||||||
newOutbounds = append(newOutbounds, s.defaultOutbounds...)
|
newOutbounds = append(newOutbounds, s.defaultOutbounds...)
|
||||||
newConfigJson := make(map[string]any)
|
newConfigJson := make(map[string]any)
|
||||||
for key, value := range s.configJson {
|
maps.Copy(newConfigJson, s.configJson)
|
||||||
newConfigJson[key] = value
|
|
||||||
}
|
|
||||||
newConfigJson["outbounds"] = newOutbounds
|
newConfigJson["outbounds"] = newOutbounds
|
||||||
newConfigJson["remarks"] = s.SubService.genRemark(inbound, client.Email, extPrxy["remark"].(string))
|
newConfigJson["remarks"] = s.SubService.genRemark(inbound, client.Email, extPrxy["remark"].(string))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -484,8 +484,8 @@ func (s *SubService) genVlessLink(inbound *model.Inbound, email string) string {
|
||||||
externalProxies, _ := stream["externalProxy"].([]any)
|
externalProxies, _ := stream["externalProxy"].([]any)
|
||||||
|
|
||||||
if len(externalProxies) > 0 {
|
if len(externalProxies) > 0 {
|
||||||
links := ""
|
links := make([]string, 0, len(externalProxies))
|
||||||
for index, externalProxy := range externalProxies {
|
for _, externalProxy := range externalProxies {
|
||||||
ep, _ := externalProxy.(map[string]any)
|
ep, _ := externalProxy.(map[string]any)
|
||||||
newSecurity, _ := ep["forceTls"].(string)
|
newSecurity, _ := ep["forceTls"].(string)
|
||||||
dest, _ := ep["dest"].(string)
|
dest, _ := ep["dest"].(string)
|
||||||
|
|
@ -511,12 +511,9 @@ func (s *SubService) genVlessLink(inbound *model.Inbound, email string) string {
|
||||||
|
|
||||||
url.Fragment = s.genRemark(inbound, email, ep["remark"].(string))
|
url.Fragment = s.genRemark(inbound, email, ep["remark"].(string))
|
||||||
|
|
||||||
if index > 0 {
|
links = append(links, url.String())
|
||||||
links += "\n"
|
|
||||||
}
|
}
|
||||||
links += url.String()
|
return strings.Join(links, "\n")
|
||||||
}
|
|
||||||
return links
|
|
||||||
}
|
}
|
||||||
|
|
||||||
link := fmt.Sprintf("vless://%s@%s:%d", uuid, address, port)
|
link := fmt.Sprintf("vless://%s@%s:%d", uuid, address, port)
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,5 @@ func HashPasswordAsBcrypt(password string) (string, error) {
|
||||||
|
|
||||||
// CheckPasswordHash verifies if the given password matches the bcrypt hash.
|
// CheckPasswordHash verifies if the given password matches the bcrypt hash.
|
||||||
func CheckPasswordHash(hash, password string) bool {
|
func CheckPasswordHash(hash, password string) bool {
|
||||||
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) == nil
|
||||||
return err == nil
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,13 +24,22 @@ type Config struct {
|
||||||
// FetchVlessFlags returns map[email]enabled
|
// FetchVlessFlags returns map[email]enabled
|
||||||
func FetchVlessFlags(cfg Config) (map[string]bool, error) {
|
func FetchVlessFlags(cfg Config) (map[string]bool, error) {
|
||||||
addr := fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
|
addr := fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
|
||||||
var conn *ldap.Conn
|
|
||||||
var err error
|
scheme := "ldap"
|
||||||
if cfg.UseTLS {
|
if cfg.UseTLS {
|
||||||
conn, err = ldap.DialTLS("tcp", addr, &tls.Config{InsecureSkipVerify: false})
|
scheme = "ldaps"
|
||||||
} else {
|
|
||||||
conn, err = ldap.Dial("tcp", addr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ldapURL := fmt.Sprintf("%s://%s", scheme, addr)
|
||||||
|
|
||||||
|
var opts []ldap.DialOpt
|
||||||
|
if cfg.UseTLS {
|
||||||
|
opts = append(opts, ldap.DialWithTLSConfig(&tls.Config{
|
||||||
|
InsecureSkipVerify: false,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := ldap.DialURL(ldapURL, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -91,13 +100,22 @@ func FetchVlessFlags(cfg Config) (map[string]bool, error) {
|
||||||
// AuthenticateUser searches user by cfg.UserAttr and attempts to bind with provided password.
|
// AuthenticateUser searches user by cfg.UserAttr and attempts to bind with provided password.
|
||||||
func AuthenticateUser(cfg Config, username, password string) (bool, error) {
|
func AuthenticateUser(cfg Config, username, password string) (bool, error) {
|
||||||
addr := fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
|
addr := fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
|
||||||
var conn *ldap.Conn
|
|
||||||
var err error
|
scheme := "ldap"
|
||||||
if cfg.UseTLS {
|
if cfg.UseTLS {
|
||||||
conn, err = ldap.DialTLS("tcp", addr, &tls.Config{InsecureSkipVerify: false})
|
scheme = "ldaps"
|
||||||
} else {
|
|
||||||
conn, err = ldap.Dial("tcp", addr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ldapURL := fmt.Sprintf("%s://%s", scheme, addr)
|
||||||
|
|
||||||
|
var opts []ldap.DialOpt
|
||||||
|
if cfg.UseTLS {
|
||||||
|
opts = append(opts, ldap.DialWithTLSConfig(&tls.Config{
|
||||||
|
InsecureSkipVerify: false,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := ldap.DialURL(ldapURL, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,10 +18,10 @@ var (
|
||||||
// init initializes the character sequences used for random string generation.
|
// init initializes the character sequences used for random string generation.
|
||||||
// It sets up arrays for numbers, lowercase letters, uppercase letters, and combinations.
|
// It sets up arrays for numbers, lowercase letters, uppercase letters, and combinations.
|
||||||
func init() {
|
func init() {
|
||||||
for i := 0; i < 10; i++ {
|
for i := range 10 {
|
||||||
numSeq[i] = rune('0' + i)
|
numSeq[i] = rune('0' + i)
|
||||||
}
|
}
|
||||||
for i := 0; i < 26; i++ {
|
for i := range 26 {
|
||||||
lowerSeq[i] = rune('a' + i)
|
lowerSeq[i] = rune('a' + i)
|
||||||
upperSeq[i] = rune('A' + i)
|
upperSeq[i] = rune('A' + i)
|
||||||
}
|
}
|
||||||
|
|
@ -40,7 +40,7 @@ func init() {
|
||||||
// Seq generates a random string of length n containing alphanumeric characters (numbers, lowercase and uppercase letters).
|
// Seq generates a random string of length n containing alphanumeric characters (numbers, lowercase and uppercase letters).
|
||||||
func Seq(n int) string {
|
func Seq(n int) string {
|
||||||
runes := make([]rune, n)
|
runes := make([]rune, n)
|
||||||
for i := 0; i < n; i++ {
|
for i := range n {
|
||||||
idx, err := rand.Int(rand.Reader, big.NewInt(int64(len(allSeq))))
|
idx, err := rand.Int(rand.Reader, big.NewInt(int64(len(allSeq))))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("crypto/rand failed: " + err.Error())
|
panic("crypto/rand failed: " + err.Error())
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ import "reflect"
|
||||||
func GetFields(t reflect.Type) []reflect.StructField {
|
func GetFields(t reflect.Type) []reflect.StructField {
|
||||||
num := t.NumField()
|
num := t.NumField()
|
||||||
fields := make([]reflect.StructField, 0, num)
|
fields := make([]reflect.StructField, 0, num)
|
||||||
for i := 0; i < num; i++ {
|
for i := range num {
|
||||||
fields = append(fields, t.Field(i))
|
fields = append(fields, t.Field(i))
|
||||||
}
|
}
|
||||||
return fields
|
return fields
|
||||||
|
|
@ -17,7 +17,7 @@ func GetFields(t reflect.Type) []reflect.StructField {
|
||||||
func GetFieldValues(v reflect.Value) []reflect.Value {
|
func GetFieldValues(v reflect.Value) []reflect.Value {
|
||||||
num := v.NumField()
|
num := v.NumField()
|
||||||
fields := make([]reflect.Value, 0, num)
|
fields := make([]reflect.Value, 0, num)
|
||||||
for i := 0; i < num; i++ {
|
for i := range num {
|
||||||
fields = append(fields, v.Field(i))
|
fields = append(fields, v.Field(i))
|
||||||
}
|
}
|
||||||
return fields
|
return fields
|
||||||
|
|
|
||||||
|
|
@ -47,11 +47,11 @@ func CPUPercentRaw() (float64, error) {
|
||||||
var out [5]uint64
|
var out [5]uint64
|
||||||
switch len(raw) {
|
switch len(raw) {
|
||||||
case 5 * 8:
|
case 5 * 8:
|
||||||
for i := 0; i < 5; i++ {
|
for i := range 5 {
|
||||||
out[i] = binary.LittleEndian.Uint64(raw[i*8 : (i+1)*8])
|
out[i] = binary.LittleEndian.Uint64(raw[i*8 : (i+1)*8])
|
||||||
}
|
}
|
||||||
case 5 * 4:
|
case 5 * 4:
|
||||||
for i := 0; i < 5; i++ {
|
for i := range 5 {
|
||||||
out[i] = uint64(binary.LittleEndian.Uint32(raw[i*4 : (i+1)*4]))
|
out[i] = uint64(binary.LittleEndian.Uint32(raw[i*4 : (i+1)*4]))
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|
|
||||||
|
|
@ -210,10 +210,10 @@ func (a *ServerController) getXrayLogs(c *gin.Context) {
|
||||||
//getting tags for freedom and blackhole outbounds
|
//getting tags for freedom and blackhole outbounds
|
||||||
config, err := a.settingService.GetDefaultXrayConfig()
|
config, err := a.settingService.GetDefaultXrayConfig()
|
||||||
if err == nil && config != nil {
|
if err == nil && config != nil {
|
||||||
if cfgMap, ok := config.(map[string]interface{}); ok {
|
if cfgMap, ok := config.(map[string]any); ok {
|
||||||
if outbounds, ok := cfgMap["outbounds"].([]interface{}); ok {
|
if outbounds, ok := cfgMap["outbounds"].([]any); ok {
|
||||||
for _, outbound := range outbounds {
|
for _, outbound := range outbounds {
|
||||||
if obMap, ok := outbound.(map[string]interface{}); ok {
|
if obMap, ok := outbound.(map[string]any); ok {
|
||||||
switch obMap["protocol"] {
|
switch obMap["protocol"] {
|
||||||
case "freedom":
|
case "freedom":
|
||||||
if tag, ok := obMap["tag"].(string); ok {
|
if tag, ok := obMap["tag"].(string); ok {
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ var (
|
||||||
type WebServer interface {
|
type WebServer interface {
|
||||||
GetCron() *cron.Cron // Get the cron scheduler
|
GetCron() *cron.Cron // Get the cron scheduler
|
||||||
GetCtx() context.Context // Get the server context
|
GetCtx() context.Context // Get the server context
|
||||||
GetWSHub() interface{} // Get the WebSocket hub (using interface{} to avoid circular dependency)
|
GetWSHub() any // Get the WebSocket hub (using any to avoid circular dependency)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SubServer interface defines methods for accessing the subscription server instance.
|
// SubServer interface defines methods for accessing the subscription server instance.
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ func (j *ClearLogsJob) Run() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear log files and copy to previous logs
|
// Clear log files and copy to previous logs
|
||||||
for i := 0; i < len(logFiles); i++ {
|
for i := range len(logFiles) {
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
// Copy to previous logs
|
// Copy to previous logs
|
||||||
logFilePrev, err := os.OpenFile(logFilesPrev[i-1], os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
|
logFilePrev, err := os.OpenFile(logFilesPrev[i-1], os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
|
||||||
|
|
|
||||||
|
|
@ -1205,7 +1205,7 @@ func (s *ServerService) GetNewmldsa65() (any, error) {
|
||||||
return keyPair, nil
|
return keyPair, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServerService) GetNewEchCert(sni string) (interface{}, error) {
|
func (s *ServerService) GetNewEchCert(sni string) (any, error) {
|
||||||
// Run the command
|
// Run the command
|
||||||
cmd := exec.Command(xray.GetBinaryPath(), "tls", "ech", "--serverName", sni)
|
cmd := exec.Command(xray.GetBinaryPath(), "tls", "ech", "--serverName", sni)
|
||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
|
|
@ -1223,7 +1223,7 @@ func (s *ServerService) GetNewEchCert(sni string) (interface{}, error) {
|
||||||
configList := lines[1]
|
configList := lines[1]
|
||||||
serverKeys := lines[3]
|
serverKeys := lines[3]
|
||||||
|
|
||||||
return map[string]interface{}{
|
return map[string]any{
|
||||||
"echServerKeys": serverKeys,
|
"echServerKeys": serverKeys,
|
||||||
"echConfigList": configList,
|
"echConfigList": configList,
|
||||||
}, nil
|
}, nil
|
||||||
|
|
|
||||||
|
|
@ -487,6 +487,6 @@ func (s *Server) GetCron() *cron.Cron {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetWSHub returns the WebSocket hub instance.
|
// GetWSHub returns the WebSocket hub instance.
|
||||||
func (s *Server) GetWSHub() interface{} {
|
func (s *Server) GetWSHub() any {
|
||||||
return s.wsHub
|
return s.wsHub
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ const (
|
||||||
// Message represents a WebSocket message
|
// Message represents a WebSocket message
|
||||||
type Message struct {
|
type Message struct {
|
||||||
Type MessageType `json:"type"`
|
Type MessageType `json:"type"`
|
||||||
Payload interface{} `json:"payload"`
|
Payload any `json:"payload"`
|
||||||
Time int64 `json:"time"`
|
Time int64 `json:"time"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -250,7 +250,7 @@ func (h *Hub) broadcastParallel(clients []*Client, message []byte) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Broadcast sends a message to all connected clients
|
// Broadcast sends a message to all connected clients
|
||||||
func (h *Hub) Broadcast(messageType MessageType, payload interface{}) {
|
func (h *Hub) Broadcast(messageType MessageType, payload any) {
|
||||||
if h == nil {
|
if h == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -289,7 +289,7 @@ func (h *Hub) Broadcast(messageType MessageType, payload interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// BroadcastToTopic sends a message only to clients subscribed to the specific topic
|
// BroadcastToTopic sends a message only to clients subscribed to the specific topic
|
||||||
func (h *Hub) BroadcastToTopic(messageType MessageType, payload interface{}) {
|
func (h *Hub) BroadcastToTopic(messageType MessageType, payload any) {
|
||||||
if h == nil {
|
if h == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ func GetHub() *Hub {
|
||||||
}
|
}
|
||||||
|
|
||||||
// BroadcastStatus broadcasts server status update to all connected clients
|
// BroadcastStatus broadcasts server status update to all connected clients
|
||||||
func BroadcastStatus(status interface{}) {
|
func BroadcastStatus(status any) {
|
||||||
hub := GetHub()
|
hub := GetHub()
|
||||||
if hub != nil {
|
if hub != nil {
|
||||||
hub.Broadcast(MessageTypeStatus, status)
|
hub.Broadcast(MessageTypeStatus, status)
|
||||||
|
|
@ -33,7 +33,7 @@ func BroadcastStatus(status interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// BroadcastTraffic broadcasts traffic statistics update to all connected clients
|
// BroadcastTraffic broadcasts traffic statistics update to all connected clients
|
||||||
func BroadcastTraffic(traffic interface{}) {
|
func BroadcastTraffic(traffic any) {
|
||||||
hub := GetHub()
|
hub := GetHub()
|
||||||
if hub != nil {
|
if hub != nil {
|
||||||
hub.Broadcast(MessageTypeTraffic, traffic)
|
hub.Broadcast(MessageTypeTraffic, traffic)
|
||||||
|
|
@ -41,7 +41,7 @@ func BroadcastTraffic(traffic interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// BroadcastInbounds broadcasts inbounds list update to all connected clients
|
// BroadcastInbounds broadcasts inbounds list update to all connected clients
|
||||||
func BroadcastInbounds(inbounds interface{}) {
|
func BroadcastInbounds(inbounds any) {
|
||||||
hub := GetHub()
|
hub := GetHub()
|
||||||
if hub != nil {
|
if hub != nil {
|
||||||
hub.Broadcast(MessageTypeInbounds, inbounds)
|
hub.Broadcast(MessageTypeInbounds, inbounds)
|
||||||
|
|
|
||||||
|
|
@ -116,7 +116,7 @@ func (x *XrayAPI) AddUser(Protocol string, inboundTag string, user map[string]an
|
||||||
}
|
}
|
||||||
// Add testseed if provided
|
// Add testseed if provided
|
||||||
if testseedVal, ok := user["testseed"]; ok {
|
if testseedVal, ok := user["testseed"]; ok {
|
||||||
if testseedArr, ok := testseedVal.([]interface{}); ok && len(testseedArr) >= 4 {
|
if testseedArr, ok := testseedVal.([]any); ok && len(testseedArr) >= 4 {
|
||||||
testseed := make([]uint32, len(testseedArr))
|
testseed := make([]uint32, len(testseedArr))
|
||||||
for i, v := range testseedArr {
|
for i, v := range testseedArr {
|
||||||
if num, ok := v.(float64); ok {
|
if num, ok := v.(float64); ok {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue