Skip to content

Commit

Permalink
add Handler.Init
Browse files Browse the repository at this point in the history
  • Loading branch information
ginuerzh committed Jul 6, 2018
1 parent 644d22d commit c242286
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 159 deletions.
40 changes: 40 additions & 0 deletions chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net"
"time"

"github.com/go-log/log"
)
Expand Down Expand Up @@ -308,3 +309,42 @@ func (c *Chain) selectRouteFor(addr string) (route *Chain, err error) {
}
return
}

// ChainOptions holds options for Chain.
type ChainOptions struct {
Retry int
Timeout time.Duration
Hosts *Hosts
Resolver Resolver
}

// ChainOption allows a common way to set chain options.
type ChainOption func(opts *ChainOptions)

// RetryChainOption specifies the times of retry used by Chain.Dial.
func RetryChainOption(retry int) ChainOption {
return func(opts *ChainOptions) {
opts.Retry = retry
}
}

// TimeoutChainOption specifies the timeout used by Chain.Dial.
func TimeoutChainOption(timeout time.Duration) ChainOption {
return func(opts *ChainOptions) {
opts.Timeout = timeout
}
}

// HostsChainOption specifies the hosts used by Chain.Dial.
func HostsChainOption(hosts *Hosts) ChainOption {
return func(opts *ChainOptions) {
opts.Hosts = hosts
}
}

// ResolverChainOption specifies the Resolver used by Chain.Dial.
func ResolverChainOption(resolver Resolver) ChainOption {
return func(opts *ChainOptions) {
opts.Resolver = resolver
}
}
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (tr *tcpTransporter) Multiplex() bool {
return false
}

// DialOptions describes the options for dialing.
// DialOptions describes the options for Transporter.Dial.
type DialOptions struct {
Timeout time.Duration
Chain *Chain
Expand Down
59 changes: 27 additions & 32 deletions cmd/gost/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,12 @@ func parseChainNode(ns string) (nodes []gost.Node, err error) {
}

func (r *route) serve() error {
baseChain, err := r.initChain()
chain, err := r.initChain()
if err != nil {
return err
}

for _, ns := range r.ServeNodes {
chain := &gost.Chain{}
*chain = *baseChain

node, err := gost.ParseNode(ns)
if err != nil {
return err
Expand Down Expand Up @@ -469,55 +466,52 @@ func (r *route) serve() error {
}
}

var handlerOptions []gost.HandlerOption
handlerOptions = append(handlerOptions,
gost.AddrHandlerOption(node.Addr),
gost.ChainHandlerOption(chain),
gost.UsersHandlerOption(users...),
gost.TLSConfigHandlerOption(tlsCfg),
gost.WhitelistHandlerOption(whitelist),
gost.BlacklistHandlerOption(blacklist),
gost.BypassHandlerOption(parseBypass(node.Get("bypass"))),
gost.StrategyHandlerOption(parseStrategy(node.Get("strategy"))),
)
var handler gost.Handler
switch node.Protocol {
case "http2":
handler = gost.HTTP2Handler(handlerOptions...)
handler = gost.HTTP2Handler()
case "socks", "socks5":
handler = gost.SOCKS5Handler(handlerOptions...)
handler = gost.SOCKS5Handler()
case "socks4", "socks4a":
handler = gost.SOCKS4Handler(handlerOptions...)
handler = gost.SOCKS4Handler()
case "ss":
handler = gost.ShadowHandler(handlerOptions...)
handler = gost.ShadowHandler()
case "http":
handler = gost.HTTPHandler(handlerOptions...)
handler = gost.HTTPHandler()
case "tcp":
handler = gost.TCPDirectForwardHandler(node.Remote, handlerOptions...)
handler = gost.TCPDirectForwardHandler(node.Remote)
case "rtcp":
handler = gost.TCPRemoteForwardHandler(node.Remote, handlerOptions...)
handler = gost.TCPRemoteForwardHandler(node.Remote)
case "udp":
handler = gost.UDPDirectForwardHandler(node.Remote, handlerOptions...)
handler = gost.UDPDirectForwardHandler(node.Remote)
case "rudp":
handler = gost.UDPRemoteForwardHandler(node.Remote, handlerOptions...)
handler = gost.UDPRemoteForwardHandler(node.Remote)
case "forward":
handler = gost.SSHForwardHandler(handlerOptions...)
handler = gost.SSHForwardHandler()
case "redirect":
handler = gost.TCPRedirectHandler(handlerOptions...)
handler = gost.TCPRedirectHandler()
case "ssu":
handler = gost.ShadowUDPdHandler(handlerOptions...)
handler = gost.ShadowUDPdHandler()
case "sni":
handler = gost.SNIHandler(handlerOptions...)
handler = gost.SNIHandler()
default:
// start from 2.5, if remote is not empty, then we assume that it is a forward tunnel.
if node.Remote != "" {
handler = gost.TCPDirectForwardHandler(node.Remote, handlerOptions...)
handler = gost.TCPDirectForwardHandler(node.Remote)
} else {
handler = gost.AutoHandler(handlerOptions...)
handler = gost.AutoHandler()
}
}

srv := &gost.Server{Listener: ln}
handler.Init(
gost.AddrHandlerOption(node.Addr),
gost.ChainHandlerOption(chain),
gost.UsersHandlerOption(users...),
gost.TLSConfigHandlerOption(tlsCfg),
gost.WhitelistHandlerOption(whitelist),
gost.BlacklistHandlerOption(blacklist),
gost.BypassHandlerOption(parseBypass(node.Get("bypass"))),
gost.StrategyHandlerOption(parseStrategy(node.Get("strategy"))),
)

chain.Resolver = parseResolver(node.Get("dns"))
if gost.Debug {
Expand All @@ -531,6 +525,7 @@ func (r *route) serve() error {
}
}

srv := &gost.Server{Listener: ln}
go srv.Serve(handler)
}

Expand Down
Loading

0 comments on commit c242286

Please sign in to comment.