From 535024c81c542a9b721ee1a7b8faa645e26ece60 Mon Sep 17 00:00:00 2001 From: sunshineplan Date: Wed, 25 Sep 2024 14:53:42 +0800 Subject: [PATCH] Comment and License (#106) --- LICENSE | 21 +++++++++++++++++++++ http.go | 12 +++++++++--- service/client.go | 4 ++-- service/httpproxy_test.go | 10 +++++----- service/main.go | 6 +++--- service/run.go | 4 ++-- 6 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f23230c --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 sunshineplan + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/http.go b/http.go index 55007f0..b848afb 100644 --- a/http.go +++ b/http.go @@ -1,3 +1,4 @@ +// Package httpproxy provides an HTTP proxy implementation package httpproxy import ( @@ -15,6 +16,7 @@ import ( "golang.org/x/net/proxy" ) +// Dialer represents a proxy dialer type Dialer struct { u *url.URL InsecureSkipVerify bool @@ -23,6 +25,7 @@ type Dialer struct { ProxyDial func(context.Context, string, string) (net.Conn, error) } +// New creates a new proxy Dialer func New(u *url.URL, forward proxy.Dialer) proxy.Dialer { d := &Dialer{u: u} if forward != nil { @@ -39,7 +42,8 @@ func New(u *url.URL, forward proxy.Dialer) proxy.Dialer { return d } -func (d *Dialer) DialWithConn(ctx context.Context, c net.Conn, network, address string) error { +// connect establishes a connection to the proxy server +func (d *Dialer) connect(c net.Conn, network, address string) error { switch network { case "tcp", "tcp6", "tcp4": default: @@ -74,6 +78,7 @@ func (d *Dialer) DialWithConn(ctx context.Context, c net.Conn, network, address return nil } +// Dial connects to the address on the named network using the proxy func (d *Dialer) Dial(network, address string) (conn net.Conn, err error) { switch network { case "tcp", "tcp6", "tcp4": @@ -92,13 +97,14 @@ func (d *Dialer) Dial(network, address string) (conn net.Conn, err error) { hostname, _, _ := strings.Cut(d.u.Host, ":") conn = tls.Client(conn, &tls.Config{ServerName: hostname, InsecureSkipVerify: d.InsecureSkipVerify}) } - if err = d.DialWithConn(context.Background(), conn, network, address); err != nil { + if err = d.connect(conn, network, address); err != nil { conn.Close() return nil, err } return } +// DialContext connects to the address on the named network using the proxy with the provided context func (d *Dialer) DialContext(ctx context.Context, network, address string) (conn net.Conn, err error) { switch network { case "tcp", "tcp6", "tcp4": @@ -114,7 +120,7 @@ func (d *Dialer) DialContext(ctx context.Context, network, address string) (conn if err != nil { return } - if err = d.DialWithConn(ctx, conn, network, address); err != nil { + if err = d.connect(conn, network, address); err != nil { conn.Close() return nil, err } diff --git a/service/client.go b/service/client.go index cedb6e7..da33a53 100644 --- a/service/client.go +++ b/service/client.go @@ -8,13 +8,13 @@ import ( "github.com/sunshineplan/httpproxy" "github.com/sunshineplan/limiter" - netproxy "golang.org/x/net/proxy" + "golang.org/x/net/proxy" ) type Client struct { *Base u *url.URL - proxy netproxy.Dialer + proxy proxy.Dialer } func NewClient(base *Base, u *url.URL) *Client { diff --git a/service/httpproxy_test.go b/service/httpproxy_test.go index 73636d6..60f9f5d 100644 --- a/service/httpproxy_test.go +++ b/service/httpproxy_test.go @@ -25,7 +25,7 @@ import ( "github.com/sunshineplan/httpproxy" "github.com/sunshineplan/limiter" - netproxy "golang.org/x/net/proxy" + "golang.org/x/net/proxy" ) var testHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -56,7 +56,7 @@ func newRequest(url string, m map[string]string) *http.Request { return req } -func response(proxy netproxy.Dialer, addr string, req *http.Request) (m map[string]string, err error) { +func do(proxy proxy.Dialer, addr string, req *http.Request) (m map[string]string, err error) { c, err := proxy.Dial("tcp", addr) if err != nil { return @@ -139,7 +139,7 @@ func TestProxy(t *testing.T) { time.Sleep(time.Second) u, _ := url.Parse("http://localhost:" + c.Port) - res, err := response(httpproxy.New(u, nil), addr, req) + res, err := do(httpproxy.New(u, nil), addr, req) if err != nil { t.Fatal(err) } @@ -177,7 +177,7 @@ func TestTLS(t *testing.T) { time.Sleep(time.Second) u, _ := url.Parse("http://localhost:" + c.Port) - res, err := response(httpproxy.New(u, nil), addr, req) + res, err := do(httpproxy.New(u, nil), addr, req) if err != nil { t.Fatal(err) } @@ -249,7 +249,7 @@ func TestAuth(t *testing.T) { time.Sleep(time.Second) u, _ := url.Parse(testcase.proxy + ":" + c.Port) - res, err := response(httpproxy.New(u, nil), addr, req) + res, err := do(httpproxy.New(u, nil), addr, req) if testcase.err != "" { if err == nil || !strings.Contains(err.Error(), testcase.err) { diff --git a/service/main.go b/service/main.go index f97ca09..ca4f731 100644 --- a/service/main.go +++ b/service/main.go @@ -68,9 +68,9 @@ server side: // client flags var ( - proxy = flag.String("proxy", "", "Proxy address") - username = flag.String("username", "", "Username") - password = flag.String("password", "", "Password") + proxyAddr = flag.String("proxy", "", "Proxy address") + username = flag.String("username", "", "Username") + password = flag.String("password", "", "Password") ) const clientFlag = ` diff --git a/service/run.go b/service/run.go index f2e4790..f87f19e 100644 --- a/service/run.go +++ b/service/run.go @@ -28,7 +28,7 @@ func run() error { if base.Port == "" { base.Port = "8080" } - runner = NewClient(base, parseProxy(*proxy)).SetProxyAuth(*username, *password) + runner = NewClient(base, parseProxy(*proxyAddr)).SetProxyAuth(*username, *password) default: return errors.New("unknow mode: " + mode) } @@ -66,7 +66,7 @@ func test() error { l.Close() if strings.ToLower(*mode) == "client" { - if _, err := url.Parse(*proxy); err != nil { + if _, err := url.Parse(*proxyAddr); err != nil { return err } }