Skip to content

Commit

Permalink
feat: support glob in exclude domain
Browse files Browse the repository at this point in the history
related to: #62
  • Loading branch information
zema1 committed Aug 27, 2024
1 parent e27eac1 commit 2c24f66
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
18 changes: 13 additions & 5 deletions ctrl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ctrl

import (
"fmt"
"github.com/gobwas/glob"
"io"
"net/http"
"strings"
Expand All @@ -27,7 +28,7 @@ type Suo5Config struct {
ExcludeDomain []string `json:"exclude_domain"`

TestExit string `json:"-"`
ExcludeDomainMap map[string]bool `json:"-"`
ExcludeGlobs []glob.Glob `json:"-"`
Offset int `json:"-"`
Header http.Header `json:"-"`
OnRemoteConnected func(e *ConnectedEvent) `json:"-"`
Expand All @@ -37,15 +38,22 @@ type Suo5Config struct {
}

func (s *Suo5Config) Parse() error {
s.parseExcludeDomain()
if err := s.parseExcludeDomain(); err != nil {
return err
}
return s.parseHeader()
}

func (s *Suo5Config) parseExcludeDomain() {
s.ExcludeDomainMap = make(map[string]bool)
func (s *Suo5Config) parseExcludeDomain() error {
s.ExcludeGlobs = make([]glob.Glob, 0)
for _, domain := range s.ExcludeDomain {
s.ExcludeDomainMap[strings.ToLower(strings.TrimSpace(domain))] = true
g, err := glob.Compile(domain)
if err != nil {
return err
}
s.ExcludeGlobs = append(s.ExcludeGlobs, g)
}
return nil
}

func (s *Suo5Config) parseHeader() error {
Expand Down
5 changes: 2 additions & 3 deletions ctrl/ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
utls "github.com/refraction-networking/utls"
"github.com/zema1/rawhttp"
"github.com/zema1/suo5/netrans"
"golang.org/x/exp/maps"
)

var rander = rand.New(rand.NewSource(time.Now().UnixNano()))
Expand All @@ -49,8 +48,8 @@ func Run(ctx context.Context, config *Suo5Config) error {
config.Header.Set("Accept-Encoding", "identity")
}

if len(config.ExcludeDomainMap) != 0 {
log.Infof("exclude domains: %v", maps.Keys(config.ExcludeDomainMap))
if len(config.ExcludeDomain) != 0 {
log.Infof("exclude domains: %v", config.ExcludeDomain)
}

tr := &http.Transport{
Expand Down
10 changes: 7 additions & 3 deletions ctrl/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@ func (m *socks5Handler) Handle(conn net.Conn) error {
return err
}

if m.config.ExcludeDomainMap[req.Addr.Host] {
log.Infof("drop connection to %s", req.Addr.Host)
return nil
if len(m.config.ExcludeGlobs) != 0 {
for _, g := range m.config.ExcludeGlobs {
if g.Match(req.Addr.Host) {
log.Debugf("drop connection to %s", req.Addr.Host)
return nil
}
}
}

log.Infof("start connection to %s", req.Addr.String())
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ go 1.20

require (
github.com/go-gost/gosocks5 v0.3.0
github.com/gobwas/glob v0.2.3
github.com/kataras/golog v0.1.8
github.com/kataras/pio v0.0.11
github.com/pkg/errors v0.9.1
github.com/refraction-networking/utls v1.6.4
github.com/stretchr/testify v1.8.4
github.com/urfave/cli/v2 v2.27.4
github.com/zema1/rawhttp v0.1.1
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-gost/gosocks5 v0.3.0 h1:Hkmp9YDRBSCJd7xywW6dBPT6B9aQTkuWd+3WCheJiJA=
github.com/go-gost/gosocks5 v0.3.0/go.mod h1:1G6I7HP7VFVxveGkoK8mnprnJqSqJjdcASKsdUn4Pp4=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/kataras/golog v0.1.8 h1:isP8th4PJH2SrbkciKnylaND9xoTtfxv++NB+DF0l9g=
github.com/kataras/golog v0.1.8/go.mod h1:rGPAin4hYROfk1qT9wZP6VY2rsb4zzc37QpdPjdkqVw=
github.com/kataras/pio v0.0.11 h1:kqreJ5KOEXGMwHAWHDwIl+mjfNCPhAwZPa8gK7MKlyw=
Expand Down Expand Up @@ -44,8 +46,6 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 h1:kx6Ds3MlpiUHKj7syVnbp57++8WpuKPcR5yjLBjvLEA=
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down

0 comments on commit 2c24f66

Please sign in to comment.