-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
304 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package compose | ||
|
||
import ( | ||
"net" | ||
"regexp" | ||
|
||
"github.com/shoriwe/fullproxy/v4/filter" | ||
"github.com/shoriwe/fullproxy/v4/utils/network" | ||
) | ||
|
||
type PortRange struct { | ||
From *int `yaml:"from,omitempty" json:"from,omitempty"` | ||
To *int `yaml:"to,omitempty" json:"to,omitempty"` | ||
} | ||
|
||
type Match struct { | ||
Host *string `yaml:"host,omitempty" json:"host,omitempty"` | ||
Port *int `yaml:"port,omitempty" json:"port,omitempty"` | ||
Range *PortRange `yaml:"portRange,omitempty" json:"portRange,omitempty"` | ||
} | ||
|
||
func (m *Match) Compile() (filter.Match, error) { | ||
var ( | ||
err error | ||
host *regexp.Regexp | ||
port int = -1 | ||
from int = -1 | ||
to int = -1 | ||
) | ||
if m.Host != nil { | ||
host, err = regexp.Compile(*m.Host) | ||
} | ||
if m.Port != nil { | ||
port = *m.Port | ||
} | ||
if m.Range != nil { | ||
if m.Range.From != nil { | ||
from = *m.Range.From | ||
} | ||
if m.Range.To != nil { | ||
to = *m.Range.To | ||
} | ||
} | ||
f := filter.Match{ | ||
Host: host, | ||
Port: port, | ||
PortRange: [2]int{from, to}, | ||
} | ||
return f, err | ||
} | ||
|
||
type Filter struct { | ||
Whitelist []Match `yaml:"whitelist,omitempty" json:"whitelist,omitempty"` | ||
Blacklist []Match `yaml:"blacklist,omitempty" json:"blacklist,omitempty"` | ||
} | ||
|
||
func (f *Filter) Listener(l net.Listener) (net.Listener, error) { | ||
var whitelist, blacklist []filter.Match | ||
for _, white := range f.Whitelist { | ||
compiled, err := white.Compile() | ||
if err != nil { | ||
return nil, err | ||
} | ||
whitelist = append(whitelist, compiled) | ||
} | ||
for _, black := range f.Blacklist { | ||
compiled, err := black.Compile() | ||
if err != nil { | ||
return nil, err | ||
} | ||
blacklist = append(blacklist, compiled) | ||
} | ||
ll := &filter.Listener{ | ||
Listener: l, | ||
Whitelist: whitelist, | ||
Blacklist: blacklist, | ||
} | ||
return ll, nil | ||
} | ||
|
||
func (f *Filter) DialFunc(dialFunc network.DialFunc) (*filter.DialFunc, error) { | ||
var whitelist, blacklist []filter.Match | ||
for _, white := range f.Whitelist { | ||
compiled, err := white.Compile() | ||
if err != nil { | ||
return nil, err | ||
} | ||
whitelist = append(whitelist, compiled) | ||
} | ||
for _, black := range f.Blacklist { | ||
compiled, err := black.Compile() | ||
if err != nil { | ||
return nil, err | ||
} | ||
blacklist = append(blacklist, compiled) | ||
} | ||
df := &filter.DialFunc{ | ||
DialFunc: dialFunc, | ||
Whitelist: whitelist, | ||
Blacklist: blacklist, | ||
} | ||
return df, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package compose | ||
|
||
import ( | ||
"net" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/shoriwe/fullproxy/v4/filter" | ||
"github.com/shoriwe/fullproxy/v4/utils/network" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMatch_Compile(t *testing.T) { | ||
t.Run("Valid", func(tt *testing.T) { | ||
m := Match{ | ||
Host: new(string), | ||
Port: new(int), | ||
Range: &PortRange{ | ||
From: new(int), | ||
To: new(int), | ||
}, | ||
} | ||
*m.Host = "127.0.0.1" | ||
*m.Port = 80 | ||
*m.Range.From = 0 | ||
*m.Range.To = 8000 | ||
match, err := m.Compile() | ||
assert.Nil(tt, err) | ||
expect := filter.Match{ | ||
Host: regexp.MustCompile("127.0.0.1"), | ||
Port: 80, | ||
PortRange: [2]int{0, 8000}, | ||
} | ||
assert.Equal(tt, expect, match) | ||
}) | ||
} | ||
|
||
func TestFilter_Listener(t *testing.T) { | ||
t.Run("Valid", func(tt *testing.T) { | ||
m := Match{ | ||
Host: new(string), | ||
Port: new(int), | ||
Range: &PortRange{ | ||
From: new(int), | ||
To: new(int), | ||
}, | ||
} | ||
*m.Host = "127.0.0.1" | ||
*m.Port = 80 | ||
*m.Range.From = 0 | ||
*m.Range.To = 8000 | ||
f := Filter{ | ||
Whitelist: []Match{m}, | ||
Blacklist: []Match{m}, | ||
} | ||
l := network.ListenAny() | ||
defer l.Close() | ||
ll, err := f.Listener(l) | ||
assert.Nil(tt, err) | ||
defer ll.Close() | ||
}) | ||
t.Run("Invalid Whitelist", func(tt *testing.T) { | ||
m := Match{ | ||
Host: new(string), | ||
Port: new(int), | ||
Range: &PortRange{ | ||
From: new(int), | ||
To: new(int), | ||
}, | ||
} | ||
*m.Host = ")" | ||
*m.Port = 80 | ||
*m.Range.From = 0 | ||
*m.Range.To = 8000 | ||
f := Filter{ | ||
Whitelist: []Match{m}, | ||
// Blacklist: []Match{m}, | ||
} | ||
l := network.ListenAny() | ||
defer l.Close() | ||
_, err := f.Listener(l) | ||
assert.NotNil(tt, err) | ||
}) | ||
t.Run("Invalid Blacklist", func(tt *testing.T) { | ||
m := Match{ | ||
Host: new(string), | ||
Port: new(int), | ||
Range: &PortRange{ | ||
From: new(int), | ||
To: new(int), | ||
}, | ||
} | ||
*m.Host = ")" | ||
*m.Port = 80 | ||
*m.Range.From = 0 | ||
*m.Range.To = 8000 | ||
f := Filter{ | ||
// Whitelist: []Match{m}, | ||
Blacklist: []Match{m}, | ||
} | ||
l := network.ListenAny() | ||
defer l.Close() | ||
_, err := f.Listener(l) | ||
assert.NotNil(tt, err) | ||
}) | ||
} | ||
|
||
func TestFilter_DialFunc(t *testing.T) { | ||
t.Run("Valid", func(tt *testing.T) { | ||
m := Match{ | ||
Host: new(string), | ||
Port: new(int), | ||
Range: &PortRange{ | ||
From: new(int), | ||
To: new(int), | ||
}, | ||
} | ||
*m.Host = "127.0.0.1" | ||
*m.Port = 80 | ||
*m.Range.From = 0 | ||
*m.Range.To = 8000 | ||
f := Filter{ | ||
Whitelist: []Match{m}, | ||
Blacklist: []Match{m}, | ||
} | ||
df, err := f.DialFunc(net.Dial) | ||
assert.Nil(tt, err) | ||
assert.NotNil(tt, df) | ||
}) | ||
t.Run("Invalid Whitelist", func(tt *testing.T) { | ||
m := Match{ | ||
Host: new(string), | ||
Port: new(int), | ||
Range: &PortRange{ | ||
From: new(int), | ||
To: new(int), | ||
}, | ||
} | ||
*m.Host = ")" | ||
*m.Port = 80 | ||
*m.Range.From = 0 | ||
*m.Range.To = 8000 | ||
f := Filter{ | ||
Whitelist: []Match{m}, | ||
// Blacklist: []Match{m}, | ||
} | ||
_, err := f.DialFunc(net.Dial) | ||
assert.NotNil(tt, err) | ||
}) | ||
t.Run("Invalid Blacklist", func(tt *testing.T) { | ||
m := Match{ | ||
Host: new(string), | ||
Port: new(int), | ||
Range: &PortRange{ | ||
From: new(int), | ||
To: new(int), | ||
}, | ||
} | ||
*m.Host = ")" | ||
*m.Port = 80 | ||
*m.Range.From = 0 | ||
*m.Range.To = 8000 | ||
f := Filter{ | ||
// Whitelist: []Match{m}, | ||
Blacklist: []Match{m}, | ||
} | ||
_, err := f.DialFunc(net.Dial) | ||
assert.NotNil(tt, err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters