2021-03-14 15:02:07 +00:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
2021-06-27 08:41:19 +00:00
|
|
|
"net/url"
|
2023-03-26 05:58:19 +00:00
|
|
|
"strings"
|
2021-06-27 08:41:19 +00:00
|
|
|
|
2021-03-14 15:02:07 +00:00
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/transport/internet"
|
|
|
|
)
|
|
|
|
|
|
|
|
const protocolName = "grpc"
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(internet.RegisterProtocolConfigCreator(protocolName, func() interface{} {
|
|
|
|
return new(Config)
|
|
|
|
}))
|
|
|
|
}
|
2021-06-27 08:41:19 +00:00
|
|
|
|
2023-03-26 05:58:19 +00:00
|
|
|
func (c *Config) getServiceName() string {
|
|
|
|
// Normal old school config
|
|
|
|
if !strings.HasPrefix(c.ServiceName, "/") {
|
|
|
|
return url.PathEscape(c.ServiceName)
|
|
|
|
}
|
2023-08-20 11:18:39 +00:00
|
|
|
|
2023-03-26 05:58:19 +00:00
|
|
|
// Otherwise new custom paths
|
2023-08-20 11:18:39 +00:00
|
|
|
lastIndex := strings.LastIndex(c.ServiceName, "/")
|
|
|
|
if lastIndex < 1 {
|
|
|
|
lastIndex = 1
|
|
|
|
}
|
|
|
|
rawServiceName := c.ServiceName[1:lastIndex] // trim from first to last '/'
|
2023-03-26 05:58:19 +00:00
|
|
|
serviceNameParts := strings.Split(rawServiceName, "/")
|
|
|
|
for i := range serviceNameParts {
|
|
|
|
serviceNameParts[i] = url.PathEscape(serviceNameParts[i])
|
|
|
|
}
|
|
|
|
return strings.Join(serviceNameParts, "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) getTunStreamName() string {
|
|
|
|
// Normal old school config
|
|
|
|
if !strings.HasPrefix(c.ServiceName, "/") {
|
|
|
|
return "Tun"
|
|
|
|
}
|
|
|
|
// Otherwise new custom paths
|
|
|
|
endingPath := c.ServiceName[strings.LastIndex(c.ServiceName, "/")+1:] // from the last '/' to end of string
|
|
|
|
return url.PathEscape(strings.Split(endingPath, "|")[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) getTunMultiStreamName() string {
|
|
|
|
// Normal old school config
|
|
|
|
if !strings.HasPrefix(c.ServiceName, "/") {
|
|
|
|
return "TunMulti"
|
|
|
|
}
|
|
|
|
// Otherwise new custom paths
|
|
|
|
endingPath := c.ServiceName[strings.LastIndex(c.ServiceName, "/")+1:] // from the last '/' to end of string
|
|
|
|
streamNames := strings.Split(endingPath, "|")
|
|
|
|
if len(streamNames) == 1 { // client side. Service name is the full path to multi tun
|
|
|
|
return url.PathEscape(streamNames[0])
|
|
|
|
} else { // server side. The second part is the path to multi tun
|
|
|
|
return url.PathEscape(streamNames[1])
|
|
|
|
}
|
2021-06-27 08:41:19 +00:00
|
|
|
}
|