Pull request: all: imp http handlers, imp docs

Merge in DNS/adguard-home from fix-openapi to master

Squashed commit of the following:

commit 0e7530472fb566e5cab73d178c8ec16e5ef11dcb
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Jan 13 17:02:06 2021 +0300

    all: imp http handlers, imp docs
This commit is contained in:
Ainar Garipov 2021-01-13 17:26:57 +03:00
parent e8c1f5c8d3
commit 4474e9fcf9
7 changed files with 87 additions and 27 deletions

View File

@ -522,12 +522,12 @@ func (s *Server) handleDOH(w http.ResponseWriter, r *http.Request) {
} }
func (s *Server) registerHandlers() { func (s *Server) registerHandlers() {
s.conf.HTTPRegister("GET", "/control/dns_info", s.handleGetConfig) s.conf.HTTPRegister(http.MethodGet, "/control/dns_info", s.handleGetConfig)
s.conf.HTTPRegister("POST", "/control/dns_config", s.handleSetConfig) s.conf.HTTPRegister(http.MethodPost, "/control/dns_config", s.handleSetConfig)
s.conf.HTTPRegister("POST", "/control/test_upstream_dns", s.handleTestUpstreamDNS) s.conf.HTTPRegister(http.MethodPost, "/control/test_upstream_dns", s.handleTestUpstreamDNS)
s.conf.HTTPRegister("GET", "/control/access/list", s.handleAccessList) s.conf.HTTPRegister(http.MethodGet, "/control/access/list", s.handleAccessList)
s.conf.HTTPRegister("POST", "/control/access/set", s.handleAccessSet) s.conf.HTTPRegister(http.MethodPost, "/control/access/set", s.handleAccessSet)
s.conf.HTTPRegister("", "/dns-query", s.handleDOH) s.conf.HTTPRegister("", "/dns-query", s.handleDOH)
} }

View File

