Files
hetty/cmd/main.go

139 lines
3.5 KiB
Go
Raw Normal View History

2019-11-17 09:54:15 +01:00
package main
import (
2019-12-01 14:07:12 +01:00
"bytes"
2019-11-17 09:54:15 +01:00
"crypto/tls"
2019-11-24 00:14:49 +01:00
"crypto/x509"
"flag"
2019-12-01 14:07:12 +01:00
"fmt"
"io/ioutil"
2019-11-17 09:54:15 +01:00
"log"
2019-12-01 14:07:12 +01:00
"net"
2019-11-17 15:09:37 +01:00
"net/http"
2019-11-30 12:43:55 +01:00
"net/http/httputil"
"net/url"
2019-12-01 14:07:12 +01:00
"os"
"strings"
2019-11-24 17:30:39 +01:00
2019-12-01 14:47:25 +01:00
"github.com/dstotijn/gurp/pkg/api"
2019-11-30 12:43:55 +01:00
"github.com/dstotijn/gurp/pkg/proxy"
2019-12-01 14:07:12 +01:00
"github.com/dstotijn/gurp/pkg/reqlog"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
2019-11-30 12:43:55 +01:00
"github.com/gorilla/mux"
2019-11-17 09:54:15 +01:00
)
2019-11-24 00:14:49 +01:00
var (
caCertFile = flag.String("cert", "", "CA certificate file path")
caKeyFile = flag.String("key", "", "CA private key file path")
2019-11-30 12:43:55 +01:00
dev = flag.Bool("dev", false, "Run in development mode")
adminPath = flag.String("adminPath", "", "File path to admin build")
2019-11-24 00:14:49 +01:00
)
2019-11-17 09:54:15 +01:00
func main() {
2019-11-24 00:14:49 +01:00
flag.Parse()
tlsCA, err := tls.LoadX509KeyPair(*caCertFile, *caKeyFile)
if err != nil {
log.Fatalf("[FATAL] Could not load CA key pair: %v", err)
}
caCert, err := x509.ParseCertificate(tlsCA.Certificate[0])
if err != nil {
log.Fatalf("[FATAL] Could not parse CA: %v", err)
}
reqLogStore := reqlog.NewRequestLogStore()
2019-12-01 14:07:12 +01:00
p, err := proxy.NewProxy(caCert, tlsCA.PrivateKey)
2019-11-24 00:14:49 +01:00
if err != nil {
log.Fatalf("[FATAL] Could not create Proxy: %v", err)
}
2019-11-17 09:54:15 +01:00
p.UseRequestModifier(func(next proxy.RequestModifyFunc) proxy.RequestModifyFunc {
return func(req *http.Request) {
next(req)
2019-12-01 14:07:12 +01:00
clone := req.Clone(req.Context())
var body []byte
if req.Body != nil {
// TODO: Use io.LimitReader.
body, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Printf("[ERROR] Could not read request body for logging: %v", err)
return
}
req.Body = ioutil.NopCloser(bytes.NewBuffer(body))
}
reqLogStore.AddRequest(*clone, body)
}
})
p.UseResponseModifier(func(next proxy.ResponseModifyFunc) proxy.ResponseModifyFunc {
return func(res *http.Response) error {
2019-12-01 14:07:12 +01:00
if err := next(res); err != nil {
return err
}
clone := *res
var body []byte
if res.Body != nil {
// TODO: Use io.LimitReader.
var err error
body, err = ioutil.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("could not read response body: %v", err)
}
res.Body = ioutil.NopCloser(bytes.NewBuffer(body))
}
reqLogStore.AddResponse(clone, body)
2019-12-01 14:07:12 +01:00
return nil
}
})
2019-12-01 14:07:12 +01:00
var adminHandler http.Handler
2019-11-30 12:43:55 +01:00
if *dev {
adminURL, err := url.Parse("http://localhost:3000")
if err != nil {
log.Fatalf("[FATAL] Invalid admin URL: %v", err)
}
2019-12-01 14:07:12 +01:00
adminHandler = httputil.NewSingleHostReverseProxy(adminURL)
2019-11-30 12:43:55 +01:00
} else {
if *adminPath == "" {
log.Fatal("[FATAL] `adminPath` must be set")
}
2019-12-01 14:07:12 +01:00
adminHandler = http.FileServer(http.Dir(*adminPath))
2019-11-30 12:43:55 +01:00
}
2019-12-01 14:07:12 +01:00
router := mux.NewRouter().SkipClean(true)
adminRouter := router.MatcherFunc(func(req *http.Request, match *mux.RouteMatch) bool {
hostname, _ := os.Hostname()
host, _, _ := net.SplitHostPort(req.Host)
return strings.EqualFold(host, hostname) || req.Host == "gurp.proxy"
2019-12-01 14:47:25 +01:00
}).Subrouter()
2019-12-01 14:07:12 +01:00
// GraphQL server.
adminRouter.Path("/api/playground").Handler(playground.Handler("GraphQL Playground", "/api/graphql"))
adminRouter.Path("/api/graphql").Handler(handler.NewDefaultServer(api.NewExecutableSchema(api.Config{Resolvers: &api.Resolver{
RequestLogStore: &reqLogStore,
}})))
2019-12-01 14:07:12 +01:00
// Admin interface.
2019-12-01 14:47:25 +01:00
adminRouter.PathPrefix("").Handler(adminHandler)
2019-12-01 14:07:12 +01:00
// Fallback (default) is the Proxy handler.
2019-11-30 12:43:55 +01:00
router.PathPrefix("").Handler(p)
2019-11-17 15:09:37 +01:00
s := &http.Server{
Addr: ":8080",
2019-11-30 12:43:55 +01:00
Handler: router,
2019-11-17 15:09:37 +01:00
TLSNextProto: map[string]func(*http.Server, *tls.Conn, http.Handler){}, // Disable HTTP/2
2019-11-17 09:54:15 +01:00
}
2019-11-24 00:14:49 +01:00
err = s.ListenAndServe()
2019-11-17 15:09:37 +01:00
if err != nil && err != http.ErrServerClosed {
2019-11-24 00:14:49 +01:00
log.Fatalf("[FATAL] HTTP server closed: %v", err)
2019-11-17 09:54:15 +01:00
}
}