pointer is unuseful for httpError func

This commit is contained in:
Aleksey Dmitrevskiy 2019-02-27 16:50:19 +03:00
parent 1223965cd4
commit a9839e95a0
2 changed files with 30 additions and 30 deletions

View File

@ -57,10 +57,10 @@ func returnOK(w http.ResponseWriter) {
} }
} }
func httpError(w *http.ResponseWriter, code int, format string, args ...interface{}) { func httpError(w http.ResponseWriter, code int, format string, args ...interface{}) {
text := fmt.Sprintf(format, args...) text := fmt.Sprintf(format, args...)
log.Println(text) log.Println(text)
http.Error(*w, text, code) http.Error(w, text, code)
} }
// --------------- // ---------------
@ -78,7 +78,7 @@ func writeAllConfigsAndReloadDNS() error {
func httpUpdateConfigReloadDNSReturnOK(w http.ResponseWriter, r *http.Request) { func httpUpdateConfigReloadDNSReturnOK(w http.ResponseWriter, r *http.Request) {
err := writeAllConfigsAndReloadDNS() err := writeAllConfigsAndReloadDNS()
if err != nil { if err != nil {
httpError(&w, http.StatusInternalServerError, "Couldn't write config file: %s", err) httpError(w, http.StatusInternalServerError, "Couldn't write config file: %s", err)
return return
} }
returnOK(w) returnOK(w)
@ -323,15 +323,15 @@ func sortByValue(m map[string]int) []string {
// ----------------------- // -----------------------
func handleSetUpstreamDNS(w http.ResponseWriter, r *http.Request) { func handleSetUpstreamDNS(w http.ResponseWriter, r *http.Request) {
setDNSServers(&w, r, true) setDNSServers(w, r, true)
} }
func handleSetBootstrapDNS(w http.ResponseWriter, r *http.Request) { func handleSetBootstrapDNS(w http.ResponseWriter, r *http.Request) {
setDNSServers(&w, r, false) setDNSServers(w, r, false)
} }
// setDNSServers sets upstream and bootstrap DNS servers // setDNSServers sets upstream and bootstrap DNS servers
func setDNSServers(w *http.ResponseWriter, r *http.Request, upstreams bool) { func setDNSServers(w http.ResponseWriter, r *http.Request, upstreams bool) {
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)
@ -381,7 +381,7 @@ func setDNSServers(w *http.ResponseWriter, r *http.Request, upstreams bool) {
httpError(w, http.StatusInternalServerError, "Couldn't reconfigure the DNS server: %s", err) httpError(w, http.StatusInternalServerError, "Couldn't reconfigure the DNS server: %s", err)
return return
} }
_, err = fmt.Fprintf(*w, "OK %d servers\n", count) _, err = fmt.Fprintf(w, "OK %d servers\n", count)
if err != nil { if err != nil {
httpError(w, http.StatusInternalServerError, "Couldn't write body: %s", err) httpError(w, http.StatusInternalServerError, "Couldn't write body: %s", err)
} }
@ -562,7 +562,7 @@ func handleFilteringAddURL(w http.ResponseWriter, r *http.Request) {
f := filter{} f := filter{}
err := json.NewDecoder(r.Body).Decode(&f) err := json.NewDecoder(r.Body).Decode(&f)
if err != nil { if err != nil {
httpError(&w, http.StatusBadRequest, "Failed to parse request body json: %s", err) httpError(w, http.StatusBadRequest, "Failed to parse request body json: %s", err)
return return
} }
@ -963,7 +963,7 @@ func handleInstallGetAddresses(w http.ResponseWriter, r *http.Request) {
ifaces, err := getValidNetInterfacesForWeb() ifaces, err := getValidNetInterfacesForWeb()
if err != nil { if err != nil {
httpError(&w, http.StatusInternalServerError, "Couldn't get interfaces: %s", err) httpError(w, http.StatusInternalServerError, "Couldn't get interfaces: %s", err)
return return
} }
@ -975,7 +975,7 @@ func handleInstallGetAddresses(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(data) err = json.NewEncoder(w).Encode(data)
if err != nil { if err != nil {
httpError(&w, http.StatusInternalServerError, "Unable to marshal default addresses to json: %s", err) httpError(w, http.StatusInternalServerError, "Unable to marshal default addresses to json: %s", err)
return return
} }
} }
@ -984,7 +984,7 @@ func handleInstallConfigure(w http.ResponseWriter, r *http.Request) {
newSettings := firstRunData{} newSettings := firstRunData{}
err := json.NewDecoder(r.Body).Decode(&newSettings) err := json.NewDecoder(r.Body).Decode(&newSettings)
if err != nil { if err != nil {
httpError(&w, http.StatusBadRequest, "Failed to parse new config json: %s", err) httpError(w, http.StatusBadRequest, "Failed to parse new config json: %s", err)
return return
} }
@ -998,14 +998,14 @@ func handleInstallConfigure(w http.ResponseWriter, r *http.Request) {
if restartHTTP { if restartHTTP {
err = checkPortAvailable(newSettings.Web.IP, newSettings.Web.Port) err = checkPortAvailable(newSettings.Web.IP, newSettings.Web.Port)
if err != nil { if err != nil {
httpError(&w, http.StatusBadRequest, "Impossible to listen on IP:port %s due to %s", net.JoinHostPort(newSettings.Web.IP, strconv.Itoa(newSettings.Web.Port)), err) httpError(w, http.StatusBadRequest, "Impossible to listen on IP:port %s due to %s", net.JoinHostPort(newSettings.Web.IP, strconv.Itoa(newSettings.Web.Port)), err)
return return
} }
} }
err = checkPacketPortAvailable(newSettings.DNS.IP, newSettings.DNS.Port) err = checkPacketPortAvailable(newSettings.DNS.IP, newSettings.DNS.Port)
if err != nil { if err != nil {
httpError(&w, http.StatusBadRequest, "Impossible to listen on IP:port %s due to %s", net.JoinHostPort(newSettings.DNS.IP, strconv.Itoa(newSettings.DNS.Port)), err) httpError(w, http.StatusBadRequest, "Impossible to listen on IP:port %s due to %s", net.JoinHostPort(newSettings.DNS.IP, strconv.Itoa(newSettings.DNS.Port)), err)
return return
} }
@ -1020,7 +1020,7 @@ func handleInstallConfigure(w http.ResponseWriter, r *http.Request) {
if config.DNS.Port != 0 { if config.DNS.Port != 0 {
err = startDNSServer() err = startDNSServer()
if err != nil { if err != nil {
httpError(&w, http.StatusInternalServerError, "Couldn't start DNS server: %s", err) httpError(w, http.StatusInternalServerError, "Couldn't start DNS server: %s", err)
return return
} }
} }
@ -1045,7 +1045,7 @@ func handleTLSStatus(w http.ResponseWriter, r *http.Request) {
func handleTLSValidate(w http.ResponseWriter, r *http.Request) { func handleTLSValidate(w http.ResponseWriter, r *http.Request) {
data, err := unmarshalTLS(r) data, err := unmarshalTLS(r)
if err != nil { if err != nil {
httpError(&w, http.StatusBadRequest, "Failed to unmarshal TLS config: %s", err) httpError(w, http.StatusBadRequest, "Failed to unmarshal TLS config: %s", err)
return return
} }
@ -1058,7 +1058,7 @@ func handleTLSValidate(w http.ResponseWriter, r *http.Request) {
if !alreadyRunning { if !alreadyRunning {
err = checkPortAvailable(config.BindHost, data.PortHTTPS) err = checkPortAvailable(config.BindHost, data.PortHTTPS)
if err != nil { if err != nil {
httpError(&w, http.StatusBadRequest, "port %d is not available, cannot enable HTTPS on it", data.PortHTTPS) httpError(w, http.StatusBadRequest, "port %d is not available, cannot enable HTTPS on it", data.PortHTTPS)
return return
} }
} }
@ -1070,7 +1070,7 @@ func handleTLSValidate(w http.ResponseWriter, r *http.Request) {
func handleTLSConfigure(w http.ResponseWriter, r *http.Request) { func handleTLSConfigure(w http.ResponseWriter, r *http.Request) {
data, err := unmarshalTLS(r) data, err := unmarshalTLS(r)
if err != nil { if err != nil {
httpError(&w, http.StatusBadRequest, "Failed to unmarshal TLS config: %s", err) httpError(w, http.StatusBadRequest, "Failed to unmarshal TLS config: %s", err)
return return
} }
@ -1083,7 +1083,7 @@ func handleTLSConfigure(w http.ResponseWriter, r *http.Request) {
if !alreadyRunning { if !alreadyRunning {
err = checkPortAvailable(config.BindHost, data.PortHTTPS) err = checkPortAvailable(config.BindHost, data.PortHTTPS)
if err != nil { if err != nil {
httpError(&w, http.StatusBadRequest, "port %d is not available, cannot enable HTTPS on it", data.PortHTTPS) httpError(w, http.StatusBadRequest, "port %d is not available, cannot enable HTTPS on it", data.PortHTTPS)
return return
} }
} }
@ -1097,7 +1097,7 @@ func handleTLSConfigure(w http.ResponseWriter, r *http.Request) {
config.TLS = data config.TLS = data
err = writeAllConfigsAndReloadDNS() err = writeAllConfigsAndReloadDNS()
if err != nil { if err != nil {
httpError(&w, http.StatusInternalServerError, "Couldn't write config file: %s", err) httpError(w, http.StatusInternalServerError, "Couldn't write config file: %s", err)
return return
} }
marshalTLS(w, data) marshalTLS(w, data)
@ -1316,7 +1316,7 @@ func marshalTLS(w http.ResponseWriter, data tlsConfig) {
} }
err := json.NewEncoder(w).Encode(data) err := json.NewEncoder(w).Encode(data)
if err != nil { if err != nil {
httpError(&w, http.StatusInternalServerError, "Failed to marshal json with TLS status: %s", err) httpError(w, http.StatusInternalServerError, "Failed to marshal json with TLS status: %s", err)
return return
} }
} }
@ -1326,12 +1326,12 @@ func marshalTLS(w http.ResponseWriter, data tlsConfig) {
// -------------- // --------------
func handleDOH(w http.ResponseWriter, r *http.Request) { func handleDOH(w http.ResponseWriter, r *http.Request) {
if r.TLS == nil { if r.TLS == nil {
httpError(&w, http.StatusNotFound, "Not Found") httpError(w, http.StatusNotFound, "Not Found")
return return
} }
if !isRunning() { if !isRunning() {
httpError(&w, http.StatusInternalServerError, "DNS server is not running") httpError(w, http.StatusInternalServerError, "DNS server is not running")
return return
} }

16
dhcp.go
View File

@ -37,7 +37,7 @@ func handleDHCPStatus(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(status) err := json.NewEncoder(w).Encode(status)
if err != nil { if err != nil {
httpError(&w, http.StatusInternalServerError, "Unable to marshal DHCP status json: %s", err) httpError(w, http.StatusInternalServerError, "Unable to marshal DHCP status json: %s", err)
return return
} }
} }
@ -46,14 +46,14 @@ func handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) {
newconfig := dhcpd.ServerConfig{} newconfig := dhcpd.ServerConfig{}
err := json.NewDecoder(r.Body).Decode(&newconfig) err := json.NewDecoder(r.Body).Decode(&newconfig)
if err != nil { if err != nil {
httpError(&w, http.StatusBadRequest, "Failed to parse new DHCP config json: %s", err) httpError(w, http.StatusBadRequest, "Failed to parse new DHCP config json: %s", err)
return return
} }
if newconfig.Enabled { if newconfig.Enabled {
err := dhcpServer.Start(&newconfig) err := dhcpServer.Start(&newconfig)
if err != nil { if err != nil {
httpError(&w, http.StatusBadRequest, "Failed to start DHCP server: %s", err) httpError(w, http.StatusBadRequest, "Failed to start DHCP server: %s", err)
return return
} }
} }
@ -72,7 +72,7 @@ func handleDHCPInterfaces(w http.ResponseWriter, r *http.Request) {
ifaces, err := getValidNetInterfaces() ifaces, err := getValidNetInterfaces()
if err != nil { if err != nil {
httpError(&w, http.StatusInternalServerError, "Couldn't get interfaces: %s", err) httpError(w, http.StatusInternalServerError, "Couldn't get interfaces: %s", err)
return return
} }
@ -87,7 +87,7 @@ func handleDHCPInterfaces(w http.ResponseWriter, r *http.Request) {
} }
addrs, err := iface.Addrs() addrs, err := iface.Addrs()
if err != nil { if err != nil {
httpError(&w, http.StatusInternalServerError, "Failed to get addresses for interface %s: %s", iface.Name, err) httpError(w, http.StatusInternalServerError, "Failed to get addresses for interface %s: %s", iface.Name, err)
return return
} }
@ -105,7 +105,7 @@ func handleDHCPInterfaces(w http.ResponseWriter, r *http.Request) {
ipnet, ok := addr.(*net.IPNet) ipnet, ok := addr.(*net.IPNet)
if !ok { if !ok {
// not an IPNet, should not happen // 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) httpError(w, http.StatusInternalServerError, "SHOULD NOT HAPPEN: got iface.Addrs() element %s that is not net.IPNet, it is %T", addr, addr)
return return
} }
// ignore link-local // ignore link-local
@ -122,7 +122,7 @@ func handleDHCPInterfaces(w http.ResponseWriter, r *http.Request) {
err = json.NewEncoder(w).Encode(response) err = json.NewEncoder(w).Encode(response)
if err != nil { if err != nil {
httpError(&w, http.StatusInternalServerError, "Failed to marshal json with available interfaces: %s", err) httpError(w, http.StatusInternalServerError, "Failed to marshal json with available interfaces: %s", err)
return return
} }
} }
@ -153,7 +153,7 @@ func handleDHCPFindActiveServer(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(result) err = json.NewEncoder(w).Encode(result)
if err != nil { if err != nil {
httpError(&w, http.StatusInternalServerError, "Failed to marshal DHCP found json: %s", err) httpError(w, http.StatusInternalServerError, "Failed to marshal DHCP found json: %s", err)
return return
} }
} }