Skip to content

Commit

Permalink
Comment and License (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshineplan authored Sep 25, 2024
1 parent 7dd084f commit 535024c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 15 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 9 additions & 3 deletions http.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package httpproxy provides an HTTP proxy implementation
package httpproxy

import (
Expand All @@ -15,6 +16,7 @@ import (
"golang.org/x/net/proxy"
)

// Dialer represents a proxy dialer
type Dialer struct {
u *url.URL
InsecureSkipVerify bool
Expand All @@ -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 {
Expand All @@ -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:
Expand Down Expand Up @@ -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":
Expand All @@ -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":
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions service/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions service/httpproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand Down
4 changes: 2 additions & 2 deletions service/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit 535024c

Please sign in to comment.