|
发表于 2023-6-11 16:54:27
|
显示全部楼层
源代码
- package main
- import (
- "encoding/json"
- "io/ioutil"
- "net/http"
- "fmt"
- "time"
- )
- type icpVar struct {
- Data interface{} `json:"data"`
- ReCode int64 `json:"reCode"`
- }
- // 自定义自己需要的端口号
- const port = "127.0.0.1:8080"
- func main() {
- slug := `
- _ _ _ _ _ _/_ _/_
- / / //_'/ //_//_//_//_//_/
- _/
- 启动日期: %s
- 启动端口: %s
- `
- timestamp := time.Now().Format("2006-01-02 15:04:05")
- fmt.Println(fmt.Sprintf(slug, timestamp, port))
- http.HandleFunc("/api", apiHandler)
- http.ListenAndServe(port, nil)
- }
- // GET请求/api?domain=xx.xx
- func apiHandler(w http.ResponseWriter, r *http.Request) {
- w.Header().Set("Content-type", "application/json")
- domain := r.URL.Query().Get("domain")
- if len(domain) <= 0 {
- json.NewEncoder(w).Encode(map[string]interface{}{"code": 4002, "msg": "domain参数不鞥为空"})
- return
- }
- req, err := http.NewRequest("GET", "https://cgi.urlsec.qq.com/index.php?m=check&a=check&url="+domain, nil)
- req.Header.Set("Referer", "https://guanjia.qq.com")
- if err != nil {
- json.NewEncoder(w).Encode(map[string]interface{}{"code": 4002, "msg": "系统错误"})
- return
- }
- client := http.DefaultClient
- response, err := client.Do(req)
- if err != nil {
- json.NewEncoder(w).Encode(map[string]interface{}{"code": 4002, "msg": "请求出错"})
- return
- }
- defer response.Body.Close()
- content, err := ioutil.ReadAll(response.Body)
- if err != nil {
- json.NewEncoder(w).Encode(map[string]interface{}{"code": 4002, "msg": "响应失败"})
- return
- }
- res := string(content)[1:]
- res = res[:len(res)-1]
- var i icpVar
- if err = json.Unmarshal([]byte(res), &i); err != nil {
- json.NewEncoder(w).Encode(map[string]interface{}{"code": 4002, "msg": "读取失败"})
- return
- }
- if i.ReCode != 0 {
- json.NewEncoder(w).Encode(map[string]interface{}{"code": i.ReCode, "msg": i.Data})
- return
- }
- data := i.Data.(map[string]interface{})
- result := data["results"].(map[string]interface{})
- resp := map[string]interface{}{
- "url": result["url"],
- "name": result["Orgnization"],
- "number": result["ICPSerial"],
- "isICP": result["isDomainICPOk"],
- }
- json.NewEncoder(w).Encode(map[string]interface{}{"code": 200, "data": resp})
- return
- }
复制代码 |
|