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
d9cce633
Commit
d9cce633
authored
Nov 12, 2018
by
ginuerzh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add live reloading support for peer config
parent
c91e424c
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
189 additions
and
115 deletions
+189
-115
cmd/gost/cfg.go
cmd/gost/cfg.go
+0
-49
cmd/gost/main.go
cmd/gost/main.go
+6
-47
cmd/gost/peer.go
cmd/gost/peer.go
+163
-0
cmd/gost/peer.json
cmd/gost/peer.json
+0
-18
cmd/gost/peer.txt
cmd/gost/peer.txt
+14
-0
node.go
node.go
+6
-1
No files found.
cmd/gost/cfg.go
View file @
d9cce633
...
@@ -173,55 +173,6 @@ func parseIP(s string, port string) (ips []string) {
...
@@ -173,55 +173,6 @@ func parseIP(s string, port string) (ips []string) {
return
return
}
}
type
peerConfig
struct
{
Strategy
string
`json:"strategy"`
Filters
[]
string
`json:"filters"`
MaxFails
int
`json:"max_fails"`
FailTimeout
int
`json:"fail_timeout"`
Nodes
[]
string
`json:"nodes"`
Bypass
*
bypass
`json:"bypass"`
// global bypass
}
type
bypass
struct
{
Reverse
bool
`json:"reverse"`
Patterns
[]
string
`json:"patterns"`
}
func
loadPeerConfig
(
peer
string
)
(
config
peerConfig
,
err
error
)
{
if
peer
==
""
{
return
}
content
,
err
:=
ioutil
.
ReadFile
(
peer
)
if
err
!=
nil
{
return
}
err
=
json
.
Unmarshal
(
content
,
&
config
)
return
}
func
(
cfg
*
peerConfig
)
Validate
()
{
if
cfg
.
MaxFails
<=
0
{
cfg
.
MaxFails
=
1
}
if
cfg
.
FailTimeout
<=
0
{
cfg
.
FailTimeout
=
30
// seconds
}
}
func
parseStrategy
(
s
string
)
gost
.
Strategy
{
switch
s
{
case
"random"
:
return
&
gost
.
RandomStrategy
{}
case
"fifo"
:
return
&
gost
.
FIFOStrategy
{}
case
"round"
:
fallthrough
default
:
return
&
gost
.
RoundStrategy
{}
}
}
func
parseBypass
(
s
string
)
*
gost
.
Bypass
{
func
parseBypass
(
s
string
)
*
gost
.
Bypass
{
if
s
==
""
{
if
s
==
""
{
return
nil
return
nil
...
...
cmd/gost/main.go
View file @
d9cce633
...
@@ -6,6 +6,7 @@ import (
...
@@ -6,6 +6,7 @@ import (
"flag"
"flag"
"fmt"
"fmt"
"net"
"net"
// _ "net/http/pprof"
// _ "net/http/pprof"
"os"
"os"
"runtime"
"runtime"
...
@@ -102,65 +103,23 @@ func (r *route) initChain() (*gost.Chain, error) {
...
@@ -102,65 +103,23 @@ func (r *route) initChain() (*gost.Chain, error) {
ngroup
.
ID
=
gid
ngroup
.
ID
=
gid
gid
++
gid
++
// parse the base node
// parse the base node
s
nodes
,
err
:=
parseChainNode
(
ns
)
nodes
,
err
:=
parseChainNode
(
ns
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
nid
:=
1
// node ID
nid
:=
1
// node ID
for
i
:=
range
nodes
{
for
i
:=
range
nodes
{
nodes
[
i
]
.
ID
=
nid
nodes
[
i
]
.
ID
=
nid
nid
++
nid
++
}
}
ngroup
.
AddNode
(
nodes
...
)
ngroup
.
AddNode
(
nodes
...
)
// parse peer nodes if exists
go
gost
.
PeriodReload
(
&
peerConfig
{
peerCfg
,
err
:=
loadPeerConfig
(
nodes
[
0
]
.
Get
(
"peer"
))
group
:
ngroup
,
if
err
!=
nil
{
baseNodes
:
nodes
,
log
.
Log
(
err
)
},
nodes
[
0
]
.
Get
(
"peer"
))
}
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
,
gost
.
WithFilter
(
&
gost
.
FailFilter
{
MaxFails
:
peerCfg
.
MaxFails
,
FailTimeout
:
time
.
Duration
(
peerCfg
.
FailTimeout
)
*
time
.
Second
,
}),
gost
.
WithStrategy
(
parseStrategy
(
strategy
)),
)
for
_
,
s
:=
range
peerCfg
.
Nodes
{
nodes
,
err
=
parseChainNode
(
s
)
if
err
!=
nil
{
return
nil
,
err
}
for
i
:=
range
nodes
{
nodes
[
i
]
.
ID
=
nid
nid
++
}
ngroup
.
AddNode
(
nodes
...
)
}
var
bypass
*
gost
.
Bypass
// global bypass
if
peerCfg
.
Bypass
!=
nil
{
bypass
=
gost
.
NewBypassPatterns
(
peerCfg
.
Bypass
.
Reverse
,
peerCfg
.
Bypass
.
Patterns
...
)
}
nodes
=
ngroup
.
Nodes
()
for
i
:=
range
nodes
{
if
nodes
[
i
]
.
Bypass
==
nil
{
nodes
[
i
]
.
Bypass
=
bypass
// use global bypass if local bypass does not exist.
}
}
chain
.
AddNodeGroup
(
ngroup
)
chain
.
AddNodeGroup
(
ngroup
)
}
}
...
...
cmd/gost/peer.go
0 → 100644
View file @
d9cce633
package
main
import
(
"bufio"
"bytes"
"encoding/json"
"io"
"io/ioutil"
"strconv"
"strings"
"time"
"github.com/ginuerzh/gost"
)
type
peerConfig
struct
{
Strategy
string
`json:"strategy"`
MaxFails
int
`json:"max_fails"`
FailTimeout
time
.
Duration
`json:"fail_timeout"`
period
time
.
Duration
// the period for live reloading
Nodes
[]
string
`json:"nodes"`
group
*
gost
.
NodeGroup
baseNodes
[]
gost
.
Node
}
type
bypass
struct
{
Reverse
bool
`json:"reverse"`
Patterns
[]
string
`json:"patterns"`
}
func
parsePeerConfig
(
cfg
string
,
group
*
gost
.
NodeGroup
,
baseNodes
[]
gost
.
Node
)
*
peerConfig
{
pc
:=
&
peerConfig
{
group
:
group
,
baseNodes
:
baseNodes
,
}
go
gost
.
PeriodReload
(
pc
,
cfg
)
return
pc
}
func
(
cfg
*
peerConfig
)
Validate
()
{
if
cfg
.
MaxFails
<=
0
{
cfg
.
MaxFails
=
1
}
if
cfg
.
FailTimeout
<=
0
{
cfg
.
FailTimeout
=
30
// seconds
}
}
func
(
cfg
*
peerConfig
)
Reload
(
r
io
.
Reader
)
error
{
if
err
:=
cfg
.
parse
(
r
);
err
!=
nil
{
return
err
}
cfg
.
Validate
()
group
:=
cfg
.
group
strategy
:=
cfg
.
Strategy
if
len
(
cfg
.
baseNodes
)
>
0
{
// overwrite the strategry in the peer config if `strategy` param exists.
if
s
:=
cfg
.
baseNodes
[
0
]
.
Get
(
"strategy"
);
s
!=
""
{
strategy
=
s
}
}
group
.
Options
=
append
([]
gost
.
SelectOption
{},
gost
.
WithFilter
(
&
gost
.
FailFilter
{
MaxFails
:
cfg
.
MaxFails
,
FailTimeout
:
time
.
Duration
(
cfg
.
FailTimeout
)
*
time
.
Second
,
}),
gost
.
WithStrategy
(
parseStrategy
(
strategy
)),
)
gNodes
:=
cfg
.
baseNodes
nid
:=
len
(
gNodes
)
+
1
for
_
,
s
:=
range
cfg
.
Nodes
{
nodes
,
err
:=
parseChainNode
(
s
)
if
err
!=
nil
{
return
err
}
for
i
:=
range
nodes
{
nodes
[
i
]
.
ID
=
nid
nid
++
}
gNodes
=
append
(
gNodes
,
nodes
...
)
}
group
.
SetNodes
(
gNodes
...
)
return
nil
}
func
(
cfg
*
peerConfig
)
parse
(
r
io
.
Reader
)
error
{
data
,
err
:=
ioutil
.
ReadAll
(
r
)
if
err
!=
nil
{
return
err
}
// compatible with JSON format
if
err
:=
json
.
NewDecoder
(
bytes
.
NewReader
(
data
))
.
Decode
(
cfg
);
err
==
nil
{
return
nil
}
split
:=
func
(
line
string
)
[]
string
{
if
line
==
""
{
return
nil
}
if
n
:=
strings
.
IndexByte
(
line
,
'#'
);
n
>=
0
{
line
=
line
[
:
n
]
}
line
=
strings
.
Replace
(
line
,
"
\t
"
,
" "
,
-
1
)
line
=
strings
.
TrimSpace
(
line
)
var
ss
[]
string
for
_
,
s
:=
range
strings
.
Split
(
line
,
" "
)
{
if
s
=
strings
.
TrimSpace
(
s
);
s
!=
""
{
ss
=
append
(
ss
,
s
)
}
}
return
ss
}
cfg
.
Nodes
=
nil
scanner
:=
bufio
.
NewScanner
(
bytes
.
NewReader
(
data
))
for
scanner
.
Scan
()
{
line
:=
scanner
.
Text
()
ss
:=
split
(
line
)
if
len
(
ss
)
<
2
{
continue
}
switch
ss
[
0
]
{
case
"strategy"
:
cfg
.
Strategy
=
ss
[
1
]
case
"max_fails"
:
cfg
.
MaxFails
,
_
=
strconv
.
Atoi
(
ss
[
1
])
case
"fail_timeout"
:
cfg
.
FailTimeout
,
_
=
time
.
ParseDuration
(
ss
[
1
])
case
"reload"
:
cfg
.
period
,
_
=
time
.
ParseDuration
(
ss
[
1
])
case
"peer"
:
cfg
.
Nodes
=
append
(
cfg
.
Nodes
,
ss
[
1
])
}
}
return
scanner
.
Err
()
}
func
(
cfg
*
peerConfig
)
Period
()
time
.
Duration
{
return
cfg
.
period
}
func
parseStrategy
(
s
string
)
gost
.
Strategy
{
switch
s
{
case
"random"
:
return
&
gost
.
RandomStrategy
{}
case
"fifo"
:
return
&
gost
.
FIFOStrategy
{}
case
"round"
:
fallthrough
default
:
return
&
gost
.
RoundStrategy
{}
}
}
cmd/gost/peer.json
deleted
100644 → 0
View file @
c91e424c
{
"strategy"
:
"round"
,
"max_fails"
:
3
,
"fail_timeout"
:
30
,
"nodes"
:[
"socks5://:1081"
,
"socks://:1082"
,
"socks4a://:1083"
],
"bypass"
:{
"reverse"
:
false
,
"patterns"
:
[
"10.0.0.1"
,
"192.168.0.0/24"
,
"*.example.com"
]
}
}
\ No newline at end of file
cmd/gost/peer.txt
0 → 100644
View file @
d9cce633
# strategy for node selecting
strategy random
max_fails 1
fail_timeout 30s
# period for live reloading
reload 10s
# peers
peer http://:18080
peer socks://:11080
peer ss://chacha20:123456@:18338
\ No newline at end of file
node.go
View file @
d9cce633
...
@@ -180,7 +180,7 @@ func NewNodeGroup(nodes ...Node) *NodeGroup {
...
@@ -180,7 +180,7 @@ func NewNodeGroup(nodes ...Node) *NodeGroup {
}
}
}
}
// AddNode a
dds node or node list into group
// AddNode a
ppends node or node list into group node.
func
(
group
*
NodeGroup
)
AddNode
(
node
...
Node
)
{
func
(
group
*
NodeGroup
)
AddNode
(
node
...
Node
)
{
if
group
==
nil
{
if
group
==
nil
{
return
return
...
@@ -188,6 +188,11 @@ func (group *NodeGroup) AddNode(node ...Node) {
...
@@ -188,6 +188,11 @@ func (group *NodeGroup) AddNode(node ...Node) {
group
.
nodes
=
append
(
group
.
nodes
,
node
...
)
group
.
nodes
=
append
(
group
.
nodes
,
node
...
)
}
}
// SetNodes replaces the group nodes to the specified nodes.
func
(
group
*
NodeGroup
)
SetNodes
(
nodes
...
Node
)
{
group
.
nodes
=
nodes
}
// SetSelector sets node selector with options for the group.
// SetSelector sets node selector with options for the group.
func
(
group
*
NodeGroup
)
SetSelector
(
selector
NodeSelector
,
opts
...
SelectOption
)
{
func
(
group
*
NodeGroup
)
SetSelector
(
selector
NodeSelector
,
opts
...
SelectOption
)
{
if
group
==
nil
{
if
group
==
nil
{
...
...
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