mirror of
https://github.com/fosrl/gerbil.git
synced 2026-03-06 10:46:41 +00:00
Add env vars
This commit is contained in:
@@ -1,21 +1,10 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
# Sample from https://github.com/traefik/traefik-library-image/blob/5070edb25b03cca6802d75d5037576c840f73fdd/v3.1/alpine/entrypoint.sh
|
|
||||||
|
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
# first arg is `-f` or `--some-option`
|
# first arg is `-f` or `--some-option`
|
||||||
if [ "${1#-}" != "$1" ]; then
|
if [ "${1#-}" != "$1" ]; then
|
||||||
set -- gerbil "$@"
|
set -- newt "$@"
|
||||||
fi
|
|
||||||
|
|
||||||
# if our command is a valid Gerbil subcommand, let's invoke it through Gerbil instead
|
|
||||||
# (this allows for "docker run gerbil version", etc)
|
|
||||||
if gerbil "$1" --help >/dev/null 2>&1
|
|
||||||
then
|
|
||||||
set -- gerbil "$@"
|
|
||||||
else
|
|
||||||
echo "= '$1' is not a Gerbil command: assuming shell execution." 1>&2
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
exec "$@"
|
exec "$@"
|
||||||
84
main.go
84
main.go
@@ -72,52 +72,78 @@ func parseLogLevel(level string) logger.LogLevel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
var (
|
||||||
var wgconfig WgConfig
|
err error
|
||||||
|
wgconfig WgConfig
|
||||||
|
interfaceName string
|
||||||
|
configFile string
|
||||||
|
remoteConfigURL string
|
||||||
|
listenAddr string
|
||||||
|
reportBandwidthTo string
|
||||||
|
generateAndSaveKeyTo string
|
||||||
|
reachableAt string
|
||||||
|
logLevel string
|
||||||
|
)
|
||||||
|
|
||||||
// Define command line flags
|
interfaceName = os.Getenv("INTERFACE")
|
||||||
interfaceNameArg := flag.String("interface", "wg0", "Name of the WireGuard interface")
|
configFile = os.Getenv("CONFIG")
|
||||||
configFile := flag.String("config", "", "Path to local configuration file")
|
remoteConfigURL = os.Getenv("REMOTE_CONFIG")
|
||||||
remoteConfigURL := flag.String("remoteConfig", "", "URL to fetch remote configuration")
|
listenAddr = os.Getenv("LISTEN")
|
||||||
listenAddrArg := flag.String("listen", ":3003", "Address to listen on")
|
reportBandwidthTo = os.Getenv("REPORT_BANDWIDTH_TO")
|
||||||
reportBandwidthTo := flag.String("reportBandwidthTo", "", "Address to listen on")
|
generateAndSaveKeyTo = os.Getenv("GENERATE_AND_SAVE_KEY_TO")
|
||||||
generateAndSaveKeyTo := flag.String("generateAndSaveKeyTo", "", "Path to save generated private key")
|
reachableAt = os.Getenv("REACHABLE_AT")
|
||||||
reachableAt := flag.String("reachableAt", "", "Endpoint of the http server to tell remote config about")
|
logLevel = os.Getenv("LOG_LEVEL")
|
||||||
logLevel := flag.String("log-level", "INFO", "Log level (DEBUG, INFO, WARN, ERROR, FATAL)")
|
|
||||||
|
|
||||||
|
if interfaceName == "" {
|
||||||
|
flag.StringVar(&interfaceName, "interface", "wg0", "Name of the WireGuard interface")
|
||||||
|
}
|
||||||
|
if configFile == "" {
|
||||||
|
flag.StringVar(&configFile, "config", "", "Path to local configuration file")
|
||||||
|
}
|
||||||
|
if remoteConfigURL == "" {
|
||||||
|
flag.StringVar(&remoteConfigURL, "remoteConfig", "", "URL to fetch remote configuration")
|
||||||
|
}
|
||||||
|
if listenAddr == "" {
|
||||||
|
flag.StringVar(&listenAddr, "listen", ":3003", "Address to listen on")
|
||||||
|
}
|
||||||
|
if reportBandwidthTo == "" {
|
||||||
|
flag.StringVar(&reportBandwidthTo, "reportBandwidthTo", "", "Address to listen on")
|
||||||
|
}
|
||||||
|
if generateAndSaveKeyTo == "" {
|
||||||
|
flag.StringVar(&generateAndSaveKeyTo, "generateAndSaveKeyTo", "", "Path to save generated private key")
|
||||||
|
}
|
||||||
|
if reachableAt == "" {
|
||||||
|
flag.StringVar(&reachableAt, "reachableAt", "", "Endpoint of the http server to tell remote config about")
|
||||||
|
}
|
||||||
|
if logLevel == "" {
|
||||||
|
flag.StringVar(&logLevel, "log-level", "INFO", "Log level (DEBUG, INFO, WARN, ERROR, FATAL)")
|
||||||
|
}
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
logger.Init()
|
logger.Init()
|
||||||
logger.GetLogger().SetLevel(parseLogLevel(*logLevel))
|
logger.GetLogger().SetLevel(parseLogLevel(logLevel))
|
||||||
|
|
||||||
if *interfaceNameArg != "" {
|
|
||||||
interfaceName = *interfaceNameArg
|
|
||||||
}
|
|
||||||
if *listenAddrArg != "" {
|
|
||||||
listenAddr = *listenAddrArg
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate that only one config option is provided
|
// Validate that only one config option is provided
|
||||||
if (*configFile != "" && *remoteConfigURL != "") || (*configFile == "" && *remoteConfigURL == "") {
|
if (configFile != "" && remoteConfigURL != "") || (configFile == "" && remoteConfigURL == "") {
|
||||||
logger.Fatal("Please provide either --config or --remoteConfig, but not both")
|
logger.Fatal("Please provide either --config or --remoteConfig, but not both")
|
||||||
}
|
}
|
||||||
|
|
||||||
var key wgtypes.Key
|
var key wgtypes.Key
|
||||||
// if generateAndSaveKeyTo is provided, generate a private key and save it to the file. if the file already exists, load the key from the file
|
// if generateAndSaveKeyTo is provided, generate a private key and save it to the file. if the file already exists, load the key from the file
|
||||||
if *generateAndSaveKeyTo != "" {
|
if generateAndSaveKeyTo != "" {
|
||||||
if _, err := os.Stat(*generateAndSaveKeyTo); os.IsNotExist(err) {
|
if _, err := os.Stat(generateAndSaveKeyTo); os.IsNotExist(err) {
|
||||||
// generate a new private key
|
// generate a new private key
|
||||||
key, err = wgtypes.GeneratePrivateKey()
|
key, err = wgtypes.GeneratePrivateKey()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal("Failed to generate private key: %v", err)
|
logger.Fatal("Failed to generate private key: %v", err)
|
||||||
}
|
}
|
||||||
// save the key to the file
|
// save the key to the file
|
||||||
err = os.WriteFile(*generateAndSaveKeyTo, []byte(key.String()), 0644)
|
err = os.WriteFile(generateAndSaveKeyTo, []byte(key.String()), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal("Failed to save private key: %v", err)
|
logger.Fatal("Failed to save private key: %v", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
keyData, err := os.ReadFile(*generateAndSaveKeyTo)
|
keyData, err := os.ReadFile(generateAndSaveKeyTo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal("Failed to read private key: %v", err)
|
logger.Fatal("Failed to read private key: %v", err)
|
||||||
}
|
}
|
||||||
@@ -138,8 +164,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Load configuration based on provided argument
|
// Load configuration based on provided argument
|
||||||
if *configFile != "" {
|
if configFile != "" {
|
||||||
wgconfig, err = loadConfig(*configFile)
|
wgconfig, err = loadConfig(configFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal("Failed to load configuration: %v", err)
|
logger.Fatal("Failed to load configuration: %v", err)
|
||||||
}
|
}
|
||||||
@@ -147,7 +173,7 @@ func main() {
|
|||||||
wgconfig.PrivateKey = key.String()
|
wgconfig.PrivateKey = key.String()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
wgconfig, err = loadRemoteConfig(*remoteConfigURL, key, *reachableAt)
|
wgconfig, err = loadRemoteConfig(remoteConfigURL, key, reachableAt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal("Failed to load configuration: %v", err)
|
logger.Fatal("Failed to load configuration: %v", err)
|
||||||
}
|
}
|
||||||
@@ -168,8 +194,8 @@ func main() {
|
|||||||
// Ensure the WireGuard peers exist
|
// Ensure the WireGuard peers exist
|
||||||
ensureWireguardPeers(wgconfig.Peers)
|
ensureWireguardPeers(wgconfig.Peers)
|
||||||
|
|
||||||
if *reportBandwidthTo != "" {
|
if reportBandwidthTo != "" {
|
||||||
go periodicBandwidthCheck(*reportBandwidthTo)
|
go periodicBandwidthCheck(reportBandwidthTo)
|
||||||
}
|
}
|
||||||
|
|
||||||
http.HandleFunc("/peer", handlePeer)
|
http.HandleFunc("/peer", handlePeer)
|
||||||
|
|||||||
Reference in New Issue
Block a user