mirror of
https://github.com/bestnite/sub2clash.git
synced 2025-06-07 00:45:01 +08:00
Compare commits
No commits in common. "b5fcbab1a5b973f0aa91e79213dec9b4f8730a31" and "db004339311100b925b7af50b9ed85ef103755f5" have entirely different histories.
b5fcbab1a5
...
db00433931
@ -18,7 +18,6 @@
|
|||||||
- Hysteria (Clash.Meta)
|
- Hysteria (Clash.Meta)
|
||||||
- Hysteria2 (Clash.Meta)
|
- Hysteria2 (Clash.Meta)
|
||||||
- Socks5
|
- Socks5
|
||||||
- Anytls (Clash.Meta)
|
|
||||||
|
|
||||||
## 使用
|
## 使用
|
||||||
|
|
||||||
|
@ -233,21 +233,6 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
|
|||||||
return temp, nil
|
return temp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchSubscriptionUserInfo(url string, userAgent string) (string, error) {
|
|
||||||
resp, err := common.Head(url, common.WithUserAgent(userAgent))
|
|
||||||
if err != nil {
|
|
||||||
logger.Logger.Debug("创建 HEAD 请求失败", zap.Error(err))
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
if userInfo := resp.Header.Get("subscription-userinfo"); userInfo != "" {
|
|
||||||
return userInfo, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Logger.Debug("目标 URL 未返回 subscription-userinfo 头", zap.Error(err))
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
func MergeSubAndTemplate(temp *model.Subscription, sub *model.Subscription, igcg bool) {
|
func MergeSubAndTemplate(temp *model.Subscription, sub *model.Subscription, igcg bool) {
|
||||||
|
|
||||||
var countryGroupNames []string
|
var countryGroupNames []string
|
||||||
|
@ -25,14 +25,6 @@ func SubHandler(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(query.Subs) == 1 {
|
|
||||||
userInfoHeader, err := fetchSubscriptionUserInfo(query.Subs[0], "clash")
|
|
||||||
if err != nil {
|
|
||||||
c.String(http.StatusInternalServerError, err.Error())
|
|
||||||
}
|
|
||||||
c.Header("subscription-userinfo", userInfoHeader)
|
|
||||||
}
|
|
||||||
|
|
||||||
if query.NodeListMode {
|
if query.NodeListMode {
|
||||||
nodelist := model.NodeList{}
|
nodelist := model.NodeList{}
|
||||||
nodelist.Proxies = sub.Proxies
|
nodelist.Proxies = sub.Proxies
|
||||||
|
@ -58,45 +58,3 @@ func Get(url string, options ...GetOption) (resp *http.Response, err error) {
|
|||||||
}
|
}
|
||||||
return nil, fmt.Errorf("请求失败:%v", err)
|
return nil, fmt.Errorf("请求失败:%v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Head(url string, options ...GetOption) (resp *http.Response, err error) {
|
|
||||||
retryTimes := config.Default.RequestRetryTimes
|
|
||||||
haveTried := 0
|
|
||||||
retryDelay := time.Second
|
|
||||||
|
|
||||||
// 解析可选参数(如 User-Agent)
|
|
||||||
getConfig := GetConfig{}
|
|
||||||
for _, option := range options {
|
|
||||||
option(&getConfig)
|
|
||||||
}
|
|
||||||
|
|
||||||
var req *http.Request
|
|
||||||
var headResp *http.Response
|
|
||||||
|
|
||||||
for haveTried < retryTimes {
|
|
||||||
client := &http.Client{}
|
|
||||||
req, err = http.NewRequest("HEAD", url, nil)
|
|
||||||
if err != nil {
|
|
||||||
haveTried++
|
|
||||||
time.Sleep(retryDelay)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置 User-Agent(如果提供)
|
|
||||||
if getConfig.userAgent != "" {
|
|
||||||
req.Header.Set("User-Agent", getConfig.userAgent)
|
|
||||||
}
|
|
||||||
|
|
||||||
headResp, err = client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
haveTried++
|
|
||||||
time.Sleep(retryDelay)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// HEAD 请求不检查 ContentLength,因为没有响应体
|
|
||||||
return headResp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, fmt.Errorf("HEAD 请求失败:%v", err)
|
|
||||||
}
|
|
||||||
|
@ -130,9 +130,6 @@ func ParseProxy(proxies ...string) []model.Proxy {
|
|||||||
if strings.HasPrefix(proxy, constant.SocksPrefix) {
|
if strings.HasPrefix(proxy, constant.SocksPrefix) {
|
||||||
proxyItem, err = parser.ParseSocks(proxy)
|
proxyItem, err = parser.ParseSocks(proxy)
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(proxy, constant.AnytlsPrefix) {
|
|
||||||
proxyItem, err = parser.ParseAnytls(proxy)
|
|
||||||
}
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
result = append(result, proxyItem)
|
result = append(result, proxyItem)
|
||||||
} else {
|
} else {
|
||||||
|
@ -10,5 +10,4 @@ const (
|
|||||||
VLESSPrefix string = "vless://"
|
VLESSPrefix string = "vless://"
|
||||||
VMessPrefix string = "vmess://"
|
VMessPrefix string = "vmess://"
|
||||||
SocksPrefix string = "socks"
|
SocksPrefix string = "socks"
|
||||||
AnytlsPrefix string = "anytls://"
|
|
||||||
)
|
)
|
||||||
|
@ -27,7 +27,6 @@ func GetSupportProxyTypes(clashType ClashType) map[string]bool {
|
|||||||
"hysteria": true,
|
"hysteria": true,
|
||||||
"hysteria2": true,
|
"hysteria2": true,
|
||||||
"socks5": true,
|
"socks5": true,
|
||||||
"anytls": true,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
133
model/proxy.go
133
model/proxy.go
@ -5,73 +5,70 @@ type SmuxStruct struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Proxy struct {
|
type Proxy struct {
|
||||||
Name string `yaml:"name,omitempty"`
|
Name string `yaml:"name,omitempty"`
|
||||||
Server string `yaml:"server,omitempty"`
|
Server string `yaml:"server,omitempty"`
|
||||||
Port int `yaml:"port,omitempty"`
|
Port int `yaml:"port,omitempty"`
|
||||||
Type string `yaml:"type,omitempty"`
|
Type string `yaml:"type,omitempty"`
|
||||||
Cipher string `yaml:"cipher,omitempty"`
|
Cipher string `yaml:"cipher,omitempty"`
|
||||||
Username string `yaml:"username,omitempty"`
|
Username string `yaml:"username,omitempty"`
|
||||||
Password string `yaml:"password,omitempty"`
|
Password string `yaml:"password,omitempty"`
|
||||||
UDP bool `yaml:"udp,omitempty"`
|
UDP bool `yaml:"udp,omitempty"`
|
||||||
UUID string `yaml:"uuid,omitempty"`
|
UUID string `yaml:"uuid,omitempty"`
|
||||||
Network string `yaml:"network,omitempty"`
|
Network string `yaml:"network,omitempty"`
|
||||||
Flow string `yaml:"flow,omitempty"`
|
Flow string `yaml:"flow,omitempty"`
|
||||||
TLS bool `yaml:"tls,omitempty"`
|
TLS bool `yaml:"tls,omitempty"`
|
||||||
ClientFingerprint string `yaml:"client-fingerprint,omitempty"`
|
ClientFingerprint string `yaml:"client-fingerprint,omitempty"`
|
||||||
Plugin string `yaml:"plugin,omitempty"`
|
Plugin string `yaml:"plugin,omitempty"`
|
||||||
PluginOpts map[string]any `yaml:"plugin-opts,omitempty"`
|
PluginOpts map[string]any `yaml:"plugin-opts,omitempty"`
|
||||||
Smux SmuxStruct `yaml:"smux,omitempty"`
|
Smux SmuxStruct `yaml:"smux,omitempty"`
|
||||||
Sni string `yaml:"sni,omitempty"`
|
Sni string `yaml:"sni,omitempty"`
|
||||||
AllowInsecure bool `yaml:"allow-insecure,omitempty"`
|
AllowInsecure bool `yaml:"allow-insecure,omitempty"`
|
||||||
Fingerprint string `yaml:"fingerprint,omitempty"`
|
Fingerprint string `yaml:"fingerprint,omitempty"`
|
||||||
SkipCertVerify bool `yaml:"skip-cert-verify,omitempty"`
|
SkipCertVerify bool `yaml:"skip-cert-verify,omitempty"`
|
||||||
Alpn []string `yaml:"alpn,omitempty"`
|
Alpn []string `yaml:"alpn,omitempty"`
|
||||||
XUDP bool `yaml:"xudp,omitempty"`
|
XUDP bool `yaml:"xudp,omitempty"`
|
||||||
Servername string `yaml:"servername,omitempty"`
|
Servername string `yaml:"servername,omitempty"`
|
||||||
WSOpts WSOptions `yaml:"ws-opts,omitempty"`
|
WSOpts WSOptions `yaml:"ws-opts,omitempty"`
|
||||||
AlterID int `yaml:"alterId,omitempty"`
|
AlterID int `yaml:"alterId,omitempty"`
|
||||||
GrpcOpts GrpcOptions `yaml:"grpc-opts,omitempty"`
|
GrpcOpts GrpcOptions `yaml:"grpc-opts,omitempty"`
|
||||||
RealityOpts RealityOptions `yaml:"reality-opts,omitempty"`
|
RealityOpts RealityOptions `yaml:"reality-opts,omitempty"`
|
||||||
Protocol string `yaml:"protocol,omitempty"`
|
Protocol string `yaml:"protocol,omitempty"`
|
||||||
Obfs string `yaml:"obfs,omitempty"`
|
Obfs string `yaml:"obfs,omitempty"`
|
||||||
ObfsParam string `yaml:"obfs-param,omitempty"`
|
ObfsParam string `yaml:"obfs-param,omitempty"`
|
||||||
ProtocolParam string `yaml:"protocol-param,omitempty"`
|
ProtocolParam string `yaml:"protocol-param,omitempty"`
|
||||||
Remarks []string `yaml:"remarks,omitempty"`
|
Remarks []string `yaml:"remarks,omitempty"`
|
||||||
HTTPOpts HTTPOptions `yaml:"http-opts,omitempty"`
|
HTTPOpts HTTPOptions `yaml:"http-opts,omitempty"`
|
||||||
HTTP2Opts HTTP2Options `yaml:"h2-opts,omitempty"`
|
HTTP2Opts HTTP2Options `yaml:"h2-opts,omitempty"`
|
||||||
PacketAddr bool `yaml:"packet-addr,omitempty"`
|
PacketAddr bool `yaml:"packet-addr,omitempty"`
|
||||||
PacketEncoding string `yaml:"packet-encoding,omitempty"`
|
PacketEncoding string `yaml:"packet-encoding,omitempty"`
|
||||||
GlobalPadding bool `yaml:"global-padding,omitempty"`
|
GlobalPadding bool `yaml:"global-padding,omitempty"`
|
||||||
AuthenticatedLength bool `yaml:"authenticated-length,omitempty"`
|
AuthenticatedLength bool `yaml:"authenticated-length,omitempty"`
|
||||||
UDPOverTCP bool `yaml:"udp-over-tcp,omitempty"`
|
UDPOverTCP bool `yaml:"udp-over-tcp,omitempty"`
|
||||||
UDPOverTCPVersion int `yaml:"udp-over-tcp-version,omitempty"`
|
UDPOverTCPVersion int `yaml:"udp-over-tcp-version,omitempty"`
|
||||||
SubName string `yaml:"-"`
|
SubName string `yaml:"-"`
|
||||||
Up string `yaml:"up,omitempty"`
|
Up string `yaml:"up,omitempty"`
|
||||||
Down string `yaml:"down,omitempty"`
|
Down string `yaml:"down,omitempty"`
|
||||||
CustomCA string `yaml:"ca,omitempty"`
|
CustomCA string `yaml:"ca,omitempty"`
|
||||||
CustomCAString string `yaml:"ca-str,omitempty"`
|
CustomCAString string `yaml:"ca-str,omitempty"`
|
||||||
CWND int `yaml:"cwnd,omitempty"`
|
CWND int `yaml:"cwnd,omitempty"`
|
||||||
Auth string `yaml:"auth,omitempty"`
|
Auth string `yaml:"auth,omitempty"`
|
||||||
ReceiveWindowConn int `yaml:"recv-window-conn,omitempty"`
|
ReceiveWindowConn int `yaml:"recv-window-conn,omitempty"`
|
||||||
ReceiveWindow int `yaml:"recv-window,omitempty"`
|
ReceiveWindow int `yaml:"recv-window,omitempty"`
|
||||||
DisableMTUDiscovery bool `yaml:"disable-mtu-discovery,omitempty"`
|
DisableMTUDiscovery bool `yaml:"disable-mtu-discovery,omitempty"`
|
||||||
FastOpen bool `yaml:"fast-open,omitempty"`
|
FastOpen bool `yaml:"fast-open,omitempty"`
|
||||||
HopInterval int `yaml:"hop-interval,omitempty"`
|
HopInterval int `yaml:"hop-interval,omitempty"`
|
||||||
Ports string `yaml:"ports,omitempty"`
|
Ports string `yaml:"ports,omitempty"`
|
||||||
AuthStringOLD string `yaml:"auth_str,omitempty"`
|
AuthStringOLD string `yaml:"auth_str,omitempty"`
|
||||||
AuthString string `yaml:"auth-str,omitempty"`
|
AuthString string `yaml:"auth-str,omitempty"`
|
||||||
Ip string `yaml:"ip,omitempty"`
|
Ip string `yaml:"ip,omitempty"`
|
||||||
Ipv6 string `yaml:"ipv6,omitempty"`
|
Ipv6 string `yaml:"ipv6,omitempty"`
|
||||||
PrivateKey string `yaml:"private-key,omitempty"`
|
PrivateKey string `yaml:"private-key,omitempty"`
|
||||||
Workers int `yaml:"workers,omitempty"`
|
Workers int `yaml:"workers,omitempty"`
|
||||||
MTU int `yaml:"mtu,omitempty"`
|
MTU int `yaml:"mtu,omitempty"`
|
||||||
PersistentKeepalive int `yaml:"persistent-keepalive,omitempty"`
|
PersistentKeepalive int `yaml:"persistent-keepalive,omitempty"`
|
||||||
Peers []WireGuardPeerOption `yaml:"peers,omitempty"`
|
Peers []WireGuardPeerOption `yaml:"peers,omitempty"`
|
||||||
RemoteDnsResolve bool `yaml:"remote-dns-resolve,omitempty"`
|
RemoteDnsResolve bool `yaml:"remote-dns-resolve,omitempty"`
|
||||||
Dns []string `yaml:"dns,omitempty"`
|
Dns []string `yaml:"dns,omitempty"`
|
||||||
IdleSessionCheckInterval int `yaml:"idle-session-check-interval,omitempty"`
|
|
||||||
IdleSessionTimeout int `yaml:"idle-session-timeout,omitempty"`
|
|
||||||
MinIdleSession int `yaml:"min-idle-session,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type WireGuardPeerOption struct {
|
type WireGuardPeerOption struct {
|
||||||
@ -101,8 +98,6 @@ func (p Proxy) MarshalYAML() (interface{}, error) {
|
|||||||
return ProxyToHysteria(p), nil
|
return ProxyToHysteria(p), nil
|
||||||
case "hysteria2":
|
case "hysteria2":
|
||||||
return ProxyToHysteria2(p), nil
|
return ProxyToHysteria2(p), nil
|
||||||
case "anytls":
|
|
||||||
return ProxyToAnytls(p), nil
|
|
||||||
default:
|
default:
|
||||||
return _Proxy(p), nil
|
return _Proxy(p), nil
|
||||||
}
|
}
|
||||||
|
@ -1,37 +0,0 @@
|
|||||||
package model
|
|
||||||
|
|
||||||
type Anytls struct {
|
|
||||||
Type string `yaml:"type"`
|
|
||||||
Name string `yaml:"name"`
|
|
||||||
Server string `yaml:"server"`
|
|
||||||
Port int `yaml:"port"`
|
|
||||||
Password string `yaml:"password,omitempty"`
|
|
||||||
Alpn []string `yaml:"alpn,omitempty"`
|
|
||||||
SNI string `yaml:"sni,omitempty"`
|
|
||||||
ClientFingerprint string `yaml:"client-fingerprint,omitempty"`
|
|
||||||
SkipCertVerify bool `yaml:"skip-cert-verify,omitempty"`
|
|
||||||
Fingerprint string `yaml:"fingerprint,omitempty"`
|
|
||||||
UDP bool `yaml:"udp,omitempty"`
|
|
||||||
IdleSessionCheckInterval int `yaml:"idle-session-check-interval,omitempty"`
|
|
||||||
IdleSessionTimeout int `yaml:"idle-session-timeout,omitempty"`
|
|
||||||
MinIdleSession int `yaml:"min-idle-session,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func ProxyToAnytls(p Proxy) Anytls {
|
|
||||||
return Anytls{
|
|
||||||
Type: "anytls",
|
|
||||||
Name: p.Name,
|
|
||||||
Server: p.Server,
|
|
||||||
Port: p.Port,
|
|
||||||
Password: p.Password,
|
|
||||||
Alpn: p.Alpn,
|
|
||||||
SNI: p.Sni,
|
|
||||||
ClientFingerprint: p.ClientFingerprint,
|
|
||||||
SkipCertVerify: p.SkipCertVerify,
|
|
||||||
Fingerprint: p.Fingerprint,
|
|
||||||
UDP: p.UDP,
|
|
||||||
IdleSessionCheckInterval: p.IdleSessionCheckInterval,
|
|
||||||
IdleSessionTimeout: p.IdleSessionTimeout,
|
|
||||||
MinIdleSession: p.MinIdleSession,
|
|
||||||
}
|
|
||||||
}
|
|
@ -6,7 +6,6 @@ type RuleProvider struct {
|
|||||||
Url string `yaml:"url,omitempty"`
|
Url string `yaml:"url,omitempty"`
|
||||||
Path string `yaml:"path,omitempty"`
|
Path string `yaml:"path,omitempty"`
|
||||||
Interval int `yaml:"interval,omitempty"`
|
Interval int `yaml:"interval,omitempty"`
|
||||||
Format string `yaml:"format,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Payload struct {
|
type Payload struct {
|
||||||
|
@ -1,74 +0,0 @@
|
|||||||
package parser
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net/url"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/nitezs/sub2clash/constant"
|
|
||||||
"github.com/nitezs/sub2clash/model"
|
|
||||||
)
|
|
||||||
|
|
||||||
func ParseAnytls(proxy string) (model.Proxy, error) {
|
|
||||||
if !strings.HasPrefix(proxy, constant.AnytlsPrefix) {
|
|
||||||
return model.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
|
||||||
}
|
|
||||||
|
|
||||||
link, err := url.Parse(proxy)
|
|
||||||
if err != nil {
|
|
||||||
return model.Proxy{}, &ParseError{
|
|
||||||
Type: ErrInvalidStruct,
|
|
||||||
Message: "url parse error",
|
|
||||||
Raw: proxy,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
username := link.User.Username()
|
|
||||||
password, exist := link.User.Password()
|
|
||||||
if !exist {
|
|
||||||
password = username
|
|
||||||
}
|
|
||||||
|
|
||||||
query := link.Query()
|
|
||||||
server := link.Hostname()
|
|
||||||
if server == "" {
|
|
||||||
return model.Proxy{}, &ParseError{
|
|
||||||
Type: ErrInvalidStruct,
|
|
||||||
Message: "missing server host",
|
|
||||||
Raw: proxy,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
portStr := link.Port()
|
|
||||||
if portStr == "" {
|
|
||||||
return model.Proxy{}, &ParseError{
|
|
||||||
Type: ErrInvalidStruct,
|
|
||||||
Message: "missing server port",
|
|
||||||
Raw: proxy,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
port, err := ParsePort(portStr)
|
|
||||||
if err != nil {
|
|
||||||
return model.Proxy{}, &ParseError{
|
|
||||||
Type: ErrInvalidPort,
|
|
||||||
Raw: portStr,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
insecure, sni := query.Get("insecure"), query.Get("sni")
|
|
||||||
insecureBool := insecure == "1"
|
|
||||||
remarks := link.Fragment
|
|
||||||
if remarks == "" {
|
|
||||||
remarks = fmt.Sprintf("%s:%s", server, portStr)
|
|
||||||
}
|
|
||||||
remarks = strings.TrimSpace(remarks)
|
|
||||||
|
|
||||||
result := model.Proxy{
|
|
||||||
Type: "anytls",
|
|
||||||
Name: remarks,
|
|
||||||
Server: server,
|
|
||||||
Port: port,
|
|
||||||
Password: password,
|
|
||||||
Sni: sni,
|
|
||||||
SkipCertVerify: insecureBool,
|
|
||||||
}
|
|
||||||
return result, nil
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user