opstools.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package ops
  2. import (
  3. "crypto/tls"
  4. "net"
  5. "regexp"
  6. "strings"
  7. "time"
  8. )
  9. // 域名信息
  10. type DomainMsg struct {
  11. CreateDate string `json:"create_date"`
  12. ExpiryDate string `json:"expiry_date"`
  13. Registrar string `json:"registrar"`
  14. }
  15. // GetDomainMsg 获取域名信息
  16. func GetDomainMsg(domain string) (dm DomainMsg, err error) {
  17. var conn net.Conn
  18. conn, err = net.Dial("tcp", "whois.verisign-grs.com:43")
  19. if err != nil {
  20. return
  21. }
  22. defer conn.Close()
  23. _, err = conn.Write([]byte(domain + "\r\n"))
  24. if err != nil {
  25. return
  26. }
  27. buf := make([]byte, 1024)
  28. var num int
  29. num, err = conn.Read(buf)
  30. if err != nil {
  31. return
  32. }
  33. response := string(buf[:num])
  34. re := regexp.MustCompile(`Creation Date: (.*)\n.*Expiry Date: (.*)\n.*Registrar: (.*)`)
  35. match := re.FindStringSubmatch(response)
  36. if len(match) > 3 {
  37. dm.CreateDate = strings.TrimSpace(strings.Split(match[1], "Creation Date:")[0])
  38. dm.ExpiryDate = strings.TrimSpace(strings.Split(match[2], "Expiry Date:")[0])
  39. dm.Registrar = strings.TrimSpace(strings.Split(match[3], "Registrar:")[0])
  40. }
  41. return
  42. }
  43. // GetDomainCertMsg 获取域名证书信息
  44. func GetDomainCertMsg(domain string) (cm tls.ConnectionState, err error) {
  45. var conn net.Conn
  46. conn, err = net.DialTimeout("tcp", domain+":443", time.Second*10)
  47. if err != nil {
  48. return
  49. }
  50. defer conn.Close()
  51. tlsConn := tls.Client(conn, &tls.Config{
  52. ServerName: domain,
  53. })
  54. defer tlsConn.Close()
  55. err = tlsConn.Handshake()
  56. if err != nil {
  57. return
  58. }
  59. cm = tlsConn.ConnectionState()
  60. return
  61. }