From 3d77f1c2f6a3440b3d7b5a7f7f216281b1e64352 Mon Sep 17 00:00:00 2001 From: sunshineplan Date: Wed, 25 Sep 2024 15:06:32 +0800 Subject: [PATCH 1/2] Remove httpproxy --- .github/dependabot.yml | 5 -- .github/workflows/dependabot.yml | 46 ----------- .github/workflows/test.yml | 2 - httpproxy/direct.go | 27 ------- httpproxy/go.mod | 5 -- httpproxy/go.sum | 2 - httpproxy/httpproxy.go | 129 ------------------------------- 7 files changed, 216 deletions(-) delete mode 100644 .github/workflows/dependabot.yml delete mode 100644 httpproxy/direct.go delete mode 100644 httpproxy/go.mod delete mode 100644 httpproxy/go.sum delete mode 100644 httpproxy/httpproxy.go diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 04e8fdd..1230149 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -1,10 +1,5 @@ version: 2 updates: - - package-ecosystem: "gomod" - directory: "/httpproxy" - schedule: - interval: "daily" - - package-ecosystem: "github-actions" directory: "/" schedule: diff --git a/.github/workflows/dependabot.yml b/.github/workflows/dependabot.yml deleted file mode 100644 index 50d4c21..0000000 --- a/.github/workflows/dependabot.yml +++ /dev/null @@ -1,46 +0,0 @@ -name: Dependabot - -on: - pull_request_target: - -jobs: - test: - if: ${{ github.actor == 'dependabot[bot]' }} - runs-on: ${{ matrix.os }} - strategy: - matrix: - os: [ windows-latest, ubuntu-latest, macos-latest ] - steps: - - name: Checkout Code - uses: actions/checkout@v4 - with: - ref: ${{ github.event.pull_request.head.sha }} - - - name: Setup Go - uses: actions/setup-go@v5 - with: - go-version: stable - - - name: Test Code - run: | - cd httpproxy - go build -v ./... - - merge: - if: ${{ github.actor == 'dependabot[bot]' }} - runs-on: ubuntu-latest - needs: test - permissions: - pull-requests: write - contents: write - steps: - - uses: actions/checkout@v4 - - uses: nick-invision/retry@v3 - with: - timeout_minutes: 60 - max_attempts: 5 - retry_wait_seconds: 60 - retry_on: error - command: gh pr merge --auto --squash ${{ github.event.pull_request.html_url }} - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1e5560a..6534ba3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -26,5 +26,3 @@ jobs: run: | go build -v ./... go test -v -race ./... - cd httpproxy - go build -v ./... diff --git a/httpproxy/direct.go b/httpproxy/direct.go deleted file mode 100644 index 47edee5..0000000 --- a/httpproxy/direct.go +++ /dev/null @@ -1,27 +0,0 @@ -package httpproxy - -import ( - "context" - "net" -) - -type direct struct{} - -// Direct implements Dialer by making network connections directly using net.Dial or net.DialContext. -var Direct = direct{} - -var ( - _ Dialer = Direct - _ ContextDialer = Direct -) - -// Dial directly invokes net.Dial with the supplied parameters. -func (direct) Dial(network, addr string) (net.Conn, error) { - return net.Dial(network, addr) -} - -// DialContext instantiates a net.Dialer and invokes its DialContext receiver with the supplied parameters. -func (direct) DialContext(ctx context.Context, network, addr string) (net.Conn, error) { - var d net.Dialer - return d.DialContext(ctx, network, addr) -} diff --git a/httpproxy/go.mod b/httpproxy/go.mod deleted file mode 100644 index 8423c82..0000000 --- a/httpproxy/go.mod +++ /dev/null @@ -1,5 +0,0 @@ -module github.com/sunshineplan/utils/httpproxy - -go 1.22 - -require golang.org/x/net v0.29.0 diff --git a/httpproxy/go.sum b/httpproxy/go.sum deleted file mode 100644 index 8a0e83a..0000000 --- a/httpproxy/go.sum +++ /dev/null @@ -1,2 +0,0 @@ -golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= -golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= diff --git a/httpproxy/httpproxy.go b/httpproxy/httpproxy.go deleted file mode 100644 index 935278d..0000000 --- a/httpproxy/httpproxy.go +++ /dev/null @@ -1,129 +0,0 @@ -package httpproxy - -import ( - "bufio" - "context" - "crypto/tls" - "encoding/base64" - "errors" - "net" - "net/http" - "net/url" - "strings" - - "golang.org/x/net/proxy" -) - -// A Dialer is a means to establish a connection. -type Dialer interface { - // Dial connects to the given address via the proxy. - Dial(network, addr string) (c net.Conn, err error) -} - -// A ContextDialer dials using a context. -type ContextDialer interface { - DialContext(ctx context.Context, network, address string) (net.Conn, error) -} - -type Proxy struct { - *url.URL - forward Dialer -} - -func New(u *url.URL, forward Dialer) *Proxy { - p := &Proxy{URL: u} - if forward == nil { - forward = Direct - } - p.forward = forward - - return p -} - -func NewDialer(u *url.URL, forward Dialer) (Dialer, error) { - return New(u, forward), nil -} - -func NewProxyDialer(u *url.URL, forward proxy.Dialer) (proxy.Dialer, error) { - return NewDialer(u, forward) -} - -func (p *Proxy) dialForward() (net.Conn, error) { - addr := p.Host - - conn, err := p.forward.Dial("tcp", addr) - if err != nil { - return nil, err - } - if p.Scheme == "https" { - colonPos := strings.LastIndex(addr, ":") - if colonPos == -1 { - colonPos = len(addr) - } - hostname := addr[:colonPos] - - conn = tls.Client(conn, &tls.Config{ServerName: hostname}) - } - - return conn, nil -} - -func (p *Proxy) DialWithHeader(addr string, header http.Header) (net.Conn, *http.Response, error) { - conn, err := p.dialForward() - if err != nil { - return nil, nil, err - } - - req := &http.Request{ - Method: "CONNECT", - URL: &url.URL{Opaque: addr}, - Host: addr, - Header: header, - } - if err := req.Write(conn); err != nil { - conn.Close() - return nil, nil, err - } - - br := bufio.NewReader(conn) - resp, err := http.ReadResponse(br, req) - if err != nil { - conn.Close() - return nil, nil, err - } - - return conn, resp, nil -} - -func (p *Proxy) Dial(network, addr string) (net.Conn, error) { - if network != "tcp" { - return nil, errors.New("network must be tcp") - } - - header := make(http.Header) - if p.User != nil { - password, _ := p.User.Password() - header.Set("Proxy-Authorization", "Basic "+basicAuth(p.User.Username(), password)) - } - - conn, resp, err := p.DialWithHeader(addr, header) - if err != nil { - return nil, err - } - if resp.StatusCode != 200 { - conn.Close() - return nil, errors.New(resp.Status) - } - - return conn, nil -} - -func basicAuth(username, password string) string { - auth := username + ":" + password - return base64.StdEncoding.EncodeToString([]byte(auth)) -} - -func init() { - proxy.RegisterDialerType("http", NewProxyDialer) - proxy.RegisterDialerType("https", NewProxyDialer) -} From e7799d217afef921c4aa27971761c01ca340bf66 Mon Sep 17 00:00:00 2001 From: sunshineplan Date: Wed, 25 Sep 2024 15:07:24 +0800 Subject: [PATCH 2/2] Add License --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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