domainsniff.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package domainsniff
  2. /*
  3. Domainsniff
  4. This package contain codes that perform project / domain specific behavior in Zoraxy
  5. If you want Zoraxy to handle a particular domain or open source project in a special way,
  6. you can add the checking logic here.
  7. */
  8. import (
  9. "crypto/tls"
  10. "net"
  11. "time"
  12. )
  13. // Check if the domain is reachable and return err if not reachable
  14. func DomainReachableWithError(domain string) error {
  15. timeout := 1 * time.Second
  16. conn, err := net.DialTimeout("tcp", domain, timeout)
  17. if err != nil {
  18. return err
  19. }
  20. conn.Close()
  21. return nil
  22. }
  23. // Check if a domain have TLS but it is self-signed or expired
  24. func DomainIsSelfSigned(domain string) (bool, error) {
  25. //Get the certificate
  26. conn, err := net.Dial("tcp", domain)
  27. if err != nil {
  28. return false, err
  29. }
  30. defer conn.Close()
  31. //Connect with TLS using insecure skip verify
  32. config := &tls.Config{
  33. InsecureSkipVerify: true,
  34. }
  35. tlsConn := tls.Client(conn, config)
  36. err = tlsConn.Handshake()
  37. if err != nil {
  38. return false, err
  39. }
  40. //Check if the certificate is self-signed
  41. cert := tlsConn.ConnectionState().PeerCertificates[0]
  42. return cert.Issuer.CommonName == cert.Subject.CommonName, nil
  43. }
  44. // Check if domain reachable
  45. func DomainReachable(domain string) bool {
  46. return DomainReachableWithError(domain) == nil
  47. }