Skip to content

Commit

Permalink
all: use netip for web
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Oct 12, 2022
1 parent 38305e5 commit e9faeb7
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 37 deletions.
14 changes: 7 additions & 7 deletions internal/aghnet/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func NetInterfaceFrom(iface *net.Interface) (niface *NetInterface, err error) {
for _, addr := range addrs {
n, ok := addr.(*net.IPNet)
if !ok {
// Should be net.IPNet, this is weird.
// Should be *net.IPNet, this is weird.
return nil, fmt.Errorf("expected %[2]s to be %[1]T, got %[2]T", n, addr)
} else if ip4 := n.IP.To4(); ip4 != nil {
n.IP = ip4
Expand All @@ -141,13 +141,13 @@ func NetInterfaceFrom(iface *net.Interface) (niface *NetInterface, err error) {
return nil, fmt.Errorf("bad address %s", n.IP)
}

ip = ip.WithZone(iface.Name)
if ip.IsLinkLocalUnicast() {
// Ignore link-local IPv4.
if ip.Is4() {
continue
}

// Ignore link-local IPv4.
//
// TODO(e.burkov): !! or not??
if ip.Is4() && ip.IsLinkLocalUnicast() {
continue
ip = ip.WithZone(iface.Name)
}

ones, _ := n.Mask.Size()
Expand Down
2 changes: 1 addition & 1 deletion internal/aghnet/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func TestBroadcastFromIPNet(t *testing.T) {
}

func TestCheckPort(t *testing.T) {
laddr := netip.AddrPortFrom(netip.AddrFrom4([4]byte{127, 0, 0, 1}), 0)
laddr := netip.AddrPortFrom(IPv4Localhost(), 0)

t.Run("tcp_bound", func(t *testing.T) {
l, err := net.Listen("tcp", laddr.String())
Expand Down
11 changes: 5 additions & 6 deletions internal/home/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package home
import (
"bytes"
"fmt"
"net"
"net/netip"
"os"
"path/filepath"
Expand Down Expand Up @@ -86,10 +85,10 @@ type configuration struct {
// It's reset after config is parsed
fileData []byte

BindHost net.IP `yaml:"bind_host"` // BindHost is the IP address of the HTTP server to bind to
BindPort int `yaml:"bind_port"` // BindPort is the port the HTTP server
BetaBindPort int `yaml:"beta_bind_port"` // BetaBindPort is the port for new client
Users []webUser `yaml:"users"` // Users that can access HTTP server
BindHost netip.Addr `yaml:"bind_host"` // BindHost is the IP address of the HTTP server to bind to
BindPort int `yaml:"bind_port"` // BindPort is the port the HTTP server
BetaBindPort int `yaml:"beta_bind_port"` // BetaBindPort is the port for new client
Users []webUser `yaml:"users"` // Users that can access HTTP server
// AuthAttempts is the maximum number of failed login attempts a user
// can do before being blocked.
AuthAttempts uint `yaml:"auth_attempts"`
Expand Down Expand Up @@ -212,7 +211,7 @@ type tlsConfigSettings struct {
var config = &configuration{
BindPort: 3000,
BetaBindPort: 0,
BindHost: net.IP{0, 0, 0, 0},
BindHost: netip.IPv4Unspecified(),
AuthAttempts: 5,
AuthBlockMin: 15,
WebSessionTTLHours: 30 * 24,
Expand Down
2 changes: 1 addition & 1 deletion internal/home/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func appendDNSAddrsWithIfaces(dst []string, src []netip.Addr) (res []string, err
// on, including the addresses on all interfaces in cases of unspecified IPs.
func collectDNSAddresses() (addrs []string, err error) {
if hosts := config.DNS.BindHosts; len(hosts) == 0 {
addr := netip.AddrFrom4([4]byte{127, 0, 0, 1})
addr := aghnet.IPv4Localhost()

addrs = appendDNSAddrs(addrs, addr)
} else {
Expand Down
6 changes: 3 additions & 3 deletions internal/home/controlinstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func (web *Web) handleInstallConfigure(w http.ResponseWriter, r *http.Request) {
copyInstallSettings(curConfig, config)

Context.firstRun = false
config.BindHost = req.Web.IP.AsSlice()
config.BindHost = req.Web.IP
config.BindPort = req.Web.Port
config.DNS.BindHosts = []netip.Addr{req.DNS.IP}
config.DNS.Port = req.DNS.Port
Expand Down Expand Up @@ -448,7 +448,7 @@ func (web *Web) handleInstallConfigure(w http.ResponseWriter, r *http.Request) {
}

web.conf.firstRun = false
web.conf.BindHost = req.Web.IP.AsSlice()
web.conf.BindHost = req.Web.IP
web.conf.BindPort = req.Web.Port

registerControlHandlers()
Expand Down Expand Up @@ -490,7 +490,7 @@ func decodeApplyConfigReq(r io.Reader) (req *applyConfigReq, restartHTTP bool, e
return nil, false, errors.Error("ports cannot be 0")
}

restartHTTP = !config.BindHost.Equal(req.Web.IP.AsSlice()) || config.BindPort != req.Web.Port
restartHTTP = config.BindHost != req.Web.IP || config.BindPort != req.Web.Port
if restartHTTP {
err = aghnet.CheckPort("tcp", netip.AddrPortFrom(req.Web.IP, uint16(req.Web.Port)))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/home/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func generateServerConfig() (newConf dnsforward.ServerConfig, err error) {
dnsConf := config.DNS
hosts := dnsConf.BindHosts
if len(hosts) == 0 {
hosts = []netip.Addr{netip.AddrFrom4([4]byte{127, 0, 0, 1})}
hosts = []netip.Addr{aghnet.IPv4Localhost()}
}

newConf = dnsforward.ServerConfig{
Expand Down
2 changes: 1 addition & 1 deletion internal/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func setupConfig(opts options) (err error) {
}

// override bind host/port from the console
if opts.bindHost != nil {
if opts.bindHost.IsValid() {
config.BindHost = opts.bindHost
}
if len(opts.pidFile) != 0 && writePIDFile(opts.pidFile) {
Expand Down
13 changes: 7 additions & 6 deletions internal/home/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package home

import (
"fmt"
"net"
"net/netip"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -35,7 +35,7 @@ type options struct {
serviceControlAction string

// bindHost is the address on which to serve the HTTP UI.
bindHost net.IP
bindHost netip.Addr

// bindPort is the port on which to serve the HTTP UI.
bindPort int
Expand Down Expand Up @@ -130,14 +130,15 @@ var cmdLineOpts = []cmdLineOpt{{
longName: "work-dir",
shortName: "w",
}, {
updateWithValue: func(o options, v string) (options, error) {
o.bindHost = net.ParseIP(v)
return o, nil
updateWithValue: func(o options, v string) (oo options, err error) {
o.bindHost, err = netip.ParseAddr(v)

return o, err
},
updateNoValue: nil,
effect: nil,
serialize: func(o options) (val string, ok bool) {
if o.bindHost == nil {
if !o.bindHost.IsValid() {
return "", false
}

Expand Down
12 changes: 7 additions & 5 deletions internal/home/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package home

import (
"fmt"
"net"
"net/netip"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -56,11 +56,13 @@ func TestParseWorkDir(t *testing.T) {
}

func TestParseBindHost(t *testing.T) {
assert.Nil(t, testParseOK(t).bindHost, "empty is not host")
assert.Equal(t, net.IPv4(1, 2, 3, 4), testParseOK(t, "-h", "1.2.3.4").bindHost, "-h is host")
wantAddr := netip.AddrFrom4([4]byte{1, 2, 3, 4})

assert.Zero(t, testParseOK(t).bindHost, "empty is not host")
assert.Equal(t, wantAddr, testParseOK(t, "-h", "1.2.3.4").bindHost, "-h is host")
testParseParamMissing(t, "-h")

assert.Equal(t, net.IPv4(1, 2, 3, 4), testParseOK(t, "--host", "1.2.3.4").bindHost, "--host is host")
assert.Equal(t, wantAddr, testParseOK(t, "--host", "1.2.3.4").bindHost, "--host is host")
testParseParamMissing(t, "--host")
}

Expand Down Expand Up @@ -149,8 +151,8 @@ func TestOptsToArgs(t *testing.T) {
opts: options{workDir: "path"},
}, {
name: "bind_host",
opts: options{bindHost: netip.AddrFrom4([4]byte{1, 2, 3, 4})},
args: []string{"-h", "1.2.3.4"},
opts: options{bindHost: net.IP{1, 2, 3, 4}},
}, {
name: "bind_port",
args: []string{"-p", "666"},
Expand Down
8 changes: 2 additions & 6 deletions internal/home/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"crypto/tls"
"io/fs"
"net"
"net/http"
"net/netip"
"sync"
Expand Down Expand Up @@ -38,8 +37,7 @@ type webConfig struct {
clientFS fs.FS
clientBetaFS fs.FS

// TODO(e.burkov): !! use netip
BindHost net.IP
BindHost netip.Addr
BindPort int
BetaBindPort int
PortHTTPS int
Expand Down Expand Up @@ -141,9 +139,7 @@ func webCheckPortAvailable(port int) (ok bool) {
return true
}

addr, ok := netip.AddrFromSlice(config.BindHost)

return ok && aghnet.CheckPort("tcp", netip.AddrPortFrom(addr.Unmap(), uint16(port))) == nil
return aghnet.CheckPort("tcp", netip.AddrPortFrom(config.BindHost, uint16(port))) == nil
}

// TLSConfigChanged updates the TLS configuration and restarts the HTTPS server
Expand Down

0 comments on commit e9faeb7

Please sign in to comment.