Skip to content

Commit

Permalink
Merge pull request #9 from mosajjal/dev-next
Browse files Browse the repository at this point in the history
random case SNI
  • Loading branch information
hiddify-com authored Nov 4, 2023
2 parents 89c4699 + 3bbd102 commit d9a47f0
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 3 deletions.
18 changes: 18 additions & 0 deletions common/tls/common.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package tls

import (
"math/rand"
"strings"
"unicode"
)

func randomizeCase(s string) string {
var result strings.Builder
for _, c := range s {
if rand.Intn(2) == 0 {
result.WriteRune(unicode.ToUpper(c))
} else {
result.WriteRune(unicode.ToLower(c))
}
}
return result.String()
}

const (
VersionTLS10 = 0x0301
VersionTLS11 = 0x0302
Expand Down
8 changes: 6 additions & 2 deletions common/tls/ech_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
cftls "github.com/sagernet/cloudflare-tls"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-dns"
dns "github.com/sagernet/sing-dns"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/ntp"

Expand Down Expand Up @@ -101,7 +101,11 @@ func NewECHClient(ctx context.Context, serverAddress string, options option.Outb
if options.DisableSNI {
tlsConfig.ServerName = "127.0.0.1"
} else {
tlsConfig.ServerName = serverName
if options.MixedCaseSNI {
tlsConfig.ServerName = randomizeCase(tlsConfig.ServerName)
} else {
tlsConfig.ServerName = serverName
}
}
if options.Insecure {
tlsConfig.InsecureSkipVerify = options.Insecure
Expand Down
6 changes: 5 additions & 1 deletion common/tls/std_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ func NewSTDClient(ctx context.Context, serverAddress string, options option.Outb
if options.DisableSNI {
tlsConfig.ServerName = "127.0.0.1"
} else {
tlsConfig.ServerName = serverName
if options.MixedCaseSNI {
tlsConfig.ServerName = randomizeCase(tlsConfig.ServerName)
} else {
tlsConfig.ServerName = serverName
}
}
if options.Insecure {
tlsConfig.InsecureSkipVerify = options.Insecure
Expand Down
4 changes: 4 additions & 0 deletions common/tls/utls_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func NewUTLSClient(ctx context.Context, serverAddress string, options option.Out
return nil, E.New("missing server_name or insecure=true")
}

if options.MixedCaseSNI {
serverName = randomizeCase(serverName)
}

var tlsConfig utls.Config
tlsConfig.Time = ntp.TimeFuncFromContext(ctx)
if options.DisableSNI {
Expand Down
1 change: 1 addition & 0 deletions option/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type InboundTLSOptions struct {
type OutboundTLSOptions struct {
Enabled bool `json:"enabled,omitempty"`
DisableSNI bool `json:"disable_sni,omitempty"`
MixedCaseSNI bool `json:"mixedcase_sni,omitempty"`
ServerName string `json:"server_name,omitempty"`
Insecure bool `json:"insecure,omitempty"`
ALPN Listable[string] `json:"alpn,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions test/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestUTLS(t *testing.T) {
TLS: &option.OutboundTLSOptions{
Enabled: true,
ServerName: "example.org",
MixedCaseSNI: true,
CertificatePath: certPem,
UTLS: &option.OutboundUTLSOptions{
Enabled: true,
Expand Down

0 comments on commit d9a47f0

Please sign in to comment.