wttr.in/cmd/srv.go

151 lines
3.3 KiB
Go
Raw Normal View History

package main
import (
"crypto/tls"
"fmt"
2022-11-20 17:53:51 +01:00
"io"
"log"
"net/http"
"time"
2022-11-29 21:38:39 +01:00
"github.com/chubin/wttr.in/internal/config"
)
2022-11-20 17:53:51 +01:00
const logLineStart = "LOG_LINE_START "
2020-05-30 18:14:17 +02:00
// plainTextAgents contains signatures of the plain-text agents
var plainTextAgents = []string{
"curl",
"httpie",
"lwp-request",
"wget",
"python-httpx",
"python-requests",
"openbsd ftp",
"powershell",
"fetch",
"aiohttp",
"http_get",
2022-03-26 19:24:23 +01:00
"xh",
}
func copyHeader(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}
2022-11-20 17:53:51 +01:00
func serveHTTP(mux *http.ServeMux, port int, logFile io.Writer, errs chan<- error) {
srv := &http.Server{
Addr: fmt.Sprintf(":%d", port),
2022-11-20 17:53:51 +01:00
ErrorLog: log.New(logFile, logLineStart, log.LstdFlags),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 1 * time.Second,
Handler: mux,
}
errs <- srv.ListenAndServe()
2022-11-20 10:03:31 +01:00
}
2022-11-20 17:53:51 +01:00
func serveHTTPS(mux *http.ServeMux, port int, logFile io.Writer, errs chan<- error) {
tlsConfig := &tls.Config{
2022-11-29 21:38:39 +01:00
// CipherSuites: []uint16{
// tls.TLS_CHACHA20_POLY1305_SHA256,
// tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
// tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
// },
// MinVersion: tls.VersionTLS13,
}
srv := &http.Server{
Addr: fmt.Sprintf(":%d", port),
2022-11-20 17:53:51 +01:00
ErrorLog: log.New(logFile, logLineStart, log.LstdFlags),
ReadTimeout: 5 * time.Second,
WriteTimeout: 20 * time.Second,
IdleTimeout: 1 * time.Second,
TLSConfig: tlsConfig,
Handler: mux,
}
2022-11-29 21:38:39 +01:00
errs <- srv.ListenAndServeTLS(config.Conf.Server.TLSCertFile, config.Conf.Server.TLSKeyFile)
2022-11-20 10:03:31 +01:00
}
func main() {
2022-11-20 10:03:31 +01:00
var (
// mux is main HTTP/HTTP requests multiplexer.
mux *http.ServeMux = http.NewServeMux()
// logger is optimized requests logger.
logger *RequestLogger = NewRequestLogger(
2022-11-29 21:38:39 +01:00
config.Conf.Logging.AccessLog,
time.Duration(config.Conf.Logging.Interval)*time.Second)
2022-11-19 19:20:35 +01:00
2022-11-27 15:51:41 +01:00
rp *RequestProcessor
2022-11-20 10:03:31 +01:00
// errs is the servers errors channel.
errs chan error = make(chan error, 1)
// numberOfServers started. If 0, exit.
numberOfServers int
2022-11-20 17:53:51 +01:00
errorsLog *LogSuppressor = NewLogSuppressor(
2022-11-29 21:38:39 +01:00
config.Conf.Logging.ErrorsLog,
2022-11-20 17:53:51 +01:00
[]string{
"error reading preface from client",
"TLS handshake error from",
},
logLineStart,
)
2022-11-27 15:51:41 +01:00
err error
2022-11-20 10:03:31 +01:00
)
2022-12-02 20:10:32 +01:00
rp, err = NewRequestProcessor(config.Conf)
2022-11-27 15:51:41 +01:00
if err != nil {
log.Fatalln("log processor initialization:", err)
}
err = errorsLog.Open()
2022-11-20 17:53:51 +01:00
if err != nil {
log.Fatalln("errors log:", err)
}
2022-11-27 15:51:41 +01:00
rp.Start()
2022-11-20 10:03:31 +01:00
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
2022-11-19 19:20:35 +01:00
if err := logger.Log(r); err != nil {
log.Println(err)
}
2020-05-30 18:14:17 +02:00
// printStat()
2022-11-27 15:51:41 +01:00
response, err := rp.ProcessRequest(r)
if err != nil {
log.Println(err)
return
}
if response.StatusCode == 0 {
log.Println("status code 0", response)
return
}
copyHeader(w.Header(), response.Header)
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(response.StatusCode)
w.Write(response.Body)
})
2022-11-29 21:38:39 +01:00
if config.Conf.Server.PortHTTP != 0 {
go serveHTTP(mux, config.Conf.Server.PortHTTP, errorsLog, errs)
2022-11-20 10:03:31 +01:00
numberOfServers++
}
2022-11-29 21:38:39 +01:00
if config.Conf.Server.PortHTTPS != 0 {
go serveHTTPS(mux, config.Conf.Server.PortHTTPS, errorsLog, errs)
2022-11-20 10:03:31 +01:00
numberOfServers++
}
if numberOfServers == 0 {
log.Println("no servers configured; exiting")
return
}
log.Fatal(<-errs) // block until one of the servers writes an error
}