github.com/xraypb/Xray-core@v1.8.1/transport/internet/grpc/config.go (about)

     1  package grpc
     2  
     3  import (
     4  	"net/url"
     5  	"strings"
     6  
     7  	"github.com/xraypb/Xray-core/common"
     8  	"github.com/xraypb/Xray-core/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  	// Otherwise new custom paths
    25  	rawServiceName := c.ServiceName[1:strings.LastIndex(c.ServiceName, "/")] // trim from first to last '/'
    26  	serviceNameParts := strings.Split(rawServiceName, "/")
    27  	for i := range serviceNameParts {
    28  		serviceNameParts[i] = url.PathEscape(serviceNameParts[i])
    29  	}
    30  	return strings.Join(serviceNameParts, "/")
    31  }
    32  
    33  func (c *Config) getTunStreamName() string {
    34  	// Normal old school config
    35  	if !strings.HasPrefix(c.ServiceName, "/") {
    36  		return "Tun"
    37  	}
    38  	// Otherwise new custom paths
    39  	endingPath := c.ServiceName[strings.LastIndex(c.ServiceName, "/")+1:] // from the last '/' to end of string
    40  	return url.PathEscape(strings.Split(endingPath, "|")[0])
    41  }
    42  
    43  func (c *Config) getTunMultiStreamName() string {
    44  	// Normal old school config
    45  	if !strings.HasPrefix(c.ServiceName, "/") {
    46  		return "TunMulti"
    47  	}
    48  	// Otherwise new custom paths
    49  	endingPath := c.ServiceName[strings.LastIndex(c.ServiceName, "/")+1:] // from the last '/' to end of string
    50  	streamNames := strings.Split(endingPath, "|")
    51  	if len(streamNames) == 1 { // client side. Service name is the full path to multi tun
    52  		return url.PathEscape(streamNames[0])
    53  	} else { // server side. The second part is the path to multi tun
    54  		return url.PathEscape(streamNames[1])
    55  	}
    56  }