74 lines
1.8 KiB
Go
74 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/signal"
|
|
"strings"
|
|
"syscall"
|
|
|
|
"proxy-pool/internal/config"
|
|
"proxy-pool/internal/controller/bootstrap"
|
|
"proxy-pool/internal/platform/logging"
|
|
)
|
|
|
|
const (
|
|
configEnvironment = "PROXY_POOL_CONFIG"
|
|
fingerprintKeyEnvironment = "PROXY_POOL_CONFIG_FINGERPRINT_KEY"
|
|
)
|
|
|
|
type environmentLookup func(string) string
|
|
type controllerRun func(context.Context, bootstrap.Options) error
|
|
|
|
func main() {
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
os.Exit(execute(ctx, os.Args[1:], os.Getenv, bootstrap.Run, os.Stderr))
|
|
}
|
|
|
|
func execute(
|
|
ctx context.Context,
|
|
args []string,
|
|
getenv environmentLookup,
|
|
run controllerRun,
|
|
stderr io.Writer,
|
|
) int {
|
|
flags := flag.NewFlagSet("proxy-controller", flag.ContinueOnError)
|
|
flags.SetOutput(stderr)
|
|
configPath := flags.String("config", "", "configuration file path")
|
|
if err := flags.Parse(args); err != nil {
|
|
if errors.Is(err, flag.ErrHelp) {
|
|
return 0
|
|
}
|
|
return 2
|
|
}
|
|
if flags.NArg() != 0 {
|
|
_, _ = fmt.Fprintln(stderr, "proxy-controller: unexpected positional arguments")
|
|
return 2
|
|
}
|
|
if *configPath == "" && getenv != nil {
|
|
*configPath = getenv(configEnvironment)
|
|
}
|
|
if strings.TrimSpace(*configPath) != *configPath || *configPath == "" || ctx == nil || run == nil {
|
|
_, _ = fmt.Fprintf(stderr, "proxy-controller: -config or %s is required\n", configEnvironment)
|
|
return 2
|
|
}
|
|
|
|
var fingerprintKey []byte
|
|
if getenv != nil {
|
|
fingerprintKey = []byte(getenv(fingerprintKeyEnvironment))
|
|
}
|
|
err := run(ctx, bootstrap.Options{
|
|
ConfigPath: *configPath, Resolver: config.OSResolver{}, FingerprintKey: fingerprintKey,
|
|
})
|
|
if err == nil || (errors.Is(err, context.Canceled) && ctx.Err() != nil) {
|
|
return 0
|
|
}
|
|
logging.WriteProcessError(stderr, "proxy-controller", err)
|
|
return 1
|
|
}
|