Dawnline 0.20.0
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / Backend (Go: vet, tests, a real ubusd and uhttpd) (push) Blocked by required conditions
CI / Package files (push) Blocked by required conditions

- **Backend rewritten in Go.** One daemon, `dawnlined`, replaces the whole ucode backend: the rpcd plugin, the agent, the CLI and the updater. The package no longer depends on ucode.
- **Per-architecture builds.** The release carries the backend for arm64, ARMv7, mips, mipsle and amd64; the router fetches its own after the package is installed and checks its sha256. Dawnline does not run on other architectures (older ARM, 32-bit x86, RISC-V).
- **Faster on the router.** A firewall sync takes 0.06 s instead of 0.9 s; a full apply takes about 300 ms. The portal is served by the daemon itself, without a CGI process per request.
- **Live pages.** The admin and the portal follow a live stream from the router instead of re-fetching the whole state every 30 seconds.
- **Admin redesigned in the portal's style.** Three sections (Overview · Log · Settings); a server is a page of its own with People, Places and Settings; a device opens as a dialog over its person.
- **Changes wait for «Apply».** Every setting, and a person's access, is staged in one dock before it reaches the router.
- **Configuration rollout.** After a change that makes devices fetch their configuration again, the admin shows how many have fetched it and who has not. Every setting shows beforehand (⟳N) how many devices it will touch.
- **Waiting for a decision.** Overview and People put first the people who need one (access ended, limit reached, a device that does not connect), each with its one action a tap away.
- **Bulk actions on people.** «Select» (or a long press on a card) picks several people to extend by a month, pause or resume at once.
- **Undo.** An action done from a card or a request is undone from its toast.
- **Update dialogs redesigned.** An update is one view from its question to its result; Dawnline's own update runs in it like every other, and the page reloads itself once the new version answers. Other versions are a list, each with its notes and Install on a page of its own.
- **Site to site: networks behind a router.** A router connected as a device can have networks behind it (up to eight, /16 to /30): the server routes them and applies the router's access to them. A network that shares addresses with a tunnel, the router's own networks or another site is refused by name.
- **Routers from the portal.** People can add a router and name its networks on their own page; the admin accepts or declines with a reason, or lets them through without asking.
- **Check access and Health know the networks behind routers.**
- **Usage limits reworked.** A monthly limit no longer follows the calendar. A person's month starts with their first traffic and ends a month later; the next one starts only when they connect again, so time away uses up none of it. Before, everyone's limit reset on the 1st.
- Extra data lasts until that person's month ends; «Reset usage» starts the month anew.
- A limit is for a month or for the whole term; the admin and the portal show when it resets.
- Charts that said «this month» show the last 30 days.
- After the update nobody loses what was counted: the month that was running becomes the person's first and ends on the 1st.
- **Access by time of day removed.** The Hours limit is gone from the admin, the portal and the backend; saved hours are dropped.
- **Expiry simplified.** An end date always stops access, with a heads-up three days before; the per-person choice is gone.
- **No false disconnects from sleeping phones.** A phone or a tablet stays online through ten minutes of silence.
- **Mobile Safari fixes.** Pressing a control no longer sends the page to its top; tab tracks that do not fit scroll; switches animate after a press.
This commit is contained in:
Aemeath committed 2026-11-13 09:43:50 +03:00
1 parent f8130e39a8
commit ac90575b0b
788 files changed
+139244 -41720

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
# Test fixture: a small installation (one server, two people, one device).
# migrate.test.uc expects bob's random code, the server's stored MTU 1380,
# the old portal title and uhttpd's error_page: the one-time steps remove them.
config settings 'settings'
option portal_enabled '1'
option portal_port '8443'
option portal_host 'vpn.example.com'
option portal_title 'My VPN'
option log_connections '0'
option update_interval '7d'
list update_targets 'app'
config server 'main'
option name 'Main'
option ifname 'awgmain'
option port '51820'
option subnet '10.9.0.0/29'
option private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='
option public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='
option mtu '1380'
option jc '4'
option jmin '64'
option jmax '128'
option s4 '12'
config person 'alice'
option server 'main'
option name 'Alice'
option code 'abcdef-ghijkl-mnopqr'
option access 'both'
config person 'bob'
option server 'main'
option name 'Bob'
option code 'XyZ12345'
option quota_gb '2'
config device 'alice_phone'
option person 'alice'
option name 'Phone'
option kind 'phone'
option address '10.9.0.2'
option public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='
+211
View File
@@ -0,0 +1,211 @@
{
"canon": {
"small": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\toption portal_port '8443'\n\toption portal_host 'vpn.example.com'\n\toption portal_title 'My VPN'\n\toption log_connections '0'\n\toption update_interval '7d'\n\tlist update_targets 'app'\n\nconfig server 'main'\n\toption name 'Main'\n\toption ifname 'awgmain'\n\toption port '51820'\n\toption subnet '10.9.0.0/29'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu '1380'\n\toption jc '4'\n\toption jmin '64'\n\toption jmax '128'\n\toption s4 '12'\n\nconfig person 'alice'\n\toption server 'main'\n\toption name 'Alice'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption access 'both'\n\nconfig person 'bob'\n\toption server 'main'\n\toption name 'Bob'\n\toption code 'XyZ12345'\n\toption quota_gb '2'\n\nconfig device 'alice_phone'\n\toption person 'alice'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.9.0.2'\n\toption public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='\n\n",
"rich": "\nconfig settings 'settings'\n\toption portal_enabled 'yes'\n\toption portal_port '99999'\n\toption portal_host 'vpn.example.com'\n\tlist portal_alt 'alt.example.com'\n\tlist portal_alt ''\n\tlist portal_alt '203.0.113.7'\n\toption portal_redirect 'https://example.com/'\n\toption default_device_limit '5'\n\toption hostnames 'off'\n\toption domain 'home.arpa'\n\toption geo_enabled 'TRUE'\n\toption geo_provider 'nowhere'\n\toption log_level 'debug'\n\toption watchdog '0'\n\toption log_keep_days '14'\n\toption log_max '50'\n\toption log_persist '10min'\n\tlist log_category 'connections'\n\tlist log_category 'problems'\n\toption usage_keep_days ' 90 '\n\toption usage_keep_months 'many'\n\toption home_dns_fallback 'none'\n\toption update_check '0'\n\toption update_interval '12h'\n\toption update_auto 'on'\n\toption update_mode 'immediate'\n\toption update_time '25:00'\n\tlist update_targets 'app'\n\tlist update_targets 'firmware'\n\tlist update_targets 'amneziawg'\n\toption update_keep '1'\n\nconfig server 'home'\n\toption name 'Home'\n\toption ifname 'dawn0'\n\toption port '4500'\n\toption subnet '10.77.0.0/24'\n\toption endpoint 'vpn.example.com'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu 'auto'\n\tlist dns '10.77.0.1'\n\tlist dns '1.1.1.1'\n\toption keepalive '0'\n\toption preset 'quiet'\n\toption auto_wan '0'\n\toption wan_device 'pppoe-wan'\n\tlist lan_device 'br-lan'\n\tlist lan_device 'br-guest'\n\toption lan_like '0'\n\toption mesh 'circles'\n\toption jc '5'\n\toption jmin '11'\n\toption jmax '42'\n\toption s1 '12'\n\toption h1 '1'\n\toption i1 '<r 2><b 0x01000001000000000000>'\n\toption header_protection_key 'yvVKA41To0000000000000000000IUdcl4ZiyFE='\n\toption rekey_after_time '100-120'\n\toption random_trailers 'on'\n\nconfig server 'cabin'\n\toption port 'x'\n\toption dns '9.9.9.9, 149.112.112.112'\n\toption device_mesh '0'\n\toption enabled '0'\n\toption mesh 'everyone'\n\nconfig person 'anna'\n\toption server 'home'\n\toption name 'Anna'\n\toption access 'both'\n\toption device_limit '2'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption code_set '1790000000'\n\toption expires '2026-12-31'\n\toption quota_gb '1.5'\n\toption quota_period 'total'\n\toption dns_mode 'home'\n\toption bonus_gb '20'\n\toption bonus_until '1795000000'\n\tlist service 'nas'\n\tlist home_net 'br-lan'\n\toption circle 'family'\n\toption note 'A note, with a comma'\n\toption created '1780000000'\n\nconfig person 'boris'\n\toption server 'cabin'\n\toption access 'everything'\n\toption paused '1'\n\toption quota_gb 'lots'\n\toption bonus_month '2026-12'\n\toption service 'nas printer'\n\nconfig person 'nobody'\n\nconfig device 'd_1'\n\toption person 'anna'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.77.0.2'\n\toption public_key 'u4pL0000000000000000000000000000DyRrogDM='\n\toption preshared_key 'Yc0000000000000000000000000000bRABGUovbrNy4='\n\toption created '1780000100'\n\toption created_by 'portal'\n\toption mtu '1280'\n\toption keepalive '0'\n\toption lan_bypass '1'\n\nconfig device 'd_2'\n\toption person 'anna'\n\toption kind 'toaster'\n\toption disabled 'yes'\n\toption keepalive 'often'\n\nconfig device 'd_3'\n\toption person 'gone'\n\toption name 'Orphan'\n\toption kind 'router'\n\toption keepalive '60'\n\nconfig circle 'family'\n\toption server 'home'\n\toption name 'Family'\n\toption color '11'\n\nconfig service 'nas'\n\toption name 'NAS'\n\toption address '192.168.1.10'\n\tlist port '445'\n\tlist port '8080-8090'\n\nconfig service 'printer'\n\toption address '192.168.1.20'\n\n"
},
"steps": [
{
"op": [
"set",
"settings",
"portal_port",
"9443"
],
"ok": true,
"file": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\toption portal_port '9443'\n\toption portal_host 'vpn.example.com'\n\toption portal_title 'My VPN'\n\toption log_connections '0'\n\toption update_interval '7d'\n\tlist update_targets 'app'\n\nconfig server 'main'\n\toption name 'Main'\n\toption ifname 'awgmain'\n\toption port '51820'\n\toption subnet '10.9.0.0/29'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu '1380'\n\toption jc '4'\n\toption jmin '64'\n\toption jmax '128'\n\toption s4 '12'\n\nconfig person 'alice'\n\toption server 'main'\n\toption name 'Alice'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption access 'both'\n\nconfig person 'bob'\n\toption server 'main'\n\toption name 'Bob'\n\toption code 'XyZ12345'\n\toption quota_gb '2'\n\nconfig device 'alice_phone'\n\toption person 'alice'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.9.0.2'\n\toption public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='\n\n"
},
{
"op": [
"set",
"settings",
"brand_new",
"it's a 'quoted' value"
],
"ok": true,
"file": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\toption portal_port '9443'\n\toption portal_host 'vpn.example.com'\n\toption portal_title 'My VPN'\n\toption log_connections '0'\n\toption update_interval '7d'\n\tlist update_targets 'app'\n\toption brand_new 'it'\\''s a '\\''quoted'\\'' value'\n\nconfig server 'main'\n\toption name 'Main'\n\toption ifname 'awgmain'\n\toption port '51820'\n\toption subnet '10.9.0.0/29'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu '1380'\n\toption jc '4'\n\toption jmin '64'\n\toption jmax '128'\n\toption s4 '12'\n\nconfig person 'alice'\n\toption server 'main'\n\toption name 'Alice'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption access 'both'\n\nconfig person 'bob'\n\toption server 'main'\n\toption name 'Bob'\n\toption code 'XyZ12345'\n\toption quota_gb '2'\n\nconfig device 'alice_phone'\n\toption person 'alice'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.9.0.2'\n\toption public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='\n\n"
},
{
"op": [
"set",
"settings",
"a_list",
[
"one",
"two words",
""
]
],
"ok": true,
"file": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\toption portal_port '9443'\n\toption portal_host 'vpn.example.com'\n\toption portal_title 'My VPN'\n\toption log_connections '0'\n\toption update_interval '7d'\n\tlist update_targets 'app'\n\toption brand_new 'it'\\''s a '\\''quoted'\\'' value'\n\tlist a_list 'one'\n\tlist a_list 'two words'\n\tlist a_list ''\n\nconfig server 'main'\n\toption name 'Main'\n\toption ifname 'awgmain'\n\toption port '51820'\n\toption subnet '10.9.0.0/29'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu '1380'\n\toption jc '4'\n\toption jmin '64'\n\toption jmax '128'\n\toption s4 '12'\n\nconfig person 'alice'\n\toption server 'main'\n\toption name 'Alice'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption access 'both'\n\nconfig person 'bob'\n\toption server 'main'\n\toption name 'Bob'\n\toption code 'XyZ12345'\n\toption quota_gb '2'\n\nconfig device 'alice_phone'\n\toption person 'alice'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.9.0.2'\n\toption public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='\n\n"
},
{
"op": [
"set",
"settings",
"brand_new",
"changed in place"
],
"ok": true,
"file": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\toption portal_port '9443'\n\toption portal_host 'vpn.example.com'\n\toption portal_title 'My VPN'\n\toption log_connections '0'\n\toption update_interval '7d'\n\tlist update_targets 'app'\n\toption brand_new 'changed in place'\n\tlist a_list 'one'\n\tlist a_list 'two words'\n\tlist a_list ''\n\nconfig server 'main'\n\toption name 'Main'\n\toption ifname 'awgmain'\n\toption port '51820'\n\toption subnet '10.9.0.0/29'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu '1380'\n\toption jc '4'\n\toption jmin '64'\n\toption jmax '128'\n\toption s4 '12'\n\nconfig person 'alice'\n\toption server 'main'\n\toption name 'Alice'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption access 'both'\n\nconfig person 'bob'\n\toption server 'main'\n\toption name 'Bob'\n\toption code 'XyZ12345'\n\toption quota_gb '2'\n\nconfig device 'alice_phone'\n\toption person 'alice'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.9.0.2'\n\toption public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='\n\n"
},
{
"op": [
"set",
"settings",
"a_list",
[
"only"
]
],
"ok": true,
"file": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\toption portal_port '9443'\n\toption portal_host 'vpn.example.com'\n\toption portal_title 'My VPN'\n\toption log_connections '0'\n\toption update_interval '7d'\n\tlist update_targets 'app'\n\toption brand_new 'changed in place'\n\tlist a_list 'only'\n\nconfig server 'main'\n\toption name 'Main'\n\toption ifname 'awgmain'\n\toption port '51820'\n\toption subnet '10.9.0.0/29'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu '1380'\n\toption jc '4'\n\toption jmin '64'\n\toption jmax '128'\n\toption s4 '12'\n\nconfig person 'alice'\n\toption server 'main'\n\toption name 'Alice'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption access 'both'\n\nconfig person 'bob'\n\toption server 'main'\n\toption name 'Bob'\n\toption code 'XyZ12345'\n\toption quota_gb '2'\n\nconfig device 'alice_phone'\n\toption person 'alice'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.9.0.2'\n\toption public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='\n\n"
},
{
"op": [
"set",
"settings",
"a_list",
"now an option"
],
"ok": true,
"file": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\toption portal_port '9443'\n\toption portal_host 'vpn.example.com'\n\toption portal_title 'My VPN'\n\toption log_connections '0'\n\toption update_interval '7d'\n\tlist update_targets 'app'\n\toption brand_new 'changed in place'\n\toption a_list 'now an option'\n\nconfig server 'main'\n\toption name 'Main'\n\toption ifname 'awgmain'\n\toption port '51820'\n\toption subnet '10.9.0.0/29'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu '1380'\n\toption jc '4'\n\toption jmin '64'\n\toption jmax '128'\n\toption s4 '12'\n\nconfig person 'alice'\n\toption server 'main'\n\toption name 'Alice'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption access 'both'\n\nconfig person 'bob'\n\toption server 'main'\n\toption name 'Bob'\n\toption code 'XyZ12345'\n\toption quota_gb '2'\n\nconfig device 'alice_phone'\n\toption person 'alice'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.9.0.2'\n\toption public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='\n\n"
},
{
"op": [
"set",
"settings",
"portal_port",
[
"now",
"a list"
]
],
"ok": true,
"file": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\tlist portal_port 'now'\n\tlist portal_port 'a list'\n\toption portal_host 'vpn.example.com'\n\toption portal_title 'My VPN'\n\toption log_connections '0'\n\toption update_interval '7d'\n\tlist update_targets 'app'\n\toption brand_new 'changed in place'\n\toption a_list 'now an option'\n\nconfig server 'main'\n\toption name 'Main'\n\toption ifname 'awgmain'\n\toption port '51820'\n\toption subnet '10.9.0.0/29'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu '1380'\n\toption jc '4'\n\toption jmin '64'\n\toption jmax '128'\n\toption s4 '12'\n\nconfig person 'alice'\n\toption server 'main'\n\toption name 'Alice'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption access 'both'\n\nconfig person 'bob'\n\toption server 'main'\n\toption name 'Bob'\n\toption code 'XyZ12345'\n\toption quota_gb '2'\n\nconfig device 'alice_phone'\n\toption person 'alice'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.9.0.2'\n\toption public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='\n\n"
},
{
"op": [
"delete",
"settings",
"brand_new"
],
"ok": true,
"file": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\tlist portal_port 'now'\n\tlist portal_port 'a list'\n\toption portal_host 'vpn.example.com'\n\toption portal_title 'My VPN'\n\toption log_connections '0'\n\toption update_interval '7d'\n\tlist update_targets 'app'\n\toption a_list 'now an option'\n\nconfig server 'main'\n\toption name 'Main'\n\toption ifname 'awgmain'\n\toption port '51820'\n\toption subnet '10.9.0.0/29'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu '1380'\n\toption jc '4'\n\toption jmin '64'\n\toption jmax '128'\n\toption s4 '12'\n\nconfig person 'alice'\n\toption server 'main'\n\toption name 'Alice'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption access 'both'\n\nconfig person 'bob'\n\toption server 'main'\n\toption name 'Bob'\n\toption code 'XyZ12345'\n\toption quota_gb '2'\n\nconfig device 'alice_phone'\n\toption person 'alice'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.9.0.2'\n\toption public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='\n\n"
},
{
"op": [
"set",
"settings",
"brand_new",
"back, at the end"
],
"ok": true,
"file": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\tlist portal_port 'now'\n\tlist portal_port 'a list'\n\toption portal_host 'vpn.example.com'\n\toption portal_title 'My VPN'\n\toption log_connections '0'\n\toption update_interval '7d'\n\tlist update_targets 'app'\n\toption a_list 'now an option'\n\toption brand_new 'back, at the end'\n\nconfig server 'main'\n\toption name 'Main'\n\toption ifname 'awgmain'\n\toption port '51820'\n\toption subnet '10.9.0.0/29'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu '1380'\n\toption jc '4'\n\toption jmin '64'\n\toption jmax '128'\n\toption s4 '12'\n\nconfig person 'alice'\n\toption server 'main'\n\toption name 'Alice'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption access 'both'\n\nconfig person 'bob'\n\toption server 'main'\n\toption name 'Bob'\n\toption code 'XyZ12345'\n\toption quota_gb '2'\n\nconfig device 'alice_phone'\n\toption person 'alice'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.9.0.2'\n\toption public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='\n\n"
},
{
"op": [
"add",
"person",
"zed"
],
"ok": true,
"file": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\tlist portal_port 'now'\n\tlist portal_port 'a list'\n\toption portal_host 'vpn.example.com'\n\toption portal_title 'My VPN'\n\toption log_connections '0'\n\toption update_interval '7d'\n\tlist update_targets 'app'\n\toption a_list 'now an option'\n\toption brand_new 'back, at the end'\n\nconfig server 'main'\n\toption name 'Main'\n\toption ifname 'awgmain'\n\toption port '51820'\n\toption subnet '10.9.0.0/29'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu '1380'\n\toption jc '4'\n\toption jmin '64'\n\toption jmax '128'\n\toption s4 '12'\n\nconfig person 'alice'\n\toption server 'main'\n\toption name 'Alice'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption access 'both'\n\nconfig person 'bob'\n\toption server 'main'\n\toption name 'Bob'\n\toption code 'XyZ12345'\n\toption quota_gb '2'\n\nconfig device 'alice_phone'\n\toption person 'alice'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.9.0.2'\n\toption public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='\n\nconfig person 'zed'\n\n"
},
{
"op": [
"set",
"zed",
"name",
"Zed"
],
"ok": true,
"file": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\tlist portal_port 'now'\n\tlist portal_port 'a list'\n\toption portal_host 'vpn.example.com'\n\toption portal_title 'My VPN'\n\toption log_connections '0'\n\toption update_interval '7d'\n\tlist update_targets 'app'\n\toption a_list 'now an option'\n\toption brand_new 'back, at the end'\n\nconfig server 'main'\n\toption name 'Main'\n\toption ifname 'awgmain'\n\toption port '51820'\n\toption subnet '10.9.0.0/29'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu '1380'\n\toption jc '4'\n\toption jmin '64'\n\toption jmax '128'\n\toption s4 '12'\n\nconfig person 'alice'\n\toption server 'main'\n\toption name 'Alice'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption access 'both'\n\nconfig person 'bob'\n\toption server 'main'\n\toption name 'Bob'\n\toption code 'XyZ12345'\n\toption quota_gb '2'\n\nconfig device 'alice_phone'\n\toption person 'alice'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.9.0.2'\n\toption public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='\n\nconfig person 'zed'\n\toption name 'Zed'\n\n"
},
{
"op": [
"set",
"zed",
"multi",
"line one\nline two"
],
"ok": true,
"file": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\tlist portal_port 'now'\n\tlist portal_port 'a list'\n\toption portal_host 'vpn.example.com'\n\toption portal_title 'My VPN'\n\toption log_connections '0'\n\toption update_interval '7d'\n\tlist update_targets 'app'\n\toption a_list 'now an option'\n\toption brand_new 'back, at the end'\n\nconfig server 'main'\n\toption name 'Main'\n\toption ifname 'awgmain'\n\toption port '51820'\n\toption subnet '10.9.0.0/29'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu '1380'\n\toption jc '4'\n\toption jmin '64'\n\toption jmax '128'\n\toption s4 '12'\n\nconfig person 'alice'\n\toption server 'main'\n\toption name 'Alice'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption access 'both'\n\nconfig person 'bob'\n\toption server 'main'\n\toption name 'Bob'\n\toption code 'XyZ12345'\n\toption quota_gb '2'\n\nconfig device 'alice_phone'\n\toption person 'alice'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.9.0.2'\n\toption public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='\n\nconfig person 'zed'\n\toption name 'Zed'\n\toption multi 'line one\nline two'\n\n"
},
{
"op": [
"add",
"device",
"zphone"
],
"ok": true,
"file": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\tlist portal_port 'now'\n\tlist portal_port 'a list'\n\toption portal_host 'vpn.example.com'\n\toption portal_title 'My VPN'\n\toption log_connections '0'\n\toption update_interval '7d'\n\tlist update_targets 'app'\n\toption a_list 'now an option'\n\toption brand_new 'back, at the end'\n\nconfig server 'main'\n\toption name 'Main'\n\toption ifname 'awgmain'\n\toption port '51820'\n\toption subnet '10.9.0.0/29'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu '1380'\n\toption jc '4'\n\toption jmin '64'\n\toption jmax '128'\n\toption s4 '12'\n\nconfig person 'alice'\n\toption server 'main'\n\toption name 'Alice'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption access 'both'\n\nconfig person 'bob'\n\toption server 'main'\n\toption name 'Bob'\n\toption code 'XyZ12345'\n\toption quota_gb '2'\n\nconfig device 'alice_phone'\n\toption person 'alice'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.9.0.2'\n\toption public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='\n\nconfig person 'zed'\n\toption name 'Zed'\n\toption multi 'line one\nline two'\n\nconfig device 'zphone'\n\n"
},
{
"op": [
"set",
"zphone",
"person",
"zed"
],
"ok": true,
"file": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\tlist portal_port 'now'\n\tlist portal_port 'a list'\n\toption portal_host 'vpn.example.com'\n\toption portal_title 'My VPN'\n\toption log_connections '0'\n\toption update_interval '7d'\n\tlist update_targets 'app'\n\toption a_list 'now an option'\n\toption brand_new 'back, at the end'\n\nconfig server 'main'\n\toption name 'Main'\n\toption ifname 'awgmain'\n\toption port '51820'\n\toption subnet '10.9.0.0/29'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu '1380'\n\toption jc '4'\n\toption jmin '64'\n\toption jmax '128'\n\toption s4 '12'\n\nconfig person 'alice'\n\toption server 'main'\n\toption name 'Alice'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption access 'both'\n\nconfig person 'bob'\n\toption server 'main'\n\toption name 'Bob'\n\toption code 'XyZ12345'\n\toption quota_gb '2'\n\nconfig device 'alice_phone'\n\toption person 'alice'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.9.0.2'\n\toption public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='\n\nconfig person 'zed'\n\toption name 'Zed'\n\toption multi 'line one\nline two'\n\nconfig device 'zphone'\n\toption person 'zed'\n\n"
},
{
"op": [
"set",
"zed",
"server",
"home"
],
"ok": true,
"file": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\tlist portal_port 'now'\n\tlist portal_port 'a list'\n\toption portal_host 'vpn.example.com'\n\toption portal_title 'My VPN'\n\toption log_connections '0'\n\toption update_interval '7d'\n\tlist update_targets 'app'\n\toption a_list 'now an option'\n\toption brand_new 'back, at the end'\n\nconfig server 'main'\n\toption name 'Main'\n\toption ifname 'awgmain'\n\toption port '51820'\n\toption subnet '10.9.0.0/29'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu '1380'\n\toption jc '4'\n\toption jmin '64'\n\toption jmax '128'\n\toption s4 '12'\n\nconfig person 'alice'\n\toption server 'main'\n\toption name 'Alice'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption access 'both'\n\nconfig person 'bob'\n\toption server 'main'\n\toption name 'Bob'\n\toption code 'XyZ12345'\n\toption quota_gb '2'\n\nconfig device 'alice_phone'\n\toption person 'alice'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.9.0.2'\n\toption public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='\n\nconfig person 'zed'\n\toption name 'Zed'\n\toption multi 'line one\nline two'\n\toption server 'home'\n\nconfig device 'zphone'\n\toption person 'zed'\n\n"
},
{
"op": [
"add",
"circle",
"zed"
],
"ok": true,
"file": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\tlist portal_port 'now'\n\tlist portal_port 'a list'\n\toption portal_host 'vpn.example.com'\n\toption portal_title 'My VPN'\n\toption log_connections '0'\n\toption update_interval '7d'\n\tlist update_targets 'app'\n\toption a_list 'now an option'\n\toption brand_new 'back, at the end'\n\nconfig server 'main'\n\toption name 'Main'\n\toption ifname 'awgmain'\n\toption port '51820'\n\toption subnet '10.9.0.0/29'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu '1380'\n\toption jc '4'\n\toption jmin '64'\n\toption jmax '128'\n\toption s4 '12'\n\nconfig person 'alice'\n\toption server 'main'\n\toption name 'Alice'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption access 'both'\n\nconfig person 'bob'\n\toption server 'main'\n\toption name 'Bob'\n\toption code 'XyZ12345'\n\toption quota_gb '2'\n\nconfig device 'alice_phone'\n\toption person 'alice'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.9.0.2'\n\toption public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='\n\nconfig circle 'zed'\n\toption name 'Zed'\n\toption multi 'line one\nline two'\n\toption server 'home'\n\nconfig device 'zphone'\n\toption person 'zed'\n\n"
},
{
"op": [
"delete",
"zphone"
],
"ok": true,
"file": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\tlist portal_port 'now'\n\tlist portal_port 'a list'\n\toption portal_host 'vpn.example.com'\n\toption portal_title 'My VPN'\n\toption log_connections '0'\n\toption update_interval '7d'\n\tlist update_targets 'app'\n\toption a_list 'now an option'\n\toption brand_new 'back, at the end'\n\nconfig server 'main'\n\toption name 'Main'\n\toption ifname 'awgmain'\n\toption port '51820'\n\toption subnet '10.9.0.0/29'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu '1380'\n\toption jc '4'\n\toption jmin '64'\n\toption jmax '128'\n\toption s4 '12'\n\nconfig person 'alice'\n\toption server 'main'\n\toption name 'Alice'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption access 'both'\n\nconfig person 'bob'\n\toption server 'main'\n\toption name 'Bob'\n\toption code 'XyZ12345'\n\toption quota_gb '2'\n\nconfig device 'alice_phone'\n\toption person 'alice'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.9.0.2'\n\toption public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='\n\nconfig circle 'zed'\n\toption name 'Zed'\n\toption multi 'line one\nline two'\n\toption server 'home'\n\n"
},
{
"op": [
"delete",
"settings",
"no_such_option"
],
"ok": false,
"file": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\tlist portal_port 'now'\n\tlist portal_port 'a list'\n\toption portal_host 'vpn.example.com'\n\toption portal_title 'My VPN'\n\toption log_connections '0'\n\toption update_interval '7d'\n\tlist update_targets 'app'\n\toption a_list 'now an option'\n\toption brand_new 'back, at the end'\n\nconfig server 'main'\n\toption name 'Main'\n\toption ifname 'awgmain'\n\toption port '51820'\n\toption subnet '10.9.0.0/29'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu '1380'\n\toption jc '4'\n\toption jmin '64'\n\toption jmax '128'\n\toption s4 '12'\n\nconfig person 'alice'\n\toption server 'main'\n\toption name 'Alice'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption access 'both'\n\nconfig person 'bob'\n\toption server 'main'\n\toption name 'Bob'\n\toption code 'XyZ12345'\n\toption quota_gb '2'\n\nconfig device 'alice_phone'\n\toption person 'alice'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.9.0.2'\n\toption public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='\n\nconfig circle 'zed'\n\toption name 'Zed'\n\toption multi 'line one\nline two'\n\toption server 'home'\n\n"
},
{
"op": [
"set",
"settings",
"empty",
""
],
"ok": true,
"file": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\tlist portal_port 'now'\n\tlist portal_port 'a list'\n\toption portal_host 'vpn.example.com'\n\toption portal_title 'My VPN'\n\toption log_connections '0'\n\toption update_interval '7d'\n\tlist update_targets 'app'\n\toption a_list 'now an option'\n\toption brand_new 'back, at the end'\n\nconfig server 'main'\n\toption name 'Main'\n\toption ifname 'awgmain'\n\toption port '51820'\n\toption subnet '10.9.0.0/29'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu '1380'\n\toption jc '4'\n\toption jmin '64'\n\toption jmax '128'\n\toption s4 '12'\n\nconfig person 'alice'\n\toption server 'main'\n\toption name 'Alice'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption access 'both'\n\nconfig person 'bob'\n\toption server 'main'\n\toption name 'Bob'\n\toption code 'XyZ12345'\n\toption quota_gb '2'\n\nconfig device 'alice_phone'\n\toption person 'alice'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.9.0.2'\n\toption public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='\n\nconfig circle 'zed'\n\toption name 'Zed'\n\toption multi 'line one\nline two'\n\toption server 'home'\n\n"
},
{
"op": [
"set",
"settings",
"empty_list",
[
]
],
"ok": false,
"file": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\tlist portal_port 'now'\n\tlist portal_port 'a list'\n\toption portal_host 'vpn.example.com'\n\toption portal_title 'My VPN'\n\toption log_connections '0'\n\toption update_interval '7d'\n\tlist update_targets 'app'\n\toption a_list 'now an option'\n\toption brand_new 'back, at the end'\n\nconfig server 'main'\n\toption name 'Main'\n\toption ifname 'awgmain'\n\toption port '51820'\n\toption subnet '10.9.0.0/29'\n\toption private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\toption public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='\n\toption mtu '1380'\n\toption jc '4'\n\toption jmin '64'\n\toption jmax '128'\n\toption s4 '12'\n\nconfig person 'alice'\n\toption server 'main'\n\toption name 'Alice'\n\toption code 'abcdef-ghijkl-mnopqr'\n\toption access 'both'\n\nconfig person 'bob'\n\toption server 'main'\n\toption name 'Bob'\n\toption code 'XyZ12345'\n\toption quota_gb '2'\n\nconfig device 'alice_phone'\n\toption person 'alice'\n\toption name 'Phone'\n\toption kind 'phone'\n\toption address '10.9.0.2'\n\toption public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='\n\nconfig circle 'zed'\n\toption name 'Zed'\n\toption multi 'line one\nline two'\n\toption server 'home'\n\n"
}
]
}
+242
View File
@@ -0,0 +1,242 @@
// Package uci reads a configuration file of OpenWrt's UCI the way libuci does, without the uci program:
// the backend reads /etc/config/dawnline on every change, and a test reads fixtures on any system.
//
// Reading only. Whoever writes the file (the ucode backend's store, until the model's writer is ported)
// replaces it whole; this package never holds it open.
//
// The syntax, as libuci's parser takes it: one command a line (or several parted by `;`), `package`,
// `config <type> [<name>]`, `option <name> <value>`, `list <name> <value>`; a word is bare, in single
// quotes (taken as written) or in double quotes (a backslash takes the next character), and pieces that
// touch are one word; `#` begins a comment where a word could begin.
package uci
import (
"fmt"
"strings"
)
// Value is what an option holds: one string (`option`), or a list (`list`, one entry a line).
type Value struct {
Str string
List []string
IsList bool
}
// Section is one `config` block.
type Section struct {
Type string
Name string
// Anonymous: the file gave it no name. libuci names such a section by its place and a hash of its
// content; here it gets its place only (Dawnline's own file names every section).
Anonymous bool
Values map[string]Value
// Order is the options' names in the order they stand in the file: what is written keeps it.
Order []string
}
// Config is a parsed file: its sections in the order they stand.
type Config struct {
Sections []*Section
}
// Get returns an option's string. A list has none (ok is false): its entries are List's.
func (s *Section) Get(name string) (string, bool) {
v, ok := s.Values[name]
if !ok || v.IsList {
return "", false
}
return v.Str, true
}
// Section returns the section of that name, or nil.
func (c *Config) Section(name string) *Section {
for _, s := range c.Sections {
if s.Name == name {
return s
}
}
return nil
}
// OfType returns the sections of a type, in the file's order.
func (c *Config) OfType(kind string) []*Section {
var out []*Section
for _, s := range c.Sections {
if s.Type == kind {
out = append(out, s)
}
}
return out
}
// Parse reads a file's text.
func Parse(text string) (*Config, error) {
cmds, err := commands(text)
if err != nil {
return nil, err
}
c := &Config{}
var cur *Section
for _, cmd := range cmds {
w := cmd.words
switch w[0] {
case "package":
// one package a file; its name is the file's
case "config":
if len(w) < 2 {
return nil, fmt.Errorf("uci: line %d: config without a type", cmd.line)
}
name := ""
if len(w) > 2 {
name = w[2]
}
cur = c.open(w[1], name)
case "option", "list":
if cur == nil {
return nil, fmt.Errorf("uci: line %d: %s outside a section", cmd.line, w[0])
}
if len(w) < 3 {
return nil, fmt.Errorf("uci: line %d: %s needs a name and a value", cmd.line, w[0])
}
cur.set(w[1], w[2], w[0] == "list")
default:
return nil, fmt.Errorf("uci: line %d: unknown command %q", cmd.line, w[0])
}
}
return c, nil
}
// open starts a section. A name the file used before goes on with that section (its type becomes the new
// one), as libuci merges them.
func (c *Config) open(kind, name string) *Section {
if name != "" {
if s := c.Section(name); s != nil {
s.Type = kind
return s
}
}
s := &Section{Type: kind, Name: name, Values: map[string]Value{}}
if name == "" {
s.Anonymous = true
s.Name = fmt.Sprintf("cfg%02x", len(c.Sections)+1)
}
c.Sections = append(c.Sections, s)
return s
}
// set stores a value: an option replaces what was there; a list entry is added to a list, and turns an
// option of that name into a list that begins with it.
func (s *Section) set(name, value string, list bool) {
if _, had := s.Values[name]; !had {
s.Order = append(s.Order, name)
}
if !list {
s.Values[name] = Value{Str: value}
return
}
old, had := s.Values[name]
switch {
case !had:
s.Values[name] = Value{IsList: true, List: []string{value}}
case old.IsList:
old.List = append(old.List, value)
s.Values[name] = old
default:
s.Values[name] = Value{IsList: true, List: []string{old.Str, value}}
}
}
type command struct {
words []string
// line is where the command begins, for an error.
line int
}
// commands cuts a file into commands: a line, or a part of one up to a `;`. A quoted word may run over
// several lines (a value with line breaks in it).
func commands(text string) ([]command, error) {
var out []command
var cur []string
line, began := 1, 1
flush := func() {
if len(cur) > 0 {
out = append(out, command{words: cur, line: began})
}
cur = nil
}
i := 0
for i < len(text) {
switch c := text[i]; {
case c == '\n':
flush()
line++
i++
case c == ' ' || c == '\t' || c == '\r':
i++
case c == ';':
flush()
i++
case c == '#':
for i < len(text) && text[i] != '\n' {
i++
}
default:
if len(cur) == 0 {
began = line
}
var b strings.Builder
piece:
for i < len(text) {
switch c := text[i]; c {
case ' ', '\t', '\r', '\n', ';':
break piece
case '\'':
end := strings.IndexByte(text[i+1:], '\'')
if end < 0 {
return nil, fmt.Errorf("uci: line %d: a single quote is not closed", line)
}
quoted := text[i+1 : i+1+end]
line += strings.Count(quoted, "\n")
b.WriteString(quoted)
i += end + 2
case '"':
i++
closed := false
for i < len(text) {
if text[i] == '\\' && i+1 < len(text) {
b.WriteByte(text[i+1])
i += 2
continue
}
if text[i] == '"' {
i++
closed = true
break
}
if text[i] == '\n' {
line++
}
b.WriteByte(text[i])
i++
}
if !closed {
return nil, fmt.Errorf("uci: line %d: a double quote is not closed", line)
}
case '\\':
if i+1 < len(text) {
b.WriteByte(text[i+1])
i += 2
} else {
i++
}
default:
b.WriteByte(c)
i++
}
}
cur = append(cur, b.String())
}
}
flush()
return out, nil
}
+83
View File
@@ -0,0 +1,83 @@
package uci
import (
"os"
"reflect"
"testing"
)
func TestParse(t *testing.T) {
c, err := Parse(`# a comment
package dawnline
config settings 'settings'
option portal_enabled '1' # after a value
option quoted "a \"b\" c"
option glued 'one'"two"three
option bare vpn.example.com
list update_targets 'app'
list update_targets 'packages'
option twice 'old'
option twice 'new'
option then_list 'first'
list then_list 'second'
option lines 'two
lines'
config server main; option name Main
config device
option name 'no name of its own'
config settings 'settings'
option later 'merged'
`)
if err != nil {
t.Fatal(err)
}
s := c.Section("settings")
want := map[string]Value{
"portal_enabled": {Str: "1"},
"quoted": {Str: `a "b" c`},
"glued": {Str: "onetwothree"},
"bare": {Str: "vpn.example.com"},
"update_targets": {IsList: true, List: []string{"app", "packages"}},
"twice": {Str: "new"},
"then_list": {IsList: true, List: []string{"first", "second"}},
"lines": {Str: "two\nlines"},
"later": {Str: "merged"},
}
if !reflect.DeepEqual(s.Values, want) {
t.Fatalf("settings: %#v", s.Values)
}
if len(c.Sections) != 3 || c.Sections[1].Name != "main" || c.Sections[1].Values["name"].Str != "Main" {
t.Fatalf("sections: %+v", c.Sections)
}
if d := c.OfType("device"); len(d) != 1 || !d[0].Anonymous || d[0].Name == "" {
t.Fatalf("the anonymous section: %+v", d)
}
if _, ok := s.Get("update_targets"); ok {
t.Fatal("a list read as one string")
}
}
func TestParseErrors(t *testing.T) {
for _, text := range []string{"option a b", "config", "config s 'x'\noption a", "config s x\noption a 'open", "frobnicate x"} {
if _, err := Parse(text); err == nil {
t.Errorf("%q was accepted", text)
}
}
}
// The project's own fixture parses whole.
func TestFixture(t *testing.T) {
raw, err := os.ReadFile("testdata/household.uci")
if err != nil {
t.Skip(err)
}
c, err := Parse(string(raw))
if err != nil {
t.Fatal(err)
}
if len(c.OfType("server")) != 1 || len(c.OfType("person")) != 2 || len(c.OfType("device")) != 1 {
t.Fatalf("sections: %d servers, %d people, %d devices", len(c.OfType("server")), len(c.OfType("person")), len(c.OfType("device")))
}
}
+118
View File
@@ -0,0 +1,118 @@
package uci
import "strings"
// Writing a file as libuci commits it, and changing one as a cursor does, so that what the Go backend
// saves is byte for byte what the ucode backend would have (testdata/write.uc, scripts/ucode-golden.sh):
//
// config <type> '<name>'
// option <name> '<value>'
// list <name> '<entry>'
//
// each section after an empty line, a single quote inside a value as '\''.
func quote(s string) string { return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'" }
// Format is the file's text.
func (c *Config) Format() string {
var b strings.Builder
for _, s := range c.Sections {
b.WriteString("\nconfig " + s.Type)
if !s.Anonymous {
b.WriteString(" " + quote(s.Name))
}
b.WriteString("\n")
for _, name := range s.Order {
v, ok := s.Values[name]
if !ok {
continue
}
if !v.IsList {
b.WriteString("\toption " + name + " " + quote(v.Str) + "\n")
continue
}
for _, e := range v.List {
b.WriteString("\tlist " + name + " " + quote(e) + "\n")
}
}
}
// the file ends with an empty line
if len(c.Sections) > 0 {
b.WriteString("\n")
}
return b.String()
}
// Add makes a section, or gives the one of that name another type (its options stay). It returns the section.
func (c *Config) Add(kind, name string) *Section {
return c.open(kind, name)
}
// Set gives an option a value; it keeps its place when it was there, else goes last. An empty value
// removes the option, as a cursor does. False when there is no such section.
func (c *Config) Set(section, option, value string) bool {
s := c.Section(section)
if s == nil {
return false
}
if value == "" {
s.remove(option)
return true
}
if _, had := s.Values[option]; !had {
s.Order = append(s.Order, option)
}
s.Values[option] = Value{Str: value}
return true
}
// SetList gives an option a list of entries. An empty list changes nothing and is refused, as a cursor
// refuses it.
func (c *Config) SetList(section, option string, list []string) bool {
s := c.Section(section)
if s == nil || len(list) == 0 {
return false
}
// The cursor's own ways, as the goldens have them: a list of one entry is taken out and added anew, so
// it goes last whatever was there; a longer one is set in the place of what was there (its first entry
// set, the others added to it), and goes last only when nothing was.
if len(list) == 1 {
s.remove(option)
}
if _, had := s.Values[option]; !had {
s.Order = append(s.Order, option)
}
s.Values[option] = Value{IsList: true, List: append([]string{}, list...)}
return true
}
func (s *Section) remove(option string) bool {
if _, had := s.Values[option]; !had {
return false
}
delete(s.Values, option)
for i, n := range s.Order {
if n == option {
s.Order = append(s.Order[:i], s.Order[i+1:]...)
break
}
}
return true
}
// Delete removes an option; false when it was not there.
func (c *Config) Delete(section, option string) bool {
s := c.Section(section)
return s != nil && s.remove(option)
}
// DeleteSection removes a section; false when it was not there.
func (c *Config) DeleteSection(name string) bool {
for i, s := range c.Sections {
if s.Name == name {
c.Sections = append(c.Sections[:i], c.Sections[i+1:]...)
return true
}
}
return false
}
+83
View File
@@ -0,0 +1,83 @@
package uci
import (
"encoding/json"
"os"
"testing"
)
// The model's fixtures as libuci commits them, and one of them through a row of changes, file for file
// (testdata/write.uc, scripts/ucode-golden.sh).
func TestWrittenAsLibuci(t *testing.T) {
raw, err := os.ReadFile("testdata/write.json")
if err != nil {
t.Fatal(err)
}
var golden struct {
Canon map[string]string `json:"canon"`
Steps []struct {
Op []any `json:"op"`
OK bool `json:"ok"`
File string `json:"file"`
} `json:"steps"`
}
if err := json.Unmarshal(raw, &golden); err != nil {
t.Fatal(err)
}
if len(golden.Canon) != 2 || len(golden.Steps) < 18 {
t.Fatal("the golden is too small")
}
for name, want := range golden.Canon {
text, err := os.ReadFile("../model/testdata/" + name + ".uci")
if err != nil {
t.Fatal(err)
}
c, err := Parse(string(text))
if err != nil {
t.Fatal(err)
}
if got := c.Format(); got != want {
t.Errorf("%s is written otherwise:\n%s\n--- libuci:\n%s", name, got, want)
}
// what is written reads back as itself
again, err := Parse(c.Format())
if err != nil || again.Format() != want {
t.Errorf("%s does not read back as written: %v", name, err)
}
}
text, _ := os.ReadFile("../model/testdata/small.uci")
file := string(text)
for i, step := range golden.Steps {
// as the golden: read anew, one change, written
c, err := Parse(file)
if err != nil {
t.Fatalf("step %d: %v", i, err)
}
str := func(n int) string { s, _ := step.Op[n].(string); return s }
var ok bool
switch str(0) {
case "add":
ok = c.Add(str(1), str(2)) != nil
case "delete":
if len(step.Op) > 2 {
ok = c.Delete(str(1), str(2))
} else {
ok = c.DeleteSection(str(1))
}
case "set":
if list, isList := step.Op[3].([]any); isList {
var entries []string
for _, e := range list {
entries = append(entries, e.(string))
}
ok = c.SetList(str(1), str(2), entries)
} else {
ok = c.Set(str(1), str(2), str(3))
}
}
file = c.Format()
if ok != step.OK || file != step.File {
t.Fatalf("step %d %v: ok %v (libuci %v)\n%s\n--- libuci:\n%s", i, step.Op, ok, step.OK, file, step.File)
}
}
}