-
Notifications
You must be signed in to change notification settings - Fork 0
/
TorchServer.go
140 lines (126 loc) · 3.85 KB
/
TorchServer.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"net/http"
"fmt"
// "database/sql"
"time"
"math/rand"
"strconv"
// "math"
// "bytes"
// "strings"
"os"
"os/exec"
"io/ioutil"
"encoding/json"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"./ServerUtils"
)
var appid string
var secret string
func init() {
fileObj, _ := os.OpenFile("config.cfg", os.O_RDONLY, 0644)
defer fileObj.Close()
content, _ := ioutil.ReadAll(fileObj)
cfg, _ := JsonToMap(string(content))
appid = cfg["appid"].(string)
secret = cfg["secret"].(string)
}
func main() {
// Echo instance
e := echo.New()
// Middleware
// e.Use(middleware.Logger())
e.Use(middleware.Recover())
// Routes
// e.POST("/addtorch", AddTorch)
e.GET("/getml", GetMusicList)
e.GET("/test", ServerTestGet) // ok
e.GET("/session", LoginSession)
e.GET("/login", LoginUser)
// Start server
// e.Start(":53249")
e.Logger.Fatal(e.Start(":5959"))
// e.Logger.Fatal(e.StartTLS(":5959", "cert.pem", "key.pem"))
// fmt.Println("Start")
}
func LoginSession(c echo.Context) error {
js_code := c.QueryParam("code")
wxApi := fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%+v&secret=%+v&js_code=%+v&grant_type=authorization_code", appid, secret, js_code)
client := &http.Client{}
client.Timeout = 5 * time.Second
req, _ := http.NewRequest("GET", wxApi, nil)
req.Header.Set("User-Agent", "wx")
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) // 读取Body
content := string(body)
fmt.Println(content)
data, _ := JsonToMap(content)
return c.String(http.StatusOK, data["session_key"].(string))
}
func LoginUser(c echo.Context) error {
ed := c.QueryParam("ed")
iv := c.QueryParam("iv")
sk := c.QueryParam("sk")
r := rand.New(rand.NewSource(time.Now().UnixNano()))
tmpFile := "Temp/" + strconv.FormatInt(int64(r.Intn(99999)), 16) + ".txt"
os.Remove(tmpFile)
content := sk + "\r\n" + ed + "\r\n" + iv
// fmt.Println(content)
fileObj, _ := os.OpenFile(tmpFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
fileObj.WriteString(content)
fileObj.Close()
cmd := exec.Command("python", "-u", "decode.py", tmpFile)
buf, _ := cmd.Output()
data, _ := JsonToMap(string(buf))
openid := data["openId"].(string)
fmt.Printf("output: %+v: %+v\n", tmpFile, openid)
os.Remove(tmpFile)
playlist := ServerUtils.FindUser(openid)
// fmt.Printf("err: %v",err)
return c.String(http.StatusOK, MapToJson(playlist))
}
func GetMusicList(c echo.Context) error {
playlist_id := c.QueryParam("playlist_id")
musicList := ServerUtils.FindPlaylist(playlist_id)
// fmt.Println(musicList)
return c.String(http.StatusOK, MapToJson(musicList))
}
// post
func AddTorch(c echo.Context) error {
body, _ := ioutil.ReadAll(c.Request().Body)
content := string(body)
data, _ := JsonToMap(content)
fmt.Println(data)
return c.String(http.StatusOK, "200")
}
func ServerTestGet(c echo.Context) error {
return c.String(http.StatusOK, "ok")
}
func JsonToMap(jsonStr string) (map[string]interface{}, []map[string]interface{}) {
var mapResult map[string]interface{}
var mapListResult []map[string]interface{}
err := json.Unmarshal([]byte(jsonStr), &mapResult)
errList := json.Unmarshal([]byte(jsonStr), &mapListResult)
if err != nil && errList != nil {
fmt.Println(fmt.Sprintf("JsonToMap err: %v", err))
fmt.Println(fmt.Sprintf("JsonToMap err: %v", errList))
fmt.Println(fmt.Sprintf("JsonToMap err: %v", jsonStr))
// return mapResult, mapListResult
}
return mapResult, mapListResult
}
func MapToJson(mapData interface{}) string {
jsonStr, err := json.Marshal(mapData)
if err != nil {
fmt.Println(fmt.Sprintf("MapToJson err: %v", err))
fmt.Println(fmt.Sprintf("MapToJson err: %v", mapData))
return ""
}
return string(jsonStr)
}