@@ -3,12 +3,10 @@ package ccm
33import (
44 "bytes"
55 "context"
6- stdTLS "crypto/tls"
76 "encoding/json"
87 "errors"
98 "io"
109 "mime"
11- "net"
1210 "net/http"
1311 "strconv"
1412 "strings"
@@ -17,7 +15,6 @@ import (
1715
1816 "github.com/sagernet/sing-box/adapter"
1917 boxService "github.com/sagernet/sing-box/adapter/service"
20- "github.com/sagernet/sing-box/common/dialer"
2118 "github.com/sagernet/sing-box/common/listener"
2219 "github.com/sagernet/sing-box/common/tls"
2320 C "github.com/sagernet/sing-box/constant"
@@ -26,9 +23,7 @@ import (
2623 "github.com/sagernet/sing/common"
2724 "github.com/sagernet/sing/common/buf"
2825 E "github.com/sagernet/sing/common/exceptions"
29- M "github.com/sagernet/sing/common/metadata"
3026 N "github.com/sagernet/sing/common/network"
31- "github.com/sagernet/sing/common/ntp"
3227 aTLS "github.com/sagernet/sing/common/tls"
3328
3429 "github.com/anthropics/anthropic-sdk-go"
@@ -114,7 +109,6 @@ type Service struct {
114109 ctx context.Context
115110 logger log.ContextLogger
116111 options option.CCMServiceOptions
117- httpClient * http.Client
118112 httpHeaders http.Header
119113 listener * listener.Listener
120114 tlsConfig tls.ServerConfig
@@ -139,30 +133,6 @@ func NewService(ctx context.Context, logger log.ContextLogger, tag string, optio
139133 return nil , E .Cause (err , "validate options" )
140134 }
141135
142- serviceDialer , err := dialer .NewWithOptions (dialer.Options {
143- Context : ctx ,
144- Options : option.DialerOptions {
145- Detour : options .Detour ,
146- },
147- RemoteIsDomain : true ,
148- })
149- if err != nil {
150- return nil , E .Cause (err , "create dialer" )
151- }
152-
153- httpClient := & http.Client {
154- Transport : & http.Transport {
155- ForceAttemptHTTP2 : true ,
156- TLSClientConfig : & stdTLS.Config {
157- RootCAs : adapter .RootPoolFromContext (ctx ),
158- Time : ntp .TimeFuncFromContext (ctx ),
159- },
160- DialContext : func (ctx context.Context , network , addr string ) (net.Conn , error ) {
161- return serviceDialer .DialContext (ctx , network , M .ParseSocksaddr (addr ))
162- },
163- },
164- }
165-
166136 userManager := & UserManager {
167137 tokenMap : make (map [string ]string ),
168138 }
@@ -172,7 +142,6 @@ func NewService(ctx context.Context, logger log.ContextLogger, tag string, optio
172142 ctx : ctx ,
173143 logger : logger ,
174144 options : options ,
175- httpClient : httpClient ,
176145 httpHeaders : options .Headers .Build (),
177146 listener : listener .New (listener.Options {
178147 Context : ctx ,
@@ -184,7 +153,7 @@ func NewService(ctx context.Context, logger log.ContextLogger, tag string, optio
184153 }
185154
186155 if len (options .Credentials ) > 0 {
187- providers , allDefaults , err := buildCredentialProviders (options , httpClient , logger )
156+ providers , allDefaults , err := buildCredentialProviders (ctx , options , logger )
188157 if err != nil {
189158 return nil , E .Cause (err , "build credential providers" )
190159 }
@@ -197,10 +166,14 @@ func NewService(ctx context.Context, logger log.ContextLogger, tag string, optio
197166 }
198167 service .userCredentialMap = userCredentialMap
199168 } else {
200- credential := newDefaultCredential ("default" , option.CCMDefaultCredentialOptions {
169+ credential , err := newDefaultCredential (ctx , "default" , option.CCMDefaultCredentialOptions {
201170 CredentialPath : options .CredentialPath ,
202171 UsagesPath : options .UsagesPath ,
203- }, httpClient , logger )
172+ Detour : options .Detour ,
173+ }, logger )
174+ if err != nil {
175+ return nil , err
176+ }
204177 service .legacyCredential = credential
205178 service .legacyProvider = & singleCredentialProvider {credential : credential }
206179 service .allDefaults = []* defaultCredential {credential }
@@ -402,7 +375,7 @@ func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
402375
403376 proxyRequest .Header .Set ("Authorization" , "Bearer " + accessToken )
404377
405- response , err := s .httpClient .Do (proxyRequest )
378+ response , err := credential .httpClient .Do (proxyRequest )
406379 if err != nil {
407380 writeJSONError (w , r , http .StatusBadGateway , "api_error" , err .Error ())
408381 return
@@ -417,7 +390,7 @@ func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
417390 }
418391 response .Body .Close ()
419392 s .logger .Info ("retrying with credential " , nextCredential .tag , " after 429 from " , credential .tag )
420- retryResponse , retryErr := retryRequestWithBody (s . httpClient , r , bodyBytes , nextCredential , s .httpHeaders )
393+ retryResponse , retryErr := retryRequestWithBody (r , bodyBytes , nextCredential , s .httpHeaders )
421394 if retryErr != nil {
422395 s .logger .Error ("retry request: " , retryErr )
423396 writeJSONError (w , r , http .StatusBadGateway , "api_error" , retryErr .Error ())
0 commit comments