feat: upgrade core to v0.4.0, pass network to admission, add dial options
Upgrade github.com/go-gost/core from v0.3.3 to v0.4.0, adapting to interface changes: - Admit(ctx, network, addr, opts) now accepts a network parameter - Router.Dial now accepts variadic chain.DialOption, forwarded to the route's Dial call along with router-level options - gRPC/HTTP admission plugins include the new Network field Also fix two typos: ResoloverNodeOption -> ResolverNodeOption, WithHostOpton -> WithHostOption.
This commit is contained in:
@@ -104,7 +104,7 @@ func NewAdmission(opts ...Option) admission.Admission {
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *localAdmission) Admit(ctx context.Context, addr string, opts ...admission.Option) bool {
|
||||
func (p *localAdmission) Admit(ctx context.Context, network, addr string, opts ...admission.Option) bool {
|
||||
if addr == "" || p == nil {
|
||||
return true
|
||||
}
|
||||
@@ -292,9 +292,9 @@ func AdmissionGroup(admissions ...admission.Admission) admission.Admission {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *admissionGroup) Admit(ctx context.Context, addr string, opts ...admission.Option) bool {
|
||||
func (p *admissionGroup) Admit(ctx context.Context, network, addr string, opts ...admission.Option) bool {
|
||||
for _, admission := range p.admissions {
|
||||
if admission != nil && !admission.Admit(ctx, addr, opts...) {
|
||||
if admission != nil && !admission.Admit(ctx, network, addr, opts...) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ func NewGRPCPlugin(name string, addr string, opts ...plugin.Option) admission.Ad
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) Admit(ctx context.Context, addr string, opts ...admission.Option) bool {
|
||||
func (p *grpcPlugin) Admit(ctx context.Context, network, addr string, opts ...admission.Option) bool {
|
||||
if p.client == nil {
|
||||
return false
|
||||
}
|
||||
@@ -56,6 +56,7 @@ func (p *grpcPlugin) Admit(ctx context.Context, addr string, opts ...admission.O
|
||||
r, err := p.client.Admit(ctx,
|
||||
&proto.AdmissionRequest{
|
||||
Service: options.Service,
|
||||
Network: network,
|
||||
Addr: addr,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
type httpPluginRequest struct {
|
||||
Service string `json:"service"`
|
||||
Network string `json:"network"`
|
||||
Addr string `json:"addr"`
|
||||
}
|
||||
|
||||
@@ -45,7 +46,7 @@ func NewHTTPPlugin(name string, url string, opts ...plugin.Option) admission.Adm
|
||||
}
|
||||
}
|
||||
|
||||
func (p *httpPlugin) Admit(ctx context.Context, addr string, opts ...admission.Option) (ok bool) {
|
||||
func (p *httpPlugin) Admit(ctx context.Context, network, addr string, opts ...admission.Option) (ok bool) {
|
||||
if p.client == nil {
|
||||
return
|
||||
}
|
||||
@@ -57,6 +58,7 @@ func (p *httpPlugin) Admit(ctx context.Context, addr string, opts ...admission.O
|
||||
|
||||
rb := httpPluginRequest{
|
||||
Service: options.Service,
|
||||
Network: network,
|
||||
Addr: addr,
|
||||
}
|
||||
v, err := json.Marshal(&rb)
|
||||
|
||||
@@ -35,7 +35,7 @@ func WrapConn(admission admission.Admission, c net.Conn) net.Conn {
|
||||
|
||||
func (c *serverConn) Read(b []byte) (n int, err error) {
|
||||
if c.admission != nil &&
|
||||
!c.admission.Admit(context.Background(), c.RemoteAddr().String()) {
|
||||
!c.admission.Admit(context.Background(), c.RemoteAddr().Network(), c.RemoteAddr().String()) {
|
||||
err = io.EOF
|
||||
return
|
||||
}
|
||||
@@ -95,7 +95,7 @@ func (c *packetConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
|
||||
}
|
||||
|
||||
if c.admission != nil &&
|
||||
!c.admission.Admit(context.Background(), addr.String()) {
|
||||
!c.admission.Admit(context.Background(), addr.Network(), addr.String()) {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -159,7 +159,7 @@ func (c *udpConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
|
||||
return
|
||||
}
|
||||
if c.admission != nil &&
|
||||
!c.admission.Admit(context.Background(), addr.String()) {
|
||||
!c.admission.Admit(context.Background(), addr.Network(), addr.String()) {
|
||||
continue
|
||||
}
|
||||
return
|
||||
@@ -174,7 +174,7 @@ func (c *udpConn) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) {
|
||||
return
|
||||
}
|
||||
if c.admission != nil &&
|
||||
!c.admission.Admit(context.Background(), addr.String()) {
|
||||
!c.admission.Admit(context.Background(), addr.Network(), addr.String()) {
|
||||
continue
|
||||
}
|
||||
return
|
||||
@@ -192,7 +192,7 @@ func (c *udpConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *net.UDPAd
|
||||
return
|
||||
}
|
||||
if c.admission != nil &&
|
||||
!c.admission.Admit(context.Background(), addr.String()) {
|
||||
!c.admission.Admit(context.Background(), addr.Network(), addr.String()) {
|
||||
continue
|
||||
}
|
||||
return
|
||||
|
||||
@@ -47,7 +47,7 @@ func (ln *listener) Accept() (net.Conn, error) {
|
||||
}
|
||||
|
||||
if ln.admission != nil &&
|
||||
!ln.admission.Admit(ctx, clientAddr.String(), admission.WithService(ln.service)) {
|
||||
!ln.admission.Admit(ctx, ln.Addr().Network(), clientAddr.String(), admission.WithService(ln.service)) {
|
||||
c.Close()
|
||||
continue
|
||||
}
|
||||
|
||||
+5
-3
@@ -43,7 +43,7 @@ func (r *Router) Options() *chain.RouterOptions {
|
||||
return &r.options
|
||||
}
|
||||
|
||||
func (r *Router) Dial(ctx context.Context, network, address string) (conn net.Conn, err error) {
|
||||
func (r *Router) Dial(ctx context.Context, network, address string, opts ...chain.DialOption) (conn net.Conn, err error) {
|
||||
host := address
|
||||
if h, _, _ := net.SplitHostPort(address); h != "" {
|
||||
host = h
|
||||
@@ -54,7 +54,7 @@ func (r *Router) Dial(ctx context.Context, network, address string) (conn net.Co
|
||||
"sid": xctx.SidFromContext(ctx),
|
||||
})
|
||||
|
||||
conn, err = r.dial(ctx, network, address, log)
|
||||
conn, err = r.dial(ctx, network, address, log, opts...)
|
||||
if err != nil {
|
||||
r.record(ctx, recorder.RecorderServiceRouterDialAddressError, []byte(host))
|
||||
return
|
||||
@@ -81,7 +81,7 @@ func (r *Router) record(ctx context.Context, name string, data []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Router) dial(ctx context.Context, network, address string, log logger.Logger) (conn net.Conn, err error) {
|
||||
func (r *Router) dial(ctx context.Context, network, address string, log logger.Logger, callerOpts ...chain.DialOption) (conn net.Conn, err error) {
|
||||
count := r.options.Retries + 1
|
||||
if count <= 0 {
|
||||
count = 1
|
||||
@@ -131,10 +131,12 @@ func (r *Router) dial(ctx context.Context, network, address string, log logger.L
|
||||
route = DefaultRoute
|
||||
}
|
||||
conn, err = route.Dial(ctx, network, ipAddr,
|
||||
append([]chain.DialOption{
|
||||
chain.InterfaceDialOption(r.options.IfceName),
|
||||
chain.NetnsDialOption(r.options.Netns),
|
||||
chain.SockOptsDialOption(r.options.SockOpts),
|
||||
chain.LoggerDialOption(log),
|
||||
}, callerOpts...)...,
|
||||
)
|
||||
if err == nil {
|
||||
break
|
||||
|
||||
@@ -142,7 +142,7 @@ func ParseNode(hop string, cfg *config.NodeConfig, log logger.Logger) (*chain.No
|
||||
opts := []chain.NodeOption{
|
||||
chain.TransportNodeOption(tr),
|
||||
chain.BypassNodeOption(xbypass.BypassGroup(bypass_parser.List(cfg.Bypass, cfg.Bypasses...)...)),
|
||||
chain.ResoloverNodeOption(registry.ResolverRegistry().Get(cfg.Resolver)),
|
||||
chain.ResolverNodeOption(registry.ResolverRegistry().Get(cfg.Resolver)),
|
||||
chain.HostMapperNodeOption(registry.HostsRegistry().Get(cfg.Hosts)),
|
||||
chain.MetadataNodeOption(md),
|
||||
chain.NetworkNodeOption(cfg.Network),
|
||||
|
||||
@@ -7,7 +7,7 @@ require (
|
||||
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
|
||||
github.com/gin-contrib/cors v1.7.2
|
||||
github.com/gin-gonic/gin v1.10.1
|
||||
github.com/go-gost/core v0.3.3
|
||||
github.com/go-gost/core v0.4.0
|
||||
github.com/go-gost/go-shadowsocks2 v0.1.2
|
||||
github.com/go-gost/gosocks4 v0.0.1
|
||||
github.com/go-gost/gosocks5 v0.4.2
|
||||
|
||||
@@ -51,6 +51,8 @@ github.com/gin-gonic/gin v1.10.1 h1:T0ujvqyCSqRopADpgPgiTT63DUQVSfojyME59Ei63pQ=
|
||||
github.com/gin-gonic/gin v1.10.1/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
|
||||
github.com/go-gost/core v0.3.3 h1:YN15FQptQoNB0MlMR283C99w2EeGql9Cm+4QVlN6zs0=
|
||||
github.com/go-gost/core v0.3.3/go.mod h1:WGI43jOka7FAsSAwi/fSMaqxdR+E339ycb4NBGlFr6A=
|
||||
github.com/go-gost/core v0.4.0 h1:jLuIMO7ZMJX/BDCYLrYXAmIykT+HkqEuzaTeLMdrY1c=
|
||||
github.com/go-gost/core v0.4.0/go.mod h1:WGI43jOka7FAsSAwi/fSMaqxdR+E339ycb4NBGlFr6A=
|
||||
github.com/go-gost/go-shadowsocks2 v0.1.2 h1:qgTUQS6aCGzsFArjiKjLP0a7zEVtEYVCRVc5LjOsaVk=
|
||||
github.com/go-gost/go-shadowsocks2 v0.1.2/go.mod h1:866zFNNI3He6Wef1M/IvAjTal74WhcfKfBgRpTlkKys=
|
||||
github.com/go-gost/gosocks4 v0.0.1 h1:+k1sec8HlELuQV7rWftIkmy8UijzUt2I6t+iMPlGB2s=
|
||||
|
||||
+2
-2
@@ -140,7 +140,7 @@ func (p *chainHop) Select(ctx context.Context, opts ...hop.SelectOption) *chain.
|
||||
|
||||
// hop level bypass
|
||||
if p.options.bypass != nil &&
|
||||
p.options.bypass.Contains(ctx, options.Network, options.Addr, bypass.WithHostOpton(options.Host)) {
|
||||
p.options.bypass.Contains(ctx, options.Network, options.Addr, bypass.WithHostOption(options.Host)) {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -151,7 +151,7 @@ func (p *chainHop) Select(ctx context.Context, opts ...hop.SelectOption) *chain.
|
||||
}
|
||||
// node level bypass
|
||||
if node.Options().Bypass != nil &&
|
||||
node.Options().Bypass.Contains(ctx, options.Network, options.Addr, bypass.WithHostOpton(options.Host)) {
|
||||
node.Options().Bypass.Contains(ctx, options.Network, options.Addr, bypass.WithHostOption(options.Host)) {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -30,10 +30,10 @@ type admissionWrapper struct {
|
||||
r *admissionRegistry
|
||||
}
|
||||
|
||||
func (w *admissionWrapper) Admit(ctx context.Context, addr string, opts ...admission.Option) bool {
|
||||
func (w *admissionWrapper) Admit(ctx context.Context, network, addr string, opts ...admission.Option) bool {
|
||||
p := w.r.get(w.name)
|
||||
if p == nil {
|
||||
return false
|
||||
}
|
||||
return p.Admit(ctx, addr, opts...)
|
||||
return p.Admit(ctx, network, addr, opts...)
|
||||
}
|
||||
|
||||
+1
-1
@@ -447,7 +447,7 @@ func admission(tree *matchersTree, names ...string) error {
|
||||
return false
|
||||
}
|
||||
if adm := registry.AdmissionRegistry().Get(name); adm != nil {
|
||||
return adm.Admit(context.Background(), req.ClientIP.String())
|
||||
return adm.Admit(context.Background(), "ip", req.ClientIP.String())
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
+1
-1
@@ -247,7 +247,7 @@ func (s *defaultService) Serve() error {
|
||||
}
|
||||
}
|
||||
if s.options.admission != nil &&
|
||||
!s.options.admission.Admit(ctx, srcAddr.String(), admission.WithService(s.name)) {
|
||||
!s.options.admission.Admit(ctx, srcAddr.Network(), srcAddr.String(), admission.WithService(s.name)) {
|
||||
conn.Close()
|
||||
log.Debugf("admission: %s is denied", srcAddr)
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user