extensions.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package tdproto
  2. // Package tdproto, in addition to generated functions, has some manual extensions.
  3. import (
  4. "encoding/binary"
  5. "net"
  6. )
  7. // InitTLSDecoySpec creates TLSDecoySpec from ip address and server name.
  8. // Other feilds, such as Pubkey, Timeout and Tcpwin are left unset.
  9. // InitTLSDecoySpec creates TLSDecoySpec from ip address and server name.
  10. // Other feilds, such as Pubkey, Timeout and Tcpwin are left unset.
  11. func InitTLSDecoySpec(ip string, sni string) *TLSDecoySpec {
  12. _ip := net.ParseIP(ip)
  13. var ipUint32 *uint32
  14. var ipv6Bytes []byte
  15. if _ip.To4() != nil {
  16. ipUint32 = new(uint32)
  17. *ipUint32 = binary.BigEndian.Uint32(net.ParseIP(ip).To4())
  18. } else if _ip.To16() != nil {
  19. ipv6Bytes = _ip
  20. }
  21. tlsDecoy := TLSDecoySpec{Hostname: &sni, Ipv4Addr: ipUint32, Ipv6Addr: ipv6Bytes}
  22. return &tlsDecoy
  23. }
  24. // GetIpAddrStr returns IP address of TLSDecoySpec as a string.
  25. func (ds *TLSDecoySpec) GetIpAddrStr() string {
  26. if ds == nil {
  27. return ""
  28. }
  29. if ds.Ipv4Addr != nil {
  30. _ip := make(net.IP, 4)
  31. binary.BigEndian.PutUint32(_ip, ds.GetIpv4Addr())
  32. return net.JoinHostPort(_ip.To4().String(), "443")
  33. }
  34. if ds.Ipv6Addr != nil {
  35. return net.JoinHostPort(net.IP(ds.Ipv6Addr).String(), "443")
  36. }
  37. return ""
  38. }