Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ioutil & magic numbers #571

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package goproxy

import (
"bytes"
"io/ioutil"
"io"
"net"
"net/http"
"regexp"
Expand Down Expand Up @@ -189,6 +189,7 @@ func StatusCodeIs(codes ...int) RespCondition {
// You will use the ReqProxyConds struct to register a ReqHandler, that would filter
// the request, only if all the given ReqCondition matched.
// Typical usage:
//
// proxy.OnRequest(UrlIs("example.com/foo"),UrlMatches(regexp.MustParse(`.*\.exampl.\com\./.*`)).Do(...)
func (proxy *ProxyHttpServer) OnRequest(conds ...ReqCondition) *ReqProxyConds {
return &ReqProxyConds{proxy, conds}
Expand All @@ -209,6 +210,7 @@ func (pcond *ReqProxyConds) DoFunc(f func(req *http.Request, ctx *ProxyCtx) (*ht
// ReqProxyConds.Do will register the ReqHandler on the proxy,
// the ReqHandler will handle the HTTP request if all the conditions
// aggregated in the ReqProxyConds are met. Typical usage:
//
// proxy.OnRequest().Do(handler) // will call handler.Handle(req,ctx) on every request to the proxy
// proxy.OnRequest(cond1,cond2).Do(handler)
// // given request to the proxy, will test if cond1.HandleReq(req,ctx) && cond2.HandleReq(req,ctx) are true
Expand All @@ -235,6 +237,7 @@ func (pcond *ReqProxyConds) Do(h ReqHandler) {
// connection.
// The ConnectAction struct contains possible tlsConfig that will be used for eavesdropping. If nil, the proxy
// will use the default tls configuration.
//
// proxy.OnRequest().HandleConnect(goproxy.AlwaysReject) // rejects all CONNECT requests
func (pcond *ReqProxyConds) HandleConnect(h HttpsHandler) {
pcond.proxy.httpsHandlers = append(pcond.proxy.httpsHandlers,
Expand All @@ -250,6 +253,7 @@ func (pcond *ReqProxyConds) HandleConnect(h HttpsHandler) {

// HandleConnectFunc is equivalent to HandleConnect,
// for example, accepting CONNECT request if they contain a password in header
//
// io.WriteString(h,password)
// passHash := h.Sum(nil)
// proxy.OnRequest().HandleConnectFunc(func(host string, ctx *ProxyCtx) (*ConnectAction, string) {
Expand Down Expand Up @@ -310,6 +314,7 @@ func (pcond *ProxyConds) Do(h RespHandler) {
}

// OnResponse is used when adding a response-filter to the HTTP proxy, usual pattern is
//
// proxy.OnResponse(cond1,cond2).Do(handler) // handler.Handle(resp,ctx) will be used
// // if cond1.HandleResp(resp) && cond2.HandleResp(resp)
func (proxy *ProxyHttpServer) OnResponse(conds ...RespCondition) *ProxyConds {
Expand All @@ -318,13 +323,15 @@ func (proxy *ProxyHttpServer) OnResponse(conds ...RespCondition) *ProxyConds {

// AlwaysMitm is a HttpsHandler that always eavesdrop https connections, for example to
// eavesdrop all https connections to www.google.com, we can use
//
// proxy.OnRequest(goproxy.ReqHostIs("www.google.com")).HandleConnect(goproxy.AlwaysMitm)
var AlwaysMitm FuncHttpsHandler = func(host string, ctx *ProxyCtx) (*ConnectAction, string) {
return MitmConnect, host
}

// AlwaysReject is a HttpsHandler that drops any CONNECT request, for example, this code will disallow
// connections to hosts on any other port than 443
//
// proxy.OnRequest(goproxy.Not(goproxy.ReqHostMatches(regexp.MustCompile(":443$"))).
// HandleConnect(goproxy.AlwaysReject)
var AlwaysReject FuncHttpsHandler = func(host string, ctx *ProxyCtx) (*ConnectAction, string) {
Expand All @@ -336,14 +343,14 @@ var AlwaysReject FuncHttpsHandler = func(host string, ctx *ProxyCtx) (*ConnectAc
// and will replace the body of the original response with the resulting byte array.
func HandleBytes(f func(b []byte, ctx *ProxyCtx) []byte) RespHandler {
return FuncRespHandler(func(resp *http.Response, ctx *ProxyCtx) *http.Response {
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
if err != nil {
ctx.Warnf("Cannot read response %s", err)
return resp
}
resp.Body.Close()

resp.Body = ioutil.NopCloser(bytes.NewBuffer(f(b, ctx)))
resp.Body = io.NopCloser(bytes.NewBuffer(f(b, ctx)))
return resp
})
}
2 changes: 1 addition & 1 deletion dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestIsLocalHost(t *testing.T) {
addr = net.JoinHostPort(host, port)
}
t.Run(addr, func(t *testing.T) {
req, err := http.NewRequest("GET", "http://"+addr, http.NoBody)
req, err := http.NewRequest(http.MethodGet, "http://"+addr, http.NoBody)
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions examples/cascadeproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"encoding/base64"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -82,7 +82,7 @@ func main() {

// fire a http request: client --> middle proxy --> end proxy --> internet
proxyUrl := "http://localhost:8081"
request, err := http.NewRequest("GET", "https://ip.cn", nil)
request, err := http.NewRequest(http.MethodGet, "https://ip.cn", nil)
if err != nil {
log.Fatalf("new request failed:%v", err)
}
Expand All @@ -94,7 +94,7 @@ func main() {

}
defer rsp.Body.Close()
data, _ := ioutil.ReadAll(rsp.Body)
data, _ := io.ReadAll(rsp.Body)

if rsp.StatusCode != http.StatusOK {
log.Fatalf("status %d, data %s", rsp.StatusCode, data)
Expand Down
7 changes: 4 additions & 3 deletions examples/goproxy-jquery-version/jquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package main

import (
"bytes"
"io/ioutil"
"io"
"log"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"
)
Expand All @@ -24,7 +25,7 @@ func equal(u, v []string) bool {
}

func readFile(fname string, t *testing.T) string {
b, err := ioutil.ReadFile(fname)
b, err := os.ReadFile(fname)
if err != nil {
t.Fatal("readFile", err)
}
Expand Down Expand Up @@ -77,7 +78,7 @@ func get(t *testing.T, server *httptest.Server, client *http.Client, url string)
if err != nil {
t.Fatal("cannot get proxy", err)
}
ioutil.ReadAll(resp.Body)
io.ReadAll(resp.Body)
resp.Body.Close()
}

Expand Down
2 changes: 1 addition & 1 deletion examples/goproxy-transparent/transparent.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func main() {
return
}
connectReq := &http.Request{
Method: "CONNECT",
Method: http.MethodConnect,
URL: &url.URL{
Opaque: tlsConn.Host(),
Host: net.JoinHostPort(tlsConn.Host(), "443"),
Expand Down
19 changes: 9 additions & 10 deletions examples/goproxy-yui-minify/yui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
// using the command line utility YUI compressor http://yui.github.io/yuicompressor/
// Example usage:
//
// ./yui -java /usr/local/bin/java -yuicompressor ~/Downloads/yuicompressor-2.4.8.jar
// $ curl -vx localhost:8080 http://golang.org/lib/godoc/godocs.js
// (function(){function g(){var u=$("#search");if(u.length===0){return}function t(){if(....
// $ curl http://golang.org/lib/godoc/godocs.js | head -n 3
// // Copyright 2012 The Go Authors. All rights reserved.
// // Use of this source code is governed by a BSD-style
// // license that can be found in the LICENSE file.
// ./yui -java /usr/local/bin/java -yuicompressor ~/Downloads/yuicompressor-2.4.8.jar
// $ curl -vx localhost:8080 http://golang.org/lib/godoc/godocs.js
// (function(){function g(){var u=$("#search");if(u.length===0){return}function t(){if(....
// $ curl http://golang.org/lib/godoc/godocs.js | head -n 3
// // Copyright 2012 The Go Authors. All rights reserved.
// // Use of this source code is governed by a BSD-style
// // license that can be found in the LICENSE file.
package main

import (
"flag"
"io"
"io/ioutil"
"log"
"net/http"
"os"
Expand All @@ -33,7 +32,7 @@ func main() {
yuicompressordir := flag.String("yuicompressordir", ".", "a folder to search yuicompressor in, will be ignored if yuicompressor is set")
flag.Parse()
if *yuicompressor == "" {
files, err := ioutil.ReadDir(*yuicompressordir)
files, err := os.ReadDir(*yuicompressordir)
if err != nil {
log.Fatal("Cannot find yuicompressor jar")
}
Expand Down Expand Up @@ -76,7 +75,7 @@ func main() {
go func() {
defer stderr.Close()
const kb = 1024
msg, err := ioutil.ReadAll(&io.LimitedReader{stderr, 50 * kb})
msg, err := io.ReadAll(&io.LimitedReader{stderr, 50 * kb})
if len(msg) != 0 {
ctx.Logf("Error executing yuicompress: %s", string(msg))
}
Expand Down
6 changes: 3 additions & 3 deletions ext/auth/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package auth
import (
"bytes"
"encoding/base64"
"io/ioutil"
"io"
"net/http"
"strings"

Expand All @@ -15,15 +15,15 @@ var unauthorizedMsg = []byte("407 Proxy Authentication Required")
func BasicUnauthorized(req *http.Request, realm string) *http.Response {
// TODO(elazar): verify realm is well formed
return &http.Response{
StatusCode: 407,
StatusCode: http.StatusProxyAuthRequired,
ProtoMajor: 1,
ProtoMinor: 1,
Request: req,
Header: http.Header{
"Proxy-Authenticate": []string{"Basic realm=" + realm},
"Proxy-Connection": []string{"close"},
},
Body: ioutil.NopCloser(bytes.NewBuffer(unauthorizedMsg)),
Body: io.NopCloser(bytes.NewBuffer(unauthorizedMsg)),
ContentLength: int64(len(unauthorizedMsg)),
}
}
Expand Down
9 changes: 4 additions & 5 deletions ext/auth/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package auth_test
import (
"encoding/base64"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -115,12 +114,12 @@ func TestBasicAuth(t *testing.T) {
if resp.Header.Get("Proxy-Authenticate") != "Basic realm=my_realm" {
t.Error("Expected Proxy-Authenticate header got", resp.Header.Get("Proxy-Authenticate"))
}
if resp.StatusCode != 407 {
if resp.StatusCode != http.StatusProxyAuthRequired {
t.Error("Expected status 407 Proxy Authentication Required, got", resp.Status)
}

// with auth
req, err := http.NewRequest("GET", background.URL, nil)
req, err := http.NewRequest(http.MethodGet, background.URL, nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -130,10 +129,10 @@ func TestBasicAuth(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
if resp.StatusCode != http.StatusOK {
t.Error("Expected status 200 OK, got", resp.Status)
}
msg, err := ioutil.ReadAll(resp.Body)
msg, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
Expand Down
7 changes: 3 additions & 4 deletions ext/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"errors"
"io"
"io/ioutil"
"net/http"
"strings"

Expand Down Expand Up @@ -37,7 +36,7 @@ var IsWebRelatedText goproxy.RespCondition = goproxy.ContentTypeIs("text/html",
// guessing Html charset encoding from the <META> tags is not yet implemented.
func HandleString(f func(s string, ctx *goproxy.ProxyCtx) string) goproxy.RespHandler {
return HandleStringReader(func(r io.Reader, ctx *goproxy.ProxyCtx) io.Reader {
b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
if err != nil {
ctx.Warnf("Cannot read string from resp body: %v", err)
return r
Expand Down Expand Up @@ -74,10 +73,10 @@ func HandleStringReader(f func(r io.Reader, ctx *goproxy.ProxyCtx) io.Reader) go
return resp
}
newr := charset.NewTranslatingReader(f(r, ctx), tr)
resp.Body = &readFirstCloseBoth{ioutil.NopCloser(newr), resp.Body}
resp.Body = &readFirstCloseBoth{io.NopCloser(newr), resp.Body}
} else {
//no translation is needed, already at utf-8
resp.Body = &readFirstCloseBoth{ioutil.NopCloser(f(resp.Body, ctx)), resp.Body}
resp.Body = &readFirstCloseBoth{io.NopCloser(f(resp.Body, ctx)), resp.Body}
}
return resp
})
Expand Down
4 changes: 2 additions & 2 deletions ext/html/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package goproxy_html_test
import (
"github.com/elazarl/goproxy"
"github.com/elazarl/goproxy/ext/html"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -39,7 +39,7 @@ func TestCharset(t *testing.T) {
if err != nil {
t.Fatal("GET:", err)
}
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal("readAll:", err)
}
Expand Down
6 changes: 3 additions & 3 deletions ext/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
_ "image/gif"
"image/jpeg"
"image/png"
"io/ioutil"
"io"
"net/http"
)

Expand All @@ -25,7 +25,7 @@ func HandleImage(f func(img image.Image, ctx *ProxyCtx) image.Image) RespHandler
if !RespIsImage.HandleResp(resp, ctx) {
return resp
}
if resp.StatusCode != 200 {
if resp.StatusCode != http.StatusOK {
// we might get 304 - not modified response without data
return resp
}
Expand Down Expand Up @@ -72,7 +72,7 @@ func HandleImage(f func(img image.Image, ctx *ProxyCtx) image.Image) RespHandler
default:
panic("unhandlable type" + contentType)
}
resp.Body = ioutil.NopCloser(buf)
resp.Body = io.NopCloser(buf)
return resp
})
}
Loading
Loading