Merge: + DHCP: Reset DHCP configuration; Refactor
Close #1024 * commit 'a6926169815e03d00f9cfac0417da6f797d539ae': - clients: fix tests * clients: refactor + openapi: /dhcp/reset + client: handle DHCP reset + POST /control/dhcp/reset: Reset DHCP configuration * dhcp: move HTTP handlers to dhcpd/ * dhcp,clients: DHCP server module is passed to Clients module during initialization.
This commit is contained in:
commit
73d17ffa81
|
@ -28,6 +28,7 @@ Contents:
|
||||||
* "Enable DHCP" command
|
* "Enable DHCP" command
|
||||||
* Static IP check/set
|
* Static IP check/set
|
||||||
* Add a static lease
|
* Add a static lease
|
||||||
|
* API: Reset DHCP configuration
|
||||||
* DNS access settings
|
* DNS access settings
|
||||||
* List access settings
|
* List access settings
|
||||||
* Set access settings
|
* Set access settings
|
||||||
|
@ -543,6 +544,20 @@ Response:
|
||||||
200 OK
|
200 OK
|
||||||
|
|
||||||
|
|
||||||
|
### API: Reset DHCP configuration
|
||||||
|
|
||||||
|
Clear all DHCP leases and configuration settings.
|
||||||
|
DHCP server will be stopped if it's currently running.
|
||||||
|
|
||||||
|
Request:
|
||||||
|
|
||||||
|
POST /control/dhcp/reset
|
||||||
|
|
||||||
|
Response:
|
||||||
|
|
||||||
|
200 OK
|
||||||
|
|
||||||
|
|
||||||
## TLS
|
## TLS
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
"dhcp_leases": "DHCP leases",
|
"dhcp_leases": "DHCP leases",
|
||||||
"dhcp_static_leases": "DHCP static leases",
|
"dhcp_static_leases": "DHCP static leases",
|
||||||
"dhcp_leases_not_found": "No DHCP leases found",
|
"dhcp_leases_not_found": "No DHCP leases found",
|
||||||
"dhcp_config_saved": "Saved DHCP server config",
|
"dhcp_config_saved": "DHCP config successfully saved",
|
||||||
"form_error_required": "Required field",
|
"form_error_required": "Required field",
|
||||||
"form_error_ip4_format": "Invalid IPv4 format",
|
"form_error_ip4_format": "Invalid IPv4 format",
|
||||||
"form_error_ip6_format": "Invalid IPv6 format",
|
"form_error_ip6_format": "Invalid IPv6 format",
|
||||||
|
@ -45,6 +45,7 @@
|
||||||
"dhcp_new_static_lease": "New static lease",
|
"dhcp_new_static_lease": "New static lease",
|
||||||
"dhcp_static_leases_not_found": "No DHCP static leases found",
|
"dhcp_static_leases_not_found": "No DHCP static leases found",
|
||||||
"dhcp_add_static_lease": "Add static lease",
|
"dhcp_add_static_lease": "Add static lease",
|
||||||
|
"dhcp_reset": "Are you sure you want to reset DHCP config?",
|
||||||
"delete_confirm": "Are you sure you want to delete \"{{key}}\"?",
|
"delete_confirm": "Are you sure you want to delete \"{{key}}\"?",
|
||||||
"form_enter_hostname": "Enter hostname",
|
"form_enter_hostname": "Enter hostname",
|
||||||
"error_details": "Error details",
|
"error_details": "Error details",
|
||||||
|
|
|
@ -470,6 +470,22 @@ export const toggleDhcp = values => async (dispatch) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const resetDhcpRequest = createAction('RESET_DHCP_REQUEST');
|
||||||
|
export const resetDhcpSuccess = createAction('RESET_DHCP_SUCCESS');
|
||||||
|
export const resetDhcpFailure = createAction('RESET_DHCP_FAILURE');
|
||||||
|
|
||||||
|
export const resetDhcp = () => async (dispatch) => {
|
||||||
|
dispatch(resetDhcpRequest());
|
||||||
|
try {
|
||||||
|
const status = await apiClient.resetDhcp();
|
||||||
|
dispatch(resetDhcpSuccess(status));
|
||||||
|
dispatch(addSuccessToast('dhcp_config_saved'));
|
||||||
|
} catch (error) {
|
||||||
|
dispatch(addErrorToast({ error }));
|
||||||
|
dispatch(resetDhcpFailure());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
export const toggleLeaseModal = createAction('TOGGLE_LEASE_MODAL');
|
export const toggleLeaseModal = createAction('TOGGLE_LEASE_MODAL');
|
||||||
|
|
||||||
export const addStaticLeaseRequest = createAction('ADD_STATIC_LEASE_REQUEST');
|
export const addStaticLeaseRequest = createAction('ADD_STATIC_LEASE_REQUEST');
|
||||||
|
|
|
@ -248,6 +248,7 @@ class Api {
|
||||||
DHCP_INTERFACES = { path: 'dhcp/interfaces', method: 'GET' };
|
DHCP_INTERFACES = { path: 'dhcp/interfaces', method: 'GET' };
|
||||||
DHCP_ADD_STATIC_LEASE = { path: 'dhcp/add_static_lease', method: 'POST' };
|
DHCP_ADD_STATIC_LEASE = { path: 'dhcp/add_static_lease', method: 'POST' };
|
||||||
DHCP_REMOVE_STATIC_LEASE = { path: 'dhcp/remove_static_lease', method: 'POST' };
|
DHCP_REMOVE_STATIC_LEASE = { path: 'dhcp/remove_static_lease', method: 'POST' };
|
||||||
|
DHCP_RESET = { path: 'dhcp/reset', method: 'POST' };
|
||||||
|
|
||||||
getDhcpStatus() {
|
getDhcpStatus() {
|
||||||
const { path, method } = this.DHCP_STATUS;
|
const { path, method } = this.DHCP_STATUS;
|
||||||
|
@ -295,6 +296,11 @@ class Api {
|
||||||
return this.makeRequest(path, method, parameters);
|
return this.makeRequest(path, method, parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resetDhcp() {
|
||||||
|
const { path, method } = this.DHCP_RESET;
|
||||||
|
return this.makeRequest(path, method);
|
||||||
|
}
|
||||||
|
|
||||||
// Installation
|
// Installation
|
||||||
INSTALL_GET_ADDRESSES = { path: 'install/get_addresses', method: 'GET' };
|
INSTALL_GET_ADDRESSES = { path: 'install/get_addresses', method: 'GET' };
|
||||||
INSTALL_CONFIGURE = { path: 'install/configure', method: 'POST' };
|
INSTALL_CONFIGURE = { path: 'install/configure', method: 'POST' };
|
||||||
|
|
|
@ -50,6 +50,23 @@ const renderInterfaceValues = (interfaceValues => (
|
||||||
</ul>
|
</ul>
|
||||||
));
|
));
|
||||||
|
|
||||||
|
const clearFields = (change, resetDhcp, t) => {
|
||||||
|
const fields = {
|
||||||
|
interface_name: '',
|
||||||
|
gateway_ip: '',
|
||||||
|
subnet_mask: '',
|
||||||
|
range_start: '',
|
||||||
|
range_end: '',
|
||||||
|
lease_duration: 86400,
|
||||||
|
};
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-alert
|
||||||
|
if (window.confirm(t('dhcp_reset'))) {
|
||||||
|
Object.keys(fields).forEach(field => change(field, fields[field]));
|
||||||
|
resetDhcp();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let Form = (props) => {
|
let Form = (props) => {
|
||||||
const {
|
const {
|
||||||
t,
|
t,
|
||||||
|
@ -61,6 +78,8 @@ let Form = (props) => {
|
||||||
interfaceValue,
|
interfaceValue,
|
||||||
processingConfig,
|
processingConfig,
|
||||||
processingInterfaces,
|
processingInterfaces,
|
||||||
|
resetDhcp,
|
||||||
|
change,
|
||||||
} = props;
|
} = props;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -160,6 +179,7 @@ let Form = (props) => {
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div className="btn-list">
|
||||||
<button
|
<button
|
||||||
type="submit"
|
type="submit"
|
||||||
className="btn btn-success btn-standard"
|
className="btn btn-success btn-standard"
|
||||||
|
@ -167,24 +187,34 @@ let Form = (props) => {
|
||||||
>
|
>
|
||||||
{t('save_config')}
|
{t('save_config')}
|
||||||
</button>
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="btn btn-secondary btn-standart"
|
||||||
|
disabled={submitting || processingConfig}
|
||||||
|
onClick={() => clearFields(change, resetDhcp, t)}
|
||||||
|
>
|
||||||
|
<Trans>reset_settings</Trans>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</form>
|
</form>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
Form.propTypes = {
|
Form.propTypes = {
|
||||||
handleSubmit: PropTypes.func,
|
handleSubmit: PropTypes.func.isRequired,
|
||||||
submitting: PropTypes.bool,
|
submitting: PropTypes.bool.isRequired,
|
||||||
invalid: PropTypes.bool,
|
invalid: PropTypes.bool.isRequired,
|
||||||
interfaces: PropTypes.object,
|
interfaces: PropTypes.object.isRequired,
|
||||||
interfaceValue: PropTypes.string,
|
interfaceValue: PropTypes.string,
|
||||||
initialValues: PropTypes.object,
|
initialValues: PropTypes.object.isRequired,
|
||||||
processingConfig: PropTypes.bool,
|
processingConfig: PropTypes.bool.isRequired,
|
||||||
processingInterfaces: PropTypes.bool,
|
processingInterfaces: PropTypes.bool.isRequired,
|
||||||
enabled: PropTypes.bool,
|
enabled: PropTypes.bool.isRequired,
|
||||||
t: PropTypes.func,
|
t: PropTypes.func.isRequired,
|
||||||
|
resetDhcp: PropTypes.func.isRequired,
|
||||||
|
change: PropTypes.func.isRequired,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const selector = formValueSelector('dhcpForm');
|
const selector = formValueSelector('dhcpForm');
|
||||||
|
|
||||||
Form = connect((state) => {
|
Form = connect((state) => {
|
||||||
|
|
|
@ -154,7 +154,15 @@ class Dhcp extends Component {
|
||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { t, dhcp } = this.props;
|
const {
|
||||||
|
t,
|
||||||
|
dhcp,
|
||||||
|
resetDhcp,
|
||||||
|
findActiveDhcp,
|
||||||
|
addStaticLease,
|
||||||
|
removeStaticLease,
|
||||||
|
toggleLeaseModal,
|
||||||
|
} = this.props;
|
||||||
const statusButtonClass = classnames({
|
const statusButtonClass = classnames({
|
||||||
'btn btn-primary btn-standard': true,
|
'btn btn-primary btn-standard': true,
|
||||||
'btn btn-primary btn-standard btn-loading': dhcp.processingStatus,
|
'btn btn-primary btn-standard btn-loading': dhcp.processingStatus,
|
||||||
|
@ -184,6 +192,7 @@ class Dhcp extends Component {
|
||||||
processingConfig={dhcp.processingConfig}
|
processingConfig={dhcp.processingConfig}
|
||||||
processingInterfaces={dhcp.processingInterfaces}
|
processingInterfaces={dhcp.processingInterfaces}
|
||||||
enabled={enabled}
|
enabled={enabled}
|
||||||
|
resetDhcp={resetDhcp}
|
||||||
/>
|
/>
|
||||||
<hr />
|
<hr />
|
||||||
<div className="card-actions mb-3">
|
<div className="card-actions mb-3">
|
||||||
|
@ -191,9 +200,7 @@ class Dhcp extends Component {
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className={statusButtonClass}
|
className={statusButtonClass}
|
||||||
onClick={() =>
|
onClick={() => findActiveDhcp(interface_name)}
|
||||||
this.props.findActiveDhcp(interface_name)
|
|
||||||
}
|
|
||||||
disabled={
|
disabled={
|
||||||
enabled || !interface_name || dhcp.processingConfig
|
enabled || !interface_name || dhcp.processingConfig
|
||||||
}
|
}
|
||||||
|
@ -232,9 +239,9 @@ class Dhcp extends Component {
|
||||||
<StaticLeases
|
<StaticLeases
|
||||||
staticLeases={dhcp.staticLeases}
|
staticLeases={dhcp.staticLeases}
|
||||||
isModalOpen={dhcp.isModalOpen}
|
isModalOpen={dhcp.isModalOpen}
|
||||||
addStaticLease={this.props.addStaticLease}
|
addStaticLease={addStaticLease}
|
||||||
removeStaticLease={this.props.removeStaticLease}
|
removeStaticLease={removeStaticLease}
|
||||||
toggleLeaseModal={this.props.toggleLeaseModal}
|
toggleLeaseModal={toggleLeaseModal}
|
||||||
processingAdding={dhcp.processingAdding}
|
processingAdding={dhcp.processingAdding}
|
||||||
processingDeleting={dhcp.processingDeleting}
|
processingDeleting={dhcp.processingDeleting}
|
||||||
/>
|
/>
|
||||||
|
@ -243,7 +250,7 @@ class Dhcp extends Component {
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="btn btn-success btn-standard mt-3"
|
className="btn btn-success btn-standard mt-3"
|
||||||
onClick={() => this.props.toggleLeaseModal()}
|
onClick={() => toggleLeaseModal()}
|
||||||
>
|
>
|
||||||
<Trans>dhcp_add_static_lease</Trans>
|
<Trans>dhcp_add_static_lease</Trans>
|
||||||
</button>
|
</button>
|
||||||
|
@ -260,16 +267,17 @@ class Dhcp extends Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
Dhcp.propTypes = {
|
Dhcp.propTypes = {
|
||||||
dhcp: PropTypes.object,
|
dhcp: PropTypes.object.isRequired,
|
||||||
toggleDhcp: PropTypes.func,
|
toggleDhcp: PropTypes.func.isRequired,
|
||||||
getDhcpStatus: PropTypes.func,
|
getDhcpStatus: PropTypes.func.isRequired,
|
||||||
setDhcpConfig: PropTypes.func,
|
setDhcpConfig: PropTypes.func.isRequired,
|
||||||
findActiveDhcp: PropTypes.func,
|
findActiveDhcp: PropTypes.func.isRequired,
|
||||||
addStaticLease: PropTypes.func,
|
addStaticLease: PropTypes.func.isRequired,
|
||||||
removeStaticLease: PropTypes.func,
|
removeStaticLease: PropTypes.func.isRequired,
|
||||||
toggleLeaseModal: PropTypes.func,
|
toggleLeaseModal: PropTypes.func.isRequired,
|
||||||
getDhcpInterfaces: PropTypes.func,
|
getDhcpInterfaces: PropTypes.func.isRequired,
|
||||||
t: PropTypes.func,
|
t: PropTypes.func.isRequired,
|
||||||
|
resetDhcp: PropTypes.func.isRequired,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default withNamespaces()(Dhcp);
|
export default withNamespaces()(Dhcp);
|
||||||
|
|
|
@ -8,6 +8,7 @@ import {
|
||||||
toggleLeaseModal,
|
toggleLeaseModal,
|
||||||
addStaticLease,
|
addStaticLease,
|
||||||
removeStaticLease,
|
removeStaticLease,
|
||||||
|
resetDhcp,
|
||||||
} from '../actions';
|
} from '../actions';
|
||||||
import Dhcp from '../components/Settings/Dhcp';
|
import Dhcp from '../components/Settings/Dhcp';
|
||||||
|
|
||||||
|
@ -28,6 +29,7 @@ const mapDispatchToProps = {
|
||||||
toggleLeaseModal,
|
toggleLeaseModal,
|
||||||
addStaticLease,
|
addStaticLease,
|
||||||
removeStaticLease,
|
removeStaticLease,
|
||||||
|
resetDhcp,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default connect(
|
export default connect(
|
||||||
|
|
|
@ -289,6 +289,16 @@ const dhcp = handleActions(
|
||||||
return newState;
|
return newState;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
[actions.resetDhcpRequest]: state => ({ ...state, processingReset: true }),
|
||||||
|
[actions.resetDhcpFailure]: state => ({ ...state, processingReset: false }),
|
||||||
|
[actions.resetDhcpSuccess]: state => ({
|
||||||
|
...state,
|
||||||
|
processingReset: false,
|
||||||
|
config: {
|
||||||
|
enabled: false,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
|
||||||
[actions.toggleLeaseModal]: (state) => {
|
[actions.toggleLeaseModal]: (state) => {
|
||||||
const newState = {
|
const newState = {
|
||||||
...state,
|
...state,
|
||||||
|
|
|
@ -0,0 +1,485 @@
|
||||||
|
package dhcpd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/AdguardTeam/golibs/file"
|
||||||
|
"github.com/AdguardTeam/golibs/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func httpError(r *http.Request, w http.ResponseWriter, code int, format string, args ...interface{}) {
|
||||||
|
text := fmt.Sprintf(format, args...)
|
||||||
|
log.Info("DHCP: %s %s: %s", r.Method, r.URL, text)
|
||||||
|
http.Error(w, text, code)
|
||||||
|
}
|
||||||
|
|
||||||
|
// []Lease -> JSON
|
||||||
|
func convertLeases(inputLeases []Lease, includeExpires bool) []map[string]string {
|
||||||
|
leases := []map[string]string{}
|
||||||
|
for _, l := range inputLeases {
|
||||||
|
lease := map[string]string{
|
||||||
|
"mac": l.HWAddr.String(),
|
||||||
|
"ip": l.IP.String(),
|
||||||
|
"hostname": l.Hostname,
|
||||||
|
}
|
||||||
|
|
||||||
|
if includeExpires {
|
||||||
|
lease["expires"] = l.Expiry.Format(time.RFC3339)
|
||||||
|
}
|
||||||
|
|
||||||
|
leases = append(leases, lease)
|
||||||
|
}
|
||||||
|
return leases
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleDHCPStatus(w http.ResponseWriter, r *http.Request) {
|
||||||
|
leases := convertLeases(s.Leases(), true)
|
||||||
|
staticLeases := convertLeases(s.StaticLeases(), false)
|
||||||
|
status := map[string]interface{}{
|
||||||
|
"config": s.conf,
|
||||||
|
"leases": leases,
|
||||||
|
"static_leases": staticLeases,
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
err := json.NewEncoder(w).Encode(status)
|
||||||
|
if err != nil {
|
||||||
|
httpError(r, w, http.StatusInternalServerError, "Unable to marshal DHCP status json: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type staticLeaseJSON struct {
|
||||||
|
HWAddr string `json:"mac"`
|
||||||
|
IP string `json:"ip"`
|
||||||
|
Hostname string `json:"hostname"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type dhcpServerConfigJSON struct {
|
||||||
|
ServerConfig `json:",inline"`
|
||||||
|
StaticLeases []staticLeaseJSON `json:"static_leases"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
|
newconfig := dhcpServerConfigJSON{}
|
||||||
|
err := json.NewDecoder(r.Body).Decode(&newconfig)
|
||||||
|
if err != nil {
|
||||||
|
httpError(r, w, http.StatusBadRequest, "Failed to parse new DHCP config json: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = s.CheckConfig(newconfig.ServerConfig)
|
||||||
|
if err != nil {
|
||||||
|
httpError(r, w, http.StatusBadRequest, "Invalid DHCP configuration: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = s.Stop()
|
||||||
|
if err != nil {
|
||||||
|
log.Error("failed to stop the DHCP server: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = s.Init(newconfig.ServerConfig)
|
||||||
|
if err != nil {
|
||||||
|
httpError(r, w, http.StatusBadRequest, "Invalid DHCP configuration: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.conf.ConfigModified()
|
||||||
|
|
||||||
|
if newconfig.Enabled {
|
||||||
|
|
||||||
|
staticIP, err := hasStaticIP(newconfig.InterfaceName)
|
||||||
|
if !staticIP && err == nil {
|
||||||
|
err = setStaticIP(newconfig.InterfaceName)
|
||||||
|
if err != nil {
|
||||||
|
httpError(r, w, http.StatusInternalServerError, "Failed to configure static IP: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = s.Start()
|
||||||
|
if err != nil {
|
||||||
|
httpError(r, w, http.StatusBadRequest, "Failed to start DHCP server: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type netInterface struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
MTU int `json:"mtu"`
|
||||||
|
HardwareAddr string `json:"hardware_address"`
|
||||||
|
Addresses []string `json:"ip_addresses"`
|
||||||
|
Flags string `json:"flags"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// getValidNetInterfaces returns interfaces that are eligible for DNS and/or DHCP
|
||||||
|
// invalid interface is a ppp interface or the one that doesn't allow broadcasts
|
||||||
|
func getValidNetInterfaces() ([]net.Interface, error) {
|
||||||
|
ifaces, err := net.Interfaces()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Couldn't get list of interfaces: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
netIfaces := []net.Interface{}
|
||||||
|
|
||||||
|
for i := range ifaces {
|
||||||
|
if ifaces[i].Flags&net.FlagPointToPoint != 0 {
|
||||||
|
// this interface is ppp, we're not interested in this one
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
iface := ifaces[i]
|
||||||
|
netIfaces = append(netIfaces, iface)
|
||||||
|
}
|
||||||
|
|
||||||
|
return netIfaces, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleDHCPInterfaces(w http.ResponseWriter, r *http.Request) {
|
||||||
|
response := map[string]interface{}{}
|
||||||
|
|
||||||
|
ifaces, err := getValidNetInterfaces()
|
||||||
|
if err != nil {
|
||||||
|
httpError(r, w, http.StatusInternalServerError, "Couldn't get interfaces: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, iface := range ifaces {
|
||||||
|
if iface.Flags&net.FlagLoopback != 0 {
|
||||||
|
// it's a loopback, skip it
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if iface.Flags&net.FlagBroadcast == 0 {
|
||||||
|
// this interface doesn't support broadcast, skip it
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
addrs, err := iface.Addrs()
|
||||||
|
if err != nil {
|
||||||
|
httpError(r, w, http.StatusInternalServerError, "Failed to get addresses for interface %s: %s", iface.Name, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonIface := netInterface{
|
||||||
|
Name: iface.Name,
|
||||||
|
MTU: iface.MTU,
|
||||||
|
HardwareAddr: iface.HardwareAddr.String(),
|
||||||
|
}
|
||||||
|
|
||||||
|
if iface.Flags != 0 {
|
||||||
|
jsonIface.Flags = iface.Flags.String()
|
||||||
|
}
|
||||||
|
// we don't want link-local addresses in json, so skip them
|
||||||
|
for _, addr := range addrs {
|
||||||
|
ipnet, ok := addr.(*net.IPNet)
|
||||||
|
if !ok {
|
||||||
|
// not an IPNet, should not happen
|
||||||
|
httpError(r, w, http.StatusInternalServerError, "SHOULD NOT HAPPEN: got iface.Addrs() element %s that is not net.IPNet, it is %T", addr, addr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// ignore link-local
|
||||||
|
if ipnet.IP.IsLinkLocalUnicast() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
jsonIface.Addresses = append(jsonIface.Addresses, ipnet.IP.String())
|
||||||
|
}
|
||||||
|
if len(jsonIface.Addresses) != 0 {
|
||||||
|
response[iface.Name] = jsonIface
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.NewEncoder(w).Encode(response)
|
||||||
|
if err != nil {
|
||||||
|
httpError(r, w, http.StatusInternalServerError, "Failed to marshal json with available interfaces: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Perform the following tasks:
|
||||||
|
// . Search for another DHCP server running
|
||||||
|
// . Check if a static IP is configured for the network interface
|
||||||
|
// Respond with results
|
||||||
|
func (s *Server) handleDHCPFindActiveServer(w http.ResponseWriter, r *http.Request) {
|
||||||
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
errorText := fmt.Sprintf("failed to read request body: %s", err)
|
||||||
|
log.Error(errorText)
|
||||||
|
http.Error(w, errorText, http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
interfaceName := strings.TrimSpace(string(body))
|
||||||
|
if interfaceName == "" {
|
||||||
|
errorText := fmt.Sprintf("empty interface name specified")
|
||||||
|
log.Error(errorText)
|
||||||
|
http.Error(w, errorText, http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
found, err := CheckIfOtherDHCPServersPresent(interfaceName)
|
||||||
|
|
||||||
|
othSrv := map[string]interface{}{}
|
||||||
|
foundVal := "no"
|
||||||
|
if found {
|
||||||
|
foundVal = "yes"
|
||||||
|
} else if err != nil {
|
||||||
|
foundVal = "error"
|
||||||
|
othSrv["error"] = err.Error()
|
||||||
|
}
|
||||||
|
othSrv["found"] = foundVal
|
||||||
|
|
||||||
|
staticIP := map[string]interface{}{}
|
||||||
|
isStaticIP, err := hasStaticIP(interfaceName)
|
||||||
|
staticIPStatus := "yes"
|
||||||
|
if err != nil {
|
||||||
|
staticIPStatus = "error"
|
||||||
|
staticIP["error"] = err.Error()
|
||||||
|
} else if !isStaticIP {
|
||||||
|
staticIPStatus = "no"
|
||||||
|
staticIP["ip"] = getFullIP(interfaceName)
|
||||||
|
}
|
||||||
|
staticIP["static"] = staticIPStatus
|
||||||
|
|
||||||
|
result := map[string]interface{}{}
|
||||||
|
result["other_server"] = othSrv
|
||||||
|
result["static_ip"] = staticIP
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
err = json.NewEncoder(w).Encode(result)
|
||||||
|
if err != nil {
|
||||||
|
httpError(r, w, http.StatusInternalServerError, "Failed to marshal DHCP found json: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if network interface has a static IP configured
|
||||||
|
func hasStaticIP(ifaceName string) (bool, error) {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
return false, errors.New("Can't detect static IP: not supported on Windows")
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := ioutil.ReadFile("/etc/dhcpcd.conf")
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
lines := strings.Split(string(body), "\n")
|
||||||
|
nameLine := fmt.Sprintf("interface %s", ifaceName)
|
||||||
|
withinInterfaceCtx := false
|
||||||
|
|
||||||
|
for _, line := range lines {
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
|
||||||
|
if withinInterfaceCtx && len(line) == 0 {
|
||||||
|
// an empty line resets our state
|
||||||
|
withinInterfaceCtx = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(line) == 0 || line[0] == '#' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
|
||||||
|
if !withinInterfaceCtx {
|
||||||
|
if line == nameLine {
|
||||||
|
// we found our interface
|
||||||
|
withinInterfaceCtx = true
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
if strings.HasPrefix(line, "interface ") {
|
||||||
|
// we found another interface - reset our state
|
||||||
|
withinInterfaceCtx = false
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(line, "static ip_address=") {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get IP address with netmask
|
||||||
|
func getFullIP(ifaceName string) string {
|
||||||
|
cmd := exec.Command("ip", "-oneline", "-family", "inet", "address", "show", ifaceName)
|
||||||
|
log.Tracef("executing %s %v", cmd.Path, cmd.Args)
|
||||||
|
d, err := cmd.Output()
|
||||||
|
if err != nil || cmd.ProcessState.ExitCode() != 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
fields := strings.Fields(string(d))
|
||||||
|
if len(fields) < 4 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
_, _, err = net.ParseCIDR(fields[3])
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return fields[3]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get gateway IP address
|
||||||
|
func getGatewayIP(ifaceName string) string {
|
||||||
|
cmd := exec.Command("ip", "route", "show", "dev", ifaceName)
|
||||||
|
log.Tracef("executing %s %v", cmd.Path, cmd.Args)
|
||||||
|
d, err := cmd.Output()
|
||||||
|
if err != nil || cmd.ProcessState.ExitCode() != 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
fields := strings.Fields(string(d))
|
||||||
|
if len(fields) < 3 || fields[0] != "default" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
ip := net.ParseIP(fields[2])
|
||||||
|
if ip == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return fields[2]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set a static IP for network interface
|
||||||
|
func setStaticIP(ifaceName string) error {
|
||||||
|
ip := getFullIP(ifaceName)
|
||||||
|
if len(ip) == 0 {
|
||||||
|
return errors.New("Can't get IP address")
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := ioutil.ReadFile("/etc/dhcpcd.conf")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ip4, _, err := net.ParseCIDR(ip)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
add := fmt.Sprintf("\ninterface %s\nstatic ip_address=%s\n",
|
||||||
|
ifaceName, ip)
|
||||||
|
body = append(body, []byte(add)...)
|
||||||
|
|
||||||
|
gatewayIP := getGatewayIP(ifaceName)
|
||||||
|
if len(gatewayIP) != 0 {
|
||||||
|
add = fmt.Sprintf("static routers=%s\n",
|
||||||
|
gatewayIP)
|
||||||
|
body = append(body, []byte(add)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
add = fmt.Sprintf("static domain_name_servers=%s\n\n",
|
||||||
|
ip4)
|
||||||
|
body = append(body, []byte(add)...)
|
||||||
|
|
||||||
|
err = file.SafeWrite("/etc/dhcpcd.conf", body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleDHCPAddStaticLease(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
lj := staticLeaseJSON{}
|
||||||
|
err := json.NewDecoder(r.Body).Decode(&lj)
|
||||||
|
if err != nil {
|
||||||
|
httpError(r, w, http.StatusBadRequest, "json.Decode: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ip, _ := parseIPv4(lj.IP)
|
||||||
|
if ip == nil {
|
||||||
|
httpError(r, w, http.StatusBadRequest, "invalid IP")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
mac, _ := net.ParseMAC(lj.HWAddr)
|
||||||
|
|
||||||
|
lease := Lease{
|
||||||
|
IP: ip,
|
||||||
|
HWAddr: mac,
|
||||||
|
Hostname: lj.Hostname,
|
||||||
|
}
|
||||||
|
err = s.AddStaticLease(lease)
|
||||||
|
if err != nil {
|
||||||
|
httpError(r, w, http.StatusBadRequest, "%s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleDHCPRemoveStaticLease(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
lj := staticLeaseJSON{}
|
||||||
|
err := json.NewDecoder(r.Body).Decode(&lj)
|
||||||
|
if err != nil {
|
||||||
|
httpError(r, w, http.StatusBadRequest, "json.Decode: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ip, _ := parseIPv4(lj.IP)
|
||||||
|
if ip == nil {
|
||||||
|
httpError(r, w, http.StatusBadRequest, "invalid IP")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
mac, _ := net.ParseMAC(lj.HWAddr)
|
||||||
|
|
||||||
|
lease := Lease{
|
||||||
|
IP: ip,
|
||||||
|
HWAddr: mac,
|
||||||
|
Hostname: lj.Hostname,
|
||||||
|
}
|
||||||
|
err = s.RemoveStaticLease(lease)
|
||||||
|
if err != nil {
|
||||||
|
httpError(r, w, http.StatusBadRequest, "%s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleReset(w http.ResponseWriter, r *http.Request) {
|
||||||
|
err := s.Stop()
|
||||||
|
if err != nil {
|
||||||
|
log.Error("DHCP: Stop: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.Remove(s.conf.DBFilePath)
|
||||||
|
if err != nil && !os.IsNotExist(err) {
|
||||||
|
log.Error("DHCP: os.Remove: %s: %s", s.conf.DBFilePath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
oldconf := s.conf
|
||||||
|
s.conf = ServerConfig{}
|
||||||
|
s.conf.LeaseDuration = 86400
|
||||||
|
s.conf.ICMPTimeout = 1000
|
||||||
|
s.conf.WorkDir = oldconf.WorkDir
|
||||||
|
s.conf.HTTPRegister = oldconf.HTTPRegister
|
||||||
|
s.conf.ConfigModified = oldconf.ConfigModified
|
||||||
|
s.conf.DBFilePath = oldconf.DBFilePath
|
||||||
|
s.conf.ConfigModified()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) registerHandlers() {
|
||||||
|
s.conf.HTTPRegister("GET", "/control/dhcp/status", s.handleDHCPStatus)
|
||||||
|
s.conf.HTTPRegister("GET", "/control/dhcp/interfaces", s.handleDHCPInterfaces)
|
||||||
|
s.conf.HTTPRegister("POST", "/control/dhcp/set_config", s.handleDHCPSetConfig)
|
||||||
|
s.conf.HTTPRegister("POST", "/control/dhcp/find_active_dhcp", s.handleDHCPFindActiveServer)
|
||||||
|
s.conf.HTTPRegister("POST", "/control/dhcp/add_static_lease", s.handleDHCPAddStaticLease)
|
||||||
|
s.conf.HTTPRegister("POST", "/control/dhcp/remove_static_lease", s.handleDHCPRemoveStaticLease)
|
||||||
|
s.conf.HTTPRegister("POST", "/control/dhcp/reset", s.handleReset)
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -38,13 +39,20 @@ type ServerConfig struct {
|
||||||
SubnetMask string `json:"subnet_mask" yaml:"subnet_mask"`
|
SubnetMask string `json:"subnet_mask" yaml:"subnet_mask"`
|
||||||
RangeStart string `json:"range_start" yaml:"range_start"`
|
RangeStart string `json:"range_start" yaml:"range_start"`
|
||||||
RangeEnd string `json:"range_end" yaml:"range_end"`
|
RangeEnd string `json:"range_end" yaml:"range_end"`
|
||||||
LeaseDuration uint `json:"lease_duration" yaml:"lease_duration"` // in seconds
|
LeaseDuration uint32 `json:"lease_duration" yaml:"lease_duration"` // in seconds
|
||||||
WorkDir string `json:"-" yaml:"-"`
|
|
||||||
DBFilePath string `json:"-" yaml:"-"` // path to DB file
|
|
||||||
|
|
||||||
// IP conflict detector: time (ms) to wait for ICMP reply.
|
// IP conflict detector: time (ms) to wait for ICMP reply.
|
||||||
// 0: disable
|
// 0: disable
|
||||||
ICMPTimeout uint `json:"icmp_timeout_msec" yaml:"icmp_timeout_msec"`
|
ICMPTimeout uint32 `json:"icmp_timeout_msec" yaml:"icmp_timeout_msec"`
|
||||||
|
|
||||||
|
WorkDir string `json:"-" yaml:"-"`
|
||||||
|
DBFilePath string `json:"-" yaml:"-"` // path to DB file
|
||||||
|
|
||||||
|
// Called when the configuration is changed by HTTP request
|
||||||
|
ConfigModified func() `json:"-" yaml:"-"`
|
||||||
|
|
||||||
|
// Register an HTTP handler
|
||||||
|
HTTPRegister func(string, string, func(http.ResponseWriter, *http.Request)) `json:"-" yaml:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Server - the current state of the DHCP server
|
// Server - the current state of the DHCP server
|
||||||
|
@ -88,6 +96,16 @@ func (s *Server) CheckConfig(config ServerConfig) error {
|
||||||
return tmpServer.setConfig(config)
|
return tmpServer.setConfig(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create - create object
|
||||||
|
func Create(config ServerConfig) *Server {
|
||||||
|
s := Server{}
|
||||||
|
s.conf = config
|
||||||
|
if s.conf.HTTPRegister != nil {
|
||||||
|
s.registerHandlers()
|
||||||
|
}
|
||||||
|
return &s
|
||||||
|
}
|
||||||
|
|
||||||
// Init checks the configuration and initializes the server
|
// Init checks the configuration and initializes the server
|
||||||
func (s *Server) Init(config ServerConfig) error {
|
func (s *Server) Init(config ServerConfig) error {
|
||||||
err := s.setConfig(config)
|
err := s.setConfig(config)
|
||||||
|
@ -98,10 +116,12 @@ func (s *Server) Init(config ServerConfig) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) setConfig(config ServerConfig) error {
|
// WriteDiskConfig - write configuration
|
||||||
s.conf = config
|
func (s *Server) WriteDiskConfig(c *ServerConfig) {
|
||||||
s.conf.DBFilePath = filepath.Join(config.WorkDir, dbFilename)
|
*c = s.conf
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) setConfig(config ServerConfig) error {
|
||||||
iface, err := net.InterfaceByName(config.InterfaceName)
|
iface, err := net.InterfaceByName(config.InterfaceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
printInterfaces()
|
printInterfaces()
|
||||||
|
@ -153,6 +173,12 @@ func (s *Server) setConfig(config ServerConfig) error {
|
||||||
dhcp4.OptionDomainNameServer: s.ipnet.IP,
|
dhcp4.OptionDomainNameServer: s.ipnet.IP,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
oldconf := s.conf
|
||||||
|
s.conf = config
|
||||||
|
s.conf.WorkDir = oldconf.WorkDir
|
||||||
|
s.conf.HTTPRegister = oldconf.HTTPRegister
|
||||||
|
s.conf.ConfigModified = oldconf.ConfigModified
|
||||||
|
s.conf.DBFilePath = filepath.Join(config.WorkDir, dbFilename)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/AdguardTeam/AdGuardHome/dhcpd"
|
||||||
"github.com/AdguardTeam/golibs/log"
|
"github.com/AdguardTeam/golibs/log"
|
||||||
"github.com/AdguardTeam/golibs/utils"
|
"github.com/AdguardTeam/golibs/utils"
|
||||||
)
|
)
|
||||||
|
@ -59,20 +60,29 @@ type clientsContainer struct {
|
||||||
idIndex map[string]*Client // IP -> client
|
idIndex map[string]*Client // IP -> client
|
||||||
ipHost map[string]*ClientHost // IP -> Hostname
|
ipHost map[string]*ClientHost // IP -> Hostname
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
|
|
||||||
|
dhcpServer *dhcpd.Server
|
||||||
|
|
||||||
|
testing bool // if TRUE, this object is used for internal tests
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init initializes clients container
|
// Init initializes clients container
|
||||||
// Note: this function must be called only once
|
// Note: this function must be called only once
|
||||||
func (clients *clientsContainer) Init(objects []clientObject) {
|
func (clients *clientsContainer) Init(objects []clientObject, dhcpServer *dhcpd.Server) {
|
||||||
if clients.list != nil {
|
if clients.list != nil {
|
||||||
log.Fatal("clients.list != nil")
|
log.Fatal("clients.list != nil")
|
||||||
}
|
}
|
||||||
clients.list = make(map[string]*Client)
|
clients.list = make(map[string]*Client)
|
||||||
clients.idIndex = make(map[string]*Client)
|
clients.idIndex = make(map[string]*Client)
|
||||||
clients.ipHost = make(map[string]*ClientHost)
|
clients.ipHost = make(map[string]*ClientHost)
|
||||||
|
clients.dhcpServer = dhcpServer
|
||||||
clients.addFromConfig(objects)
|
clients.addFromConfig(objects)
|
||||||
|
|
||||||
|
if !clients.testing {
|
||||||
go clients.periodicUpdate()
|
go clients.periodicUpdate()
|
||||||
|
|
||||||
|
clients.registerWebHandlers()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type clientObject struct {
|
type clientObject struct {
|
||||||
|
@ -190,7 +200,10 @@ func (clients *clientsContainer) Find(ip string) (Client, bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macFound := config.dhcpServer.FindMACbyIP(ipAddr)
|
if clients.dhcpServer == nil {
|
||||||
|
return Client{}, false
|
||||||
|
}
|
||||||
|
macFound := clients.dhcpServer.FindMACbyIP(ipAddr)
|
||||||
if macFound == nil {
|
if macFound == nil {
|
||||||
return Client{}, false
|
return Client{}, false
|
||||||
}
|
}
|
||||||
|
@ -533,13 +546,16 @@ func (clients *clientsContainer) addFromSystemARP() {
|
||||||
|
|
||||||
// add clients from DHCP that have non-empty Hostname property
|
// add clients from DHCP that have non-empty Hostname property
|
||||||
func (clients *clientsContainer) addFromDHCP() {
|
func (clients *clientsContainer) addFromDHCP() {
|
||||||
leases := config.dhcpServer.Leases()
|
if clients.dhcpServer == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
leases := clients.dhcpServer.Leases()
|
||||||
n := 0
|
n := 0
|
||||||
for _, l := range leases {
|
for _, l := range leases {
|
||||||
if len(l.Hostname) == 0 {
|
if len(l.Hostname) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ok, _ := config.clients.AddHost(l.IP.String(), l.Hostname, ClientSourceDHCP)
|
ok, _ := clients.AddHost(l.IP.String(), l.Hostname, ClientSourceDHCP)
|
||||||
if ok {
|
if ok {
|
||||||
n++
|
n++
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,15 +36,15 @@ type clientListJSON struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// respond with information about configured clients
|
// respond with information about configured clients
|
||||||
func handleGetClients(w http.ResponseWriter, r *http.Request) {
|
func (clients *clientsContainer) handleGetClients(w http.ResponseWriter, r *http.Request) {
|
||||||
data := clientListJSON{}
|
data := clientListJSON{}
|
||||||
|
|
||||||
config.clients.lock.Lock()
|
clients.lock.Lock()
|
||||||
for _, c := range config.clients.list {
|
for _, c := range clients.list {
|
||||||
cj := clientToJSON(c)
|
cj := clientToJSON(c)
|
||||||
data.Clients = append(data.Clients, cj)
|
data.Clients = append(data.Clients, cj)
|
||||||
}
|
}
|
||||||
for ip, ch := range config.clients.ipHost {
|
for ip, ch := range clients.ipHost {
|
||||||
cj := clientHostJSON{
|
cj := clientHostJSON{
|
||||||
IP: ip,
|
IP: ip,
|
||||||
Name: ch.Host,
|
Name: ch.Host,
|
||||||
|
@ -69,7 +69,7 @@ func handleGetClients(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
data.AutoClients = append(data.AutoClients, cj)
|
data.AutoClients = append(data.AutoClients, cj)
|
||||||
}
|
}
|
||||||
config.clients.lock.Unlock()
|
clients.lock.Unlock()
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
e := json.NewEncoder(w).Encode(data)
|
e := json.NewEncoder(w).Encode(data)
|
||||||
|
@ -139,7 +139,7 @@ func clientHostToJSON(ip string, ch ClientHost) clientHostJSONWithID {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a new client
|
// Add a new client
|
||||||
func handleAddClient(w http.ResponseWriter, r *http.Request) {
|
func (clients *clientsContainer) handleAddClient(w http.ResponseWriter, r *http.Request) {
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpError(w, http.StatusBadRequest, "failed to read request body: %s", err)
|
httpError(w, http.StatusBadRequest, "failed to read request body: %s", err)
|
||||||
|
@ -158,7 +158,7 @@ func handleAddClient(w http.ResponseWriter, r *http.Request) {
|
||||||
httpError(w, http.StatusBadRequest, "%s", err)
|
httpError(w, http.StatusBadRequest, "%s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ok, err := config.clients.Add(*c)
|
ok, err := clients.Add(*c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpError(w, http.StatusBadRequest, "%s", err)
|
httpError(w, http.StatusBadRequest, "%s", err)
|
||||||
return
|
return
|
||||||
|
@ -173,7 +173,7 @@ func handleAddClient(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove client
|
// Remove client
|
||||||
func handleDelClient(w http.ResponseWriter, r *http.Request) {
|
func (clients *clientsContainer) handleDelClient(w http.ResponseWriter, r *http.Request) {
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpError(w, http.StatusBadRequest, "failed to read request body: %s", err)
|
httpError(w, http.StatusBadRequest, "failed to read request body: %s", err)
|
||||||
|
@ -187,7 +187,7 @@ func handleDelClient(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !config.clients.Del(cj.Name) {
|
if !clients.Del(cj.Name) {
|
||||||
httpError(w, http.StatusBadRequest, "Client not found")
|
httpError(w, http.StatusBadRequest, "Client not found")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -202,7 +202,7 @@ type updateJSON struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update client's properties
|
// Update client's properties
|
||||||
func handleUpdateClient(w http.ResponseWriter, r *http.Request) {
|
func (clients *clientsContainer) handleUpdateClient(w http.ResponseWriter, r *http.Request) {
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpError(w, http.StatusBadRequest, "failed to read request body: %s", err)
|
httpError(w, http.StatusBadRequest, "failed to read request body: %s", err)
|
||||||
|
@ -226,7 +226,7 @@ func handleUpdateClient(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = config.clients.Update(dj.Name, *c)
|
err = clients.Update(dj.Name, *c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpError(w, http.StatusBadRequest, "%s", err)
|
httpError(w, http.StatusBadRequest, "%s", err)
|
||||||
return
|
return
|
||||||
|
@ -237,7 +237,7 @@ func handleUpdateClient(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the list of clients by IP address list
|
// Get the list of clients by IP address list
|
||||||
func handleFindClient(w http.ResponseWriter, r *http.Request) {
|
func (clients *clientsContainer) handleFindClient(w http.ResponseWriter, r *http.Request) {
|
||||||
q := r.URL.Query()
|
q := r.URL.Query()
|
||||||
data := []map[string]interface{}{}
|
data := []map[string]interface{}{}
|
||||||
for i := 0; ; i++ {
|
for i := 0; ; i++ {
|
||||||
|
@ -246,9 +246,9 @@ func handleFindClient(w http.ResponseWriter, r *http.Request) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
el := map[string]interface{}{}
|
el := map[string]interface{}{}
|
||||||
c, ok := config.clients.Find(ip)
|
c, ok := clients.Find(ip)
|
||||||
if !ok {
|
if !ok {
|
||||||
ch, ok := config.clients.FindAutoClient(ip)
|
ch, ok := clients.FindAutoClient(ip)
|
||||||
if !ok {
|
if !ok {
|
||||||
continue // a client with this IP isn't found
|
continue // a client with this IP isn't found
|
||||||
}
|
}
|
||||||
|
@ -277,10 +277,10 @@ func handleFindClient(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterClientsHandlers registers HTTP handlers
|
// RegisterClientsHandlers registers HTTP handlers
|
||||||
func RegisterClientsHandlers() {
|
func (clients *clientsContainer) registerWebHandlers() {
|
||||||
httpRegister("GET", "/control/clients", handleGetClients)
|
httpRegister("GET", "/control/clients", clients.handleGetClients)
|
||||||
httpRegister("POST", "/control/clients/add", handleAddClient)
|
httpRegister("POST", "/control/clients/add", clients.handleAddClient)
|
||||||
httpRegister("POST", "/control/clients/delete", handleDelClient)
|
httpRegister("POST", "/control/clients/delete", clients.handleDelClient)
|
||||||
httpRegister("POST", "/control/clients/update", handleUpdateClient)
|
httpRegister("POST", "/control/clients/update", clients.handleUpdateClient)
|
||||||
httpRegister("GET", "/control/clients/find", handleFindClient)
|
httpRegister("GET", "/control/clients/find", clients.handleFindClient)
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,8 +11,9 @@ func TestClients(t *testing.T) {
|
||||||
var e error
|
var e error
|
||||||
var b bool
|
var b bool
|
||||||
clients := clientsContainer{}
|
clients := clientsContainer{}
|
||||||
|
clients.testing = true
|
||||||
|
|
||||||
clients.Init(nil)
|
clients.Init(nil, nil)
|
||||||
|
|
||||||
// add
|
// add
|
||||||
c = Client{
|
c = Client{
|
||||||
|
@ -149,7 +150,8 @@ func TestClients(t *testing.T) {
|
||||||
func TestClientsWhois(t *testing.T) {
|
func TestClientsWhois(t *testing.T) {
|
||||||
var c Client
|
var c Client
|
||||||
clients := clientsContainer{}
|
clients := clientsContainer{}
|
||||||
clients.Init(nil)
|
clients.testing = true
|
||||||
|
clients.Init(nil, nil)
|
||||||
|
|
||||||
whois := [][]string{{"orgname", "orgname-val"}, {"country", "country-val"}}
|
whois := [][]string{{"orgname", "orgname-val"}, {"country", "country-val"}}
|
||||||
// set whois info on new client
|
// set whois info on new client
|
||||||
|
|
|
@ -67,7 +67,7 @@ type configuration struct {
|
||||||
dnsctx dnsContext
|
dnsctx dnsContext
|
||||||
dnsFilter *dnsfilter.Dnsfilter
|
dnsFilter *dnsfilter.Dnsfilter
|
||||||
dnsServer *dnsforward.Server
|
dnsServer *dnsforward.Server
|
||||||
dhcpServer dhcpd.Server
|
dhcpServer *dhcpd.Server
|
||||||
httpServer *http.Server
|
httpServer *http.Server
|
||||||
httpsServer HTTPSServer
|
httpsServer HTTPSServer
|
||||||
|
|
||||||
|
@ -325,6 +325,12 @@ func (c *configuration) write() error {
|
||||||
config.DNS.DnsfilterConf = c
|
config.DNS.DnsfilterConf = c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.dhcpServer != nil {
|
||||||
|
c := dhcpd.ServerConfig{}
|
||||||
|
config.dhcpServer.WriteDiskConfig(&c)
|
||||||
|
config.DHCP = c
|
||||||
|
}
|
||||||
|
|
||||||
configFile := config.getConfigFilename()
|
configFile := config.getConfigFilename()
|
||||||
log.Debug("Writing YAML file: %s", configFile)
|
log.Debug("Writing YAML file: %s", configFile)
|
||||||
yamlText, err := yaml.Marshal(&config)
|
yamlText, err := yaml.Marshal(&config)
|
||||||
|
|
|
@ -424,12 +424,6 @@ func registerControlHandlers() {
|
||||||
httpRegister(http.MethodGet, "/control/i18n/current_language", handleI18nCurrentLanguage)
|
httpRegister(http.MethodGet, "/control/i18n/current_language", handleI18nCurrentLanguage)
|
||||||
http.HandleFunc("/control/version.json", postInstall(optionalAuth(handleGetVersionJSON)))
|
http.HandleFunc("/control/version.json", postInstall(optionalAuth(handleGetVersionJSON)))
|
||||||
httpRegister(http.MethodPost, "/control/update", handleUpdate)
|
httpRegister(http.MethodPost, "/control/update", handleUpdate)
|
||||||
httpRegister(http.MethodGet, "/control/dhcp/status", handleDHCPStatus)
|
|
||||||
httpRegister(http.MethodGet, "/control/dhcp/interfaces", handleDHCPInterfaces)
|
|
||||||
httpRegister(http.MethodPost, "/control/dhcp/set_config", handleDHCPSetConfig)
|
|
||||||
httpRegister(http.MethodPost, "/control/dhcp/find_active_dhcp", handleDHCPFindActiveServer)
|
|
||||||
httpRegister(http.MethodPost, "/control/dhcp/add_static_lease", handleDHCPAddStaticLease)
|
|
||||||
httpRegister(http.MethodPost, "/control/dhcp/remove_static_lease", handleDHCPRemoveStaticLease)
|
|
||||||
|
|
||||||
httpRegister(http.MethodGet, "/control/access/list", handleAccessList)
|
httpRegister(http.MethodGet, "/control/access/list", handleAccessList)
|
||||||
httpRegister(http.MethodPost, "/control/access/set", handleAccessSet)
|
httpRegister(http.MethodPost, "/control/access/set", handleAccessSet)
|
||||||
|
@ -437,7 +431,6 @@ func registerControlHandlers() {
|
||||||
|
|
||||||
RegisterFilteringHandlers()
|
RegisterFilteringHandlers()
|
||||||
RegisterTLSHandlers()
|
RegisterTLSHandlers()
|
||||||
RegisterClientsHandlers()
|
|
||||||
RegisterBlockedServicesHandlers()
|
RegisterBlockedServicesHandlers()
|
||||||
RegisterAuthHandlers()
|
RegisterAuthHandlers()
|
||||||
|
|
||||||
|
|
417
home/dhcp.go
417
home/dhcp.go
|
@ -1,426 +1,9 @@
|
||||||
package home
|
package home
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"os/exec"
|
|
||||||
"runtime"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/AdguardTeam/AdGuardHome/dhcpd"
|
|
||||||
"github.com/AdguardTeam/golibs/file"
|
|
||||||
"github.com/AdguardTeam/golibs/log"
|
|
||||||
"github.com/joomcode/errorx"
|
"github.com/joomcode/errorx"
|
||||||
)
|
)
|
||||||
|
|
||||||
// []dhcpd.Lease -> JSON
|
|
||||||
func convertLeases(inputLeases []dhcpd.Lease, includeExpires bool) []map[string]string {
|
|
||||||
leases := []map[string]string{}
|
|
||||||
for _, l := range inputLeases {
|
|
||||||
lease := map[string]string{
|
|
||||||
"mac": l.HWAddr.String(),
|
|
||||||
"ip": l.IP.String(),
|
|
||||||
"hostname": l.Hostname,
|
|
||||||
}
|
|
||||||
|
|
||||||
if includeExpires {
|
|
||||||
lease["expires"] = l.Expiry.Format(time.RFC3339)
|
|
||||||
}
|
|
||||||
|
|
||||||
leases = append(leases, lease)
|
|
||||||
}
|
|
||||||
return leases
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleDHCPStatus(w http.ResponseWriter, r *http.Request) {
|
|
||||||
leases := convertLeases(config.dhcpServer.Leases(), true)
|
|
||||||
staticLeases := convertLeases(config.dhcpServer.StaticLeases(), false)
|
|
||||||
status := map[string]interface{}{
|
|
||||||
"config": config.DHCP,
|
|
||||||
"leases": leases,
|
|
||||||
"static_leases": staticLeases,
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
err := json.NewEncoder(w).Encode(status)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, http.StatusInternalServerError, "Unable to marshal DHCP status json: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type leaseJSON struct {
|
|
||||||
HWAddr string `json:"mac"`
|
|
||||||
IP string `json:"ip"`
|
|
||||||
Hostname string `json:"hostname"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type dhcpServerConfigJSON struct {
|
|
||||||
dhcpd.ServerConfig `json:",inline"`
|
|
||||||
StaticLeases []leaseJSON `json:"static_leases"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) {
|
|
||||||
newconfig := dhcpServerConfigJSON{}
|
|
||||||
err := json.NewDecoder(r.Body).Decode(&newconfig)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, http.StatusBadRequest, "Failed to parse new DHCP config json: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
newconfig.ServerConfig.WorkDir = config.ourWorkingDir
|
|
||||||
err = config.dhcpServer.CheckConfig(newconfig.ServerConfig)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, http.StatusBadRequest, "Invalid DHCP configuration: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = config.dhcpServer.Stop()
|
|
||||||
if err != nil {
|
|
||||||
log.Error("failed to stop the DHCP server: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = config.dhcpServer.Init(newconfig.ServerConfig)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, http.StatusBadRequest, "Invalid DHCP configuration: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if newconfig.Enabled {
|
|
||||||
|
|
||||||
staticIP, err := hasStaticIP(newconfig.InterfaceName)
|
|
||||||
if !staticIP && err == nil {
|
|
||||||
err = setStaticIP(newconfig.InterfaceName)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, http.StatusInternalServerError, "Failed to configure static IP: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = config.dhcpServer.Start()
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, http.StatusBadRequest, "Failed to start DHCP server: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
config.DHCP = newconfig.ServerConfig
|
|
||||||
httpUpdateConfigReloadDNSReturnOK(w, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleDHCPInterfaces(w http.ResponseWriter, r *http.Request) {
|
|
||||||
response := map[string]interface{}{}
|
|
||||||
|
|
||||||
ifaces, err := getValidNetInterfaces()
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, http.StatusInternalServerError, "Couldn't get interfaces: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, iface := range ifaces {
|
|
||||||
if iface.Flags&net.FlagLoopback != 0 {
|
|
||||||
// it's a loopback, skip it
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if iface.Flags&net.FlagBroadcast == 0 {
|
|
||||||
// this interface doesn't support broadcast, skip it
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
addrs, err := iface.Addrs()
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, http.StatusInternalServerError, "Failed to get addresses for interface %s: %s", iface.Name, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
jsonIface := netInterface{
|
|
||||||
Name: iface.Name,
|
|
||||||
MTU: iface.MTU,
|
|
||||||
HardwareAddr: iface.HardwareAddr.String(),
|
|
||||||
}
|
|
||||||
|
|
||||||
if iface.Flags != 0 {
|
|
||||||
jsonIface.Flags = iface.Flags.String()
|
|
||||||
}
|
|
||||||
// we don't want link-local addresses in json, so skip them
|
|
||||||
for _, addr := range addrs {
|
|
||||||
ipnet, ok := addr.(*net.IPNet)
|
|
||||||
if !ok {
|
|
||||||
// not an IPNet, should not happen
|
|
||||||
httpError(w, http.StatusInternalServerError, "SHOULD NOT HAPPEN: got iface.Addrs() element %s that is not net.IPNet, it is %T", addr, addr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// ignore link-local
|
|
||||||
if ipnet.IP.IsLinkLocalUnicast() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
jsonIface.Addresses = append(jsonIface.Addresses, ipnet.IP.String())
|
|
||||||
}
|
|
||||||
if len(jsonIface.Addresses) != 0 {
|
|
||||||
response[iface.Name] = jsonIface
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
err = json.NewEncoder(w).Encode(response)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, http.StatusInternalServerError, "Failed to marshal json with available interfaces: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Perform the following tasks:
|
|
||||||
// . Search for another DHCP server running
|
|
||||||
// . Check if a static IP is configured for the network interface
|
|
||||||
// Respond with results
|
|
||||||
func handleDHCPFindActiveServer(w http.ResponseWriter, r *http.Request) {
|
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
|
||||||
if err != nil {
|
|
||||||
errorText := fmt.Sprintf("failed to read request body: %s", err)
|
|
||||||
log.Error(errorText)
|
|
||||||
http.Error(w, errorText, http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
interfaceName := strings.TrimSpace(string(body))
|
|
||||||
if interfaceName == "" {
|
|
||||||
errorText := fmt.Sprintf("empty interface name specified")
|
|
||||||
log.Error(errorText)
|
|
||||||
http.Error(w, errorText, http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
found, err := dhcpd.CheckIfOtherDHCPServersPresent(interfaceName)
|
|
||||||
|
|
||||||
othSrv := map[string]interface{}{}
|
|
||||||
foundVal := "no"
|
|
||||||
if found {
|
|
||||||
foundVal = "yes"
|
|
||||||
} else if err != nil {
|
|
||||||
foundVal = "error"
|
|
||||||
othSrv["error"] = err.Error()
|
|
||||||
}
|
|
||||||
othSrv["found"] = foundVal
|
|
||||||
|
|
||||||
staticIP := map[string]interface{}{}
|
|
||||||
isStaticIP, err := hasStaticIP(interfaceName)
|
|
||||||
staticIPStatus := "yes"
|
|
||||||
if err != nil {
|
|
||||||
staticIPStatus = "error"
|
|
||||||
staticIP["error"] = err.Error()
|
|
||||||
} else if !isStaticIP {
|
|
||||||
staticIPStatus = "no"
|
|
||||||
staticIP["ip"] = getFullIP(interfaceName)
|
|
||||||
}
|
|
||||||
staticIP["static"] = staticIPStatus
|
|
||||||
|
|
||||||
result := map[string]interface{}{}
|
|
||||||
result["other_server"] = othSrv
|
|
||||||
result["static_ip"] = staticIP
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
err = json.NewEncoder(w).Encode(result)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, http.StatusInternalServerError, "Failed to marshal DHCP found json: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if network interface has a static IP configured
|
|
||||||
func hasStaticIP(ifaceName string) (bool, error) {
|
|
||||||
if runtime.GOOS == "windows" {
|
|
||||||
return false, errors.New("Can't detect static IP: not supported on Windows")
|
|
||||||
}
|
|
||||||
|
|
||||||
body, err := ioutil.ReadFile("/etc/dhcpcd.conf")
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
lines := strings.Split(string(body), "\n")
|
|
||||||
nameLine := fmt.Sprintf("interface %s", ifaceName)
|
|
||||||
withinInterfaceCtx := false
|
|
||||||
|
|
||||||
for _, line := range lines {
|
|
||||||
line = strings.TrimSpace(line)
|
|
||||||
|
|
||||||
if withinInterfaceCtx && len(line) == 0 {
|
|
||||||
// an empty line resets our state
|
|
||||||
withinInterfaceCtx = false
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(line) == 0 || line[0] == '#' {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
line = strings.TrimSpace(line)
|
|
||||||
|
|
||||||
if !withinInterfaceCtx {
|
|
||||||
if line == nameLine {
|
|
||||||
// we found our interface
|
|
||||||
withinInterfaceCtx = true
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
if strings.HasPrefix(line, "interface ") {
|
|
||||||
// we found another interface - reset our state
|
|
||||||
withinInterfaceCtx = false
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if strings.HasPrefix(line, "static ip_address=") {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get IP address with netmask
|
|
||||||
func getFullIP(ifaceName string) string {
|
|
||||||
cmd := exec.Command("ip", "-oneline", "-family", "inet", "address", "show", ifaceName)
|
|
||||||
log.Tracef("executing %s %v", cmd.Path, cmd.Args)
|
|
||||||
d, err := cmd.Output()
|
|
||||||
if err != nil || cmd.ProcessState.ExitCode() != 0 {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
fields := strings.Fields(string(d))
|
|
||||||
if len(fields) < 4 {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
_, _, err = net.ParseCIDR(fields[3])
|
|
||||||
if err != nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return fields[3]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get gateway IP address
|
|
||||||
func getGatewayIP(ifaceName string) string {
|
|
||||||
cmd := exec.Command("ip", "route", "show", "dev", ifaceName)
|
|
||||||
log.Tracef("executing %s %v", cmd.Path, cmd.Args)
|
|
||||||
d, err := cmd.Output()
|
|
||||||
if err != nil || cmd.ProcessState.ExitCode() != 0 {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
fields := strings.Fields(string(d))
|
|
||||||
if len(fields) < 3 || fields[0] != "default" {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
ip := net.ParseIP(fields[2])
|
|
||||||
if ip == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return fields[2]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set a static IP for network interface
|
|
||||||
func setStaticIP(ifaceName string) error {
|
|
||||||
ip := getFullIP(ifaceName)
|
|
||||||
if len(ip) == 0 {
|
|
||||||
return errors.New("Can't get IP address")
|
|
||||||
}
|
|
||||||
|
|
||||||
body, err := ioutil.ReadFile("/etc/dhcpcd.conf")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
ip4, _, err := net.ParseCIDR(ip)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
add := fmt.Sprintf("\ninterface %s\nstatic ip_address=%s\n",
|
|
||||||
ifaceName, ip)
|
|
||||||
body = append(body, []byte(add)...)
|
|
||||||
|
|
||||||
gatewayIP := getGatewayIP(ifaceName)
|
|
||||||
if len(gatewayIP) != 0 {
|
|
||||||
add = fmt.Sprintf("static routers=%s\n",
|
|
||||||
gatewayIP)
|
|
||||||
body = append(body, []byte(add)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
add = fmt.Sprintf("static domain_name_servers=%s\n\n",
|
|
||||||
ip4)
|
|
||||||
body = append(body, []byte(add)...)
|
|
||||||
|
|
||||||
err = file.SafeWrite("/etc/dhcpcd.conf", body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleDHCPAddStaticLease(w http.ResponseWriter, r *http.Request) {
|
|
||||||
|
|
||||||
lj := leaseJSON{}
|
|
||||||
err := json.NewDecoder(r.Body).Decode(&lj)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, http.StatusBadRequest, "json.Decode: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ip := parseIPv4(lj.IP)
|
|
||||||
if ip == nil {
|
|
||||||
httpError(w, http.StatusBadRequest, "invalid IP")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
mac, _ := net.ParseMAC(lj.HWAddr)
|
|
||||||
|
|
||||||
lease := dhcpd.Lease{
|
|
||||||
IP: ip,
|
|
||||||
HWAddr: mac,
|
|
||||||
Hostname: lj.Hostname,
|
|
||||||
}
|
|
||||||
err = config.dhcpServer.AddStaticLease(lease)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, http.StatusBadRequest, "%s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
returnOK(w)
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleDHCPRemoveStaticLease(w http.ResponseWriter, r *http.Request) {
|
|
||||||
|
|
||||||
lj := leaseJSON{}
|
|
||||||
err := json.NewDecoder(r.Body).Decode(&lj)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, http.StatusBadRequest, "json.Decode: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ip := parseIPv4(lj.IP)
|
|
||||||
if ip == nil {
|
|
||||||
httpError(w, http.StatusBadRequest, "invalid IP")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
mac, _ := net.ParseMAC(lj.HWAddr)
|
|
||||||
|
|
||||||
lease := dhcpd.Lease{
|
|
||||||
IP: ip,
|
|
||||||
HWAddr: mac,
|
|
||||||
Hostname: lj.Hostname,
|
|
||||||
}
|
|
||||||
err = config.dhcpServer.RemoveStaticLease(lease)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, http.StatusBadRequest, "%s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
returnOK(w)
|
|
||||||
}
|
|
||||||
|
|
||||||
func startDHCPServer() error {
|
func startDHCPServer() error {
|
||||||
if !config.DHCP.Enabled {
|
if !config.DHCP.Enabled {
|
||||||
// not enabled, don't do anything
|
// not enabled, don't do anything
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package home
|
package home
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -338,21 +337,6 @@ func _Func() string {
|
||||||
return path.Base(f.Name())
|
return path.Base(f.Name())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse input string and return IPv4 address
|
|
||||||
func parseIPv4(s string) net.IP {
|
|
||||||
ip := net.ParseIP(s)
|
|
||||||
if ip == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
v4InV6Prefix := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}
|
|
||||||
if !bytes.Equal(ip[0:12], v4InV6Prefix) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return ip.To4()
|
|
||||||
}
|
|
||||||
|
|
||||||
// SplitNext - split string by a byte and return the first chunk
|
// SplitNext - split string by a byte and return the first chunk
|
||||||
// Whitespace is trimmed
|
// Whitespace is trimmed
|
||||||
func SplitNext(str *string, splitBy byte) string {
|
func SplitNext(str *string, splitBy byte) string {
|
||||||
|
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/AdguardTeam/AdGuardHome/dhcpd"
|
||||||
"github.com/AdguardTeam/golibs/log"
|
"github.com/AdguardTeam/golibs/log"
|
||||||
"github.com/NYTimes/gziphandler"
|
"github.com/NYTimes/gziphandler"
|
||||||
"github.com/gobuffalo/packr"
|
"github.com/gobuffalo/packr"
|
||||||
|
@ -118,7 +119,11 @@ func run(args options) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
config.clients.Init(config.Clients)
|
config.DHCP.WorkDir = config.ourWorkingDir
|
||||||
|
config.DHCP.HTTPRegister = httpRegister
|
||||||
|
config.DHCP.ConfigModified = onConfigModified
|
||||||
|
config.dhcpServer = dhcpd.Create(config.DHCP)
|
||||||
|
config.clients.Init(config.Clients, config.dhcpServer)
|
||||||
config.Clients = nil
|
config.Clients = nil
|
||||||
|
|
||||||
if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") &&
|
if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") &&
|
||||||
|
|
|
@ -451,6 +451,16 @@ paths:
|
||||||
200:
|
200:
|
||||||
description: OK
|
description: OK
|
||||||
|
|
||||||
|
/dhcp/reset:
|
||||||
|
post:
|
||||||
|
tags:
|
||||||
|
- dhcp
|
||||||
|
operationId: dhcpReset
|
||||||
|
summary: "Reset DHCP configuration"
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: OK
|
||||||
|
|
||||||
# --------------------------------------------------
|
# --------------------------------------------------
|
||||||
# Filtering status methods
|
# Filtering status methods
|
||||||
# --------------------------------------------------
|
# --------------------------------------------------
|
||||||
|
|
Loading…
Reference in New Issue