@@ -633,12 +633,6 @@ func (st *ServerType) serversFromPairings(
633
633
srv .AutoHTTPS = new (caddyhttp.AutoHTTPSConfig )
634
634
}
635
635
srv .AutoHTTPS .IgnoreLoadedCerts = true
636
-
637
- case "prefer_wildcard" :
638
- if srv .AutoHTTPS == nil {
639
- srv .AutoHTTPS = new (caddyhttp.AutoHTTPSConfig )
640
- }
641
- srv .AutoHTTPS .PreferWildcard = true
642
636
}
643
637
}
644
638
@@ -706,16 +700,6 @@ func (st *ServerType) serversFromPairings(
706
700
return specificity (iLongestHost ) > specificity (jLongestHost )
707
701
})
708
702
709
- // collect all hosts that have a wildcard in them
710
- wildcardHosts := []string {}
711
- for _ , sblock := range p .serverBlocks {
712
- for _ , addr := range sblock .parsedKeys {
713
- if strings .HasPrefix (addr .Host , "*." ) {
714
- wildcardHosts = append (wildcardHosts , addr .Host [2 :])
715
- }
716
- }
717
- }
718
-
719
703
var hasCatchAllTLSConnPolicy , addressQualifiesForTLS bool
720
704
autoHTTPSWillAddConnPolicy := srv .AutoHTTPS == nil || ! srv .AutoHTTPS .Disabled
721
705
@@ -801,7 +785,13 @@ func (st *ServerType) serversFromPairings(
801
785
cp .FallbackSNI = fallbackSNI
802
786
}
803
787
804
- // only append this policy if it actually changes something
788
+ // only append this policy if it actually changes something,
789
+ // or if the configuration explicitly automates certs for
790
+ // these names (this is necessary to hoist a connection policy
791
+ // above one that may manually load a wildcard cert that would
792
+ // otherwise clobber the automated one; the code that appends
793
+ // policies that manually load certs comes later, so they're
794
+ // lower in the list)
805
795
if ! cp .SettingsEmpty () || mapContains (forceAutomatedNames , hosts ) {
806
796
srv .TLSConnPolicies = append (srv .TLSConnPolicies , cp )
807
797
hasCatchAllTLSConnPolicy = len (hosts ) == 0
@@ -841,18 +831,6 @@ func (st *ServerType) serversFromPairings(
841
831
addressQualifiesForTLS = true
842
832
}
843
833
844
- // If prefer wildcard is enabled, then we add hosts that are
845
- // already covered by the wildcard to the skip list
846
- if addressQualifiesForTLS && srv .AutoHTTPS != nil && srv .AutoHTTPS .PreferWildcard {
847
- baseDomain := addr .Host
848
- if idx := strings .Index (baseDomain , "." ); idx != - 1 {
849
- baseDomain = baseDomain [idx + 1 :]
850
- }
851
- if ! strings .HasPrefix (addr .Host , "*." ) && slices .Contains (wildcardHosts , baseDomain ) {
852
- srv .AutoHTTPS .SkipCerts = append (srv .AutoHTTPS .SkipCerts , addr .Host )
853
- }
854
- }
855
-
856
834
// predict whether auto-HTTPS will add the conn policy for us; if so, we
857
835
// may not need to add one for this server
858
836
autoHTTPSWillAddConnPolicy = autoHTTPSWillAddConnPolicy &&
@@ -1083,11 +1061,40 @@ func consolidateConnPolicies(cps caddytls.ConnectionPolicies) (caddytls.Connecti
1083
1061
1084
1062
// if they're exactly equal in every way, just keep one of them
1085
1063
if reflect .DeepEqual (cps [i ], cps [j ]) {
1086
- cps = append (cps [: j ], cps [ j + 1 :] ... )
1064
+ cps = slices . Delete (cps , j , j + 1 )
1087
1065
i --
1088
1066
break
1089
1067
}
1090
1068
1069
+ // as a special case, if there are adjacent TLS conn policies that are identical except
1070
+ // by their matchers, and the matchers are specifically just ServerName ("sni") matchers
1071
+ // (by far the most common), we can combine them into a single policy
1072
+ if i == j - 1 && len (cps [i ].MatchersRaw ) == 1 && len (cps [j ].MatchersRaw ) == 1 {
1073
+ if iSNIMatcherJSON , ok := cps [i ].MatchersRaw ["sni" ]; ok {
1074
+ if jSNIMatcherJSON , ok := cps [j ].MatchersRaw ["sni" ]; ok {
1075
+ // position of policies and the matcher criteria check out; if settings are
1076
+ // the same, then we can combine the policies; we have to unmarshal and
1077
+ // remarshal the matchers though
1078
+ if cps [i ].SettingsEqual (* cps [j ]) {
1079
+ var iSNIMatcher caddytls.MatchServerName
1080
+ if err := json .Unmarshal (iSNIMatcherJSON , & iSNIMatcher ); err == nil {
1081
+ var jSNIMatcher caddytls.MatchServerName
1082
+ if err := json .Unmarshal (jSNIMatcherJSON , & jSNIMatcher ); err == nil {
1083
+ iSNIMatcher = append (iSNIMatcher , jSNIMatcher ... )
1084
+ cps [i ].MatchersRaw ["sni" ], err = json .Marshal (iSNIMatcher )
1085
+ if err != nil {
1086
+ return nil , fmt .Errorf ("recombining SNI matchers: %v" , err )
1087
+ }
1088
+ cps = slices .Delete (cps , j , j + 1 )
1089
+ i --
1090
+ break
1091
+ }
1092
+ }
1093
+ }
1094
+ }
1095
+ }
1096
+ }
1097
+
1091
1098
// if they have the same matcher, try to reconcile each field: either they must
1092
1099
// be identical, or we have to be able to combine them safely
1093
1100
if reflect .DeepEqual (cps [i ].MatchersRaw , cps [j ].MatchersRaw ) {
@@ -1189,12 +1196,13 @@ func consolidateConnPolicies(cps caddytls.ConnectionPolicies) (caddytls.Connecti
1189
1196
}
1190
1197
}
1191
1198
1192
- cps = append (cps [: j ], cps [ j + 1 :] ... )
1199
+ cps = slices . Delete (cps , j , j + 1 )
1193
1200
i --
1194
1201
break
1195
1202
}
1196
1203
}
1197
1204
}
1205
+
1198
1206
return cps , nil
1199
1207
}
1200
1208
0 commit comments