From 8725c1df7a95b74a8053aa6832b2b0d467ce8fbb Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Wed, 23 Jan 2019 17:26:15 +0300 Subject: [PATCH] Add stub OpenAPI methods --- config.go | 11 +++++++++++ control.go | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/config.go b/config.go index 0e680a1b..6d1a723c 100644 --- a/config.go +++ b/config.go @@ -40,6 +40,7 @@ type configuration struct { Filters []filter `yaml:"filters"` UserRules []string `yaml:"user_rules"` DHCP dhcpd.ServerConfig `yaml:"dhcp"` + TLS tlsConfig `yaml:"tls"` logSettings `yaml:",inline"` @@ -60,6 +61,16 @@ type dnsConfig struct { var defaultDNS = []string{"tls://1.1.1.1", "tls://1.0.0.1"} +// field ordering is important -- yaml fields will mirror ordering from here +type tlsConfig struct { + ServerName string `yaml:"server_name" json:"server_name"` + ForceHTTPS bool `yaml:"force_https" json:"force_https"` + PortHTTPS int `yaml:"port_https" json:"port_https"` + PortDNSOverTLS int `yaml:"port_dns_over_tls" json:"port_dns_over_tls"` + CertificateChain string `yaml:"certificate_chain" json:"certificate_chain"` + PrivateKey string `yaml:"private_key" json:"private_key"` +} + // initialize to default values, will be changed later when reading config or parsing command line var config = configuration{ ourConfigFilename: "AdGuardHome.yaml", diff --git a/control.go b/control.go index 35373c2c..71bb3dd5 100644 --- a/control.go +++ b/control.go @@ -1025,6 +1025,29 @@ func handleInstallConfigure(w http.ResponseWriter, r *http.Request) { } } +// --- +// TLS +// --- +func handleTLSStatus(w http.ResponseWriter, r *http.Request) { + err := json.NewEncoder(w).Encode(&config.TLS) + if err != nil { + httpError(w, http.StatusInternalServerError, "Failed to marshal json with TLS status: %s", err) + return + } +} + +func handleTLSConfigure(w http.ResponseWriter, r *http.Request) { + newconfig := tlsConfig{} + err := json.NewDecoder(r.body).Decode(&newconfig) + if err != nil { + httpError(w, http.StatusBadRequest, "Failed to parse new TLS config json: %s", err) + return + } + + // TODO: validate before applying + config.TLS = newconfig +} + func registerInstallHandlers() { http.HandleFunc("/control/install/get_addresses", preInstall(ensureGET(handleInstallGetAddresses))) http.HandleFunc("/control/install/configure", preInstall(ensurePOST(handleInstallConfigure))) @@ -1068,4 +1091,7 @@ func registerControlHandlers() { http.HandleFunc("/control/dhcp/interfaces", postInstall(optionalAuth(ensureGET(handleDHCPInterfaces)))) http.HandleFunc("/control/dhcp/set_config", postInstall(optionalAuth(ensurePOST(handleDHCPSetConfig)))) http.HandleFunc("/control/dhcp/find_active_dhcp", postInstall(optionalAuth(ensurePOST(handleDHCPFindActiveServer)))) + + http.HandleFunc("/control/tls/status", postInstall(optionalAuth(ensureGET(handleTLSStatus)))) + http.HandleFunc("/control/tls/configure", postInstall(optionalAuth(ensurePOST(handleTLSConfigure)))) }