astraproxy/main.go

130 lines
3.2 KiB
Go
Raw Normal View History

2020-05-19 21:53:13 +02:00
package main
import (
2021-02-26 08:09:55 +01:00
"crypto/tls"
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
2020-05-19 21:53:13 +02:00
)
2021-06-09 14:49:44 +02:00
var (
version = "undefined"
)
2020-05-19 21:53:13 +02:00
func perror(msg string) {
2021-02-26 08:09:55 +01:00
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, msg)
2020-05-19 21:53:13 +02:00
}
func arg_fail(msg string) {
2021-02-26 08:09:55 +01:00
perror(msg)
perror("Usage:")
flag.PrintDefaults()
os.Exit(2)
2020-05-19 21:53:13 +02:00
}
type CLIArgs struct {
2021-02-26 08:09:55 +01:00
bind_address string
auth string
verbosity int
timeout time.Duration
cert, key, cafile string
2021-02-26 08:14:05 +01:00
list_ciphers bool
2021-02-26 08:30:18 +01:00
ciphers string
2021-02-26 08:35:17 +01:00
disableHTTP2 bool
2021-06-09 14:49:44 +02:00
showVersion bool
2020-05-19 21:53:13 +02:00
}
2021-02-26 08:14:05 +01:00
func list_ciphers() {
2021-02-26 08:09:55 +01:00
for _, cipher := range tls.CipherSuites() {
fmt.Println(cipher.Name)
}
}
2020-05-19 21:53:13 +02:00
func parse_args() CLIArgs {
2021-02-26 08:09:55 +01:00
var args CLIArgs
flag.StringVar(&args.bind_address, "bind-address", ":8080", "HTTP proxy listen address")
flag.StringVar(&args.auth, "auth", "none://", "auth parameters")
flag.IntVar(&args.verbosity, "verbosity", 20, "logging verbosity "+
"(10 - debug, 20 - info, 30 - warning, 40 - error, 50 - critical)")
flag.DurationVar(&args.timeout, "timeout", 10*time.Second, "timeout for network operations")
flag.StringVar(&args.cert, "cert", "", "enable TLS and use certificate")
flag.StringVar(&args.key, "key", "", "key for TLS certificate")
flag.StringVar(&args.cafile, "cafile", "", "CA file to authenticate clients with certificates")
2021-02-26 08:14:05 +01:00
flag.BoolVar(&args.list_ciphers, "list-ciphers", false, "list ciphersuites")
2021-02-26 08:30:18 +01:00
flag.StringVar(&args.ciphers, "ciphers", "", "colon-separated list of enabled ciphers")
2021-02-26 08:35:17 +01:00
flag.BoolVar(&args.disableHTTP2, "disable-http2", false, "disable HTTP2")
2021-06-09 14:49:44 +02:00
flag.BoolVar(&args.showVersion, "version", false, "show program version and exit")
2021-02-26 08:09:55 +01:00
flag.Parse()
return args
2020-05-19 21:53:13 +02:00
}
func run() int {
2021-02-26 08:09:55 +01:00
args := parse_args()
2021-06-09 14:49:44 +02:00
if args.showVersion {
fmt.Println(version)
return 0
}
2021-02-26 08:14:05 +01:00
if args.list_ciphers {
list_ciphers()
2021-02-26 08:09:55 +01:00
return 0
}
2020-05-19 21:53:13 +02:00
2021-02-26 08:09:55 +01:00
logWriter := NewLogWriter(os.Stderr)
defer logWriter.Close()
2020-05-19 21:53:13 +02:00
2021-02-26 08:09:55 +01:00
mainLogger := NewCondLogger(log.New(logWriter, "MAIN : ",
log.LstdFlags|log.Lshortfile),
args.verbosity)
proxyLogger := NewCondLogger(log.New(logWriter, "PROXY : ",
log.LstdFlags|log.Lshortfile),
args.verbosity)
2020-05-24 00:18:01 +02:00
2021-02-26 08:09:55 +01:00
auth, err := NewAuth(args.auth)
if err != nil {
mainLogger.Critical("Failed to instantiate auth provider: %v", err)
return 3
}
2020-05-24 00:18:01 +02:00
2021-02-26 08:09:55 +01:00
server := http.Server{
Addr: args.bind_address,
Handler: NewProxyHandler(args.timeout, auth, proxyLogger),
ErrorLog: log.New(logWriter, "HTTPSRV : ", log.LstdFlags|log.Lshortfile),
ReadTimeout: 0,
ReadHeaderTimeout: 0,
WriteTimeout: 0,
IdleTimeout: 0,
}
2020-05-24 00:18:01 +02:00
2021-02-26 08:35:17 +01:00
if args.disableHTTP2 {
server.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))
}
2021-02-26 08:09:55 +01:00
mainLogger.Info("Starting proxy server...")
if args.cert != "" {
cfg, err1 := makeServerTLSConfig(args.cert, args.key, args.cafile)
if err1 != nil {
mainLogger.Critical("TLS config construction failed: %v", err)
return 3
}
2021-02-26 08:30:18 +01:00
cfg.CipherSuites = makeCipherList(args.ciphers)
2021-02-26 08:09:55 +01:00
server.TLSConfig = cfg
err = server.ListenAndServeTLS("", "")
} else {
err = server.ListenAndServe()
}
mainLogger.Critical("Server terminated with a reason: %v", err)
mainLogger.Info("Shutting down...")
return 0
2020-05-19 21:53:13 +02:00
}
func main() {
2021-02-26 08:09:55 +01:00
os.Exit(run())
2020-05-19 21:53:13 +02:00
}