mirror of
https://github.com/yhl452493373/frps-panel.git
synced 2026-04-04 14:27:00 +08:00
change config from ini to toml (processing)
This commit is contained in:
@@ -1,13 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"frps-panel/pkg/server"
|
||||
"frps-panel/pkg/server/controller"
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/spf13/cobra"
|
||||
"gopkg.in/ini.v1"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -23,7 +20,7 @@ var (
|
||||
|
||||
func init() {
|
||||
rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "version of frps-panel")
|
||||
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "./frps-panel.ini", "config file of frps-panel")
|
||||
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "./frps-panel.toml", "config file of frps-panel")
|
||||
}
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
@@ -41,7 +38,10 @@ var rootCmd = &cobra.Command{
|
||||
}
|
||||
rootDir := filepath.Dir(executable)
|
||||
|
||||
config, tls, err := ParseConfigFile(configFile)
|
||||
configDir := filepath.Dir(configFile)
|
||||
tokensFile := filepath.Join(configDir, "frps-tokens.toml")
|
||||
|
||||
config, tls, err := parseConfigFile(configFile, tokensFile)
|
||||
if err != nil {
|
||||
log.Printf("fail to start frps-panel : %v", err)
|
||||
return err
|
||||
@@ -69,211 +69,59 @@ func Execute() {
|
||||
}
|
||||
}
|
||||
|
||||
func ParseConfigFile(file string) (controller.HandleController, server.TLS, error) {
|
||||
|
||||
var config Config
|
||||
readFile, _ := os.ReadFile("/Volumes/Working/Works/Git Sources/frps-panel/config/frps-panel.toml")
|
||||
_ = toml.Unmarshal(readFile, &config)
|
||||
log.Printf("%v", config)
|
||||
f, err := os.Create("/Volumes/Working/Works/Git Sources/frps-panel/config/frps-panel-new.toml")
|
||||
func parseConfigFile(configFile, tokensFile string) (controller.HandleController, server.TLS, error) {
|
||||
var config controller.Config
|
||||
_, err := toml.DecodeFile(configFile, &config)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := toml.NewEncoder(f).Encode(config); err != nil {
|
||||
// failed to encode
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := f.Close(); err != nil {
|
||||
// failed to close the file
|
||||
log.Fatal(err)
|
||||
|
||||
log.Fatalf("decode config file %v error: %v", configFile, err)
|
||||
}
|
||||
|
||||
common := controller.CommonInfo{}
|
||||
users := make(map[string]controller.TokenInfo)
|
||||
ports := make(map[string][]string)
|
||||
domains := make(map[string][]string)
|
||||
subdomains := make(map[string][]string)
|
||||
_, err = toml.DecodeFile(tokensFile, &config)
|
||||
if err != nil {
|
||||
log.Fatalf("decode token file %v error: %v", tokensFile, err)
|
||||
}
|
||||
|
||||
config.Common.DashboardTls = strings.HasPrefix("https://", strings.ToLower(config.Common.DashboardAddr))
|
||||
|
||||
//f, err := os.Create("/Volumes/Work/Git Sources/frps-panel/config/frps-panel-new.toml")
|
||||
//if err != nil {
|
||||
// log.Fatal(err)
|
||||
//}
|
||||
//if err := toml.NewEncoder(f).Encode(config); err != nil {
|
||||
// // failed to encode
|
||||
// log.Fatal(err)
|
||||
//}
|
||||
//if err := f.Close(); err != nil {
|
||||
// // failed to close the file
|
||||
// log.Fatal(err)
|
||||
//}
|
||||
|
||||
tls := server.TLS{
|
||||
Enable: false,
|
||||
Enable: config.Common.TlsMode,
|
||||
Protocol: "HTTP",
|
||||
}
|
||||
|
||||
iniFile, err := ini.LoadSources(ini.LoadOptions{
|
||||
Insensitive: false,
|
||||
InsensitiveSections: false,
|
||||
InsensitiveKeys: false,
|
||||
IgnoreInlineComment: true,
|
||||
AllowBooleanKeys: true,
|
||||
}, file)
|
||||
if err != nil {
|
||||
var pathError *fs.PathError
|
||||
if errors.As(err, &pathError) {
|
||||
log.Printf("token file %s not found", file)
|
||||
} else {
|
||||
log.Printf("fail to parse token file %s : %v", file, err)
|
||||
}
|
||||
return controller.HandleController{
|
||||
CommonInfo: common,
|
||||
Tokens: nil,
|
||||
Ports: nil,
|
||||
Domains: nil,
|
||||
Subdomains: nil,
|
||||
IniFile: iniFile,
|
||||
}, tls, err
|
||||
}
|
||||
|
||||
commonSection, err := iniFile.GetSection("common")
|
||||
if err != nil {
|
||||
log.Printf("fail to get [common] section from file %s : %v", file, err)
|
||||
return controller.HandleController{
|
||||
CommonInfo: common,
|
||||
Tokens: nil,
|
||||
Ports: nil,
|
||||
Domains: nil,
|
||||
Subdomains: nil,
|
||||
IniFile: iniFile,
|
||||
}, tls, err
|
||||
}
|
||||
common.PluginAddr = commonSection.Key("plugin_addr").MustString("0.0.0.0")
|
||||
common.PluginPort = commonSection.Key("plugin_port").MustInt(7200)
|
||||
common.User = commonSection.Key("admin_user").Value()
|
||||
common.Pwd = commonSection.Key("admin_pwd").Value()
|
||||
common.KeepTime = commonSection.Key("admin_keep_time").MustInt(0)
|
||||
common.DashboardAddr = commonSection.Key("dashboard_addr").MustString("127.0.0.1")
|
||||
common.DashboardPort = commonSection.Key("dashboard_port").MustInt(7500)
|
||||
common.DashboardUser = commonSection.Key("dashboard_user").Value()
|
||||
common.DashboardPwd = commonSection.Key("dashboard_pwd").Value()
|
||||
common.DashboardTLS = strings.HasPrefix(strings.ToLower(common.DashboardAddr), "https://")
|
||||
|
||||
if common.KeepTime < 0 {
|
||||
common.KeepTime = 0
|
||||
}
|
||||
|
||||
tls.Enable = commonSection.Key("tls_mode").MustBool(false)
|
||||
tls.Cert = commonSection.Key("tls_cert_file").MustString("")
|
||||
tls.Key = commonSection.Key("tls_key_file").MustString("")
|
||||
if tls.Enable {
|
||||
tls.Protocol = "HTTPS"
|
||||
}
|
||||
if tls.Enable && (strings.TrimSpace(tls.Cert) == "" || strings.TrimSpace(tls.Key) == "") {
|
||||
tls.Enable = false
|
||||
tls.Protocol = "HTTP"
|
||||
log.Printf("fail to enable tls: tls cert or key not exist, use http as default.")
|
||||
}
|
||||
|
||||
portsSection, err := iniFile.GetSection("ports")
|
||||
if err != nil {
|
||||
log.Printf("fail to get [ports] section from file %s : %v", file, err)
|
||||
return controller.HandleController{
|
||||
CommonInfo: common,
|
||||
Tokens: nil,
|
||||
Ports: nil,
|
||||
Domains: nil,
|
||||
Subdomains: nil,
|
||||
IniFile: iniFile,
|
||||
}, tls, err
|
||||
}
|
||||
for _, key := range portsSection.Keys() {
|
||||
user := key.Name()
|
||||
value := key.Value()
|
||||
port := strings.Split(controller.TrimAllSpaceReg.ReplaceAllString(value, ""), ",")
|
||||
ports[user] = port
|
||||
}
|
||||
|
||||
domainsSection, err := iniFile.GetSection("domains")
|
||||
if err != nil {
|
||||
log.Printf("fail to get [domains] section from file %s : %v", file, err)
|
||||
return controller.HandleController{
|
||||
CommonInfo: common,
|
||||
Tokens: nil,
|
||||
Ports: nil,
|
||||
Domains: nil,
|
||||
Subdomains: nil,
|
||||
IniFile: iniFile,
|
||||
}, tls, err
|
||||
}
|
||||
for _, key := range domainsSection.Keys() {
|
||||
user := key.Name()
|
||||
value := key.Value()
|
||||
domain := strings.Split(controller.TrimAllSpaceReg.ReplaceAllString(value, ""), ",")
|
||||
domains[user] = domain
|
||||
}
|
||||
|
||||
subdomainsSection, err := iniFile.GetSection("subdomains")
|
||||
if err != nil {
|
||||
log.Printf("fail to get [subdomains] section from file %s : %v", file, err)
|
||||
return controller.HandleController{
|
||||
CommonInfo: common,
|
||||
Tokens: nil,
|
||||
Ports: nil,
|
||||
Domains: nil,
|
||||
Subdomains: nil,
|
||||
IniFile: iniFile,
|
||||
}, tls, err
|
||||
}
|
||||
for _, key := range subdomainsSection.Keys() {
|
||||
user := key.Name()
|
||||
value := key.Value()
|
||||
subdomain := strings.Split(controller.TrimAllSpaceReg.ReplaceAllString(value, ""), ",")
|
||||
subdomains[user] = subdomain
|
||||
}
|
||||
|
||||
usersSection, err := iniFile.GetSection("users")
|
||||
if err != nil {
|
||||
log.Printf("fail to get [users] section from file %s : %v", file, err)
|
||||
return controller.HandleController{
|
||||
CommonInfo: common,
|
||||
Tokens: nil,
|
||||
Ports: nil,
|
||||
Domains: nil,
|
||||
Subdomains: nil,
|
||||
IniFile: iniFile,
|
||||
}, tls, err
|
||||
}
|
||||
|
||||
disabledSection, err := iniFile.GetSection("disabled")
|
||||
if err != nil {
|
||||
log.Printf("fail to get [disabled] section from file %s : %v", file, err)
|
||||
return controller.HandleController{
|
||||
CommonInfo: common,
|
||||
Tokens: nil,
|
||||
Ports: nil,
|
||||
Domains: nil,
|
||||
Subdomains: nil,
|
||||
IniFile: iniFile,
|
||||
}, tls, err
|
||||
}
|
||||
|
||||
keys := usersSection.Keys()
|
||||
for _, key := range keys {
|
||||
comment, found := strings.CutPrefix(key.Comment, ";")
|
||||
if !found {
|
||||
comment, found = strings.CutPrefix(comment, "#")
|
||||
if strings.TrimSpace(tls.Cert) == "" || strings.TrimSpace(tls.Key) == "" {
|
||||
tls.Enable = false
|
||||
tls.Protocol = "HTTP"
|
||||
log.Printf("fail to enable tls: tls cert or key not exist, use http as default.")
|
||||
}
|
||||
token := controller.TokenInfo{
|
||||
User: key.Name(),
|
||||
Token: key.Value(),
|
||||
Comment: comment,
|
||||
Ports: strings.Join(ports[key.Name()], ","),
|
||||
Domains: strings.Join(domains[key.Name()], ","),
|
||||
Subdomains: strings.Join(subdomains[key.Name()], ","),
|
||||
Status: !(disabledSection.HasKey(key.Name()) && disabledSection.Key(key.Name()).Value() == "disable"),
|
||||
}
|
||||
users[token.User] = token
|
||||
}
|
||||
|
||||
tokens := make(map[string]controller.TokenInfo)
|
||||
|
||||
for _, token := range config.Tokens {
|
||||
tokens[token.User] = token
|
||||
}
|
||||
|
||||
return controller.HandleController{
|
||||
CommonInfo: common,
|
||||
Tokens: users,
|
||||
Ports: ports,
|
||||
Domains: domains,
|
||||
Subdomains: subdomains,
|
||||
ConfigFile: configFile,
|
||||
IniFile: iniFile,
|
||||
CommonInfo: config.Common,
|
||||
Tokens: tokens,
|
||||
Version: version,
|
||||
ConfigFile: configFile,
|
||||
TokensFile: tokensFile,
|
||||
}, tls, nil
|
||||
}
|
||||
|
||||
func decode() {
|
||||
|
||||
}
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
package main
|
||||
|
||||
type Config struct {
|
||||
Common CommonInfo
|
||||
Tokens []TokenInfo
|
||||
}
|
||||
|
||||
type CommonInfo struct {
|
||||
PluginAddr string `toml:"plugin_addr" commented:"true"`
|
||||
PluginPort int `toml:"plugin_port"`
|
||||
AdminUser string `toml:"admin_user"`
|
||||
AdminPwd string `toml:"admin_pwd"`
|
||||
AdminKeepTime int `toml:"admin_keep_time"`
|
||||
TlsMode bool `toml:"tls_mode"`
|
||||
TlsCertFile string `toml:"tls_cert_file"`
|
||||
TlsKeyFile string `toml:"tls_key_file"`
|
||||
DashboardAddr string `toml:"dashboard_addr"`
|
||||
DashboardPort int `toml:"dashboard_port"`
|
||||
DashboardUser string `toml:"dashboard_user"`
|
||||
DashboardPwd string `toml:"dashboard_pwd"`
|
||||
}
|
||||
|
||||
type TokenInfo struct {
|
||||
User string `toml:"user" json:"user" form:"user"`
|
||||
Token string `toml:"token" json:"token" form:"token"`
|
||||
Comment string `toml:"comment" json:"comment" form:"comment"`
|
||||
Ports []any `toml:"ports" json:"ports" from:"ports"`
|
||||
Domains []string `toml:"domains" json:"domains" from:"domains"`
|
||||
Subdomains []string `toml:"subdomains" json:"subdomains" from:"subdomains"`
|
||||
Status bool `toml:"status" json:"status" form:"status"`
|
||||
}
|
||||
Reference in New Issue
Block a user