123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- package domainsniff
- import (
- "crypto/tls"
- "encoding/json"
- "fmt"
- "net"
- "net/http"
- "strings"
- "time"
- "imuslab.com/zoraxy/mod/utils"
- )
- func DomainReachableWithError(domain string) error {
- timeout := 1 * time.Second
- conn, err := net.DialTimeout("tcp", domain, timeout)
- if err != nil {
- return err
- }
- conn.Close()
- return nil
- }
- func DomainIsSelfSigned(domain string) bool {
-
- host, port, err := net.SplitHostPort(domain)
- if err != nil {
- host = domain
- } else {
- domain = host + ":" + port
- }
- if !strings.Contains(domain, ":") {
- domain = domain + ":443"
- }
-
- conn, err := net.Dial("tcp", domain)
- if err != nil {
- return false
- }
- defer conn.Close()
-
- tlsConn := tls.Client(conn, nil)
- err = tlsConn.Handshake()
- if err == nil {
-
- fmt.Println()
- return false
- }
-
- config := &tls.Config{
- InsecureSkipVerify: true,
- }
- tlsConn = tls.Client(conn, config)
- err = tlsConn.Handshake()
-
- return err == nil
- }
- func DomainReachable(domain string) bool {
- return DomainReachableWithError(domain) == nil
- }
- func DomainUsesTLS(targetURL string) bool {
-
- httpsUrl := fmt.Sprintf("https://%s", targetURL)
- httpUrl := fmt.Sprintf("http://%s", targetURL)
- client := http.Client{Timeout: 5 * time.Second}
- resp, err := client.Head(httpsUrl)
- if err == nil && resp.StatusCode == http.StatusOK {
- return true
- }
- resp, err = client.Head(httpUrl)
- if err == nil && resp.StatusCode == http.StatusOK {
- return false
- }
-
- return false
- }
- func RequireWebsocketHeaderCopy(r *http.Request) bool {
-
- if IsProxmox(r) {
- return false
- }
-
- return true
- }
- func HandleCheckSiteSupportTLS(w http.ResponseWriter, r *http.Request) {
- targetURL, err := utils.PostPara(r, "url")
- if err != nil {
- utils.SendErrorResponse(w, "invalid url given")
- return
- }
-
- _, err = utils.PostBool(r, "selfsignchk")
- if err == nil {
-
- type result struct {
- Protocol string `json:"protocol"`
- SelfSign bool `json:"selfsign"`
- }
- scanResult := result{Protocol: "http", SelfSign: false}
- if DomainUsesTLS(targetURL) {
- scanResult.Protocol = "https"
- if DomainIsSelfSigned(targetURL) {
- scanResult.SelfSign = true
- }
- }
- js, _ := json.Marshal(scanResult)
- utils.SendJSONResponse(w, string(js))
- return
- }
- if DomainUsesTLS(targetURL) {
- js, _ := json.Marshal("https")
- utils.SendJSONResponse(w, string(js))
- return
- } else {
- js, _ := json.Marshal("http")
- utils.SendJSONResponse(w, string(js))
- return
- }
- }
|