feat: implement core functionality for super-frpc service
This commit introduces the core functionality for the super-frpc service including: - Configuration management - User authentication and authorization - Database integration - FRPC instance management - API endpoints for user operations - Token-based authentication system - Password hashing and validation
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
ListenAddr string `json:"listenAddr"`
|
||||
ListenPort string `json:"listenPort"`
|
||||
FrpcPath string `json:"frpcPath"`
|
||||
InstancePath string `json:"instancePath"`
|
||||
Debug bool `json:"debug"`
|
||||
}
|
||||
|
||||
var globalConfig *Config
|
||||
|
||||
func LoadConfig(configPath string) (*Config, error) {
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read config file: %w", err)
|
||||
}
|
||||
|
||||
var config Config
|
||||
if err := json.Unmarshal(data, &config); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse config file: %w", err)
|
||||
}
|
||||
|
||||
if config.ListenAddr == "" {
|
||||
config.ListenAddr = "0.0.0.0"
|
||||
}
|
||||
|
||||
if config.ListenPort == "" {
|
||||
config.ListenPort = "8080"
|
||||
}
|
||||
|
||||
if config.FrpcPath == "" {
|
||||
config.FrpcPath = "/usr/bin/frpc"
|
||||
}
|
||||
|
||||
if config.InstancePath == "" {
|
||||
config.InstancePath = "./configs"
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(config.InstancePath, 0755); err != nil {
|
||||
return nil, fmt.Errorf("failed to create config directory: %w", err)
|
||||
}
|
||||
|
||||
globalConfig = &config
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
func GetConfig() (*Config, error) {
|
||||
if globalConfig == nil {
|
||||
return nil, errors.New("config not loaded")
|
||||
}
|
||||
return globalConfig, nil
|
||||
}
|
||||
|
||||
func SaveConfig(configPath string, config *Config) error {
|
||||
data, err := json.MarshalIndent(config, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal config: %w", err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(configPath, data, 0644); err != nil {
|
||||
return fmt.Errorf("failed to write config file: %w", err)
|
||||
}
|
||||
|
||||
globalConfig = config
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user