add bindAddress configuration option (#320)

* add bindAddress configuration option

* clarify that bindAddress can be a hostname
This commit is contained in:
tobi 2021-11-22 10:55:52 +01:00 committed by GitHub
parent 6c0550e76f
commit 1ded58b34b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 3 deletions

View file

@ -62,6 +62,12 @@ func generalFlags(flagNames, envNames config.Flags, defaults config.Defaults) []
Value: defaults.Protocol, Value: defaults.Protocol,
EnvVars: []string{envNames.Protocol}, EnvVars: []string{envNames.Protocol},
}, },
&cli.StringFlag{
Name: flagNames.BindAddress,
Usage: "Bind address to use for the GoToSocial server (eg., 0.0.0.0, 172.138.0.9, [::], localhost). For ipv6, enclose the address in square brackets, eg [2a02:1807:1430:500:13f5:5efb:3f3a:1191]. Default binds to all interfaces.",
Value: defaults.BindAddress,
EnvVars: []string{envNames.BindAddress},
},
&cli.IntFlag{ &cli.IntFlag{
Name: flagNames.Port, Name: flagNames.Port,
Usage: "Port to use for GoToSocial. Change this to 443 if you're running the binary directly on the host machine.", Usage: "Port to use for GoToSocial. Change this to 443 if you're running the binary directly on the host machine.",

View file

@ -1,6 +1,6 @@
# General # General
The top-level configuration for GoToSocial, including basic things like host and transport protocol. The top-level configuration for GoToSocial, including basic things like host, port, bind address and transport protocol.
The only things you *really* need to set here are `host`, which should be the hostname where your instance is reachable, and probably `port`. The only things you *really* need to set here are `host`, which should be the hostname where your instance is reachable, and probably `port`.
@ -48,6 +48,15 @@ accountDomain: ""
# Default: "https" # Default: "https"
protocol: "https" protocol: "https"
# String. Address to bind the GoToSocial server to.
# This can be an IPv4 address or an IPv6 address (surrounded in square brackets), or a hostname.
# Default value will bind to all interfaces.
# You probably won't need to change this unless you're setting GoToSocial up in some fancy way or
# you have specific networking requirements.
# Examples: ["0.0.0.0", "172.128.0.16", "localhost", "[::]", "[2a02:1807:1430:500:13f5:5efb:3f3a:1191]"]
# Default: "0.0.0.0"
bindAddress: "0.0.0.0"
# Int. Listen port for the GoToSocial webserver + API. If you're running behind a reverse proxy and/or in a docker, # Int. Listen port for the GoToSocial webserver + API. If you're running behind a reverse proxy and/or in a docker,
# container, just set this to whatever you like (or leave the default), and make sure it's forwarded properly. # container, just set this to whatever you like (or leave the default), and make sure it's forwarded properly.
# If you are running with built-in letsencrypt enabled, and running GoToSocial directly on a host machine, you will # If you are running with built-in letsencrypt enabled, and running GoToSocial directly on a host machine, you will

View file

@ -55,6 +55,15 @@ accountDomain: ""
# Default: "https" # Default: "https"
protocol: "https" protocol: "https"
# String. Address to bind the GoToSocial server to.
# This can be an IPv4 address or an IPv6 address (surrounded in square brackets), or a hostname.
# Default value will bind to all interfaces.
# You probably won't need to change this unless you're setting GoToSocial up in some fancy way or
# you have specific networking requirements.
# Examples: ["0.0.0.0", "172.128.0.16", "localhost", "[::]", "[2a02:1807:1430:500:13f5:5efb:3f3a:1191]"]
# Default: "0.0.0.0"
bindAddress: "0.0.0.0"
# Int. Listen port for the GoToSocial webserver + API. If you're running behind a reverse proxy and/or in a docker, # Int. Listen port for the GoToSocial webserver + API. If you're running behind a reverse proxy and/or in a docker,
# container, just set this to whatever you like (or leave the default), and make sure it's forwarded properly. # container, just set this to whatever you like (or leave the default), and make sure it's forwarded properly.
# If you are running with built-in letsencrypt enabled, and running GoToSocial directly on a host machine, you will # If you are running with built-in letsencrypt enabled, and running GoToSocial directly on a host machine, you will

View file

@ -53,6 +53,7 @@ type Config struct {
Host string `yaml:"host"` Host string `yaml:"host"`
AccountDomain string `yaml:"accountDomain"` AccountDomain string `yaml:"accountDomain"`
Protocol string `yaml:"protocol"` Protocol string `yaml:"protocol"`
BindAddress string `yaml:"bindAddress"`
Port int `yaml:"port"` Port int `yaml:"port"`
TrustedProxies []string `yaml:"trustedProxies"` TrustedProxies []string `yaml:"trustedProxies"`
DBConfig *DBConfig `yaml:"db"` DBConfig *DBConfig `yaml:"db"`
@ -159,6 +160,10 @@ func (c *Config) ParseCLIFlags(f KeyedFlags, version string) error {
return errors.New("protocol was not set") return errors.New("protocol was not set")
} }
if c.BindAddress == "" || f.IsSet(fn.BindAddress) {
c.BindAddress = f.String(fn.BindAddress)
}
if c.Port == 0 || f.IsSet(fn.Port) { if c.Port == 0 || f.IsSet(fn.Port) {
c.Port = f.Int(fn.Port) c.Port = f.Int(fn.Port)
} }
@ -374,6 +379,7 @@ type Flags struct {
Host string Host string
AccountDomain string AccountDomain string
Protocol string Protocol string
BindAddress string
Port string Port string
TrustedProxies string TrustedProxies string
@ -438,6 +444,7 @@ type Defaults struct {
Host string Host string
AccountDomain string AccountDomain string
Protocol string Protocol string
BindAddress string
Port int Port int
TrustedProxies []string TrustedProxies []string
SoftwareVersion string SoftwareVersion string
@ -505,6 +512,7 @@ func GetFlagNames() Flags {
Host: "host", Host: "host",
AccountDomain: "account-domain", AccountDomain: "account-domain",
Protocol: "protocol", Protocol: "protocol",
BindAddress: "bind-address",
Port: "port", Port: "port",
TrustedProxies: "trusted-proxies", TrustedProxies: "trusted-proxies",
@ -572,6 +580,7 @@ func GetEnvNames() Flags {
Host: "GTS_HOST", Host: "GTS_HOST",
AccountDomain: "GTS_ACCOUNT_DOMAIN", AccountDomain: "GTS_ACCOUNT_DOMAIN",
Protocol: "GTS_PROTOCOL", Protocol: "GTS_PROTOCOL",
BindAddress: "GTS_BIND_ADDRESS",
Port: "GTS_PORT", Port: "GTS_PORT",
TrustedProxies: "GTS_TRUSTED_PROXIES", TrustedProxies: "GTS_TRUSTED_PROXIES",

View file

@ -11,6 +11,7 @@ func TestDefault() *Config {
Host: defaults.Host, Host: defaults.Host,
AccountDomain: defaults.AccountDomain, AccountDomain: defaults.AccountDomain,
Protocol: defaults.Protocol, Protocol: defaults.Protocol,
BindAddress: defaults.BindAddress,
Port: defaults.Port, Port: defaults.Port,
TrustedProxies: defaults.TrustedProxies, TrustedProxies: defaults.TrustedProxies,
SoftwareVersion: defaults.SoftwareVersion, SoftwareVersion: defaults.SoftwareVersion,
@ -85,6 +86,7 @@ func Default() *Config {
ApplicationName: defaults.ApplicationName, ApplicationName: defaults.ApplicationName,
Host: defaults.Host, Host: defaults.Host,
Protocol: defaults.Protocol, Protocol: defaults.Protocol,
BindAddress: defaults.BindAddress,
Port: defaults.Port, Port: defaults.Port,
TrustedProxies: defaults.TrustedProxies, TrustedProxies: defaults.TrustedProxies,
SoftwareVersion: defaults.SoftwareVersion, SoftwareVersion: defaults.SoftwareVersion,
@ -161,6 +163,7 @@ func GetDefaults() Defaults {
Host: "", Host: "",
AccountDomain: "", AccountDomain: "",
Protocol: "https", Protocol: "https",
BindAddress: "0.0.0.0",
Port: 8080, Port: 8080,
TrustedProxies: []string{"127.0.0.1/32"}, // localhost TrustedProxies: []string{"127.0.0.1/32"}, // localhost
@ -227,6 +230,7 @@ func GetTestDefaults() Defaults {
Host: "localhost:8080", Host: "localhost:8080",
AccountDomain: "localhost:8080", AccountDomain: "localhost:8080",
Protocol: "http", Protocol: "http",
BindAddress: "127.0.0.1",
Port: 8080, Port: 8080,
TrustedProxies: []string{"127.0.0.1/32"}, TrustedProxies: []string{"127.0.0.1/32"},

View file

@ -72,7 +72,8 @@ func (r *router) Start() {
if r.config.LetsEncryptConfig.Enabled { if r.config.LetsEncryptConfig.Enabled {
// serve the http handler on the selected letsencrypt port, for receiving letsencrypt requests and solving their devious riddles // serve the http handler on the selected letsencrypt port, for receiving letsencrypt requests and solving their devious riddles
go func() { go func() {
if err := http.ListenAndServe(fmt.Sprintf(":%d", r.config.LetsEncryptConfig.Port), r.certManager.HTTPHandler(http.HandlerFunc(httpsRedirect))); err != nil && err != http.ErrServerClosed { listen := fmt.Sprintf("%s:%d", r.config.BindAddress, r.config.LetsEncryptConfig.Port)
if err := http.ListenAndServe(listen, r.certManager.HTTPHandler(http.HandlerFunc(httpsRedirect))); err != nil && err != http.ErrServerClosed {
logrus.Fatalf("listen: %s", err) logrus.Fatalf("listen: %s", err)
} }
}() }()
@ -138,8 +139,9 @@ func New(ctx context.Context, cfg *config.Config, db db.DB) (Router, error) {
} }
// create the http server here, passing the gin engine as handler // create the http server here, passing the gin engine as handler
listen := fmt.Sprintf("%s:%d", cfg.BindAddress, cfg.Port)
s := &http.Server{ s := &http.Server{
Addr: fmt.Sprintf(":%d", cfg.Port), Addr: listen,
Handler: engine, Handler: engine,
ReadTimeout: readTimeout, ReadTimeout: readTimeout,
WriteTimeout: writeTimeout, WriteTimeout: writeTimeout,