Files
sing-box-extended-mirror/parser/link/utils.go
T

76 lines
1.8 KiB
Go

package link
import (
"encoding/json"
"regexp"
"strconv"
"strings"
"github.com/sagernet/sing-box/common"
"github.com/sagernet/sing-box/option"
F "github.com/sagernet/sing/common/format"
"github.com/sagernet/sing/common/json/badoption"
)
func shadowsocksPluginName(plugin string) string {
if index := strings.Index(plugin, ";"); index != -1 {
return plugin[:index]
}
return plugin
}
func shadowsocksPluginOptions(plugin string) string {
if index := strings.Index(plugin, ";"); index != -1 {
return plugin[index+1:]
}
return ""
}
func v2rayTransportWsPath(WebsocketOptions *option.V2RayWebsocketOptions, path string) {
reg := regexp.MustCompile(`^(.*?)(?:\?ed=(\d*))?$`)
result := reg.FindStringSubmatch(path)
if result == nil {
WebsocketOptions.Path = path
return
}
WebsocketOptions.Path = result[1]
if result[2] != "" {
WebsocketOptions.EarlyDataHeaderName = "Sec-WebSocket-Protocol"
WebsocketOptions.MaxEarlyData = common.StringToType[uint32](result[2])
}
}
func v2rayTransportWs(host string, path string) option.V2RayWebsocketOptions {
var WebsocketOptions option.V2RayWebsocketOptions
if host != "" {
WebsocketOptions.Headers = common.StringToType[badoption.HTTPHeader](F.ToString("Host: ", host))
}
if path != "" {
v2rayTransportWsPath(&WebsocketOptions, path)
}
return WebsocketOptions
}
func decodeOption[Value any](fields map[string]json.RawMessage, name string, alias string, destination *Value) {
raw, exists := fields[name]
if !exists {
raw, exists = fields[alias]
}
if !exists {
return
}
var value Value
if json.Unmarshal(raw, &value) == nil {
*destination = value
return
}
if integer, ok := any(destination).(*int64); ok {
var text string
if json.Unmarshal(raw, &text) == nil {
if parsed, err := strconv.ParseInt(text, 10, 64); err == nil {
*integer = parsed
}
}
}
}