inbound.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. package inbound
  2. import (
  3. "bytes"
  4. "context"
  5. gotls "crypto/tls"
  6. "io"
  7. "reflect"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "unsafe"
  12. "github.com/xtls/xray-core/common"
  13. "github.com/xtls/xray-core/common/buf"
  14. "github.com/xtls/xray-core/common/errors"
  15. "github.com/xtls/xray-core/common/log"
  16. "github.com/xtls/xray-core/common/net"
  17. "github.com/xtls/xray-core/common/protocol"
  18. "github.com/xtls/xray-core/common/retry"
  19. "github.com/xtls/xray-core/common/session"
  20. "github.com/xtls/xray-core/common/signal"
  21. "github.com/xtls/xray-core/common/task"
  22. "github.com/xtls/xray-core/core"
  23. "github.com/xtls/xray-core/features/dns"
  24. feature_inbound "github.com/xtls/xray-core/features/inbound"
  25. "github.com/xtls/xray-core/features/policy"
  26. "github.com/xtls/xray-core/features/routing"
  27. "github.com/xtls/xray-core/proxy"
  28. "github.com/xtls/xray-core/proxy/vless"
  29. "github.com/xtls/xray-core/proxy/vless/encoding"
  30. "github.com/xtls/xray-core/transport/internet/reality"
  31. "github.com/xtls/xray-core/transport/internet/stat"
  32. "github.com/xtls/xray-core/transport/internet/tls"
  33. )
  34. func init() {
  35. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  36. var dc dns.Client
  37. if err := core.RequireFeatures(ctx, func(d dns.Client) error {
  38. dc = d
  39. return nil
  40. }); err != nil {
  41. return nil, err
  42. }
  43. c := config.(*Config)
  44. validator := new(vless.MemoryValidator)
  45. for _, user := range c.Clients {
  46. u, err := user.ToMemoryUser()
  47. if err != nil {
  48. return nil, errors.New("failed to get VLESS user").Base(err).AtError()
  49. }
  50. if err := validator.Add(u); err != nil {
  51. return nil, errors.New("failed to initiate user").Base(err).AtError()
  52. }
  53. }
  54. return New(ctx, c, dc, validator)
  55. }))
  56. }
  57. // Handler is an inbound connection handler that handles messages in VLess protocol.
  58. type Handler struct {
  59. inboundHandlerManager feature_inbound.Manager
  60. policyManager policy.Manager
  61. validator vless.Validator
  62. dns dns.Client
  63. fallbacks map[string]map[string]map[string]*Fallback // or nil
  64. // regexps map[string]*regexp.Regexp // or nil
  65. }
  66. // New creates a new VLess inbound handler.
  67. func New(ctx context.Context, config *Config, dc dns.Client, validator vless.Validator) (*Handler, error) {
  68. v := core.MustFromContext(ctx)
  69. handler := &Handler{
  70. inboundHandlerManager: v.GetFeature(feature_inbound.ManagerType()).(feature_inbound.Manager),
  71. policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
  72. dns: dc,
  73. validator: validator,
  74. }
  75. if config.Fallbacks != nil {
  76. handler.fallbacks = make(map[string]map[string]map[string]*Fallback)
  77. // handler.regexps = make(map[string]*regexp.Regexp)
  78. for _, fb := range config.Fallbacks {
  79. if handler.fallbacks[fb.Name] == nil {
  80. handler.fallbacks[fb.Name] = make(map[string]map[string]*Fallback)
  81. }
  82. if handler.fallbacks[fb.Name][fb.Alpn] == nil {
  83. handler.fallbacks[fb.Name][fb.Alpn] = make(map[string]*Fallback)
  84. }
  85. handler.fallbacks[fb.Name][fb.Alpn][fb.Path] = fb
  86. /*
  87. if fb.Path != "" {
  88. if r, err := regexp.Compile(fb.Path); err != nil {
  89. return nil, errors.New("invalid path regexp").Base(err).AtError()
  90. } else {
  91. handler.regexps[fb.Path] = r
  92. }
  93. }
  94. */
  95. }
  96. if handler.fallbacks[""] != nil {
  97. for name, apfb := range handler.fallbacks {
  98. if name != "" {
  99. for alpn := range handler.fallbacks[""] {
  100. if apfb[alpn] == nil {
  101. apfb[alpn] = make(map[string]*Fallback)
  102. }
  103. }
  104. }
  105. }
  106. }
  107. for _, apfb := range handler.fallbacks {
  108. if apfb[""] != nil {
  109. for alpn, pfb := range apfb {
  110. if alpn != "" { // && alpn != "h2" {
  111. for path, fb := range apfb[""] {
  112. if pfb[path] == nil {
  113. pfb[path] = fb
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. if handler.fallbacks[""] != nil {
  121. for name, apfb := range handler.fallbacks {
  122. if name != "" {
  123. for alpn, pfb := range handler.fallbacks[""] {
  124. for path, fb := range pfb {
  125. if apfb[alpn][path] == nil {
  126. apfb[alpn][path] = fb
  127. }
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. return handler, nil
  135. }
  136. func isMuxAndNotXUDP(request *protocol.RequestHeader, first *buf.Buffer) bool {
  137. if request.Command != protocol.RequestCommandMux {
  138. return false
  139. }
  140. if first.Len() < 7 {
  141. return true
  142. }
  143. firstBytes := first.Bytes()
  144. return !(firstBytes[2] == 0 && // ID high
  145. firstBytes[3] == 0 && // ID low
  146. firstBytes[6] == 2) // Network type: UDP
  147. }
  148. // Close implements common.Closable.Close().
  149. func (h *Handler) Close() error {
  150. return errors.Combine(common.Close(h.validator))
  151. }
  152. // AddUser implements proxy.UserManager.AddUser().
  153. func (h *Handler) AddUser(ctx context.Context, u *protocol.MemoryUser) error {
  154. return h.validator.Add(u)
  155. }
  156. // RemoveUser implements proxy.UserManager.RemoveUser().
  157. func (h *Handler) RemoveUser(ctx context.Context, e string) error {
  158. return h.validator.Del(e)
  159. }
  160. // GetUser implements proxy.UserManager.GetUser().
  161. func (h *Handler) GetUser(ctx context.Context, email string) *protocol.MemoryUser {
  162. return h.validator.GetByEmail(email)
  163. }
  164. // GetUsers implements proxy.UserManager.GetUsers().
  165. func (h *Handler) GetUsers(ctx context.Context) []*protocol.MemoryUser {
  166. return h.validator.GetAll()
  167. }
  168. // GetUsersCount implements proxy.UserManager.GetUsersCount().
  169. func (h *Handler) GetUsersCount(context.Context) int64 {
  170. return h.validator.GetCount()
  171. }
  172. // Network implements proxy.Inbound.Network().
  173. func (*Handler) Network() []net.Network {
  174. return []net.Network{net.Network_TCP, net.Network_UNIX}
  175. }
  176. // Process implements proxy.Inbound.Process().
  177. func (h *Handler) Process(ctx context.Context, network net.Network, connection stat.Connection, dispatcher routing.Dispatcher) error {
  178. iConn := connection
  179. if statConn, ok := iConn.(*stat.CounterConnection); ok {
  180. iConn = statConn.Connection
  181. }
  182. sessionPolicy := h.policyManager.ForLevel(0)
  183. if err := connection.SetReadDeadline(time.Now().Add(sessionPolicy.Timeouts.Handshake)); err != nil {
  184. return errors.New("unable to set read deadline").Base(err).AtWarning()
  185. }
  186. first := buf.FromBytes(make([]byte, buf.Size))
  187. first.Clear()
  188. firstLen, _ := first.ReadFrom(connection)
  189. errors.LogInfo(ctx, "firstLen = ", firstLen)
  190. reader := &buf.BufferedReader{
  191. Reader: buf.NewReader(connection),
  192. Buffer: buf.MultiBuffer{first},
  193. }
  194. var request *protocol.RequestHeader
  195. var requestAddons *encoding.Addons
  196. var err error
  197. napfb := h.fallbacks
  198. isfb := napfb != nil
  199. if isfb && firstLen < 18 {
  200. err = errors.New("fallback directly")
  201. } else {
  202. request, requestAddons, isfb, err = encoding.DecodeRequestHeader(isfb, first, reader, h.validator)
  203. }
  204. if err != nil {
  205. if isfb {
  206. if err := connection.SetReadDeadline(time.Time{}); err != nil {
  207. errors.LogWarningInner(ctx, err, "unable to set back read deadline")
  208. }
  209. errors.LogInfoInner(ctx, err, "fallback starts")
  210. name := ""
  211. alpn := ""
  212. if tlsConn, ok := iConn.(*tls.Conn); ok {
  213. cs := tlsConn.ConnectionState()
  214. name = cs.ServerName
  215. alpn = cs.NegotiatedProtocol
  216. errors.LogInfo(ctx, "realName = "+name)
  217. errors.LogInfo(ctx, "realAlpn = "+alpn)
  218. } else if realityConn, ok := iConn.(*reality.Conn); ok {
  219. cs := realityConn.ConnectionState()
  220. name = cs.ServerName
  221. alpn = cs.NegotiatedProtocol
  222. errors.LogInfo(ctx, "realName = "+name)
  223. errors.LogInfo(ctx, "realAlpn = "+alpn)
  224. }
  225. name = strings.ToLower(name)
  226. alpn = strings.ToLower(alpn)
  227. if len(napfb) > 1 || napfb[""] == nil {
  228. if name != "" && napfb[name] == nil {
  229. match := ""
  230. for n := range napfb {
  231. if n != "" && strings.Contains(name, n) && len(n) > len(match) {
  232. match = n
  233. }
  234. }
  235. name = match
  236. }
  237. }
  238. if napfb[name] == nil {
  239. name = ""
  240. }
  241. apfb := napfb[name]
  242. if apfb == nil {
  243. return errors.New(`failed to find the default "name" config`).AtWarning()
  244. }
  245. if apfb[alpn] == nil {
  246. alpn = ""
  247. }
  248. pfb := apfb[alpn]
  249. if pfb == nil {
  250. return errors.New(`failed to find the default "alpn" config`).AtWarning()
  251. }
  252. path := ""
  253. if len(pfb) > 1 || pfb[""] == nil {
  254. /*
  255. if lines := bytes.Split(firstBytes, []byte{'\r', '\n'}); len(lines) > 1 {
  256. if s := bytes.Split(lines[0], []byte{' '}); len(s) == 3 {
  257. if len(s[0]) < 8 && len(s[1]) > 0 && len(s[2]) == 8 {
  258. errors.New("realPath = " + string(s[1])).AtInfo().WriteToLog(sid)
  259. for _, fb := range pfb {
  260. if fb.Path != "" && h.regexps[fb.Path].Match(s[1]) {
  261. path = fb.Path
  262. break
  263. }
  264. }
  265. }
  266. }
  267. }
  268. */
  269. if firstLen >= 18 && first.Byte(4) != '*' { // not h2c
  270. firstBytes := first.Bytes()
  271. for i := 4; i <= 8; i++ { // 5 -> 9
  272. if firstBytes[i] == '/' && firstBytes[i-1] == ' ' {
  273. search := len(firstBytes)
  274. if search > 64 {
  275. search = 64 // up to about 60
  276. }
  277. for j := i + 1; j < search; j++ {
  278. k := firstBytes[j]
  279. if k == '\r' || k == '\n' { // avoid logging \r or \n
  280. break
  281. }
  282. if k == '?' || k == ' ' {
  283. path = string(firstBytes[i:j])
  284. errors.LogInfo(ctx, "realPath = "+path)
  285. if pfb[path] == nil {
  286. path = ""
  287. }
  288. break
  289. }
  290. }
  291. break
  292. }
  293. }
  294. }
  295. }
  296. fb := pfb[path]
  297. if fb == nil {
  298. return errors.New(`failed to find the default "path" config`).AtWarning()
  299. }
  300. ctx, cancel := context.WithCancel(ctx)
  301. timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)
  302. ctx = policy.ContextWithBufferPolicy(ctx, sessionPolicy.Buffer)
  303. var conn net.Conn
  304. if err := retry.ExponentialBackoff(5, 100).On(func() error {
  305. var dialer net.Dialer
  306. conn, err = dialer.DialContext(ctx, fb.Type, fb.Dest)
  307. if err != nil {
  308. return err
  309. }
  310. return nil
  311. }); err != nil {
  312. return errors.New("failed to dial to " + fb.Dest).Base(err).AtWarning()
  313. }
  314. defer conn.Close()
  315. serverReader := buf.NewReader(conn)
  316. serverWriter := buf.NewWriter(conn)
  317. postRequest := func() error {
  318. defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly)
  319. if fb.Xver != 0 {
  320. ipType := 4
  321. remoteAddr, remotePort, err := net.SplitHostPort(connection.RemoteAddr().String())
  322. if err != nil {
  323. ipType = 0
  324. }
  325. localAddr, localPort, err := net.SplitHostPort(connection.LocalAddr().String())
  326. if err != nil {
  327. ipType = 0
  328. }
  329. if ipType == 4 {
  330. for i := 0; i < len(remoteAddr); i++ {
  331. if remoteAddr[i] == ':' {
  332. ipType = 6
  333. break
  334. }
  335. }
  336. }
  337. pro := buf.New()
  338. defer pro.Release()
  339. switch fb.Xver {
  340. case 1:
  341. if ipType == 0 {
  342. pro.Write([]byte("PROXY UNKNOWN\r\n"))
  343. break
  344. }
  345. if ipType == 4 {
  346. pro.Write([]byte("PROXY TCP4 " + remoteAddr + " " + localAddr + " " + remotePort + " " + localPort + "\r\n"))
  347. } else {
  348. pro.Write([]byte("PROXY TCP6 " + remoteAddr + " " + localAddr + " " + remotePort + " " + localPort + "\r\n"))
  349. }
  350. case 2:
  351. pro.Write([]byte("\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A")) // signature
  352. if ipType == 0 {
  353. pro.Write([]byte("\x20\x00\x00\x00")) // v2 + LOCAL + UNSPEC + UNSPEC + 0 bytes
  354. break
  355. }
  356. if ipType == 4 {
  357. pro.Write([]byte("\x21\x11\x00\x0C")) // v2 + PROXY + AF_INET + STREAM + 12 bytes
  358. pro.Write(net.ParseIP(remoteAddr).To4())
  359. pro.Write(net.ParseIP(localAddr).To4())
  360. } else {
  361. pro.Write([]byte("\x21\x21\x00\x24")) // v2 + PROXY + AF_INET6 + STREAM + 36 bytes
  362. pro.Write(net.ParseIP(remoteAddr).To16())
  363. pro.Write(net.ParseIP(localAddr).To16())
  364. }
  365. p1, _ := strconv.ParseUint(remotePort, 10, 16)
  366. p2, _ := strconv.ParseUint(localPort, 10, 16)
  367. pro.Write([]byte{byte(p1 >> 8), byte(p1), byte(p2 >> 8), byte(p2)})
  368. }
  369. if err := serverWriter.WriteMultiBuffer(buf.MultiBuffer{pro}); err != nil {
  370. return errors.New("failed to set PROXY protocol v", fb.Xver).Base(err).AtWarning()
  371. }
  372. }
  373. if err := buf.Copy(reader, serverWriter, buf.UpdateActivity(timer)); err != nil {
  374. return errors.New("failed to fallback request payload").Base(err).AtInfo()
  375. }
  376. return nil
  377. }
  378. writer := buf.NewWriter(connection)
  379. getResponse := func() error {
  380. defer timer.SetTimeout(sessionPolicy.Timeouts.UplinkOnly)
  381. if err := buf.Copy(serverReader, writer, buf.UpdateActivity(timer)); err != nil {
  382. return errors.New("failed to deliver response payload").Base(err).AtInfo()
  383. }
  384. return nil
  385. }
  386. if err := task.Run(ctx, task.OnSuccess(postRequest, task.Close(serverWriter)), task.OnSuccess(getResponse, task.Close(writer))); err != nil {
  387. common.Interrupt(serverReader)
  388. common.Interrupt(serverWriter)
  389. return errors.New("fallback ends").Base(err).AtInfo()
  390. }
  391. return nil
  392. }
  393. if errors.Cause(err) != io.EOF {
  394. log.Record(&log.AccessMessage{
  395. From: connection.RemoteAddr(),
  396. To: "",
  397. Status: log.AccessRejected,
  398. Reason: err,
  399. })
  400. err = errors.New("invalid request from ", connection.RemoteAddr()).Base(err).AtInfo()
  401. }
  402. return err
  403. }
  404. if err := connection.SetReadDeadline(time.Time{}); err != nil {
  405. errors.LogWarningInner(ctx, err, "unable to set back read deadline")
  406. }
  407. errors.LogInfo(ctx, "received request for ", request.Destination())
  408. inbound := session.InboundFromContext(ctx)
  409. if inbound == nil {
  410. panic("no inbound metadata")
  411. }
  412. inbound.Name = "vless"
  413. inbound.User = request.User
  414. account := request.User.Account.(*vless.MemoryAccount)
  415. responseAddons := &encoding.Addons{
  416. // Flow: requestAddons.Flow,
  417. }
  418. var input *bytes.Reader
  419. var rawInput *bytes.Buffer
  420. switch requestAddons.Flow {
  421. case vless.XRV:
  422. if account.Flow == requestAddons.Flow {
  423. inbound.CanSpliceCopy = 2
  424. switch request.Command {
  425. case protocol.RequestCommandUDP:
  426. return errors.New(requestAddons.Flow + " doesn't support UDP").AtWarning()
  427. case protocol.RequestCommandMux:
  428. fallthrough // we will break Mux connections that contain TCP requests
  429. case protocol.RequestCommandTCP:
  430. var t reflect.Type
  431. var p uintptr
  432. if tlsConn, ok := iConn.(*tls.Conn); ok {
  433. if tlsConn.ConnectionState().Version != gotls.VersionTLS13 {
  434. return errors.New(`failed to use `+requestAddons.Flow+`, found outer tls version `, tlsConn.ConnectionState().Version).AtWarning()
  435. }
  436. t = reflect.TypeOf(tlsConn.Conn).Elem()
  437. p = uintptr(unsafe.Pointer(tlsConn.Conn))
  438. } else if realityConn, ok := iConn.(*reality.Conn); ok {
  439. t = reflect.TypeOf(realityConn.Conn).Elem()
  440. p = uintptr(unsafe.Pointer(realityConn.Conn))
  441. } else {
  442. return errors.New("XTLS only supports TLS and REALITY directly for now.").AtWarning()
  443. }
  444. i, _ := t.FieldByName("input")
  445. r, _ := t.FieldByName("rawInput")
  446. input = (*bytes.Reader)(unsafe.Pointer(p + i.Offset))
  447. rawInput = (*bytes.Buffer)(unsafe.Pointer(p + r.Offset))
  448. }
  449. } else {
  450. return errors.New("account " + account.ID.String() + " is not able to use the flow " + requestAddons.Flow).AtWarning()
  451. }
  452. case "":
  453. inbound.CanSpliceCopy = 3
  454. if account.Flow == vless.XRV && (request.Command == protocol.RequestCommandTCP || isMuxAndNotXUDP(request, first)) {
  455. return errors.New("account " + account.ID.String() + " is rejected since the client flow is empty. Note that the pure TLS proxy has certain TLS in TLS characters.").AtWarning()
  456. }
  457. default:
  458. return errors.New("unknown request flow " + requestAddons.Flow).AtWarning()
  459. }
  460. if request.Command != protocol.RequestCommandMux {
  461. ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
  462. From: connection.RemoteAddr(),
  463. To: request.Destination(),
  464. Status: log.AccessAccepted,
  465. Reason: "",
  466. Email: request.User.Email,
  467. })
  468. } else if account.Flow == vless.XRV {
  469. ctx = session.ContextWithAllowedNetwork(ctx, net.Network_UDP)
  470. }
  471. sessionPolicy = h.policyManager.ForLevel(request.User.Level)
  472. ctx, cancel := context.WithCancel(ctx)
  473. timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)
  474. inbound.Timer = timer
  475. ctx = policy.ContextWithBufferPolicy(ctx, sessionPolicy.Buffer)
  476. link, err := dispatcher.Dispatch(ctx, request.Destination())
  477. if err != nil {
  478. return errors.New("failed to dispatch request to ", request.Destination()).Base(err).AtWarning()
  479. }
  480. serverReader := link.Reader // .(*pipe.Reader)
  481. serverWriter := link.Writer // .(*pipe.Writer)
  482. trafficState := proxy.NewTrafficState(account.ID.Bytes())
  483. postRequest := func() error {
  484. defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly)
  485. // default: clientReader := reader
  486. clientReader := encoding.DecodeBodyAddons(reader, request, requestAddons)
  487. var err error
  488. if requestAddons.Flow == vless.XRV {
  489. ctx1 := session.ContextWithInbound(ctx, nil) // TODO enable splice
  490. clientReader = proxy.NewVisionReader(clientReader, trafficState, true, ctx1)
  491. err = encoding.XtlsRead(clientReader, serverWriter, timer, connection, input, rawInput, trafficState, nil, true, ctx1)
  492. } else {
  493. // from clientReader.ReadMultiBuffer to serverWriter.WriteMultiBuffer
  494. err = buf.Copy(clientReader, serverWriter, buf.UpdateActivity(timer))
  495. }
  496. if err != nil {
  497. return errors.New("failed to transfer request payload").Base(err).AtInfo()
  498. }
  499. return nil
  500. }
  501. getResponse := func() error {
  502. defer timer.SetTimeout(sessionPolicy.Timeouts.UplinkOnly)
  503. bufferWriter := buf.NewBufferedWriter(buf.NewWriter(connection))
  504. if err := encoding.EncodeResponseHeader(bufferWriter, request, responseAddons); err != nil {
  505. return errors.New("failed to encode response header").Base(err).AtWarning()
  506. }
  507. // default: clientWriter := bufferWriter
  508. clientWriter := encoding.EncodeBodyAddons(bufferWriter, request, requestAddons, trafficState, false, ctx)
  509. multiBuffer, err1 := serverReader.ReadMultiBuffer()
  510. if err1 != nil {
  511. return err1 // ...
  512. }
  513. if err := clientWriter.WriteMultiBuffer(multiBuffer); err != nil {
  514. return err // ...
  515. }
  516. // Flush; bufferWriter.WriteMultiBuffer now is bufferWriter.writer.WriteMultiBuffer
  517. if err := bufferWriter.SetBuffered(false); err != nil {
  518. return errors.New("failed to write A response payload").Base(err).AtWarning()
  519. }
  520. var err error
  521. if requestAddons.Flow == vless.XRV {
  522. err = encoding.XtlsWrite(serverReader, clientWriter, timer, connection, trafficState, nil, false, ctx)
  523. } else {
  524. // from serverReader.ReadMultiBuffer to clientWriter.WriteMultiBuffer
  525. err = buf.Copy(serverReader, clientWriter, buf.UpdateActivity(timer))
  526. }
  527. if err != nil {
  528. return errors.New("failed to transfer response payload").Base(err).AtInfo()
  529. }
  530. // Indicates the end of response payload.
  531. switch responseAddons.Flow {
  532. default:
  533. }
  534. return nil
  535. }
  536. if err := task.Run(ctx, task.OnSuccess(postRequest, task.Close(serverWriter)), getResponse); err != nil {
  537. common.Interrupt(serverReader)
  538. common.Interrupt(serverWriter)
  539. return errors.New("connection ends").Base(err).AtInfo()
  540. }
  541. return nil
  542. }