Skip to content

Dns

airvpn.web.services.dns

DnsManager

Bases: ClientService

Manages the authenticated user's custom DNS configuration.

Wraps the AJAX endpoints behind https://airvpn.org/dns/ to view available DNS lists and toggle or edit the user's custom DNS selection.

Attributes:

Name Type Description
lists list[Dns]

All DNS lists currently available.

current Current

The user's currently active DNS configuration.

device str | None

The device this configuration applies to, if any.

add_answer(host: str, type: AnswerType = AnswerType.EXACT, action: ActionType = ActionType.DENY, records: list[Record] | None = None) -> Answer

Create a new answer rule and add it to the current configuration.

Parameters:

Name Type Description Default
host str

The hostname or pattern the rule matches against.

required
type AnswerType

How host is matched (e.g. exact, wildcard). Defaults to AnswerType.EXACT.

EXACT
action ActionType

The action to take when the rule matches (e.g. allow, deny). Defaults to ActionType.DENY.

DENY
records list[Record] | None

Initial list of Record instances to attach to the answer. Defaults to an empty list.

None

Returns:

Type Description
Answer

The newly created Answer.

add_list(dns: DnsList | list[DnsList] | list[str] | str)

Add one or more DNS lists to the current configuration.

Parameters:

Name Type Description Default
dns DnsList | list[DnsList] | list[str] | str

A Dns instance, DNS list code, or a list of either, to add to the current selection.

required

add_record(answer: Answer, type: RecordType, value: str) -> Record

Create a record, attach it to an answer, and save the change.

Parameters:

Name Type Description Default
answer Answer

The Answer instance to attach the new record to.

required
type RecordType

The RecordType of the record to create (e.g. A, AAAA, CNAME).

required
value str

The record's value (e.g. an IP address or hostname).

required

Returns:

Type Description
Record

The Answer the record was added to.

edit_request(name, value, **kwargs)

Send a generic edit_<name> action to the endpoint.

Convenience wrapper around request for the common pattern of editing a single field by name.

Parameters:

Name Type Description Default
name

Name of the field to edit; sent as the edit_{name} action.

required
value

New value to set for the field.

required
**kwargs

Additional form fields to send along with the request (e.g. an id or port identifying the target record).

{}

Returns:

Type Description

The parsed JSON response from the server.

Raises:

Type Description
APIError

If the response is a dict containing a non-None "error" field.

from_dns(session: WebSession, device: str) classmethod

Create a DnsManager scoped to a specific device.

Parameters:

Name Type Description Default
session WebSession

The WebSession to use for requests.

required
device str

The device identifier to scope the configuration to.

required

Returns:

Type Description

A new DnsManager instance for the given device.

get_list(dns: DnsList | str)

Resolve a DNS list to a Dns instance.

Parameters:

Name Type Description Default
dns DnsList | str

Dns instance or DNS list code to resolve.

required

Returns:

Type Description

The matching Dns instance.

Raises:

Type Description
ValidationError

If dns is a string code that doesn't match any known DNS list.

remove_answer(answer: Answer)

Remove an answer from the current DNS configuration and save the change.

Parameters:

Name Type Description Default
answer Answer

The Answer instance to remove.

required

Raises:

Type Description
ValueError

If answer is not present in self.current.answers.

remove_list(dns: DnsList)

Remove a DNS list from the current configuration and save the change.

Parameters:

Name Type Description Default
dns DnsList

The DNS list code (str) to remove from the current selection.

required

Raises:

Type Description
ValueError

If dns is not present in self.current.lists.

remove_record(answer: Answer, record: Record)

Remove a record from an answer's record list and save the change.

Parameters:

Name Type Description Default
answer Answer

The Answer instance containing the record.

required
record Record

The Record instance to remove.

required

Raises:

Type Description
ValueError

If record is not present in answer.records.

request(action: str, is_act: bool = False, **kwargs)

Send an AJAX action request to the endpoint.

Automatically attaches the CSRF token (fetching it first if not already known) and requests an AJAX-rendered response.

Parameters:

Name Type Description Default
action str

Name of the action to perform.

required
is_act bool

If the action param is act instead.

False
**kwargs

Additional form fields to send along with the request.

{}

Returns:

Type Description

The parsed JSON response from the server.

Raises:

Type Description
APIError

If the response is a dict containing a non-None "error" field.

save()

Persist the current DNS configuration to the server.

toggle()

Toggle custom DNS on or off and save the change.

update()

Refresh lists and current from the server.

Fetches the DNS page and parses the embedded JSON data to repopulate lists, the internal DNS lookup map, current, and device.