Skip to content

Commit

Permalink
Support dial via no_proxy and only_proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Nov 18, 2021
1 parent 4075bc8 commit 19ba391
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 5 deletions.
4 changes: 2 additions & 2 deletions chain/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ func BridgeWithConfig(ctx context.Context, chain config.Chain, d bool) error {
dials := chain.Proxy[1:]

if len(dials) != 0 {
b, err := Default.BridgeChainWithConfig(local.LOCAL, dials...)
d, err := Default.BridgeChainWithConfig(local.LOCAL, dials...)
if err != nil {
return err
}
dialer = b
dialer = d
}

// No listener is set, use stdio.
Expand Down
23 changes: 20 additions & 3 deletions chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ import (

// BridgeChain is a bridger that supports multiple crossing of bridger.
type BridgeChain struct {
proto map[string]bridge.Bridger
DialerFunc func(dialer bridge.Dialer) bridge.Dialer
proto map[string]bridge.Bridger
}

// NewBridgeChain create a new BridgeChain.
func NewBridgeChain() *BridgeChain {
return &BridgeChain{map[string]bridge.Bridger{}}
return &BridgeChain{
proto: map[string]bridge.Bridger{},
DialerFunc: NewEnvDialer,
}
}

// BridgeChain is multiple crossing of bridge.
Expand All @@ -42,6 +46,19 @@ func (b *BridgeChain) BridgeChain(dialer bridge.Dialer, addresses ...string) (br

// BridgeChainWithConfig is multiple crossing of bridge.
func (b *BridgeChain) BridgeChainWithConfig(dialer bridge.Dialer, addresses ...config.Node) (bridge.Dialer, error) {
if len(addresses) == 0 {
return dialer, nil
}
d, err := b.bridgeChainWithConfig(dialer, addresses...)
if err != nil {
return nil, err
}
if b.DialerFunc != nil {
d = b.DialerFunc(d)
}
return d, nil
}
func (b *BridgeChain) bridgeChainWithConfig(dialer bridge.Dialer, addresses ...config.Node) (bridge.Dialer, error) {
if len(addresses) == 0 {
return dialer, nil
}
Expand All @@ -54,7 +71,7 @@ func (b *BridgeChain) BridgeChainWithConfig(dialer bridge.Dialer, addresses ...c
if len(addresses) == 0 {
return d, nil
}
return b.BridgeChainWithConfig(d, addresses...)
return b.bridgeChainWithConfig(d, addresses...)
}

func (b *BridgeChain) Dial(dialer bridge.Dialer, addresses []string, probeUrl string) (bridge.Dialer, error) {
Expand Down
87 changes: 87 additions & 0 deletions chain/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package chain

import (
"context"
"net"
"os"
"strings"

"github.com/wzshiming/bridge"
"github.com/wzshiming/bridge/protocols/local"
"github.com/wzshiming/hostmatcher"
)

var (
NoProxy hostmatcher.Matcher
OnlyProxy hostmatcher.Matcher
)

func init() {
noProxy, ok := os.LookupEnv("no_proxy")
if !ok {
noProxy, ok = os.LookupEnv("NO_PROXY")
}
if ok && noProxy != "" {
list := strings.Split(noProxy, ",")
if len(list) != 0 {
NoProxy = hostmatcher.NewMatcher(list)
}
}

onlyProxy, ok := os.LookupEnv("only_proxy")
if !ok {
onlyProxy, ok = os.LookupEnv("ONLY_PROXY")
}
if ok && onlyProxy != "" {
list := strings.Split(onlyProxy, ",")
if len(list) != 0 {
OnlyProxy = hostmatcher.NewMatcher(list)
}
}
}

func NewEnvDialer(dialer bridge.Dialer) bridge.Dialer {
if OnlyProxy == nil && NoProxy == nil {
return dialer
}
if OnlyProxy != nil {
dialer = NewShuntDialer(local.LOCAL, dialer, OnlyProxy)
}
if NoProxy != nil {
dialer = NewShuntDialer(dialer, local.LOCAL, NoProxy)
}
if l, ok := dialer.(bridge.ListenConfig); ok {
return struct {
bridge.Dialer
bridge.ListenConfig
}{
dialer,
l,
}
}
return dialer
}

type shuntDialer struct {
dialer bridge.Dialer
matchDialer bridge.Dialer
matcher hostmatcher.Matcher
}

func NewShuntDialer(dialer bridge.Dialer, matchDialer bridge.Dialer, matcher hostmatcher.Matcher) bridge.Dialer {
if matcher == nil || matchDialer == nil {
return dialer
}
return &shuntDialer{
dialer: dialer,
matchDialer: matchDialer,
matcher: matcher,
}
}

func (s *shuntDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
if s.matcher.Match(address) {
return s.matchDialer.DialContext(ctx, network, address)
}
return s.dialer.DialContext(ctx, network, address)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (

require (
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
github.com/wzshiming/hostmatcher v0.0.1 // indirect
github.com/wzshiming/sshd v0.1.0 // indirect
github.com/wzshiming/trie v0.0.1 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/wzshiming/commandproxy v0.2.0 h1:uPVhgIj2YSncRUo6g9smGR6OMzsIg7lwklcM
github.com/wzshiming/commandproxy v0.2.0/go.mod h1:wS6+aJ9KMHciqYX3xmDO0W+QVY0zvngeBvmoIFMfq8A=
github.com/wzshiming/emux v0.1.4 h1:oZzViIVdFrFuKto3Ii2bqvZT/X4GRqvrrZEYAoYxLhQ=
github.com/wzshiming/emux v0.1.4/go.mod h1:VQF6NoR4nfm3+OrKZLx47JuxuDeWemHDc0a4qDNtFtg=
github.com/wzshiming/hostmatcher v0.0.1 h1:vPrUEXsd+khvN5W7I4rJ73vYaUvnBCodvtuyCU+q1HQ=
github.com/wzshiming/hostmatcher v0.0.1/go.mod h1:3EyfIDuzQyo0tqrCtcUkD5UvAsdxOUb/RbcYOiPTh1E=
github.com/wzshiming/httpproxy v0.4.1 h1:6K78q0Vi+/GM4h9KygTGi7nuVn5Xgk5oHqjJyaGLHcQ=
github.com/wzshiming/httpproxy v0.4.1/go.mod h1:drZR+iQLfYsQi7/nETLjyB9Z0f5KDXQo2PEkJBbg3xQ=
github.com/wzshiming/notify v0.1.0 h1:BZND3IfiWkyDseAiaqLb43mTU8vefmmK0gOdnuschPI=
Expand Down
5 changes: 5 additions & 0 deletions internal/scheme/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func SplitSchemeAddr(addr string) (string, string, bool) {

u, _ := url.Parse(addr)
if u != nil && u.Scheme != "" {
// scheme:?args=...
if u.ForceQuery {
return u.Scheme, u.Opaque, true
}

// scheme://host
if u.Opaque == "" {
if u.Host != "" {
Expand Down
8 changes: 8 additions & 0 deletions internal/scheme/scheme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ func Test_ResolveProtocol(t *testing.T) {
wantAddress: "",
wantOk: true,
},
{
args: args{
addr: "nc:?",
},
wantNetwork: "nc",
wantAddress: "",
wantOk: true,
},
{
args: args{
addr: "nc:cmd --",
Expand Down

0 comments on commit 19ba391

Please sign in to comment.