github.com/laof/lite-speed-test@v0.0.0-20230930011949-1f39b7037845/constant/metadata.go (about)

     1  package constant
     2  
     3  import (
     4  	"encoding/json"
     5  	"net"
     6  	"strconv"
     7  	"time"
     8  
     9  	"github.com/laof/lite-speed-test/transport/socks5"
    10  )
    11  
    12  // Socks addr type
    13  const (
    14  	TCP NetWork = iota
    15  	UDP
    16  
    17  	HTTP Type = iota
    18  	HTTPCONNECT
    19  	SOCKS
    20  	REDIR
    21  	TPROXY
    22  	TEST
    23  )
    24  
    25  type NetWork int
    26  
    27  func (n NetWork) String() string {
    28  	if n == TCP {
    29  		return "tcp"
    30  	}
    31  	return "udp"
    32  }
    33  
    34  func (n NetWork) MarshalJSON() ([]byte, error) {
    35  	return json.Marshal(n.String())
    36  }
    37  
    38  type Type int
    39  
    40  func (t Type) String() string {
    41  	switch t {
    42  	case HTTP:
    43  		return "HTTP"
    44  	case HTTPCONNECT:
    45  		return "HTTP Connect"
    46  	case SOCKS:
    47  		return "Socks5"
    48  	case REDIR:
    49  		return "Redir"
    50  	case TPROXY:
    51  		return "TProxy"
    52  	case TEST:
    53  		return "Test"
    54  	default:
    55  		return "Unknown"
    56  	}
    57  }
    58  
    59  func (t Type) MarshalJSON() ([]byte, error) {
    60  	return json.Marshal(t.String())
    61  }
    62  
    63  // Metadata is used to store connection address
    64  type Metadata struct {
    65  	NetWork NetWork `json:"network"` // tcp =3 udp = 4
    66  	Type    Type    `json:"type"`
    67  	SrcIP   net.IP  `json:"sourceIP"`
    68  	DstIP   net.IP  `json:"destinationIP"`
    69  	SrcPort string  `json:"sourcePort"`
    70  	DstPort string  `json:"destinationPort"`
    71  	Host    string  `json:"host"`
    72  	Timeout time.Duration
    73  }
    74  
    75  func (m *Metadata) RemoteAddress() string {
    76  	return net.JoinHostPort(m.String(), m.DstPort)
    77  }
    78  
    79  func (m *Metadata) SourceAddress() string {
    80  	return net.JoinHostPort(m.SrcIP.String(), m.SrcPort)
    81  }
    82  
    83  // IPv4=1 DomainName=3 IPv6=4
    84  func (m *Metadata) AddrType() int {
    85  	switch true {
    86  	case m.Host != "" || m.DstIP == nil:
    87  		return socks5.AtypDomainName
    88  	case m.DstIP.To4() != nil:
    89  		return socks5.AtypIPv4
    90  	default:
    91  		return socks5.AtypIPv6
    92  	}
    93  }
    94  
    95  func (m *Metadata) Resolved() bool {
    96  	return m.DstIP != nil
    97  }
    98  
    99  func (m *Metadata) UDPAddr() *net.UDPAddr {
   100  	if m.NetWork != UDP || m.DstIP == nil {
   101  		return nil
   102  	}
   103  	port, _ := strconv.Atoi(m.DstPort)
   104  	return &net.UDPAddr{
   105  		IP:   m.DstIP,
   106  		Port: port,
   107  	}
   108  }
   109  
   110  func (m *Metadata) String() string {
   111  	if m.Host != "" {
   112  		return m.Host
   113  	} else if m.DstIP != nil {
   114  		return m.DstIP.String()
   115  	} else {
   116  		return "<nil>"
   117  	}
   118  }
   119  
   120  func (m *Metadata) Valid() bool {
   121  	return m.Host != "" || m.DstIP != nil
   122  }