-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main'
# Conflicts: # go.mod
- Loading branch information
Showing
3 changed files
with
68 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,81 @@ | ||
// Package main is the entry point for the Bepass application. | ||
package main | ||
|
||
import ( | ||
"bepass/cmd/core" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"log" | ||
"bepass/logger" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"strings" | ||
"syscall" | ||
|
||
"github.com/peterbourgon/ff/v4" | ||
"github.com/peterbourgon/ff/v4/ffhelp" | ||
) | ||
|
||
var configPath string | ||
|
||
func loadConfig() { | ||
if configPath != "" { | ||
viper.SetConfigFile(configPath) | ||
} else { | ||
viper.SetConfigName("config") | ||
viper.AddConfigPath(".") | ||
viper.SetConfigType("json") | ||
func main() { | ||
fs := ff.NewFlags("Bepass") | ||
fs.StringVar(&configPath, 'c', "config", "./config.json", "Path to configuration file") | ||
|
||
err := ff.Parse(fs, os.Args[1:]) | ||
switch { | ||
case errors.Is(err, ff.ErrHelp): | ||
fmt.Fprintf(os.Stderr, "%s\n", ffhelp.Flags(fs)) | ||
os.Exit(0) | ||
case err != nil: | ||
fmt.Fprintf(os.Stderr, "error: %v\n", err) | ||
os.Exit(1) | ||
} | ||
viper.AutomaticEnv() | ||
|
||
err := viper.ReadInConfig() | ||
// Load and validate configuration from JSON file | ||
config, err := loadConfig(configPath) | ||
if err != nil { | ||
log.Fatal(err) | ||
logger.Fatal("", err) | ||
} | ||
} | ||
|
||
func init() { | ||
cobra.OnInitialize(loadConfig) | ||
} | ||
|
||
func main() { | ||
rootCmd := &cobra.Command{ | ||
Use: "Bepass", | ||
Short: "Bepass is an Anti DPI and anti censorship proxy solution", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
config := &core.Config{} | ||
err := viper.Unmarshal(&config) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
err = core.RunServer(config, true) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
}, | ||
// Run the server with the loaded configuration | ||
err = core.RunServer(config, true) | ||
if err != nil { | ||
logger.Fatal("", err) | ||
} | ||
|
||
rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "./config.json", "Path to configuration file") | ||
err := viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config")) | ||
// Handle graceful shutdown | ||
handleShutdown() | ||
} | ||
|
||
func loadConfig(configPath string) (*core.Config, error) { | ||
file, err := os.Open(configPath) | ||
if err != nil { | ||
log.Fatal(err) | ||
return nil, err | ||
} | ||
viper.SetEnvPrefix("Bepass") | ||
defer file.Close() | ||
|
||
err = rootCmd.Execute() | ||
config := &core.Config{} | ||
decoder := json.NewDecoder(file) | ||
err = decoder.Decode(config) | ||
if err != nil { | ||
log.Fatal(err) | ||
if strings.Contains(err.Error(), "invalid character") { | ||
return nil, fmt.Errorf("configuration file is not valid JSON") | ||
} | ||
return nil, err | ||
} | ||
|
||
return config, nil | ||
} | ||
|
||
func handleShutdown() { | ||
c := make(chan os.Signal, 1) | ||
signal.Notify(c, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) | ||
|
||
// Block until a signal is received. | ||
<-c | ||
|
||
// Perform cleanup or shutdown tasks here. | ||
fmt.Println("Shutting down gracefully...") | ||
os.Exit(0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.