Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions subscription/parser/clash.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package parser

import (
"context"
"encoding/base64"
"encoding/pem"
"strings"
"time"

C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
Expand Down Expand Up @@ -162,6 +165,41 @@ func ParseClashSubscription(_ context.Context, content string) ([]option.Outboun
Username: httpOption.UserName,
Password: httpOption.Password,
}
case constant.AnyTLS:
anytlsOption := &clash_outbound.AnyTLSOption{}
err = decoder.Decode(proxyMapping, anytlsOption)
if err != nil {
return nil, err
}
echOptions, err := clashECH(anytlsOption.ECHOpts)
if err != nil {
return nil, E.Cause(err, "parse ECH options for proxy ", i)
}
outbound.Type = C.TypeAnyTLS
outbound.Options = &option.AnyTLSOutboundOptions{
ServerOptions: option.ServerOptions{
Server: anytlsOption.Server,
ServerPort: uint16(anytlsOption.Port),
},
Password: anytlsOption.Password,
OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
TLS: &option.OutboundTLSOptions{
Enabled: true,
ALPN: anytlsOption.ALPN,
ServerName: anytlsOption.SNI,
Insecure: anytlsOption.SkipCertVerify,
ECH: echOptions,
UTLS: clashUTLS(anytlsOption.ClientFingerprint),
CertificatePath: anytlsOption.Fingerprint,
},
},
IdleSessionCheckInterval: badoption.Duration(time.Duration(anytlsOption.IdleSessionCheckInterval) * time.Second),
IdleSessionTimeout: badoption.Duration(time.Duration(anytlsOption.IdleSessionTimeout) * time.Second),
MinIdleSession: anytlsOption.MinIdleSession,
}
default:
// Skip unsupported proxy types
continue
}
outbounds = append(outbounds, outbound)
}
Expand Down Expand Up @@ -283,3 +321,38 @@ func clashStringList(list []string) string {
}
return ""
}

func clashECH(echOpts clash_outbound.ECHOptions) (*option.OutboundECHOptions, error) {
if !echOpts.Enable {
return nil, nil
}
echOptions := &option.OutboundECHOptions{
Enabled: true,
}
if echOpts.Config != "" {
// Decode base64 ECH config from Clash
decoded, err := base64.StdEncoding.DecodeString(echOpts.Config)
if err != nil {
return nil, E.Cause(err, "decode ECH config from base64")
}

// Convert to PEM format for sing-box
pemBlock := &pem.Block{
Type: "ECH CONFIGS",
Bytes: decoded,
}
pemBytes := pem.EncodeToMemory(pemBlock)
echOptions.Config = []string{string(pemBytes)}
}
return echOptions, nil
}

func clashUTLS(fingerprint string) *option.OutboundUTLSOptions {
if fingerprint == "" {
return nil
}
return &option.OutboundUTLSOptions{
Enabled: true,
Fingerprint: fingerprint,
}
}
Loading