http.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package outbound
  2. import (
  3. "context"
  4. "net"
  5. "os"
  6. "github.com/sagernet/sing/common/bufio"
  7. M "github.com/sagernet/sing/common/metadata"
  8. N "github.com/sagernet/sing/common/network"
  9. "github.com/sagernet/sing/protocol/http"
  10. "github.com/sagernet/sing-box/adapter"
  11. C "github.com/sagernet/sing-box/constant"
  12. "github.com/sagernet/sing-box/log"
  13. "github.com/sagernet/sing-box/option"
  14. "github.com/sagernet/sing-box/outbound/dialer"
  15. )
  16. var _ adapter.Outbound = (*HTTP)(nil)
  17. type HTTP struct {
  18. myOutboundAdapter
  19. client *http.Client
  20. }
  21. func NewHTTP(router adapter.Router, logger log.Logger, tag string, options option.HTTPOutboundOptions) *HTTP {
  22. return &HTTP{
  23. myOutboundAdapter{
  24. protocol: C.TypeHTTP,
  25. logger: logger,
  26. tag: tag,
  27. network: []string{C.NetworkTCP},
  28. },
  29. http.NewClient(dialer.New(router, options.DialerOptions), M.ParseSocksaddrHostPort(options.Server, options.ServerPort), options.Username, options.Password),
  30. }
  31. }
  32. func (h *HTTP) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  33. h.logger.WithContext(ctx).Info("outbound connection to ", destination)
  34. return h.client.DialContext(ctx, network, destination)
  35. }
  36. func (h *HTTP) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  37. return nil, os.ErrInvalid
  38. }
  39. func (h *HTTP) NewConnection(ctx context.Context, conn net.Conn, destination M.Socksaddr) error {
  40. outConn, err := h.DialContext(ctx, C.NetworkTCP, destination)
  41. if err != nil {
  42. return err
  43. }
  44. return bufio.CopyConn(ctx, conn, outConn)
  45. }
  46. func (h *HTTP) NewPacketConnection(ctx context.Context, conn N.PacketConn, destination M.Socksaddr) error {
  47. return os.ErrInvalid
  48. }