new: add random secret if not provided
This commit is contained in:
@@ -2,8 +2,10 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -70,6 +72,9 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
if opt.EnableClashApi {
|
if opt.EnableClashApi {
|
||||||
|
if opt.ClashApiSecret == "" {
|
||||||
|
opt.ClashApiSecret = generateRandomString(16)
|
||||||
|
}
|
||||||
options.Experimental = &option.ExperimentalOptions{
|
options.Experimental = &option.ExperimentalOptions{
|
||||||
ClashAPI: &option.ClashAPIOptions{
|
ClashAPI: &option.ClashAPIOptions{
|
||||||
ExternalController: fmt.Sprintf("%s:%d", "127.0.0.1", opt.ClashApiPort),
|
ExternalController: fmt.Sprintf("%s:%d", "127.0.0.1", opt.ClashApiPort),
|
||||||
@@ -632,3 +637,21 @@ func removeDuplicateStr(strSlice []string) []string {
|
|||||||
}
|
}
|
||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func generateRandomString(length int) string {
|
||||||
|
// Determine the number of bytes needed
|
||||||
|
bytesNeeded := (length*6 + 7) / 8
|
||||||
|
|
||||||
|
// Generate random bytes
|
||||||
|
randomBytes := make([]byte, bytesNeeded)
|
||||||
|
_, err := rand.Read(randomBytes)
|
||||||
|
if err != nil {
|
||||||
|
return "hiddify"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode random bytes to base64
|
||||||
|
randomString := base64.URLEncoding.EncodeToString(randomBytes)
|
||||||
|
|
||||||
|
// Trim padding characters and return the string
|
||||||
|
return randomString[:length]
|
||||||
|
}
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ func DefaultConfigOptions() *ConfigOptions {
|
|||||||
LogLevel: "warn",
|
LogLevel: "warn",
|
||||||
EnableClashApi: true,
|
EnableClashApi: true,
|
||||||
ClashApiPort: 6756,
|
ClashApiPort: 6756,
|
||||||
ClashApiSecret: "hiddify",
|
ClashApiSecret: "",
|
||||||
GeoIPPath: "geoip.db",
|
GeoIPPath: "geoip.db",
|
||||||
GeoSitePath: "geosite.db",
|
GeoSitePath: "geosite.db",
|
||||||
Rules: []Rule{},
|
Rules: []Rule{},
|
||||||
|
|||||||
Reference in New Issue
Block a user