获取域名的顶级域名
2024-06-16 10:54:58
代码:
// 获取顶级域名 func topDomain(url string) string { //顶级域名后缀 allDomainHz := map[string]bool{ "com": true, "tk": true, "cn": true, "de": true, "net": true, "uk": true, "org": true, "xyz": true, "ru": true, "top": true, "ga": true, "info": true, "icu": true, "cf": true, "ml": true, "nl": true, "br": true, "fr": true, "gq": true, "online": true, "eu": true, "site": true, "co": true, "club": true, "it": true, "us": true, "ca": true, "au": true, "ws": true, "in": true, "pl": true, "biz": true, "shop": true, "ch": true, "tw": true, "se": true, "loan": true, "vip": true, "es": true, "za": true, "jp": true, "be": true, "ph": true, "at": true, "work": true, "me": true, "store": true, "live": true, "cz": true, "wang": true, "dk": true, "vg": true, "buzz": true, "space": true, "ir": true, "mx": true, "nu": true, "fun": true, "app": true, "xn--p1ai": true, "pw": true, "kr": true, "website": true, "cc": true, "no": true, "io": true, "hu": true, "cyou": true, "ua": true, "tech": true, "ro": true, "id": true, "ltd": true, "win": true, "cl": true, "pro": true, "nz": true, "mobi": true, "ar": true, "bar": true, "sk": true, "life": true, "tokyo": true, "tv": true, "vn": true, "fi": true, "fm": true, "gr": true, "men": true, "link": true, "xn--fiqs8s": true, "asia": true, "cloud": true, "stream": true, "tr": true, "blog": true, "xn--node": true, "bid": true, "pt": true, "dev": true, "il": true, "my": true, "click": true, "ie": true, "world": true, "page": true, "xn--fiqz9s": true, "monster": true, "art": true, "ooo": true, "xn--kprw13d": true, "review": true, "host": true, "xn--ses554g": true, "xin": true, "la": true, "ng": true, "design": true, "ae": true, "kz": true, "one": true, "hk": true, "lt": true, "today": true, "trade": true, "sg": true, "email": true, "su": true, "by": true, "digital": true, "gdn": true, "name": true, "fit": true, "date": true, "rocks": true, "cat": true, "best": true, "ee": true, "group": true, "agency": true, "ke": true, "download": true, "pe": true, "ec": true, "party": true, "africa": true, "solutions": true, "si": true, "press": true, "studio": true, "rest": true, "lv": true, "blackfriday": true, "services": true, "rs": true, "company": true, "ovh": true, "news": true, "tel": true, "ai": true, "network": true, "hr": true, "ren": true, "cfd": true, "hosting": true, "pk": true, "politie": true, "ma": true, "business": true, "lu": true, "th": true, "guru": true, "ink": true, "xxx": true, "media": true, "science": true, "bg": true, "racing": true, "london": true, "xn--mxtq1m": true, "uz": true, "nyc": true, "realtor": true, "ms": true, } //允许二级域名的顶级域名 ttErji := map[string]bool{ "com": true, "net": true, "org": true, "cn": true, "gov": true, "edu": true, } // 去掉"http://" 和 "https://://" domain := strings.ToLower(strings.ReplaceAll(strings.ReplaceAll(url, "http://", ""), "https://", "")) if idx := strings.Index(domain, "/"); idx != -1 { domain = domain[:idx] } // 获取顶级域名 topDomain := "" ls := strings.Split(domain, ".") len := len(ls) last := ls[len-1] if idx := strings.Index(last, "/"); idx != -1 { last = last[:idx] } if !allDomainHz[last] { // 不是后缀 return "" } if len < 2 { // 不是域名 topDomain = "" } else if len == 2 { if ls[0] != "" && ls[1] != "" { topDomain = ls[0] + "." + ls[1] } } else if len > 2 { // 允许作为二级后缀的部分 erji := ls[len-2] // 二级后缀 if _, ok := ttErji[erji]; ok { if ls[len-3] != "" && ls[len-2] != "" && ls[len-1] != "" { topDomain = ls[len-3] + "." + ls[len-2] + "." + ls[len-1] } } else { if ls[len-2] != "" && ls[len-1] != "" { topDomain = ls[len-2] + "." + ls[len-1] } } } if strings.HasPrefix(topDomain, "-") { topDomain = "" } return strings.ToLower(topDomain) //最终转成小写字母 }