Skip to content

Commit

Permalink
Pull request: 3172 fix mobileconfig
Browse files Browse the repository at this point in the history
Merge in DNS/adguard-home from 3172-mobileconfig to master

Updates AdguardTeam#3172.
Updates AdguardTeam#2497.

Squashed commit of the following:

commit 30549ef
Author: Eugene Burkov <[email protected]>
Date:   Tue Jun 1 21:00:17 2021 +0300

    all: log changes

commit 9b94294
Author: Ildar Kamalov <[email protected]>
Date:   Tue Jun 1 17:56:59 2021 +0300

    client: always show port input

commit 6d6a0bd
Merge: 13a3bff 77946a7
Author: Eugene Burkov <[email protected]>
Date:   Tue Jun 1 17:50:41 2021 +0300

    Merge branch 'master' into 3172-mobileconfig

commit 13a3bff
Author: Ildar Kamalov <[email protected]>
Date:   Tue Jun 1 17:20:17 2021 +0300

    client: add port to mobile config form

commit f6abe0b
Author: Eugene Burkov <[email protected]>
Date:   Mon May 31 19:43:37 2021 +0300

    home: imp cyclo

commit c304a0b
Author: Eugene Burkov <[email protected]>
Date:   Mon May 31 18:19:46 2021 +0300

    home: reduce allocs

commit 10a7678
Author: Eugene Burkov <[email protected]>
Date:   Mon May 24 20:05:08 2021 +0300

    all: make the host parameter required
  • Loading branch information
