87 lines
1.8 KiB
Go
87 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// FrpsToml 对应 frps.toml 结构
|
|
type FrpsToml struct {
|
|
BindPort int `json:"bindPort"`
|
|
Auth struct {
|
|
Token string `json:"token"`
|
|
} `json:"auth"`
|
|
Log struct {
|
|
To string `json:"to"`
|
|
Level string `json:"level"`
|
|
MaxDays int `json:"maxDays"`
|
|
} `json:"log"`
|
|
Transport struct {
|
|
TcpMux bool `json:"tcpMux"`
|
|
WireProtocolV2 bool `json:"wireProtocolV2"`
|
|
} `json:"transport"`
|
|
}
|
|
|
|
func ParseToml(content string) (*FrpsToml, error) {
|
|
lines := strings.Split(content, "\n")
|
|
result := &FrpsToml{
|
|
Transport: struct {
|
|
TcpMux bool `json:"tcpMux"`
|
|
WireProtocolV2 bool `json:"wireProtocolV2"`
|
|
}{
|
|
TcpMux: true,
|
|
WireProtocolV2: false,
|
|
},
|
|
}
|
|
|
|
for _, rawLine := range lines {
|
|
line := strings.TrimSpace(rawLine)
|
|
if line == "" || strings.HasPrefix(line, "#") {
|
|
continue
|
|
}
|
|
if strings.HasPrefix(line, "[") {
|
|
continue
|
|
}
|
|
if !strings.Contains(line, "=") {
|
|
continue
|
|
}
|
|
|
|
parts := strings.SplitN(line, "=", 2)
|
|
key := strings.TrimSpace(parts[0])
|
|
value := strings.TrimSpace(parts[1])
|
|
value = strings.Trim(value, `"`)
|
|
|
|
switch key {
|
|
case "bindPort":
|
|
result.BindPort, _ = strconv.Atoi(value)
|
|
case "token":
|
|
result.Auth.Token = value
|
|
case "level":
|
|
result.Log.Level = value
|
|
case "maxDays":
|
|
result.Log.MaxDays, _ = strconv.Atoi(value)
|
|
case "tcpMux":
|
|
result.Transport.TcpMux = value == "true"
|
|
case "wireProtocol":
|
|
result.Transport.WireProtocolV2 = value == "v2"
|
|
}
|
|
}
|
|
|
|
if result.BindPort == 0 {
|
|
return nil, fmt.Errorf("未找到 bindPort 字段")
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (f *FrpsToml) ToGlobalConfig() *GlobalConfig {
|
|
return &GlobalConfig{
|
|
BindPort: f.BindPort,
|
|
Token: f.Auth.Token,
|
|
LogLevel: f.Log.Level,
|
|
LogMaxDays: f.Log.MaxDays,
|
|
TcpMux: f.Transport.TcpMux,
|
|
}
|
|
}
|