From: Sergey Matveev Date: Wed, 15 Mar 2023 15:21:35 +0000 (+0300) Subject: More Fprintf usage X-Git-Tag: v0.1.0~9 X-Git-Url: http://www.git.stargrave.org/?p=tofuproxy.git;a=commitdiff_plain;h=c1f841e8a5e4c30710cdb2e57fd87cba9d79b97b More Fprintf usage --- diff --git a/fifos/list.go b/fifos/list.go index ff07708..80bfb14 100644 --- a/fifos/list.go +++ b/fifos/list.go @@ -36,7 +36,7 @@ func list(l *sync.RWMutex, m map[string]string, p string) { } l.RLock() for host, hsh := range m { - if _, err = fd.WriteString(fmt.Sprintf("%s\t%s\n", host, hsh)); err != nil { + if _, err = fmt.Fprintf(fd, "%s\t%s\n", host, hsh); err != nil { break } } @@ -61,7 +61,7 @@ func listHTTPAuth(p string) { } caches.HTTPAuthCacheM.RLock() for host, creds := range caches.HTTPAuthCache { - if _, err = fd.WriteString(fmt.Sprintf("%s\t%s\n", host, creds[0])); err != nil { + if _, err = fmt.Fprintf(fd, "%s\t%s\n", host, creds[0]); err != nil { break } } @@ -86,7 +86,7 @@ func listTLSAuth(p string) { } subj = cert.Subject.String() } - if _, err = fd.WriteString(fmt.Sprintf("%s\t%s\n", host, subj)); err != nil { + if _, err = fmt.Fprintf(fd, "%s\t%s\n", host, subj); err != nil { break } } diff --git a/httpauth.go b/httpauth.go index 9408d98..92eff6e 100644 --- a/httpauth.go +++ b/httpauth.go @@ -31,7 +31,7 @@ import ( func authDialog(host, realm string) (string, string, error) { var b bytes.Buffer userInit, passInit := findInNetrc(host) - b.WriteString(fmt.Sprintf(` + fmt.Fprintf(&b, ` tk_setPalette grey wm title . "Unauthorized: %s" @@ -59,7 +59,7 @@ bind . {switch -exact %%K { q {exit 0} ; # reject once l login }} -`, strings.ReplaceAll(realm, "\"", ""), userInit, passInit)) +`, strings.ReplaceAll(realm, "\"", ""), userInit, passInit) cmd := exec.Command(ttls.CmdWish) cmd.Stdin = &b out, err := cmd.Output() diff --git a/rounds/gemini.go b/rounds/gemini.go index 5c831d4..d81039f 100644 --- a/rounds/gemini.go +++ b/rounds/gemini.go @@ -151,13 +151,9 @@ func RoundGemini( w.Header().Add("Content-Type", "text/html") w.WriteHeader(http.StatusOK) u := geminifyURL(host, cols[1], paths...) - w.Write([]byte( - fmt.Sprintf( - ` + fmt.Fprintf(w, ` %d (%s) redirection -%s`, - code, codeName, u, u, - ))) +%s`, code, codeName, u, u) fifos.LogRedir <- fmt.Sprintf( "%s %s\t%d\t%s", req.Method, req.URL, code, cols[1], ) diff --git a/rounds/redirectHTML.go b/rounds/redirectHTML.go index abc657e..d139eb2 100644 --- a/rounds/redirectHTML.go +++ b/rounds/redirectHTML.go @@ -82,14 +82,13 @@ func RoundRedirectHTML( w.Header().Add("Content-Type", "text/html") w.WriteHeader(http.StatusOK) location := resp.Header.Get("Location") - w.Write([]byte( - fmt.Sprintf( - ` + fmt.Fprintf( + w, ` %d %s: %s redirection %s`, - resp.StatusCode, http.StatusText(resp.StatusCode), - redirType, location, location, - ))) + resp.StatusCode, http.StatusText(resp.StatusCode), + redirType, location, location, + ) fifos.LogRedir <- fmt.Sprintf( "%s %s\t%s\t%s", req.Method, req.URL, resp.Status, location, ) diff --git a/rounds/transcodeWebP.go b/rounds/transcodeWebP.go index 95b409a..016c617 100644 --- a/rounds/transcodeWebP.go +++ b/rounds/transcodeWebP.go @@ -65,6 +65,8 @@ func RoundTranscodeWebP( w.Header().Add("Content-Type", "image/png") w.WriteHeader(http.StatusOK) w.Write(data) - fifos.LogVarious <- fmt.Sprintf("%s %s\tWebP transcoded to PNG", req.Method, req.URL) + fifos.LogVarious <- fmt.Sprintf( + "%s %s\tWebP transcoded to PNG", req.Method, req.URL, + ) return false, nil } diff --git a/rounds/warc.go b/rounds/warc.go index 81f5e44..2d0ea7f 100644 --- a/rounds/warc.go +++ b/rounds/warc.go @@ -124,7 +124,9 @@ func RoundWARC( if rec == nil { if WARCOnly { http.NotFound(w, req) - fifos.LogNonOK <- fmt.Sprintf("%s %s\tnot in WARC", req.Method, req.URL) + fifos.LogNonOK <- fmt.Sprintf( + "%s %s\tnot in WARC", req.Method, req.URL, + ) return false, nil } return true, nil diff --git a/tls.go b/tls.go index b86f4bf..ef43964 100644 --- a/tls.go +++ b/tls.go @@ -53,11 +53,10 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) { log.Fatalln(err) } defer conn.Close() - conn.Write([]byte(fmt.Sprintf( - "%s %d %s\r\n\r\n", - req.Proto, - http.StatusOK, http.StatusText(http.StatusOK), - ))) + fmt.Fprintf( + conn, "%s %d %s\r\n\r\n", + req.Proto, http.StatusOK, http.StatusText(http.StatusOK), + ) host, _, _ := ttls.SplitHostPort(req.Host) hostCertsM.Lock() keypair, ok := hostCerts[host] diff --git a/tls/tlsauth.go b/tls/tlsauth.go index cb8fe98..bca76ef 100644 --- a/tls/tlsauth.go +++ b/tls/tlsauth.go @@ -57,7 +57,7 @@ func (g *ClientCertificateGetter) get( sigSchemes = append(sigSchemes, ss.String()) } var b bytes.Buffer - b.WriteString(fmt.Sprintf(` + fmt.Fprintf(&b, ` tk_setPalette grey wm title . "TLS client authentication: %s" @@ -94,7 +94,7 @@ foreach sigScheme {%s} { g.host, ucspi.TLSVersion(cri.Version), strings.Join(sigSchemes, " "), - )) + ) ents, err := os.ReadDir(CCerts) if err != nil { @@ -120,7 +120,7 @@ foreach sigScheme {%s} { Certificate: [][]byte{cert.Raw}, PrivateKey: prv, }) - b.WriteString(fmt.Sprintf(".lb insert end \"%d: %s\"\n", i+1, cert.Subject)) + fmt.Fprintf(&b, ".lb insert end \"%d: %s\"\n", i+1, cert.Subject) } // os.WriteFile("/tmp/tls-auth-dialog.tcl", b.Bytes(), 0666) cmd := exec.Command(CmdWish) diff --git a/tls/verify.go b/tls/verify.go index b7a317d..b8b515a 100644 --- a/tls/verify.go +++ b/tls/verify.go @@ -246,11 +246,11 @@ HostIsNotRestricted: return nil } var b bytes.Buffer - b.WriteString(fmt.Sprintf("set host \"%s\"\n", host)) + fmt.Fprintf(&b, "set host \"%s\"\n", host) if dialErr == nil { - b.WriteString(fmt.Sprintf("set err \"\"\n")) + fmt.Fprintf(&b, "set err \"\"\n") } else { - b.WriteString(fmt.Sprintf("set err \"%s\"\n", dialErr.Error())) + fmt.Fprintf(&b, "set err \"%s\"\n", dialErr.Error()) } var daneStatus string if daneExists { @@ -260,21 +260,17 @@ HostIsNotRestricted: daneStatus = "bad" } } - b.WriteString(fmt.Sprintf("set daneStatus \"%s\"\n", daneStatus)) + fmt.Fprintf(&b, "set daneStatus \"%s\"\n", daneStatus) hexCerts := make([]string, 0, len(rawCerts)) for _, rawCert := range rawCerts { hexCerts = append(hexCerts, hex.EncodeToString([]byte(certInfo(rawCert)))) } - b.WriteString(fmt.Sprintf( - "set certsTheir \"%s\"\n", strings.Join(hexCerts, " "), - )) + fmt.Fprintf(&b, "set certsTheir \"%s\"\n", strings.Join(hexCerts, " ")) hexCerts = make([]string, 0, len(certsOur)) for _, cert := range certsOur { hexCerts = append(hexCerts, hex.EncodeToString([]byte(certInfo(cert.Raw)))) } - b.WriteString(fmt.Sprintf( - "set certsOur \"%s\"\n", strings.Join(hexCerts, " "), - )) + fmt.Fprintf(&b, "set certsOur \"%s\"\n", strings.Join(hexCerts, " ")) b.WriteString(VerifyDialog) cmd := exec.Command(CmdWish) // os.WriteFile("/tmp/verify-dialog.tcl", b.Bytes(), 0666)