EugeneOne1 authored and heyxkhoa committed Mar 17, 2023
1 parent 00c77c3 commit 7743381
Show file tree
Hide file tree
Showing 17 changed files with 223 additions and 186 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*.db
*.log
*.snap
/agh-backup/
/bin/
/build/*
/build2/*
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to

### Added

- Support for custom port in DNS-over-HTTPS profiles for Apple's devices
([#3172]).
- `darwin/arm64` support ([#2443]).
- `freebsd/arm64` support ([#2441]).
- Output of the default addresses of the upstreams used for resolving PTRs for
Expand Down Expand Up @@ -53,6 +55,7 @@ released by then.
[#2441]: https://github.com/AdguardTeam/AdGuardHome/issues/2441
[#2443]: https://github.com/AdguardTeam/AdGuardHome/issues/2443
[#3136]: https://github.com/AdguardTeam/AdGuardHome/issues/3136
[#3172]: https://github.com/AdguardTeam/AdGuardHome/issues/3172
[#3184]: https://github.com/AdguardTeam/AdGuardHome/issues/3184
[#3185]: https://github.com/AdguardTeam/AdGuardHome/issues/3185
[#3186]: https://github.com/AdguardTeam/AdGuardHome/issues/3186
Expand Down
12 changes: 8 additions & 4 deletions client/src/components/ui/Guide/Guide.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ const getTabs = ({
tlsAddress,
httpsAddress,
showDnsPrivacyNotice,
server_name,
serverName,
portHttps,
t,
}) => ({
Router: {
Expand Down Expand Up @@ -276,9 +277,10 @@ const getTabs = ({
</div>
<MobileConfigForm
initialValues={{
host: server_name,
host: serverName,
clientId: '',
protocol: MOBILE_CONFIG_LINKS.DOH,
port: portHttps,
}}
/>
</>
Expand Down Expand Up @@ -311,7 +313,8 @@ const renderContent = ({ title, list, getTitle }) => (

const Guide = ({ dnsAddresses }) => {
const { t } = useTranslation();
const server_name = useSelector((state) => state.encryption?.server_name);
const serverName = useSelector((state) => state.encryption?.server_name);
const portHttps = useSelector((state) => state.encryption?.port_https);
const tlsAddress = dnsAddresses?.filter((item) => item.includes('tls://')) ?? '';
const httpsAddress = dnsAddresses?.filter((item) => item.includes('https://')) ?? '';
const showDnsPrivacyNotice = httpsAddress.length < 1 && tlsAddress.length < 1;
Expand All @@ -322,7 +325,8 @@ const Guide = ({ dnsAddresses }) => {
tlsAddress,
httpsAddress,
showDnsPrivacyNotice,
server_name,
serverName,
portHttps,
t,
});

Expand Down
64 changes: 50 additions & 14 deletions client/src/components/ui/Guide/MobileConfigForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import i18next from 'i18next';
import cn from 'classnames';

import { getPathWithQueryString } from '../../../helpers/helpers';
import { FORM_NAME, MOBILE_CONFIG_LINKS } from '../../../helpers/constants';
import { FORM_NAME, MOBILE_CONFIG_LINKS, STANDARD_HTTPS_PORT } from '../../../helpers/constants';
import {
renderInputField,
renderSelectField,
toNumber,
} from '../../../helpers/form';
import {
validateClientId,
validateServerName,
validatePort,
validateIsSafePort,
} from '../../../helpers/validators';

const getDownloadLink = (host, clientId, protocol, invalid) => {
Expand Down Expand Up @@ -53,7 +56,9 @@ const MobileConfigForm = ({ invalid }) => {
return null;
}

const { host, clientId, protocol } = formValues;
const {
host, clientId, protocol, port,
} = formValues;

const githubLink = (
<a
Expand All @@ -65,21 +70,52 @@ const MobileConfigForm = ({ invalid }) => {
</a>
);

const getHostName = () => {
if (port
&& port !== STANDARD_HTTPS_PORT
&& protocol === MOBILE_CONFIG_LINKS.DOH
) {
return `${host}:${port}`;
}

return host;
};

return (
<form onSubmit={(e) => e.preventDefault()}>
<div>
<div className="form__group form__group--settings">
<label htmlFor="host" className="form__label">
{i18next.t('dhcp_table_hostname')}
</label>
<Field
name="host"
type="text"
component={renderInputField}
className="form-control"
placeholder={i18next.t('form_enter_hostname')}
validate={validateServerName}
/>
<div className="row">
<div className="col">
<label htmlFor="host" className="form__label">
{i18next.t('dhcp_table_hostname')}
</label>
<Field
name="host"
type="text"
component={renderInputField}
className="form-control"
placeholder={i18next.t('form_enter_hostname')}
validate={validateServerName}
/>
</div>
{protocol === MOBILE_CONFIG_LINKS.DOH && (
<div className="col">
<label htmlFor="port" className="form__label">
{i18next.t('encryption_https')}
</label>
<Field
name="port"
type="number"
component={renderInputField}
className="form-control"
placeholder={i18next.t('encryption_https')}
validate={[validatePort, validateIsSafePort]}
normalize={toNumber}
/>
</div>
)}
</div>
</div>
<div className="form__group form__group--settings">
<label htmlFor="clientId" className="form__label form__label--with-desc">
Expand Down Expand Up @@ -119,7 +155,7 @@ const MobileConfigForm = ({ invalid }) => {
</div>
</div>

{getDownloadLink(host, clientId, protocol, invalid)}
{getDownloadLink(getHostName(), clientId, protocol, invalid)}
</form>
);
};
Expand Down
10 changes: 8 additions & 2 deletions internal/aghnet/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func GetSubnet(ifaceName string) *net.IPNet {

// CheckPortAvailable - check if TCP port is available
func CheckPortAvailable(host net.IP, port int) error {
ln, err := net.Listen("tcp", net.JoinHostPort(host.String(), strconv.Itoa(port)))
ln, err := net.Listen("tcp", JoinHostPort(host.String(), port))
if err != nil {
return err
}
Expand All @@ -200,7 +200,7 @@ func CheckPortAvailable(host net.IP, port int) error {

// CheckPacketPortAvailable - check if UDP port is available
func CheckPacketPortAvailable(host net.IP, port int) error {
ln, err := net.ListenPacket("udp", net.JoinHostPort(host.String(), strconv.Itoa(port)))
ln, err := net.ListenPacket("udp", JoinHostPort(host.String(), port))
if err != nil {
return err
}
Expand Down Expand Up @@ -424,3 +424,9 @@ func CollectAllIfacesAddrs() (addrs []string, err error) {

return addrs, nil
}

// JoinHostPort is a convinient wrapper for net.JoinHostPort with port of type
// int.
func JoinHostPort(host string, port int) (hostport string) {
return net.JoinHostPort(host, strconv.Itoa(port))
}
3 changes: 2 additions & 1 deletion internal/aghstrings/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func CloneSlice(a []string) (b []string) {

// Coalesce returns the first non-empty string. It is named after the function
// COALESCE in SQL except that since strings in Go are non-nullable, it uses an
// empty string as a NULL value. If strs is empty, it returns an empty string.
// empty string as a NULL value. If strs or all it's elements are empty, it
// returns an empty string.
func Coalesce(strs ...string) (res string) {
for _, s := range strs {
if s != "" {
Expand Down
5 changes: 3 additions & 2 deletions internal/dhcpd/checkother.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"runtime"
"time"

"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/insomniacslk/dhcp/dhcpv4"
Expand Down Expand Up @@ -44,7 +45,7 @@ func CheckIfOtherDHCPServersPresentV4(ifaceName string) (ok bool, err error) {
}

srcIP := ifaceIPNet[0]
src := net.JoinHostPort(srcIP.String(), "68")
src := aghnet.JoinHostPort(srcIP.String(), 68)
dst := "255.255.255.255:67"

hostname, _ := os.Hostname()
Expand Down Expand Up @@ -175,7 +176,7 @@ func CheckIfOtherDHCPServersPresentV6(ifaceName string) (ok bool, err error) {
}

srcIP := ifaceIPNet[0]
src := net.JoinHostPort(srcIP.String(), "546")
src := aghnet.JoinHostPort(srcIP.String(), 546)
dst := "[ff02::1:2]:547"

req, err := dhcpv6.NewSolicit(iface.HardwareAddr)
Expand Down
10 changes: 5 additions & 5 deletions internal/home/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"
"net/url"
"runtime"
"strconv"
"strings"

"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
Expand Down Expand Up @@ -38,9 +37,11 @@ func httpError(w http.ResponseWriter, code int, format string, args ...interface
// addresses to a slice of strings.
func appendDNSAddrs(dst []string, addrs ...net.IP) (res []string) {
for _, addr := range addrs {
hostport := addr.String()
var hostport string
if config.DNS.Port != 53 {
hostport = net.JoinHostPort(hostport, strconv.Itoa(config.DNS.Port))
hostport = aghnet.JoinHostPort(addr.String(), config.DNS.Port)
} else {
hostport = addr.String()
}

dst = append(dst, hostport)
Expand Down Expand Up @@ -303,8 +304,7 @@ func handleHTTPSRedirect(w http.ResponseWriter, r *http.Request) (ok bool) {
if r.TLS == nil && web.forceHTTPS {
hostPort := host
if port := web.conf.PortHTTPS; port != defaultHTTPSPort {
portStr := strconv.Itoa(port)
hostPort = net.JoinHostPort(host, portStr)
hostPort = aghnet.JoinHostPort(host, port)
}

httpsURL := &url.URL{
Expand Down
41 changes: 23 additions & 18 deletions internal/home/controlinstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -286,42 +285,48 @@ func shutdownSrv(ctx context.Context, cancel context.CancelFunc, srv *http.Serve

// Apply new configuration, start DNS server, restart Web server
func (web *Web) handleInstallConfigure(w http.ResponseWriter, r *http.Request) {
newSettings := applyConfigReq{}
err := json.NewDecoder(r.Body).Decode(&newSettings)
req := applyConfigReq{}
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
httpError(w, http.StatusBadRequest, "Failed to parse 'configure' JSON: %s", err)
return
}

if newSettings.Web.Port == 0 || newSettings.DNS.Port == 0 {
if req.Web.Port == 0 || req.DNS.Port == 0 {
httpError(w, http.StatusBadRequest, "port value can't be 0")
return
}

restartHTTP := true
if config.BindHost.Equal(newSettings.Web.IP) && config.BindPort == newSettings.Web.Port {
if config.BindHost.Equal(req.Web.IP) && config.BindPort == req.Web.Port {
// no need to rebind
restartHTTP = false
}

// validate that hosts and ports are bindable
if restartHTTP {
err = aghnet.CheckPortAvailable(newSettings.Web.IP, newSettings.Web.Port)
err = aghnet.CheckPortAvailable(req.Web.IP, req.Web.Port)
if err != nil {
httpError(w, http.StatusBadRequest, "Impossible to listen on IP:port %s due to %s",
net.JoinHostPort(newSettings.Web.IP.String(), strconv.Itoa(newSettings.Web.Port)), err)
httpError(
w,
http.StatusBadRequest,
"can not listen on IP:port %s: %s",
aghnet.JoinHostPort(req.Web.IP.String(), req.Web.Port),
err,
)

return
}

}

err = aghnet.CheckPacketPortAvailable(newSettings.DNS.IP, newSettings.DNS.Port)
err = aghnet.CheckPacketPortAvailable(req.DNS.IP, req.DNS.Port)
if err != nil {
httpError(w, http.StatusBadRequest, "%s", err)
return
}

err = aghnet.CheckPortAvailable(newSettings.DNS.IP, newSettings.DNS.Port)
err = aghnet.CheckPortAvailable(req.DNS.IP, req.DNS.Port)
if err != nil {
httpError(w, http.StatusBadRequest, "%s", err)
return
Expand All @@ -331,10 +336,10 @@ func (web *Web) handleInstallConfigure(w http.ResponseWriter, r *http.Request) {
copyInstallSettings(&curConfig, &config)

Context.firstRun = false
config.BindHost = newSettings.Web.IP
config.BindPort = newSettings.Web.Port
config.DNS.BindHosts = []net.IP{newSettings.DNS.IP}
config.DNS.Port = newSettings.DNS.Port
config.BindHost = req.Web.IP
config.BindPort = req.Web.Port
config.DNS.BindHosts = []net.IP{req.DNS.IP}
config.DNS.Port = req.DNS.Port

// TODO(e.burkov): StartMods() should be put in a separate goroutine at
// the moment we'll allow setting up TLS in the initial configuration or
Expand All @@ -349,8 +354,8 @@ func (web *Web) handleInstallConfigure(w http.ResponseWriter, r *http.Request) {
}

u := User{}
u.Name = newSettings.Username
Context.auth.UserAdd(&u, newSettings.Password)
u.Name = req.Username
Context.auth.UserAdd(&u, req.Password)

err = config.write()
if err != nil {
Expand All @@ -361,8 +366,8 @@ func (web *Web) handleInstallConfigure(w http.ResponseWriter, r *http.Request) {
}

web.conf.firstRun = false
web.conf.BindHost = newSettings.Web.IP
web.conf.BindPort = newSettings.Web.Port
web.conf.BindHost = req.Web.IP
web.conf.BindPort = req.Web.Port

registerControlHandlers()

Expand Down
Loading

0 comments on commit 7743381

Please sign in to comment.