Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
# Conflicts:
#	go.mod
  • Loading branch information
uoosef committed Sep 7, 2023
2 parents 8d0e7f7 + c706fbc commit bd5ae59
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 99 deletions.
99 changes: 60 additions & 39 deletions cmd/cli/main.go
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)
}
18 changes: 4 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ require (
github.com/eycorsican/go-tun2socks v0.0.0
github.com/gorilla/websocket v1.5.0
github.com/miekg/dns v1.1.55
github.com/refraction-networking/utls v1.3.2
github.com/peterbourgon/ff/v4 v4.0.0-alpha.1
github.com/refraction-networking/utls v1.4.3
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
golang.org/x/net v0.14.0
)

Expand Down Expand Up @@ -43,22 +42,14 @@ require (
github.com/goki/freetype v0.0.0-20220119013949-7a161fd3728c // indirect
github.com/gopherjs/gopherjs v1.17.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/quic-go/quic-go v0.37.4 // indirect
github.com/srwiley/oksvg v0.0.0-20220731023508-a61f04f16b76 // indirect
github.com/srwiley/rasterx v0.0.0-20210519020934-456a8d69b780 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/tevino/abool v1.2.0 // indirect
github.com/v2pro/plz v0.0.0-20221028024117-e5f9aec5b631 // indirect
github.com/yuin/goldmark v1.4.13 // indirect
Expand All @@ -69,7 +60,6 @@ require (
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/tools v0.11.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
honnef.co/go/js/dom v0.0.0-20210725211120-f030747120f2 // indirect
)
Loading

0 comments on commit bd5ae59

Please sign in to comment.