Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
G
gost
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nanahira
gost
Commits
e56fbfa8
Commit
e56fbfa8
authored
Jun 30, 2018
by
ginuerzh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add strategy param
parent
f292d8e7
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
65 additions
and
42 deletions
+65
-42
bypass.go
bypass.go
+5
-2
bypass_test.go
bypass_test.go
+3
-3
cmd/gost/main.go
cmd/gost/main.go
+9
-1
forward.go
forward.go
+40
-36
handler.go
handler.go
+8
-0
No files found.
bypass.go
View file @
e56fbfa8
...
@@ -4,6 +4,7 @@ import (
...
@@ -4,6 +4,7 @@ import (
"bytes"
"bytes"
"fmt"
"fmt"
"net"
"net"
"strconv"
"strings"
"strings"
glob
"github.com/gobwas/glob"
glob
"github.com/gobwas/glob"
...
@@ -147,8 +148,10 @@ func (bp *Bypass) Contains(addr string) bool {
...
@@ -147,8 +148,10 @@ func (bp *Bypass) Contains(addr string) bool {
return
false
return
false
}
}
// try to strip the port
// try to strip the port
if
host
,
_
,
_
:=
net
.
SplitHostPort
(
addr
);
host
!=
""
{
if
host
,
port
,
_
:=
net
.
SplitHostPort
(
addr
);
host
!=
""
&&
port
!=
""
{
addr
=
host
if
p
,
_
:=
strconv
.
Atoi
(
port
);
p
>
0
{
// port is valid
addr
=
host
}
}
}
var
matched
bool
var
matched
bool
for
_
,
matcher
:=
range
bp
.
matchers
{
for
_
,
matcher
:=
range
bp
.
matchers
{
...
...
bypass_test.go
View file @
e56fbfa8
...
@@ -77,15 +77,15 @@ var bypassTests = []struct {
...
@@ -77,15 +77,15 @@ var bypassTests = []struct {
}
}
func
TestBypass
(
t
*
testing
.
T
)
{
func
TestBypass
(
t
*
testing
.
T
)
{
for
i
,
test
:=
range
bypassTests
{
for
_
,
test
:=
range
bypassTests
{
bp
:=
NewBypassPatterns
(
test
.
patterns
,
test
.
reversed
)
bp
:=
NewBypassPatterns
(
test
.
patterns
,
test
.
reversed
)
if
bp
.
Contains
(
test
.
addr
)
!=
test
.
bypassed
{
if
bp
.
Contains
(
test
.
addr
)
!=
test
.
bypassed
{
t
.
Errorf
(
"test
%d failed"
,
i
)
t
.
Errorf
(
"test
failed: %v, %s"
,
test
.
patterns
,
test
.
addr
)
}
}
rbp
:=
NewBypassPatterns
(
test
.
patterns
,
!
test
.
reversed
)
rbp
:=
NewBypassPatterns
(
test
.
patterns
,
!
test
.
reversed
)
if
rbp
.
Contains
(
test
.
addr
)
==
test
.
bypassed
{
if
rbp
.
Contains
(
test
.
addr
)
==
test
.
bypassed
{
t
.
Errorf
(
"reverse test
%d failed"
,
i
)
t
.
Errorf
(
"reverse test
failed: %v, %s"
,
test
.
patterns
,
test
.
addr
)
}
}
}
}
}
}
cmd/gost/main.go
View file @
e56fbfa8
...
@@ -123,12 +123,18 @@ func (r *route) initChain() (*gost.Chain, error) {
...
@@ -123,12 +123,18 @@ func (r *route) initChain() (*gost.Chain, error) {
log
.
Log
(
err
)
log
.
Log
(
err
)
}
}
peerCfg
.
Validate
()
peerCfg
.
Validate
()
strategy
:=
peerCfg
.
Strategy
// overwrite the strategry in the peer config if `strategy` param exists.
if
s
:=
nodes
[
0
]
.
Get
(
"strategy"
);
s
!=
""
{
strategy
=
s
}
ngroup
.
Options
=
append
(
ngroup
.
Options
,
ngroup
.
Options
=
append
(
ngroup
.
Options
,
gost
.
WithFilter
(
&
gost
.
FailFilter
{
gost
.
WithFilter
(
&
gost
.
FailFilter
{
MaxFails
:
peerCfg
.
MaxFails
,
MaxFails
:
peerCfg
.
MaxFails
,
FailTimeout
:
time
.
Duration
(
peerCfg
.
FailTimeout
)
*
time
.
Second
,
FailTimeout
:
time
.
Duration
(
peerCfg
.
FailTimeout
)
*
time
.
Second
,
}),
}),
gost
.
WithStrategy
(
parseStrategy
(
peerCfg
.
S
trategy
)),
gost
.
WithStrategy
(
parseStrategy
(
s
trategy
)),
)
)
for
_
,
s
:=
range
peerCfg
.
Nodes
{
for
_
,
s
:=
range
peerCfg
.
Nodes
{
...
@@ -146,6 +152,7 @@ func (r *route) initChain() (*gost.Chain, error) {
...
@@ -146,6 +152,7 @@ func (r *route) initChain() (*gost.Chain, error) {
}
}
var
bypass
*
gost
.
Bypass
var
bypass
*
gost
.
Bypass
// global bypass
if
peerCfg
.
Bypass
!=
nil
{
if
peerCfg
.
Bypass
!=
nil
{
bypass
=
gost
.
NewBypassPatterns
(
peerCfg
.
Bypass
.
Patterns
,
peerCfg
.
Bypass
.
Reverse
)
bypass
=
gost
.
NewBypassPatterns
(
peerCfg
.
Bypass
.
Patterns
,
peerCfg
.
Bypass
.
Reverse
)
}
}
...
@@ -464,6 +471,7 @@ func (r *route) serve() error {
...
@@ -464,6 +471,7 @@ func (r *route) serve() error {
gost
.
WhitelistHandlerOption
(
whitelist
),
gost
.
WhitelistHandlerOption
(
whitelist
),
gost
.
BlacklistHandlerOption
(
blacklist
),
gost
.
BlacklistHandlerOption
(
blacklist
),
gost
.
BypassHandlerOption
(
parseBypass
(
node
.
Get
(
"bypass"
))),
gost
.
BypassHandlerOption
(
parseBypass
(
node
.
Get
(
"bypass"
))),
gost
.
StrategyHandlerOption
(
parseStrategy
(
node
.
Get
(
"strategy"
))),
)
)
var
handler
gost
.
Handler
var
handler
gost
.
Handler
switch
node
.
Protocol
{
switch
node
.
Protocol
{
...
...
forward.go
View file @
e56fbfa8
...
@@ -36,9 +36,17 @@ type tcpDirectForwardHandler struct {
...
@@ -36,9 +36,17 @@ type tcpDirectForwardHandler struct {
// The raddr is the remote address that the server will forward to.
// The raddr is the remote address that the server will forward to.
// NOTE: as of 2.6, remote address can be a comma-separated address list.
// NOTE: as of 2.6, remote address can be a comma-separated address list.
func
TCPDirectForwardHandler
(
raddr
string
,
opts
...
HandlerOption
)
Handler
{
func
TCPDirectForwardHandler
(
raddr
string
,
opts
...
HandlerOption
)
Handler
{
h
:=
&
tcpDirectForwardHandler
{
raddr
:
raddr
,
options
:
&
HandlerOptions
{},
}
for
_
,
opt
:=
range
opts
{
opt
(
h
.
options
)
}
group
:=
NewNodeGroup
()
group
:=
NewNodeGroup
()
group
.
SetSelector
(
&
defaultSelector
{},
group
.
SetSelector
(
&
defaultSelector
{},
WithStrategy
(
&
RoundStrategy
{}
),
WithStrategy
(
h
.
options
.
Strategy
),
WithFilter
(
&
FailFilter
{
WithFilter
(
&
FailFilter
{
MaxFails
:
1
,
MaxFails
:
1
,
FailTimeout
:
30
*
time
.
Second
,
FailTimeout
:
30
*
time
.
Second
,
...
@@ -57,15 +65,8 @@ func TCPDirectForwardHandler(raddr string, opts ...HandlerOption) Handler {
...
@@ -57,15 +65,8 @@ func TCPDirectForwardHandler(raddr string, opts ...HandlerOption) Handler {
Host
:
addr
,
Host
:
addr
,
})
})
}
}
h
.
group
=
group
h
:=
&
tcpDirectForwardHandler
{
raddr
:
raddr
,
group
:
group
,
options
:
&
HandlerOptions
{},
}
for
_
,
opt
:=
range
opts
{
opt
(
h
.
options
)
}
return
h
return
h
}
}
...
@@ -104,9 +105,17 @@ type udpDirectForwardHandler struct {
...
@@ -104,9 +105,17 @@ type udpDirectForwardHandler struct {
// The raddr is the remote address that the server will forward to.
// The raddr is the remote address that the server will forward to.
// NOTE: as of 2.6, remote address can be a comma-separated address list.
// NOTE: as of 2.6, remote address can be a comma-separated address list.
func
UDPDirectForwardHandler
(
raddr
string
,
opts
...
HandlerOption
)
Handler
{
func
UDPDirectForwardHandler
(
raddr
string
,
opts
...
HandlerOption
)
Handler
{
h
:=
&
udpDirectForwardHandler
{
raddr
:
raddr
,
options
:
&
HandlerOptions
{},
}
for
_
,
opt
:=
range
opts
{
opt
(
h
.
options
)
}
group
:=
NewNodeGroup
()
group
:=
NewNodeGroup
()
group
.
SetSelector
(
&
defaultSelector
{},
group
.
SetSelector
(
&
defaultSelector
{},
WithStrategy
(
&
RoundStrategy
{}
),
WithStrategy
(
h
.
options
.
Strategy
),
WithFilter
(
&
FailFilter
{
WithFilter
(
&
FailFilter
{
MaxFails
:
1
,
MaxFails
:
1
,
FailTimeout
:
30
*
time
.
Second
,
FailTimeout
:
30
*
time
.
Second
,
...
@@ -125,15 +134,8 @@ func UDPDirectForwardHandler(raddr string, opts ...HandlerOption) Handler {
...
@@ -125,15 +134,8 @@ func UDPDirectForwardHandler(raddr string, opts ...HandlerOption) Handler {
Host
:
addr
,
Host
:
addr
,
})
})
}
}
h
.
group
=
group
h
:=
&
udpDirectForwardHandler
{
raddr
:
raddr
,
group
:
group
,
options
:
&
HandlerOptions
{},
}
for
_
,
opt
:=
range
opts
{
opt
(
h
.
options
)
}
return
h
return
h
}
}
...
@@ -188,9 +190,17 @@ type tcpRemoteForwardHandler struct {
...
@@ -188,9 +190,17 @@ type tcpRemoteForwardHandler struct {
// The raddr is the remote address that the server will forward to.
// The raddr is the remote address that the server will forward to.
// NOTE: as of 2.6, remote address can be a comma-separated address list.
// NOTE: as of 2.6, remote address can be a comma-separated address list.
func
TCPRemoteForwardHandler
(
raddr
string
,
opts
...
HandlerOption
)
Handler
{
func
TCPRemoteForwardHandler
(
raddr
string
,
opts
...
HandlerOption
)
Handler
{
h
:=
&
tcpRemoteForwardHandler
{
raddr
:
raddr
,
options
:
&
HandlerOptions
{},
}
for
_
,
opt
:=
range
opts
{
opt
(
h
.
options
)
}
group
:=
NewNodeGroup
()
group
:=
NewNodeGroup
()
group
.
SetSelector
(
&
defaultSelector
{},
group
.
SetSelector
(
&
defaultSelector
{},
WithStrategy
(
&
RoundStrategy
{}
),
WithStrategy
(
h
.
options
.
Strategy
),
WithFilter
(
&
FailFilter
{
WithFilter
(
&
FailFilter
{
MaxFails
:
1
,
MaxFails
:
1
,
FailTimeout
:
30
*
time
.
Second
,
FailTimeout
:
30
*
time
.
Second
,
...
@@ -209,15 +219,8 @@ func TCPRemoteForwardHandler(raddr string, opts ...HandlerOption) Handler {
...
@@ -209,15 +219,8 @@ func TCPRemoteForwardHandler(raddr string, opts ...HandlerOption) Handler {
Host
:
addr
,
Host
:
addr
,
})
})
}
}
h
.
group
=
group
h
:=
&
tcpRemoteForwardHandler
{
raddr
:
raddr
,
group
:
group
,
options
:
&
HandlerOptions
{},
}
for
_
,
opt
:=
range
opts
{
opt
(
h
.
options
)
}
return
h
return
h
}
}
...
@@ -254,9 +257,17 @@ type udpRemoteForwardHandler struct {
...
@@ -254,9 +257,17 @@ type udpRemoteForwardHandler struct {
// The raddr is the remote address that the server will forward to.
// The raddr is the remote address that the server will forward to.
// NOTE: as of 2.6, remote address can be a comma-separated address list.
// NOTE: as of 2.6, remote address can be a comma-separated address list.
func
UDPRemoteForwardHandler
(
raddr
string
,
opts
...
HandlerOption
)
Handler
{
func
UDPRemoteForwardHandler
(
raddr
string
,
opts
...
HandlerOption
)
Handler
{
h
:=
&
udpRemoteForwardHandler
{
raddr
:
raddr
,
options
:
&
HandlerOptions
{},
}
for
_
,
opt
:=
range
opts
{
opt
(
h
.
options
)
}
group
:=
NewNodeGroup
()
group
:=
NewNodeGroup
()
group
.
SetSelector
(
&
defaultSelector
{},
group
.
SetSelector
(
&
defaultSelector
{},
WithStrategy
(
&
RoundStrategy
{}
),
WithStrategy
(
h
.
options
.
Strategy
),
WithFilter
(
&
FailFilter
{
WithFilter
(
&
FailFilter
{
MaxFails
:
1
,
MaxFails
:
1
,
FailTimeout
:
30
*
time
.
Second
,
FailTimeout
:
30
*
time
.
Second
,
...
@@ -274,15 +285,8 @@ func UDPRemoteForwardHandler(raddr string, opts ...HandlerOption) Handler {
...
@@ -274,15 +285,8 @@ func UDPRemoteForwardHandler(raddr string, opts ...HandlerOption) Handler {
Host
:
addr
,
Host
:
addr
,
})
})
}
}
h
.
group
=
group
h
:=
&
udpRemoteForwardHandler
{
raddr
:
raddr
,
group
:
group
,
options
:
&
HandlerOptions
{},
}
for
_
,
opt
:=
range
opts
{
opt
(
h
.
options
)
}
return
h
return
h
}
}
...
...
handler.go
View file @
e56fbfa8
...
@@ -25,6 +25,7 @@ type HandlerOptions struct {
...
@@ -25,6 +25,7 @@ type HandlerOptions struct {
Whitelist
*
Permissions
Whitelist
*
Permissions
Blacklist
*
Permissions
Blacklist
*
Permissions
Bypass
*
Bypass
Bypass
*
Bypass
Strategy
Strategy
}
}
// HandlerOption allows a common way to set handler options.
// HandlerOption allows a common way to set handler options.
...
@@ -79,6 +80,13 @@ func BypassHandlerOption(bypass *Bypass) HandlerOption {
...
@@ -79,6 +80,13 @@ func BypassHandlerOption(bypass *Bypass) HandlerOption {
}
}
}
}
// StrategyHandlerOption sets the strategy option of HandlerOptions.
func
StrategyHandlerOption
(
strategy
Strategy
)
HandlerOption
{
return
func
(
opts
*
HandlerOptions
)
{
opts
.
Strategy
=
strategy
}
}
type
autoHandler
struct
{
type
autoHandler
struct
{
options
[]
HandlerOption
options
[]
HandlerOption
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment