github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/transport/internet/grpc/config.go (about)

     1  package grpc
     2  
     3  import (
     4  	"net/url"
     5  	"strings"
     6  
     7  	"github.com/xmplusdev/xmcore/common"
     8  	"github.com/xmplusdev/xmcore/transport/internet"
     9  )
    10  
    11  const protocolName = "grpc"
    12  
    13  func init() {
    14  	common.Must(internet.RegisterProtocolConfigCreator(protocolName, func() interface{} {
    15  		return new(Config)
    16  	}))
    17  }
    18  
    19  func (c *Config) getServiceName() string {
    20  	// Normal old school config
    21  	if !strings.HasPrefix(c.ServiceName, "/") {
    22  		return url.PathEscape(c.ServiceName)
    23  	}
    24  
    25  	// Otherwise new custom paths
    26  	lastIndex := strings.LastIndex(c.ServiceName, "/")
    27  	if lastIndex < 1 {
    28  		lastIndex = 1
    29  	}
    30  	rawServiceName := c.ServiceName[1:lastIndex] // trim from first to last '/'
    31  	serviceNameParts := strings.Split(rawServiceName, "/")
    32  	for i := range serviceNameParts {
    33  		serviceNameParts[i] = url.PathEscape(serviceNameParts[i])
    34  	}
    35  	return strings.Join(serviceNameParts, "/")
    36  }
    37  
    38  func (c *Config) getTunStreamName() string {
    39  	// Normal old school config
    40  	if !strings.HasPrefix(c.ServiceName, "/") {
    41  		return "Tun"
    42  	}
    43  	// Otherwise new custom paths
    44  	endingPath := c.ServiceName[strings.LastIndex(c.ServiceName, "/")+1:] // from the last '/' to end of string
    45  	return url.PathEscape(strings.Split(endingPath, "|")[0])
    46  }
    47  
    48  func (c *Config) getTunMultiStreamName() string {
    49  	// Normal old school config
    50  	if !strings.HasPrefix(c.ServiceName, "/") {
    51  		return "TunMulti"
    52  	}
    53  	// Otherwise new custom paths
    54  	endingPath := c.ServiceName[strings.LastIndex(c.ServiceName, "/")+1:] // from the last '/' to end of string
    55  	streamNames := strings.Split(endingPath, "|")
    56  	if len(streamNames) == 1 { // client side. Service name is the full path to multi tun
    57  		return url.PathEscape(streamNames[0])
    58  	} else { // server side. The second part is the path to multi tun
    59  		return url.PathEscape(streamNames[1])
    60  	}
    61  }