-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
82 lines (71 loc) · 2.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
/**
@author: ECH2O
@time: 2018-7-4 14:37:14
@tip: access_token 有效期限为一年
*/
import (
"net/http"
"fmt"
"net/url"
"strings"
"io/ioutil"
"encoding/json"
"time"
)
func main() {
http.HandleFunc("/openlink", JDRender)
http.ListenAndServe(":8989", nil);
}
type JDConfig struct {
access_token string
app_key string
method string
sgin string
}
func conf() JDConfig {
return JDConfig{
"d485f611-7d1f-49d5-97a9-d5ac5450c659",
"8E76F960FE21B40A83B167F22223759C",
"jingdong.wxsq.mjgj.link.GetOpenLink",
"35D5AA4F312FD08D8070B951105A8D9C",
}
}
func JDRender(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
jdUrl := r.Form.Get("url")
_, err := url.ParseRequestURI(jdUrl)
if err != nil || !strings.Contains(jdUrl, "jd.com") {
errInfo := `{"errcode":-1,"message":"不支持的资源域名 Only => *.jd.com"}`
fmt.Fprintf(w, errInfo)
return
} else {
jdRurl := url.QueryEscape(strings.Split(jdUrl, ":")[1])
data := url.Values{"jump": {"0"}, "rurl": {url.QueryEscape("http://dc2.jd.com/auto.php?service=transfer&type=pms&to=" + jdRurl + "&openlink=1")}}
body := strings.NewReader(data.Encode())
clt := http.Client{}
resp, err := clt.Post(JDApi(), "application/x-www-form-urlencoded", body)
if err != nil {
fmt.Print(err.Error())
return
}
content, err := ioutil.ReadAll(resp.Body)
respBody := string(content)
var dat map[string]interface{}
if err := json.Unmarshal([]byte(respBody), &dat); err == nil {
r, _ := json.Marshal(dat["jingdong_wxsq_mjgj_link_GetOpenLink_responce"])
if err := json.Unmarshal(r, &dat); err == nil {
r, _ := json.Marshal(dat["open_link_result"])
fmt.Fprintf(w, string(r))
}
}
}
fmt.Print("\r\n访问者ip:" + r.Header.Get("X-Real-Ip") + strings.Split(r.RemoteAddr, ":")[0])
}
func JDApi() string {
return "https://api.jd.com/routerjson?access_token=" + conf().access_token +
"&app_key=" + conf().app_key +
"&method=" + conf().method +
"&v=2.0&sign=" + conf().sgin +
"×tamp=" + string(time.Now().Unix())
}