mirror of
https://github.com/nitezs/sub2sing-box.git
synced 2024-12-23 22:04:41 -05:00
整理代码
This commit is contained in:
parent
f545848ce1
commit
a7ceb4ecb0
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,6 +1,5 @@
|
|||||||
.vscode/launch.json
|
.vscode/launch.json
|
||||||
.vscode/settings.json
|
.vscode/settings.json
|
||||||
dist
|
dist
|
||||||
*test.go
|
|
||||||
template.json
|
template.json
|
||||||
.idea
|
.idea
|
@ -4,7 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"sub2sing-box/api/model"
|
"sub2sing-box/api/model"
|
||||||
"sub2sing-box/common"
|
"sub2sing-box/common"
|
||||||
putil "sub2sing-box/util"
|
"sub2sing-box/util"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@ -17,7 +17,7 @@ func Convert(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
j, err := putil.DecodeBase64(c.Query("data"))
|
j, err := util.DecodeBase64(c.Query("data"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, gin.H{
|
c.JSON(400, gin.H{
|
||||||
"error": "Invalid data",
|
"error": "Invalid data",
|
||||||
|
@ -2,9 +2,10 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"os"
|
"os"
|
||||||
. "sub2sing-box/common"
|
"sub2sing-box/common"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var subscriptions []string
|
var subscriptions []string
|
||||||
@ -39,7 +40,7 @@ var convertCmd = &cobra.Command{
|
|||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
result := ""
|
result := ""
|
||||||
var err error
|
var err error
|
||||||
result, err = Convert(
|
result, err = common.Convert(
|
||||||
subscriptions,
|
subscriptions,
|
||||||
proxies,
|
proxies,
|
||||||
template,
|
template,
|
||||||
|
@ -233,7 +233,7 @@ func ConvertCProxyToSProxy(proxy string) (model.Outbound, error) {
|
|||||||
return proxy, nil
|
return proxy, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return model.Outbound{}, errors.New("Unknown proxy format")
|
return model.Outbound{}, errors.New("unknown proxy format")
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConvertCProxyToJson(proxy string) (string, error) {
|
func ConvertCProxyToJson(proxy string) (string, error) {
|
||||||
|
11
constant/prefix.go
Normal file
11
constant/prefix.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package constant
|
||||||
|
|
||||||
|
const (
|
||||||
|
HysteriaPrefix string = "hysteria://"
|
||||||
|
Hysteria2Prefix1 string = "hysteria2://"
|
||||||
|
Hysteria2Prefix2 string = "hy2://"
|
||||||
|
ShadowsocksPrefix string = "ss://"
|
||||||
|
TrojanPrefix string = "trojan://"
|
||||||
|
VLESSPrefix string = "vless://"
|
||||||
|
VMessPrefix string = "vmess://"
|
||||||
|
)
|
232
model/config.go
232
model/config.go
@ -18,189 +18,14 @@ func (l *Listable[T]) UnmarshalJSON(data []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type Obfs struct {
|
|
||||||
Str string
|
|
||||||
Obfs *Hysteria2Obfs
|
|
||||||
IsStr bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *Obfs) UnmarshalJSON(data []byte) error {
|
|
||||||
var str string
|
|
||||||
if err := json.Unmarshal(data, &str); err == nil {
|
|
||||||
o.Str = str
|
|
||||||
o.IsStr = true
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
var obfs Hysteria2Obfs
|
|
||||||
if err := json.Unmarshal(data, &obfs); err == nil {
|
|
||||||
o.IsStr = false
|
|
||||||
o.Obfs = &obfs
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o Obfs) MarshalJSON() ([]byte, error) {
|
|
||||||
if o.IsStr {
|
|
||||||
return json.Marshal(o.Str)
|
|
||||||
}
|
|
||||||
return json.Marshal(o.Obfs)
|
|
||||||
}
|
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Log *LogOptions `json:"log,omitempty"`
|
Log any `json:"log,omitempty"`
|
||||||
DNS *DNSOptions `json:"dns,omitempty"`
|
DNS any `json:"dns,omitempty"`
|
||||||
NTP *NTPOptions `json:"ntp,omitempty"`
|
NTP any `json:"ntp,omitempty"`
|
||||||
Inbounds []Inbound `json:"inbounds,omitempty"`
|
Inbounds any `json:"inbounds,omitempty"`
|
||||||
Outbounds []Outbound `json:"outbounds,omitempty"`
|
Outbounds []Outbound `json:"outbounds,omitempty"`
|
||||||
Route *RouteOptions `json:"route,omitempty"`
|
Route *RouteOptions `json:"route,omitempty"`
|
||||||
Experimental *ExperimentalOptions `json:"experimental,omitempty"`
|
Experimental any `json:"experimental,omitempty"`
|
||||||
}
|
|
||||||
|
|
||||||
type LogOptions struct {
|
|
||||||
Disabled bool `json:"disabled,omitempty"`
|
|
||||||
Level string `json:"level,omitempty"`
|
|
||||||
Output string `json:"output,omitempty"`
|
|
||||||
Timestamp bool `json:"timestamp,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type DNSOptions struct {
|
|
||||||
Servers Listable[DNSServerOptions] `json:"servers,omitempty"`
|
|
||||||
Rules Listable[DNSRule] `json:"rules,omitempty"`
|
|
||||||
Final string `json:"final,omitempty"`
|
|
||||||
ReverseMapping bool `json:"reverse_mapping,omitempty"`
|
|
||||||
FakeIP *DNSFakeIPOptions `json:"fakeip,omitempty"`
|
|
||||||
Strategy string `json:"strategy,omitempty"`
|
|
||||||
DisableCache bool `json:"disable_cache,omitempty"`
|
|
||||||
DisableExpire bool `json:"disable_expire,omitempty"`
|
|
||||||
IndependentCache bool `json:"independent_cache,omitempty"`
|
|
||||||
ClientSubnet string `json:"client_subnet,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type DNSServerOptions struct {
|
|
||||||
Tag string `json:"tag,omitempty"`
|
|
||||||
Address string `json:"address"`
|
|
||||||
AddressResolver string `json:"address_resolver,omitempty"`
|
|
||||||
AddressStrategy string `json:"address_strategy,omitempty"`
|
|
||||||
Strategy string `json:"strategy,omitempty"`
|
|
||||||
Detour string `json:"detour,omitempty"`
|
|
||||||
ClientSubnet string `json:"client_subnet,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type DNSRule struct {
|
|
||||||
Type string `json:"type,omitempty"`
|
|
||||||
Inbound Listable[string] `json:"inbound,omitempty"`
|
|
||||||
IPVersion int `json:"ip_version,omitempty"`
|
|
||||||
QueryType Listable[string] `json:"query_type,omitempty"`
|
|
||||||
Network Listable[string] `json:"network,omitempty"`
|
|
||||||
AuthUser Listable[string] `json:"auth_user,omitempty"`
|
|
||||||
Protocol Listable[string] `json:"protocol,omitempty"`
|
|
||||||
Domain Listable[string] `json:"domain,omitempty"`
|
|
||||||
DomainSuffix Listable[string] `json:"domain_suffix,omitempty"`
|
|
||||||
DomainKeyword Listable[string] `json:"domain_keyword,omitempty"`
|
|
||||||
DomainRegex Listable[string] `json:"domain_regex,omitempty"`
|
|
||||||
Geosite Listable[string] `json:"geosite,omitempty"`
|
|
||||||
SourceGeoIP Listable[string] `json:"source_geoip,omitempty"`
|
|
||||||
GeoIP Listable[string] `json:"geoip,omitempty"`
|
|
||||||
IPCIDR Listable[string] `json:"ip_cidr,omitempty"`
|
|
||||||
IPIsPrivate bool `json:"ip_is_private,omitempty"`
|
|
||||||
SourceIPCIDR Listable[string] `json:"source_ip_cidr,omitempty"`
|
|
||||||
SourceIPIsPrivate bool `json:"source_ip_is_private,omitempty"`
|
|
||||||
SourcePort Listable[uint16] `json:"source_port,omitempty"`
|
|
||||||
SourcePortRange Listable[string] `json:"source_port_range,omitempty"`
|
|
||||||
Port Listable[uint16] `json:"port,omitempty"`
|
|
||||||
PortRange Listable[string] `json:"port_range,omitempty"`
|
|
||||||
ProcessName Listable[string] `json:"process_name,omitempty"`
|
|
||||||
ProcessPath Listable[string] `json:"process_path,omitempty"`
|
|
||||||
PackageName Listable[string] `json:"package_name,omitempty"`
|
|
||||||
User Listable[string] `json:"user,omitempty"`
|
|
||||||
UserID Listable[int32] `json:"user_id,omitempty"`
|
|
||||||
Outbound Listable[string] `json:"outbound,omitempty"`
|
|
||||||
ClashMode string `json:"clash_mode,omitempty"`
|
|
||||||
WIFISSID Listable[string] `json:"wifi_ssid,omitempty"`
|
|
||||||
WIFIBSSID Listable[string] `json:"wifi_bssid,omitempty"`
|
|
||||||
RuleSet Listable[string] `json:"rule_set,omitempty"`
|
|
||||||
RuleSetIPCIDRMatchSource bool `json:"rule_set_ipcidr_match_source,omitempty"`
|
|
||||||
Invert bool `json:"invert,omitempty"`
|
|
||||||
Server string `json:"server,omitempty"`
|
|
||||||
DisableCache bool `json:"disable_cache,omitempty"`
|
|
||||||
RewriteTTL uint32 `json:"rewrite_ttl,omitempty"`
|
|
||||||
ClientSubnet string `json:"client_subnet,omitempty"`
|
|
||||||
Mode string `json:"mode,omitempty"`
|
|
||||||
Rules Listable[DNSRule] `json:"rules,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type DNSFakeIPOptions struct {
|
|
||||||
Enabled bool `json:"enabled,omitempty"`
|
|
||||||
Inet4Range string `json:"inet4_range,omitempty"`
|
|
||||||
Inet6Range string `json:"inet6_range,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type NTPOptions struct {
|
|
||||||
Enabled bool `json:"enabled,omitempty"`
|
|
||||||
Server string `json:"server,omitempty"`
|
|
||||||
ServerPort uint16 `json:"server_port,omitempty"`
|
|
||||||
Interval string `json:"interval,omitempty"`
|
|
||||||
WriteToSystem bool `json:"write_to_system,omitempty"`
|
|
||||||
Detour string `json:"detour,omitempty"`
|
|
||||||
BindInterface string `json:"bind_interface,omitempty"`
|
|
||||||
Inet4BindAddress string `json:"inet4_bind_address,omitempty"`
|
|
||||||
Inet6BindAddress string `json:"inet6_bind_address,omitempty"`
|
|
||||||
ProtectPath string `json:"protect_path,omitempty"`
|
|
||||||
RoutingMark int `json:"routing_mark,omitempty"`
|
|
||||||
ReuseAddr bool `json:"reuse_addr,omitempty"`
|
|
||||||
ConnectTimeout string `json:"connect_timeout,omitempty"`
|
|
||||||
TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
|
|
||||||
TCPMultiPath bool `json:"tcp_multi_path,omitempty"`
|
|
||||||
UDPFragment bool `json:"udp_fragment,omitempty"`
|
|
||||||
DomainStrategy string `json:"domain_strategy,omitempty"`
|
|
||||||
FallbackDelay string `json:"fallback_delay,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Inbound struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
Tag string `json:"tag,omitempty"`
|
|
||||||
InterfaceName string `json:"interface_name,omitempty"`
|
|
||||||
MTU uint32 `json:"mtu,omitempty"`
|
|
||||||
GSO bool `json:"gso,omitempty"`
|
|
||||||
Inet4Address Listable[string] `json:"inet4_address,omitempty"`
|
|
||||||
Inet6Address Listable[string] `json:"inet6_address,omitempty"`
|
|
||||||
AutoRoute bool `json:"auto_route,omitempty"`
|
|
||||||
StrictRoute bool `json:"strict_route,omitempty"`
|
|
||||||
Inet4RouteAddress Listable[string] `json:"inet4_route_address,omitempty"`
|
|
||||||
Inet6RouteAddress Listable[string] `json:"inet6_route_address,omitempty"`
|
|
||||||
Inet4RouteExcludeAddress Listable[string] `json:"inet4_route_exclude_address,omitempty"`
|
|
||||||
Inet6RouteExcludeAddress Listable[string] `json:"inet6_route_exclude_address,omitempty"`
|
|
||||||
IncludeInterface Listable[string] `json:"include_interface,omitempty"`
|
|
||||||
ExcludeInterface Listable[string] `json:"exclude_interface,omitempty"`
|
|
||||||
IncludeUID Listable[uint32] `json:"include_uid,omitempty"`
|
|
||||||
IncludeUIDRange Listable[string] `json:"include_uid_range,omitempty"`
|
|
||||||
ExcludeUID Listable[uint32] `json:"exclude_uid,omitempty"`
|
|
||||||
ExcludeUIDRange Listable[string] `json:"exclude_uid_range,omitempty"`
|
|
||||||
IncludeAndroidUser Listable[int] `json:"include_android_user,omitempty"`
|
|
||||||
IncludePackage Listable[string] `json:"include_package,omitempty"`
|
|
||||||
ExcludePackage Listable[string] `json:"exclude_package,omitempty"`
|
|
||||||
EndpointIndependentNat bool `json:"endpoint_independent_nat,omitempty"`
|
|
||||||
UDPTimeout string `json:"udp_timeout,omitempty"`
|
|
||||||
Stack string `json:"stack,omitempty"`
|
|
||||||
Platform *TunPlatformOptions `json:"platform,omitempty"`
|
|
||||||
SniffEnabled bool `json:"sniff,omitempty"`
|
|
||||||
SniffOverrideDestination bool `json:"sniff_override_destination,omitempty"`
|
|
||||||
SniffTimeout string `json:"sniff_timeout,omitempty"`
|
|
||||||
DomainStrategy string `json:"domain_strategy,omitempty"`
|
|
||||||
UDPDisableDomainUnmapping bool `json:"udp_disable_domain_unmapping,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type TunPlatformOptions struct {
|
|
||||||
HTTPProxy *HTTPProxyOptions `json:"http_proxy,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type HTTPProxyOptions struct {
|
|
||||||
Enabled bool `json:"enabled,omitempty"`
|
|
||||||
Server string `json:"server"`
|
|
||||||
ServerPort uint16 `json:"server_port"`
|
|
||||||
BypassDomain Listable[string] `json:"bypass_domain,omitempty"`
|
|
||||||
MatchDomain Listable[string] `json:"match_domain,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type RouteOptions struct {
|
type RouteOptions struct {
|
||||||
@ -275,50 +100,3 @@ type RuleSet struct {
|
|||||||
DownloadDetour string `json:"download_detour,omitempty"`
|
DownloadDetour string `json:"download_detour,omitempty"`
|
||||||
UpdateInterval string `json:"update_interval,omitempty"`
|
UpdateInterval string `json:"update_interval,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ExperimentalOptions struct {
|
|
||||||
CacheFile *CacheFileOptions `json:"cache_file,omitempty"`
|
|
||||||
ClashAPI *ClashAPIOptions `json:"clash_api,omitempty"`
|
|
||||||
V2RayAPI *V2RayAPIOptions `json:"v2ray_api,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type CacheFileOptions struct {
|
|
||||||
Enabled bool `json:"enabled,omitempty"`
|
|
||||||
Path string `json:"path,omitempty"`
|
|
||||||
CacheID string `json:"cache_id,omitempty"`
|
|
||||||
StoreFakeIP bool `json:"store_fakeip,omitempty"`
|
|
||||||
StoreRDRC bool `json:"store_rdrc,omitempty"`
|
|
||||||
RDRCTimeout string `json:"rdrc_timeout,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ClashAPIOptions struct {
|
|
||||||
ExternalController string `json:"external_controller,omitempty"`
|
|
||||||
ExternalUI string `json:"external_ui,omitempty"`
|
|
||||||
ExternalUIDownloadURL string `json:"external_ui_download_url,omitempty"`
|
|
||||||
ExternalUIDownloadDetour string `json:"external_ui_download_detour,omitempty"`
|
|
||||||
Secret string `json:"secret,omitempty"`
|
|
||||||
DefaultMode string `json:"default_mode,omitempty"`
|
|
||||||
|
|
||||||
// Deprecated: migrated to global cache file
|
|
||||||
CacheFile string `json:"cache_file,omitempty"`
|
|
||||||
// Deprecated: migrated to global cache file
|
|
||||||
CacheID string `json:"cache_id,omitempty"`
|
|
||||||
// Deprecated: migrated to global cache file
|
|
||||||
StoreMode bool `json:"store_mode,omitempty"`
|
|
||||||
// Deprecated: migrated to global cache file
|
|
||||||
StoreSelected bool `json:"store_selected,omitempty"`
|
|
||||||
// Deprecated: migrated to global cache file
|
|
||||||
StoreFakeIP bool `json:"store_fakeip,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type V2RayAPIOptions struct {
|
|
||||||
Listen string `json:"listen,omitempty"`
|
|
||||||
Stats *V2RayStatsServiceOptions `json:"stats,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type V2RayStatsServiceOptions struct {
|
|
||||||
Enabled bool `json:"enabled,omitempty"`
|
|
||||||
Inbounds Listable[string] `json:"inbounds,omitempty"`
|
|
||||||
Outbounds Listable[string] `json:"outbounds,omitempty"`
|
|
||||||
Users Listable[string] `json:"users,omitempty"`
|
|
||||||
}
|
|
||||||
|
24
parser/error.go
Normal file
24
parser/error.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package parser
|
||||||
|
|
||||||
|
type ParseError struct {
|
||||||
|
Type ParseErrorType
|
||||||
|
Message string
|
||||||
|
Raw string
|
||||||
|
}
|
||||||
|
|
||||||
|
type ParseErrorType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
ErrInvalidPrefix ParseErrorType = "invalid url prefix"
|
||||||
|
ErrInvalidStruct ParseErrorType = "invalid struct"
|
||||||
|
ErrInvalidPort ParseErrorType = "invalid port number"
|
||||||
|
ErrCannotParseParams ParseErrorType = "cannot parse query parameters"
|
||||||
|
ErrInvalidBase64 ParseErrorType = "invalid base64"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (e *ParseError) Error() string {
|
||||||
|
if e.Message != "" {
|
||||||
|
return string(e.Type) + ": " + e.Message + " \"" + e.Raw + "\""
|
||||||
|
}
|
||||||
|
return string(e.Type)
|
||||||
|
}
|
@ -1,78 +1,76 @@
|
|||||||
package parser
|
package parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sub2sing-box/constant"
|
||||||
"sub2sing-box/model"
|
"sub2sing-box/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
//hysteria://host:port?protocol=udp&auth=123456&peer=sni.domain&insecure=1&upmbps=100&downmbps=100&alpn=hysteria&obfs=xplus&obfsParam=123456#remarks
|
|
||||||
//
|
|
||||||
//- host: hostname or IP address of the server to connect to (required)
|
|
||||||
//- port: port of the server to connect to (required)
|
|
||||||
//- protocol: protocol to use ("udp", "wechat-video", "faketcp") (optional, default: "udp")
|
|
||||||
//- auth: authentication payload (string) (optional)
|
|
||||||
//- peer: SNI for TLS (optional)
|
|
||||||
//- insecure: ignore certificate errors (optional)
|
|
||||||
//- upmbps: upstream bandwidth in Mbps (required)
|
|
||||||
//- downmbps: downstream bandwidth in Mbps (required)
|
|
||||||
//- alpn: QUIC ALPN (optional)
|
|
||||||
//- obfs: Obfuscation mode (optional, empty or "xplus")
|
|
||||||
//- obfsParam: Obfuscation password (optional)
|
|
||||||
//- remarks: remarks (optional)
|
|
||||||
|
|
||||||
func ParseHysteria(proxy string) (model.Outbound, error) {
|
func ParseHysteria(proxy string) (model.Outbound, error) {
|
||||||
if !strings.HasPrefix(proxy, "hysteria://") {
|
if !strings.HasPrefix(proxy, constant.HysteriaPrefix) {
|
||||||
return model.Outbound{}, errors.New("invalid hysteria Url")
|
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||||
}
|
}
|
||||||
parts := strings.SplitN(strings.TrimPrefix(proxy, "hysteria://"), "?", 2)
|
|
||||||
serverInfo := strings.SplitN(parts[0], ":", 2)
|
proxy = strings.TrimPrefix(proxy, constant.HysteriaPrefix)
|
||||||
|
urlParts := strings.SplitN(proxy, "?", 2)
|
||||||
|
if len(urlParts) != 2 {
|
||||||
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrInvalidStruct,
|
||||||
|
Message: "missing character '?' in url",
|
||||||
|
Raw: proxy,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
serverInfo := strings.SplitN(urlParts[0], ":", 2)
|
||||||
if len(serverInfo) != 2 {
|
if len(serverInfo) != 2 {
|
||||||
return model.Outbound{}, errors.New("invalid hysteria Url")
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrInvalidStruct,
|
||||||
|
Message: "missing server host or port",
|
||||||
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
params, err := url.ParseQuery(parts[1])
|
}
|
||||||
|
server, portStr := serverInfo[0], serverInfo[1]
|
||||||
|
|
||||||
|
port, err := ParsePort(portStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.Outbound{}, errors.New("invalid hysteria Url")
|
return model.Outbound{}, err
|
||||||
}
|
}
|
||||||
host := serverInfo[0]
|
|
||||||
port, err := strconv.Atoi(serverInfo[1])
|
params, err := url.ParseQuery(urlParts[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.Outbound{}, errors.New("invalid hysteria Url")
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrCannotParseParams,
|
||||||
|
Raw: proxy,
|
||||||
|
Message: err.Error(),
|
||||||
}
|
}
|
||||||
protocol := params.Get("protocol")
|
|
||||||
auth := params.Get("auth")
|
|
||||||
// peer := params.Get("peer")
|
|
||||||
insecure := params.Get("insecure")
|
|
||||||
upmbps := params.Get("upmbps")
|
|
||||||
downmbps := params.Get("downmbps")
|
|
||||||
obfs := params.Get("obfs")
|
|
||||||
// obfsParam := params.Get("obfsParam")
|
|
||||||
var alpn []string
|
|
||||||
if params.Get("alpn") != "" {
|
|
||||||
alpn = strings.Split(params.Get("alpn"), ",")
|
|
||||||
} else {
|
|
||||||
alpn = nil
|
|
||||||
}
|
|
||||||
remarks := ""
|
|
||||||
if strings.Contains(parts[1], "#") {
|
|
||||||
r := strings.Split(parts[1], "#")
|
|
||||||
remarks = r[len(r)-1]
|
|
||||||
} else {
|
|
||||||
remarks = serverInfo[0] + ":" + serverInfo[1]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protocol, auth, insecure, upmbps, downmbps, obfs, alpnStr := params.Get("protocol"), params.Get("auth"), params.Get("insecure"), params.Get("upmbps"), params.Get("downmbps"), params.Get("obfs"), params.Get("alpn")
|
||||||
insecureBool, err := strconv.ParseBool(insecure)
|
insecureBool, err := strconv.ParseBool(insecure)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.Outbound{}, errors.New("invalid hysteria Url")
|
insecureBool = false
|
||||||
}
|
}
|
||||||
result := model.Outbound{
|
|
||||||
|
var alpn []string
|
||||||
|
alpnStr = strings.TrimSpace(alpnStr)
|
||||||
|
if alpnStr != "" {
|
||||||
|
alpn = strings.Split(alpnStr, ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
remarks := server + ":" + portStr
|
||||||
|
if params.Get("remarks") != "" {
|
||||||
|
remarks = params.Get("remarks")
|
||||||
|
}
|
||||||
|
|
||||||
|
return model.Outbound{
|
||||||
Type: "hysteria",
|
Type: "hysteria",
|
||||||
Tag: remarks,
|
Tag: remarks,
|
||||||
HysteriaOptions: model.HysteriaOutboundOptions{
|
HysteriaOptions: model.HysteriaOutboundOptions{
|
||||||
ServerOptions: model.ServerOptions{
|
ServerOptions: model.ServerOptions{
|
||||||
Server: host,
|
Server: server,
|
||||||
ServerPort: uint16(port),
|
ServerPort: port,
|
||||||
},
|
},
|
||||||
Up: upmbps,
|
Up: upmbps,
|
||||||
Down: downmbps,
|
Down: downmbps,
|
||||||
@ -87,6 +85,5 @@ func ParseHysteria(proxy string) (model.Outbound, error) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}, nil
|
||||||
return result, nil
|
|
||||||
}
|
}
|
||||||
|
@ -1,39 +1,73 @@
|
|||||||
package parser
|
package parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
"sub2sing-box/constant"
|
||||||
"sub2sing-box/model"
|
"sub2sing-box/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
// hysteria2://letmein@example.com/?insecure=1&obfs=salamander&obfs-password=gawrgura&pinSHA256=deadbeef&sni=real.example.com
|
|
||||||
|
|
||||||
func ParseHysteria2(proxy string) (model.Outbound, error) {
|
func ParseHysteria2(proxy string) (model.Outbound, error) {
|
||||||
if !strings.HasPrefix(proxy, "hysteria2://") && !strings.HasPrefix(proxy, "hy2://") {
|
if !strings.HasPrefix(proxy, constant.Hysteria2Prefix1) &&
|
||||||
return model.Outbound{}, errors.New("invalid hysteria2 Url")
|
!strings.HasPrefix(proxy, constant.Hysteria2Prefix2) {
|
||||||
|
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||||
}
|
}
|
||||||
parts := strings.SplitN(strings.TrimPrefix(proxy, "hysteria2://"), "@", 2)
|
|
||||||
serverInfo := strings.SplitN(parts[1], "/?", 2)
|
proxy = strings.TrimPrefix(proxy, constant.Hysteria2Prefix1)
|
||||||
|
proxy = strings.TrimPrefix(proxy, constant.Hysteria2Prefix2)
|
||||||
|
urlParts := strings.SplitN(proxy, "@", 2)
|
||||||
|
if len(urlParts) != 2 {
|
||||||
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrInvalidStruct,
|
||||||
|
Message: "missing character '@' in url",
|
||||||
|
Raw: proxy,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
password := urlParts[0]
|
||||||
|
|
||||||
|
serverInfo := strings.SplitN(urlParts[1], "/?", 2)
|
||||||
|
if len(serverInfo) != 2 {
|
||||||
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrInvalidStruct,
|
||||||
|
Message: "missing params in url",
|
||||||
|
Raw: proxy,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
paramStr := serverInfo[1]
|
||||||
|
|
||||||
serverAndPort := strings.SplitN(serverInfo[0], ":", 2)
|
serverAndPort := strings.SplitN(serverInfo[0], ":", 2)
|
||||||
|
var server string
|
||||||
|
var portStr string
|
||||||
if len(serverAndPort) == 1 {
|
if len(serverAndPort) == 1 {
|
||||||
serverAndPort = append(serverAndPort, "443")
|
portStr = "443"
|
||||||
} else if len(serverAndPort) != 2 {
|
} else if len(serverAndPort) == 2 {
|
||||||
return model.Outbound{}, errors.New("invalid hysteria2 Url")
|
server, portStr = serverAndPort[0], serverAndPort[1]
|
||||||
|
} else {
|
||||||
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrInvalidStruct,
|
||||||
|
Message: "missing server host or port",
|
||||||
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
params, err := url.ParseQuery(serverInfo[1])
|
}
|
||||||
|
|
||||||
|
port, err := ParsePort(portStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.Outbound{}, errors.New("invalid hysteria2 Url")
|
return model.Outbound{}, err
|
||||||
}
|
}
|
||||||
port, err := strconv.Atoi(serverAndPort[1])
|
|
||||||
|
params, err := url.ParseQuery(paramStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.Outbound{}, errors.New("invalid hysteria2 Url")
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrCannotParseParams,
|
||||||
|
Raw: proxy,
|
||||||
|
Message: err.Error(),
|
||||||
}
|
}
|
||||||
remarks := params.Get("name")
|
}
|
||||||
server := serverAndPort[0]
|
|
||||||
password := parts[0]
|
remarks, network, obfs, obfsPassword, pinSHA256, insecure, sni := params.Get("name"), params.Get("network"), params.Get("obfs"), params.Get("obfs-password"), params.Get("pinSHA256"), params.Get("insecure"), params.Get("sni")
|
||||||
network := params.Get("network")
|
enableTLS := pinSHA256 != ""
|
||||||
|
insecureBool := insecure == "1"
|
||||||
|
|
||||||
result := model.Outbound{
|
result := model.Outbound{
|
||||||
Type: "hysteria2",
|
Type: "hysteria2",
|
||||||
Tag: remarks,
|
Tag: remarks,
|
||||||
@ -44,14 +78,14 @@ func ParseHysteria2(proxy string) (model.Outbound, error) {
|
|||||||
},
|
},
|
||||||
Password: password,
|
Password: password,
|
||||||
Obfs: &model.Hysteria2Obfs{
|
Obfs: &model.Hysteria2Obfs{
|
||||||
Type: params.Get("obfs"),
|
Type: obfs,
|
||||||
Password: params.Get("obfs-password"),
|
Password: obfsPassword,
|
||||||
},
|
},
|
||||||
OutboundTLSOptionsContainer: model.OutboundTLSOptionsContainer{
|
OutboundTLSOptionsContainer: model.OutboundTLSOptionsContainer{
|
||||||
TLS: &model.OutboundTLSOptions{Enabled: params.Get("pinSHA256") != "",
|
TLS: &model.OutboundTLSOptions{Enabled: enableTLS,
|
||||||
Insecure: params.Get("insecure") == "1",
|
Insecure: insecureBool,
|
||||||
ServerName: params.Get("sni"),
|
ServerName: sni,
|
||||||
Certificate: []string{params.Get("pinSHA256")}},
|
Certificate: []string{pinSHA256}},
|
||||||
},
|
},
|
||||||
Network: network,
|
Network: network,
|
||||||
},
|
},
|
||||||
|
@ -1,15 +1,16 @@
|
|||||||
package parser
|
package parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sub2sing-box/constant"
|
||||||
"sub2sing-box/model"
|
"sub2sing-box/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ParserMap map[string]func(string) (model.Outbound, error) = map[string]func(string) (model.Outbound, error){
|
var ParserMap map[string]func(string) (model.Outbound, error) = map[string]func(string) (model.Outbound, error){
|
||||||
"ss://": ParseShadowsocks,
|
constant.ShadowsocksPrefix: ParseShadowsocks,
|
||||||
"vmess://": ParseVmess,
|
constant.VMessPrefix: ParseVmess,
|
||||||
"trojan://": ParseTrojan,
|
constant.TrojanPrefix: ParseTrojan,
|
||||||
"vless://": ParseVless,
|
constant.VLESSPrefix: ParseVless,
|
||||||
"hysteria://": ParseHysteria,
|
constant.HysteriaPrefix: ParseHysteria,
|
||||||
"hy2://": ParseHysteria2,
|
constant.Hysteria2Prefix1: ParseHysteria2,
|
||||||
"hysteria2://": ParseHysteria2,
|
constant.Hysteria2Prefix2: ParseHysteria2,
|
||||||
}
|
}
|
||||||
|
23
parser/port.go
Normal file
23
parser/port.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package parser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ParsePort(portStr string) (uint16, error) {
|
||||||
|
port, err := strconv.Atoi(portStr)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return 0, &ParseError{
|
||||||
|
Type: ErrInvalidPort,
|
||||||
|
Message: portStr,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if port < 1 || port > 65535 {
|
||||||
|
return 0, &ParseError{
|
||||||
|
Type: ErrInvalidPort,
|
||||||
|
Message: portStr,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return uint16(port), nil
|
||||||
|
}
|
@ -1,62 +1,87 @@
|
|||||||
package parser
|
package parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
"sub2sing-box/constant"
|
||||||
"sub2sing-box/model"
|
"sub2sing-box/model"
|
||||||
"sub2sing-box/util"
|
"sub2sing-box/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParseShadowsocks(proxy string) (model.Outbound, error) {
|
func ParseShadowsocks(proxy string) (model.Outbound, error) {
|
||||||
if !strings.HasPrefix(proxy, "ss://") {
|
if !strings.HasPrefix(proxy, constant.ShadowsocksPrefix) {
|
||||||
return model.Outbound{}, errors.New("invalid ss Url")
|
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||||
}
|
}
|
||||||
parts := strings.SplitN(strings.TrimPrefix(proxy, "ss://"), "@", 2)
|
|
||||||
if len(parts) != 2 {
|
proxy = strings.TrimPrefix(proxy, constant.ShadowsocksPrefix)
|
||||||
return model.Outbound{}, errors.New("invalid ss Url")
|
urlParts := strings.SplitN(proxy, "@", 2)
|
||||||
|
if len(urlParts) != 2 {
|
||||||
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrInvalidStruct,
|
||||||
|
Message: "missing character '@' in url",
|
||||||
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
if !strings.Contains(parts[0], ":") {
|
}
|
||||||
decoded, err := util.DecodeBase64(parts[0])
|
|
||||||
|
var serverAndPort []string
|
||||||
|
if !strings.Contains(urlParts[0], ":") {
|
||||||
|
decoded, err := util.DecodeBase64(urlParts[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.Outbound{}, errors.New("invalid ss Url" + err.Error())
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrInvalidStruct,
|
||||||
|
Message: "invalid base64 encoded",
|
||||||
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
parts[0] = decoded
|
|
||||||
}
|
}
|
||||||
credentials := strings.SplitN(parts[0], ":", 2)
|
urlParts[0] = decoded
|
||||||
|
}
|
||||||
|
credentials := strings.SplitN(urlParts[0], ":", 2)
|
||||||
if len(credentials) != 2 {
|
if len(credentials) != 2 {
|
||||||
return model.Outbound{}, errors.New("invalid ss Url")
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrInvalidStruct,
|
||||||
|
Message: "missing server host or port",
|
||||||
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
serverInfo := strings.SplitN(parts[1], "#", 2)
|
|
||||||
serverAndPort := strings.SplitN(serverInfo[0], ":", 2)
|
|
||||||
if len(serverAndPort) != 2 {
|
|
||||||
return model.Outbound{}, errors.New("invalid ss Url")
|
|
||||||
}
|
}
|
||||||
port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))
|
method, password := credentials[0], credentials[1]
|
||||||
|
|
||||||
|
serverInfo := strings.SplitN(urlParts[1], "#", 2)
|
||||||
|
serverAndPort = strings.SplitN(serverInfo[0], ":", 2)
|
||||||
|
server, portStr := serverAndPort[0], serverAndPort[1]
|
||||||
|
if len(serverInfo) != 2 {
|
||||||
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrInvalidStruct,
|
||||||
|
Message: "missing server host or port",
|
||||||
|
Raw: proxy,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
port, err := ParsePort(portStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.Outbound{}, errors.New("invalid ss Url" + err.Error())
|
return model.Outbound{}, err
|
||||||
}
|
}
|
||||||
remarks := ""
|
|
||||||
|
var remarks string
|
||||||
if len(serverInfo) == 2 {
|
if len(serverInfo) == 2 {
|
||||||
unescape, err := url.QueryUnescape(serverInfo[1])
|
unescape, err := url.QueryUnescape(serverInfo[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.Outbound{}, errors.New("invalid ss Url" + err.Error())
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrInvalidStruct,
|
||||||
|
Message: "cannot unescape remarks",
|
||||||
|
Raw: proxy,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
remarks = strings.TrimSpace(unescape)
|
remarks = strings.TrimSpace(unescape)
|
||||||
} else {
|
} else {
|
||||||
remarks = strings.TrimSpace(serverAndPort[0])
|
remarks = strings.TrimSpace(server + ":" + portStr)
|
||||||
}
|
}
|
||||||
method := credentials[0]
|
|
||||||
password := credentials[1]
|
|
||||||
server := strings.TrimSpace(serverAndPort[0])
|
|
||||||
result := model.Outbound{
|
result := model.Outbound{
|
||||||
Type: "shadowsocks",
|
Type: "shadowsocks",
|
||||||
Tag: remarks,
|
Tag: remarks,
|
||||||
ShadowsocksOptions: model.ShadowsocksOutboundOptions{
|
ShadowsocksOptions: model.ShadowsocksOutboundOptions{
|
||||||
ServerOptions: model.ServerOptions{
|
ServerOptions: model.ServerOptions{
|
||||||
Server: server,
|
Server: server,
|
||||||
ServerPort: uint16(port),
|
ServerPort: port,
|
||||||
},
|
},
|
||||||
Method: method,
|
Method: method,
|
||||||
Password: password,
|
Password: password,
|
||||||
|
103
parser/trojan.go
103
parser/trojan.go
@ -1,118 +1,155 @@
|
|||||||
package parser
|
package parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
"sub2sing-box/constant"
|
||||||
"sub2sing-box/model"
|
"sub2sing-box/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParseTrojan(proxy string) (model.Outbound, error) {
|
func ParseTrojan(proxy string) (model.Outbound, error) {
|
||||||
if !strings.HasPrefix(proxy, "trojan://") {
|
if !strings.HasPrefix(proxy, constant.TrojanPrefix) {
|
||||||
return model.Outbound{}, errors.New("invalid trojan Url")
|
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||||
}
|
}
|
||||||
parts := strings.SplitN(strings.TrimPrefix(proxy, "trojan://"), "@", 2)
|
|
||||||
if len(parts) != 2 {
|
proxy = strings.TrimPrefix(proxy, constant.TrojanPrefix)
|
||||||
return model.Outbound{}, errors.New("invalid trojan Url")
|
urlParts := strings.SplitN(proxy, "@", 2)
|
||||||
|
if len(urlParts) != 2 {
|
||||||
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrInvalidStruct,
|
||||||
|
Message: "missing character '@' in url",
|
||||||
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
serverInfo := strings.SplitN(parts[1], "#", 2)
|
}
|
||||||
|
password := strings.TrimSpace(urlParts[0])
|
||||||
|
|
||||||
|
serverInfo := strings.SplitN(urlParts[1], "#", 2)
|
||||||
serverAndPortAndParams := strings.SplitN(serverInfo[0], "?", 2)
|
serverAndPortAndParams := strings.SplitN(serverInfo[0], "?", 2)
|
||||||
|
if len(serverAndPortAndParams) != 2 {
|
||||||
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrInvalidStruct,
|
||||||
|
Message: "missing character '?' in url",
|
||||||
|
Raw: proxy,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
serverAndPort := strings.SplitN(serverAndPortAndParams[0], ":", 2)
|
serverAndPort := strings.SplitN(serverAndPortAndParams[0], ":", 2)
|
||||||
|
if len(serverAndPort) != 2 {
|
||||||
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrInvalidStruct,
|
||||||
|
Message: "missing server host or port",
|
||||||
|
Raw: proxy,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
server, portStr := serverAndPort[0], serverAndPort[1]
|
||||||
|
|
||||||
params, err := url.ParseQuery(serverAndPortAndParams[1])
|
params, err := url.ParseQuery(serverAndPortAndParams[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.Outbound{}, err
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrCannotParseParams,
|
||||||
|
Raw: proxy,
|
||||||
|
Message: err.Error(),
|
||||||
}
|
}
|
||||||
if len(serverAndPort) != 2 {
|
|
||||||
return model.Outbound{}, errors.New("invalid trojan Url")
|
|
||||||
}
|
}
|
||||||
port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))
|
|
||||||
|
port, err := ParsePort(portStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.Outbound{}, err
|
return model.Outbound{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
remarks := ""
|
remarks := ""
|
||||||
if len(serverInfo) == 2 {
|
if len(serverInfo) == 2 {
|
||||||
remarks, _ = url.QueryUnescape(strings.TrimSpace(serverInfo[1]))
|
remarks, _ = url.QueryUnescape(strings.TrimSpace(serverInfo[1]))
|
||||||
} else {
|
} else {
|
||||||
remarks = serverAndPort[0]
|
remarks = serverAndPort[0]
|
||||||
}
|
}
|
||||||
server := strings.TrimSpace(serverAndPort[0])
|
|
||||||
password := strings.TrimSpace(parts[0])
|
network, security, alpnStr, sni, pbk, sid, fp, path, host, serviceName := params.Get("type"), params.Get("security"), params.Get("alpn"), params.Get("sni"), params.Get("pbk"), params.Get("sid"), params.Get("fp"), params.Get("path"), params.Get("host"), params.Get("serviceName")
|
||||||
|
|
||||||
|
var alpn []string
|
||||||
|
if strings.Contains(alpnStr, ",") {
|
||||||
|
alpn = strings.Split(alpnStr, ",")
|
||||||
|
} else {
|
||||||
|
alpn = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
enableUTLS := fp != ""
|
||||||
|
|
||||||
result := model.Outbound{
|
result := model.Outbound{
|
||||||
Type: "trojan",
|
Type: "trojan",
|
||||||
Tag: remarks,
|
Tag: remarks,
|
||||||
TrojanOptions: model.TrojanOutboundOptions{
|
TrojanOptions: model.TrojanOutboundOptions{
|
||||||
ServerOptions: model.ServerOptions{
|
ServerOptions: model.ServerOptions{
|
||||||
Server: server,
|
Server: server,
|
||||||
ServerPort: uint16(port),
|
ServerPort: port,
|
||||||
},
|
},
|
||||||
Password: password,
|
Password: password,
|
||||||
Network: params.Get("type"),
|
Network: network,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if params.Get("security") == "xtls" || params.Get("security") == "tls" {
|
|
||||||
var alpn []string
|
if security == "xtls" || security == "tls" {
|
||||||
if strings.Contains(params.Get("alpn"), ",") {
|
|
||||||
alpn = strings.Split(params.Get("alpn"), ",")
|
|
||||||
} else {
|
|
||||||
alpn = nil
|
|
||||||
}
|
|
||||||
result.TrojanOptions.OutboundTLSOptionsContainer = model.OutboundTLSOptionsContainer{
|
result.TrojanOptions.OutboundTLSOptionsContainer = model.OutboundTLSOptionsContainer{
|
||||||
TLS: &model.OutboundTLSOptions{
|
TLS: &model.OutboundTLSOptions{
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
ALPN: alpn,
|
ALPN: alpn,
|
||||||
ServerName: params.Get("sni"),
|
ServerName: sni,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Get("security") == "reality" {
|
if params.Get("security") == "reality" {
|
||||||
result.TrojanOptions.OutboundTLSOptionsContainer = model.OutboundTLSOptionsContainer{
|
result.TrojanOptions.OutboundTLSOptionsContainer = model.OutboundTLSOptionsContainer{
|
||||||
TLS: &model.OutboundTLSOptions{
|
TLS: &model.OutboundTLSOptions{
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
ServerName: params.Get("sni"),
|
ServerName: sni,
|
||||||
Reality: &model.OutboundRealityOptions{
|
Reality: &model.OutboundRealityOptions{
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
PublicKey: params.Get("pbk"),
|
PublicKey: pbk,
|
||||||
ShortID: params.Get("sid"),
|
ShortID: sid,
|
||||||
},
|
},
|
||||||
UTLS: &model.OutboundUTLSOptions{
|
UTLS: &model.OutboundUTLSOptions{
|
||||||
Enabled: params.Get("fp") != "",
|
Enabled: enableUTLS,
|
||||||
Fingerprint: params.Get("fp"),
|
Fingerprint: fp,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Get("type") == "ws" {
|
if params.Get("type") == "ws" {
|
||||||
result.TrojanOptions.Transport = &model.V2RayTransportOptions{
|
result.TrojanOptions.Transport = &model.V2RayTransportOptions{
|
||||||
Type: "ws",
|
Type: "ws",
|
||||||
WebsocketOptions: model.V2RayWebsocketOptions{
|
WebsocketOptions: model.V2RayWebsocketOptions{
|
||||||
Path: params.Get("path"),
|
Path: path,
|
||||||
Headers: map[string]string{
|
Headers: map[string]string{
|
||||||
"Host": params.Get("host"),
|
"Host": host,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Get("type") == "http" {
|
if params.Get("type") == "http" {
|
||||||
result.TrojanOptions.Transport = &model.V2RayTransportOptions{
|
result.TrojanOptions.Transport = &model.V2RayTransportOptions{
|
||||||
Type: "http",
|
Type: "http",
|
||||||
HTTPOptions: model.V2RayHTTPOptions{
|
HTTPOptions: model.V2RayHTTPOptions{
|
||||||
Host: []string{params.Get("host")},
|
Host: []string{host},
|
||||||
Path: params.Get("path"),
|
Path: params.Get("path"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Get("type") == "quic" {
|
if params.Get("type") == "quic" {
|
||||||
result.TrojanOptions.Transport = &model.V2RayTransportOptions{
|
result.TrojanOptions.Transport = &model.V2RayTransportOptions{
|
||||||
Type: "quic",
|
Type: "quic",
|
||||||
QUICOptions: model.V2RayQUICOptions{},
|
QUICOptions: model.V2RayQUICOptions{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Get("type") == "grpc" {
|
if params.Get("type") == "grpc" {
|
||||||
result.TrojanOptions.Transport = &model.V2RayTransportOptions{
|
result.TrojanOptions.Transport = &model.V2RayTransportOptions{
|
||||||
Type: "grpc",
|
Type: "grpc",
|
||||||
GRPCOptions: model.V2RayGRPCOptions{
|
GRPCOptions: model.V2RayGRPCOptions{
|
||||||
ServiceName: params.Get("serviceName"),
|
ServiceName: serviceName,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
129
parser/vless.go
129
parser/vless.go
@ -1,35 +1,59 @@
|
|||||||
package parser
|
package parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
"sub2sing-box/constant"
|
||||||
"sub2sing-box/model"
|
"sub2sing-box/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParseVless(proxy string) (model.Outbound, error) {
|
func ParseVless(proxy string) (model.Outbound, error) {
|
||||||
if !strings.HasPrefix(proxy, "vless://") {
|
if !strings.HasPrefix(proxy, constant.VLESSPrefix) {
|
||||||
return model.Outbound{}, errors.New("invalid vless Url")
|
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||||
}
|
}
|
||||||
parts := strings.SplitN(strings.TrimPrefix(proxy, "vless://"), "@", 2)
|
|
||||||
if len(parts) != 2 {
|
urlParts := strings.SplitN(strings.TrimPrefix(proxy, constant.VLESSPrefix), "@", 2)
|
||||||
return model.Outbound{}, errors.New("invalid vless Url")
|
if len(urlParts) != 2 {
|
||||||
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrInvalidStruct,
|
||||||
|
Message: "missing character '@' in url",
|
||||||
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
serverInfo := strings.SplitN(parts[1], "#", 2)
|
}
|
||||||
|
|
||||||
|
serverInfo := strings.SplitN(urlParts[1], "#", 2)
|
||||||
serverAndPortAndParams := strings.SplitN(serverInfo[0], "?", 2)
|
serverAndPortAndParams := strings.SplitN(serverInfo[0], "?", 2)
|
||||||
|
if len(serverAndPortAndParams) != 2 {
|
||||||
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrInvalidStruct,
|
||||||
|
Message: "missing character '?' in url",
|
||||||
|
Raw: proxy,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
serverAndPort := strings.SplitN(serverAndPortAndParams[0], ":", 2)
|
serverAndPort := strings.SplitN(serverAndPortAndParams[0], ":", 2)
|
||||||
|
if len(serverAndPort) != 2 {
|
||||||
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrInvalidStruct,
|
||||||
|
Message: "missing server host or port",
|
||||||
|
Raw: proxy,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
server, portStr := serverAndPort[0], serverAndPort[1]
|
||||||
|
port, err := ParsePort(portStr)
|
||||||
|
if err != nil {
|
||||||
|
return model.Outbound{}, err
|
||||||
|
}
|
||||||
|
|
||||||
params, err := url.ParseQuery(serverAndPortAndParams[1])
|
params, err := url.ParseQuery(serverAndPortAndParams[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.Outbound{}, err
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrCannotParseParams,
|
||||||
|
Raw: proxy,
|
||||||
|
Message: err.Error(),
|
||||||
}
|
}
|
||||||
if len(serverAndPort) != 2 {
|
|
||||||
return model.Outbound{}, errors.New("invalid vless Url")
|
|
||||||
}
|
|
||||||
port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))
|
|
||||||
if err != nil {
|
|
||||||
return model.Outbound{}, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
remarks := ""
|
remarks := ""
|
||||||
if len(serverInfo) == 2 {
|
if len(serverInfo) == 2 {
|
||||||
if strings.Contains(serverInfo[1], "|") {
|
if strings.Contains(serverInfo[1], "|") {
|
||||||
@ -37,92 +61,107 @@ func ParseVless(proxy string) (model.Outbound, error) {
|
|||||||
} else {
|
} else {
|
||||||
remarks, err = url.QueryUnescape(serverInfo[1])
|
remarks, err = url.QueryUnescape(serverInfo[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.Outbound{}, err
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrCannotParseParams,
|
||||||
|
Raw: proxy,
|
||||||
|
Message: err.Error(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
remarks, err = url.QueryUnescape(serverAndPort[0])
|
remarks, err = url.QueryUnescape(server)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.Outbound{}, err
|
return model.Outbound{}, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
server := strings.TrimSpace(serverAndPort[0])
|
|
||||||
uuid := strings.TrimSpace(parts[0])
|
uuid := strings.TrimSpace(urlParts[0])
|
||||||
|
flow, security, alpnStr, sni, insecure, fp, pbk, sid, path, host, serviceName := params.Get("flow"), params.Get("security"), params.Get("alpn"), params.Get("sni"), params.Get("allowInsecure"), params.Get("fp"), params.Get("pbk"), params.Get("sid"), params.Get("path"), params.Get("host"), params.Get("serviceName")
|
||||||
|
|
||||||
|
enableUTLS := fp != ""
|
||||||
|
insecureBool := insecure == "1"
|
||||||
|
var alpn []string
|
||||||
|
if strings.Contains(alpnStr, ",") {
|
||||||
|
alpn = strings.Split(alpnStr, ",")
|
||||||
|
} else {
|
||||||
|
alpn = nil
|
||||||
|
}
|
||||||
|
|
||||||
result := model.Outbound{
|
result := model.Outbound{
|
||||||
Type: "vless",
|
Type: "vless",
|
||||||
Tag: remarks,
|
Tag: remarks,
|
||||||
VLESSOptions: model.VLESSOutboundOptions{
|
VLESSOptions: model.VLESSOutboundOptions{
|
||||||
ServerOptions: model.ServerOptions{
|
ServerOptions: model.ServerOptions{
|
||||||
Server: server,
|
Server: server,
|
||||||
ServerPort: uint16(port),
|
ServerPort: port,
|
||||||
},
|
},
|
||||||
UUID: uuid,
|
UUID: uuid,
|
||||||
Flow: params.Get("flow"),
|
Flow: flow,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if params.Get("security") == "tls" {
|
|
||||||
var alpn []string
|
if security == "tls" {
|
||||||
if strings.Contains(params.Get("alpn"), ",") {
|
|
||||||
alpn = strings.Split(params.Get("alpn"), ",")
|
|
||||||
} else {
|
|
||||||
alpn = nil
|
|
||||||
}
|
|
||||||
result.VLESSOptions.OutboundTLSOptionsContainer = model.OutboundTLSOptionsContainer{
|
result.VLESSOptions.OutboundTLSOptionsContainer = model.OutboundTLSOptionsContainer{
|
||||||
TLS: &model.OutboundTLSOptions{
|
TLS: &model.OutboundTLSOptions{
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
ALPN: alpn,
|
ALPN: alpn,
|
||||||
ServerName: params.Get("sni"),
|
ServerName: sni,
|
||||||
Insecure: params.Get("allowInsecure") == "1",
|
Insecure: insecureBool,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if params.Get("fp") != "" {
|
|
||||||
result.VLESSOptions.OutboundTLSOptionsContainer.TLS.UTLS = &model.OutboundUTLSOptions{
|
result.VLESSOptions.OutboundTLSOptionsContainer.TLS.UTLS = &model.OutboundUTLSOptions{
|
||||||
Enabled: true,
|
Enabled: enableUTLS,
|
||||||
Fingerprint: params.Get("fp"),
|
Fingerprint: fp,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if params.Get("security") == "reality" {
|
if security == "reality" {
|
||||||
result.VLESSOptions.OutboundTLSOptionsContainer.TLS.Reality = &model.OutboundRealityOptions{
|
result.VLESSOptions.OutboundTLSOptionsContainer.TLS.Reality = &model.OutboundRealityOptions{
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
PublicKey: params.Get("pbk"),
|
PublicKey: pbk,
|
||||||
ShortID: params.Get("sid"),
|
ShortID: sid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Get("type") == "ws" {
|
if params.Get("type") == "ws" {
|
||||||
result.VLESSOptions.Transport = &model.V2RayTransportOptions{
|
result.VLESSOptions.Transport = &model.V2RayTransportOptions{
|
||||||
Type: "ws",
|
Type: "ws",
|
||||||
WebsocketOptions: model.V2RayWebsocketOptions{
|
WebsocketOptions: model.V2RayWebsocketOptions{
|
||||||
Path: params.Get("path"),
|
Path: path,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if params.Get("host") != "" {
|
result.VLESSOptions.Transport.WebsocketOptions.Headers["Host"] = host
|
||||||
result.VLESSOptions.Transport.WebsocketOptions.Headers["Host"] = params.Get("host")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Get("type") == "quic" {
|
if params.Get("type") == "quic" {
|
||||||
result.VLESSOptions.Transport = &model.V2RayTransportOptions{
|
result.VLESSOptions.Transport = &model.V2RayTransportOptions{
|
||||||
Type: "quic",
|
Type: "quic",
|
||||||
QUICOptions: model.V2RayQUICOptions{},
|
QUICOptions: model.V2RayQUICOptions{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Get("type") == "grpc" {
|
if params.Get("type") == "grpc" {
|
||||||
result.VLESSOptions.Transport = &model.V2RayTransportOptions{
|
result.VLESSOptions.Transport = &model.V2RayTransportOptions{
|
||||||
Type: "grpc",
|
Type: "grpc",
|
||||||
GRPCOptions: model.V2RayGRPCOptions{
|
GRPCOptions: model.V2RayGRPCOptions{
|
||||||
ServiceName: params.Get("serviceName"),
|
ServiceName: serviceName,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Get("type") == "http" {
|
if params.Get("type") == "http" {
|
||||||
host, err := url.QueryUnescape(params.Get("host"))
|
hosts, err := url.QueryUnescape(host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.Outbound{}, err
|
return model.Outbound{}, &ParseError{
|
||||||
|
Type: ErrCannotParseParams,
|
||||||
|
Raw: proxy,
|
||||||
|
Message: err.Error(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
result.VLESSOptions.Transport = &model.V2RayTransportOptions{
|
result.VLESSOptions.Transport = &model.V2RayTransportOptions{
|
||||||
Type: "http",
|
Type: "http",
|
||||||
HTTPOptions: model.V2RayHTTPOptions{
|
HTTPOptions: model.V2RayHTTPOptions{
|
||||||
Host: strings.Split(host, ","),
|
Host: strings.Split(hosts, ","),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,47 +2,53 @@ package parser
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sub2sing-box/constant"
|
||||||
"sub2sing-box/model"
|
"sub2sing-box/model"
|
||||||
"sub2sing-box/util"
|
"sub2sing-box/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParseVmess(proxy string) (model.Outbound, error) {
|
func ParseVmess(proxy string) (model.Outbound, error) {
|
||||||
if !strings.HasPrefix(proxy, "vmess://") {
|
if !strings.HasPrefix(proxy, constant.VMessPrefix) {
|
||||||
return model.Outbound{}, errors.New("invalid vmess url")
|
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||||
}
|
}
|
||||||
base64, err := util.DecodeBase64(strings.TrimPrefix(proxy, "vmess://"))
|
|
||||||
|
proxy = strings.TrimPrefix(proxy, constant.VMessPrefix)
|
||||||
|
base64, err := util.DecodeBase64(proxy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.Outbound{}, errors.New("invalid vmess url" + err.Error())
|
return model.Outbound{}, &ParseError{Type: ErrInvalidBase64, Raw: proxy, Message: err.Error()}
|
||||||
}
|
}
|
||||||
|
|
||||||
var vmess model.VmessJson
|
var vmess model.VmessJson
|
||||||
err = json.Unmarshal([]byte(base64), &vmess)
|
err = json.Unmarshal([]byte(base64), &vmess)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.Outbound{}, errors.New("invalid vmess url" + err.Error())
|
return model.Outbound{}, &ParseError{Type: ErrInvalidStruct, Raw: proxy, Message: err.Error()}
|
||||||
}
|
}
|
||||||
port := 0
|
|
||||||
|
var port uint16
|
||||||
switch vmess.Port.(type) {
|
switch vmess.Port.(type) {
|
||||||
case string:
|
case string:
|
||||||
port, err = strconv.Atoi(vmess.Port.(string))
|
port, err = ParsePort(vmess.Port.(string))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.Outbound{}, errors.New("invalid vmess url" + err.Error())
|
return model.Outbound{}, err
|
||||||
}
|
}
|
||||||
case float64:
|
case float64:
|
||||||
port = int(vmess.Port.(float64))
|
port = uint16(vmess.Port.(float64))
|
||||||
}
|
}
|
||||||
|
|
||||||
aid := 0
|
aid := 0
|
||||||
switch vmess.Aid.(type) {
|
switch vmess.Aid.(type) {
|
||||||
case string:
|
case string:
|
||||||
aid, err = strconv.Atoi(vmess.Aid.(string))
|
aid, err = strconv.Atoi(vmess.Aid.(string))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.Outbound{}, errors.New("invalid vmess url" + err.Error())
|
return model.Outbound{}, &ParseError{Type: ErrInvalidStruct, Raw: proxy, Message: err.Error()}
|
||||||
}
|
}
|
||||||
case float64:
|
case float64:
|
||||||
aid = int(vmess.Aid.(float64))
|
aid = int(vmess.Aid.(float64))
|
||||||
}
|
}
|
||||||
|
|
||||||
if vmess.Scy == "" {
|
if vmess.Scy == "" {
|
||||||
vmess.Scy = "auto"
|
vmess.Scy = "auto"
|
||||||
}
|
}
|
||||||
@ -58,7 +64,7 @@ func ParseVmess(proxy string) (model.Outbound, error) {
|
|||||||
VMessOptions: model.VMessOutboundOptions{
|
VMessOptions: model.VMessOutboundOptions{
|
||||||
ServerOptions: model.ServerOptions{
|
ServerOptions: model.ServerOptions{
|
||||||
Server: vmess.Add,
|
Server: vmess.Add,
|
||||||
ServerPort: uint16(port),
|
ServerPort: port,
|
||||||
},
|
},
|
||||||
UUID: vmess.Id,
|
UUID: vmess.Id,
|
||||||
AlterId: aid,
|
AlterId: aid,
|
||||||
|
11
test/country_test.go
Normal file
11
test/country_test.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"sub2sing-box/model"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCountry(t *testing.T) {
|
||||||
|
log.Println(model.GetContryName("US 节点"))
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user