@ -369,8 +369,8 @@ func handleLogout(w http.ResponseWriter, r *http.Request) {
// RegisterAuthHandlers - register handlers // RegisterAuthHandlers - register handlers
func RegisterAuthHandlers() { func RegisterAuthHandlers() {
Context.mux.Handle("/control/login", postInstallHandler(ensureHandler("POST", handleLogin))) Context.mux.Handle("/control/login", postInstallHandler(ensureHandler(http.MethodPost, handleLogin)))
httpRegister("GET", "/control/logout", handleLogout) httpRegister(http.MethodGet, "/control/logout", handleLogout)
} }
func parseCookie(cookie string) string { func parseCookie(cookie string) string {

View File

@ -119,7 +119,7 @@ func TestAuthHTTP(t *testing.T) {
w.hdr = make(http.Header) w.hdr = make(http.Header)
r := http.Request{} r := http.Request{}
r.Header = make(http.Header) r.Header = make(http.Header)
r.Method = "GET" r.Method = http.MethodGet
// get / - we're redirected to login page // get / - we're redirected to login page
r.URL = &url.URL{Path: "/"} r.URL = &url.URL{Path: "/"}

View File

@ -36,7 +36,7 @@ func TestAuthGL(t *testing.T) {
binary.BigEndian.PutUint32(data, tval) binary.BigEndian.PutUint32(data, tval)
} }
assert.Nil(t, ioutil.WriteFile(glFilePrefix+"test", data, 0o644)) assert.Nil(t, ioutil.WriteFile(glFilePrefix+"test", data, 0o644))
r, _ := http.NewRequest("GET", "http://localhost/", nil) r, _ := http.NewRequest(http.MethodGet, "http://localhost/", nil)
r.AddCookie(&http.Cookie{Name: glCookieName, Value: "test"}) r.AddCookie(&http.Cookie{Name: glCookieName, Value: "test"})
assert.True(t, glProcessCookie(r)) assert.True(t, glProcessCookie(r))
GLMode = false GLMode = false

View File

@ -43,7 +43,7 @@ func addDNSAddress(dnsAddresses *[]string, addr string) {
*dnsAddresses = append(*dnsAddresses, addr) *dnsAddresses = append(*dnsAddresses, addr)
} }
func handleStatus(w http.ResponseWriter, r *http.Request) { func handleStatus(w http.ResponseWriter, _ *http.Request) {
c := dnsforward.FilteringConfig{} c := dnsforward.FilteringConfig{}
if Context.dnsServer != nil { if Context.dnsServer != nil {
Context.dnsServer.WriteDiskConfig(&c) Context.dnsServer.WriteDiskConfig(&c)
@ -140,7 +140,7 @@ func ensure(method string, handler func(http.ResponseWriter, *http.Request)) fun
return return
} }
if method == "POST" || method == "PUT" || method == "DELETE" { if method == http.MethodPost || method == http.MethodPut || method == http.MethodDelete {
Context.controlLock.Lock() Context.controlLock.Lock()
defer Context.controlLock.Unlock() defer Context.controlLock.Unlock()
} }
@ -150,11 +150,11 @@ func ensure(method string, handler func(http.ResponseWriter, *http.Request)) fun
} }
func ensurePOST(handler func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) { func ensurePOST(handler func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
return ensure("POST", handler) return ensure(http.MethodPost, handler)
} }
func ensureGET(handler func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) { func ensureGET(handler func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
return ensure("GET", handler) return ensure(http.MethodGet, handler)
} }
// Bridge between http.Handler object and Go function // Bridge between http.Handler object and Go function

View File

@ -62,6 +62,8 @@
The old fields will be removed in v0.106.0. The old fields will be removed in v0.106.0.
As well as other documentation fixes.
## v0.103: API changes ## v0.103: API changes
### API: replace settings in GET /control/dns_info & POST /control/dns_config ### API: replace settings in GET /control/dns_info & POST /control/dns_config

View File

@ -608,7 +608,7 @@
'application/json': 'application/json':
'schema': 'schema':
'type': 'object' 'type': 'object'
'parameters': 'properties':
'enabled': 'enabled':
'type': 'boolean' 'type': 'boolean'
'examples': 'examples':
@ -664,7 +664,7 @@
'application/json': 'application/json':
'schema': 'schema':
'type': 'object' 'type': 'object'
'parameters': 'properties':
'enable': 'enable':
'type': 'boolean' 'type': 'boolean'
'sensitivity': 'sensitivity':
@ -705,7 +705,7 @@
'application/json': 'application/json':
'schema': 'schema':
'type': 'object' 'type': 'object'
'parameters': 'properties':
'enabled': 'enabled':
'type': 'boolean' 'type': 'boolean'
'examples': 'examples':
@ -789,6 +789,39 @@
'application/json': 'application/json':
'schema': 'schema':
'$ref': '#/components/schemas/ClientsFindResponse' '$ref': '#/components/schemas/ClientsFindResponse'
'/access/list':
'get':
'operationId': 'accessList'
'responses':
'200':
'description': 'OK.'
'content':
'application/json':
'schema':
'$ref': '#/components/schemas/AccessListResponse'
'summary': 'List (dis)allowed clients, blocked hosts, etc.'
'tags':
- 'clients'
'/access/set':
'post':
'operationId': 'accessSet'
'requestBody':
'content':
'application/json':
'schema':
'$ref': '#/components/schemas/AccessSetRequest'
'required': true
'responses':
'200':
'description': 'OK.'
'400':
'description': >
Failed to parse JSON or cannot save the list.
'500':
'description': 'Internal error.'
'summary': 'Set (dis)allowed clients, blocked hosts, etc.'
'tags':
- 'clients'
'/blocked_services/list': '/blocked_services/list':
'get': 'get':
'tags': 'tags':
@ -1124,31 +1157,35 @@
'type': 'object' 'type': 'object'
'description': 'AdGuard Home server status and configuration' 'description': 'AdGuard Home server status and configuration'
'required': 'required':
- 'dns_address' - 'dns_addresses'
- 'dns_port' - 'dns_port'
- 'http_port'
- 'protection_enabled' - 'protection_enabled'
- 'querylog_enabled'
- 'running' - 'running'
- 'bootstrap_dns'
- 'upstream_dns'
- 'version' - 'version'
- 'language' - 'language'
'properties': 'properties':
'dns_address': 'dns_addresses':
'example': ['127.0.0.1']
'items':
'type': 'string' 'type': 'string'
'example': '127.0.0.1' 'type': 'array'
'dns_port': 'dns_port':
'type': 'integer' 'type': 'integer'
'format': 'uint16' 'format': 'uint16'
'example': 53 'example': 53
'minimum': 1 'minimum': 1
'maximum': 65535 'maximum': 65535
'http_port':
'type': 'integer'
'format': 'uint16'
'example': 80
'minimum': 1
'maximum': 65535
'protection_enabled': 'protection_enabled':
'type': 'boolean' 'type': 'boolean'
'dhcp_available': 'dhcp_available':
'type': 'boolean' 'type': 'boolean'
'querylog_enabled':
'type': 'boolean'
'running': 'running':
'type': 'boolean' 'type': 'boolean'
'version': 'version':
@ -2077,6 +2114,29 @@
'description': 'Response to clients find operation' 'description': 'Response to clients find operation'
'items': 'items':
'$ref': '#/components/schemas/ClientsFindEntry' '$ref': '#/components/schemas/ClientsFindEntry'
'AccessListResponse':
'$ref': '#/components/schemas/AccessList'
'AccessSetRequest':
'$ref': '#/components/schemas/AccessList'
'AccessList':
'description': 'Client and host access list'
'properties':
'allowed_clients':
'description': 'Allowlist of clients.'
'items':
'type': 'string'
'type': 'array'
'disallowed_clients':
'description': 'Blocklist of clients.'
'items':
'type': 'string'
'type': 'array'
'blocked_hosts':
'description': 'Blocklist of hosts.'
'items':
'type': 'string'
'type': 'array'
'type': 'object'
'ClientsFindEntry': 'ClientsFindEntry':
'type': 'object' 'type': 'object'
'additionalProperties': 'additionalProperties':
@ -2249,7 +2309,6 @@
'status': 'status':
'type': 'string' 'type': 'string'
'default': '' 'default': ''
'example': ''
'can_autofix': 'can_autofix':
'type': 'boolean' 'type': 'boolean'
'example': false 'example': false
@ -2274,7 +2333,6 @@
'error': 'error':
'type': 'string' 'type': 'string'
'default': '' 'default': ''
'example': ''
'description': 'Error text. Set if static=error' 'description': 'Error text. Set if static=error'
'InitialConfigurationBeta': 'InitialConfigurationBeta':
'type': 'object' 'type': 'object'