Skip to content

Commit

Permalink
cmd/flatend: add support for specifying domains vs bind addrs
Browse files Browse the repository at this point in the history
  • Loading branch information
lithdew committed Jun 16, 2020
1 parent d81b7a6 commit 204bf30
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
23 changes: 22 additions & 1 deletion cmd/flatend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"fmt"
"net"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -35,6 +36,9 @@ func (d *Duration) UnmarshalText(text []byte) error {
}

type ConfigHTTP struct {
Domain string
Domains []string

Addr string
Addrs []string

Expand Down Expand Up @@ -63,14 +67,31 @@ type ConfigHTTP struct {
Routes []ConfigRoute
}

func (h ConfigHTTP) GetDomains() []string {
if h.Domain != "" {
return []string{h.Domain}
}
return h.Domains
}

func (h ConfigHTTP) GetAddrs() []string {
if len(h.Addrs) > 0 {
return h.Addrs
}
if h.Addr != "" {
return []string{h.Addr}
}
return h.Addrs
if h.HTTPS {
return []string{net.JoinHostPort("", "443")}
}
return []string{net.JoinHostPort("", "80")}
}

func (h ConfigHTTP) Validate() error {
if h.Domain != "" && h.Domains != nil {
return errors.New("'domain' and 'domains' cannot both be non-nil at the same time")
}

if h.Addr != "" && h.Addrs != nil {
return errors.New("'addr' and 'addrs' cannot both be non-nil at the same time")
}
Expand Down
25 changes: 22 additions & 3 deletions cmd/flatend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/lithdew/flatend"
"github.com/spf13/pflag"
"io/ioutil"
"log"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -125,7 +126,7 @@ func main() {

if cfg.HTTPS {
magic := certmagic.NewDefault()
check(magic.ManageSync(addrs))
check(magic.ManageSync(cfg.GetDomains()))

acme := certmagic.NewACMEManager(magic, certmagic.DefaultACME)
srv.Handler = acme.HTTPChallengeHandler(srv.Handler)
Expand Down Expand Up @@ -162,19 +163,34 @@ func main() {
addr := addr

go func() {
ln, err := tls.Listen("tcp", net.JoinHostPort(addr, "443"), magic.TLSConfig())
bindAddr := addr

ln, err := tls.Listen("tcp", bindAddr, magic.TLSConfig())
check(err)

log.Printf("Accepting HTTPS at: %s", ln.Addr().String())

err = srv.Serve(ln)
if !errors.Is(err, http.ErrServerClosed) {
check(err)
}
}()

go func() {
ln, err := net.Listen("tcp", net.JoinHostPort(addr, "80"))
redirectAddr := addr

host, _, err := net.SplitHostPort(redirectAddr)
if err == nil {
redirectAddr = net.JoinHostPort(host, "80")
} else {
redirectAddr = net.JoinHostPort(redirectAddr, "80")
}

ln, err := net.Listen("tcp", redirectAddr)
check(err)

log.Printf("Redirecting HTTP->HTTPS at: %s", ln.Addr().String())

err = redirect.Serve(ln)
if !errors.Is(err, http.ErrServerClosed) {
check(err)
Expand All @@ -184,10 +200,13 @@ func main() {
} else {
for _, addr := range addrs {
addr := addr

go func() {
ln, err := net.Listen("tcp", addr)
check(err)

log.Printf("Accepting HTTP at: %s", ln.Addr().String())

err = srv.Serve(ln)
if !errors.Is(err, http.ErrServerClosed) {
check(err)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
github.com/BurntSushi/toml v0.3.1
github.com/VictoriaMetrics/metrics v1.11.3 // indirect
github.com/caddyserver/certmagic v0.11.2
github.com/jpillora/backoff v1.0.0
github.com/julienschmidt/httprouter v1.3.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
github.com/OpenDNS/vegadns2client v0.0.0-20180418235048-a3fa4a771d87/go.mod h1:iGLljf5n9GjT6kc0HBvyI1nOKnGQbNB66VzSNbK5iks=
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/VictoriaMetrics/metrics v1.11.3 h1:eSfXc0CrquKa1VTNUvhP+dhNjLUZHQGTFfp19mYCQWE=
github.com/VictoriaMetrics/metrics v1.11.3/go.mod h1:LU2j9qq7xqZYXz8tF3/RQnB2z2MbZms5TDiIg9/NHiQ=
github.com/akamai/AkamaiOPEN-edgegrid-golang v0.9.8/go.mod h1:aVvklgKsPENRkl29bNwrHISa1F+YLGTHArMxZMBqWM8=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand Down Expand Up @@ -261,6 +263,10 @@ github.com/uber-go/atomic v1.3.2/go.mod h1:/Ct5t2lcmbJ4OSe/waGBoaVvVqtO0bmtfVNex
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fastrand v1.0.0 h1:LUKT9aKer2dVQNUi3waewTbKV+7H17kvWFNKs2ObdkI=
github.com/valyala/fastrand v1.0.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ=
github.com/valyala/histogram v1.0.1 h1:FzA7n2Tz/wKRMejgu3PV1vw3htAklTjjuoI6z3d4KDg=
github.com/valyala/histogram v1.0.1/go.mod h1:lQy0xA4wUz2+IUnf97SivorsJIp8FxsnRd6x25q7Mto=
github.com/vultr/govultr v0.1.4/go.mod h1:9H008Uxr/C4vFNGLqKx232C206GL0PBHzOP0809bGNA=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
Expand Down

0 comments on commit 204bf30

Please sign in to comment.