-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
203 lines (182 loc) · 3.99 KB
/
client.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package gd
import (
"context"
"crypto/md5"
"database/sql"
"encoding/hex"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/spf13/viper"
"golang.org/x/text/encoding/simplifiedchinese"
)
func DoRequest(req *http.Request) (content []byte, err error) {
var tr = &http.Transport{DisableKeepAlives: true}
var client = &http.Client{Transport: tr}
resp, err := client.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
log.Println("Response error.")
log.Println(err)
return nil, err
}
if resp.StatusCode == http.StatusOK {
content, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return
}
}
return
}
func TelegramSendMessage(token, chatId, msg string) string {
return fmt.Sprintf(`https://api.telegram.org/bot%s/sendMessage?chat_id=%s&text=%s`, token, chatId, msg)
}
/* For debug West use */
func WestDoRequest(req *http.Request) (content []byte, err error) {
var tr = &http.Transport{DisableKeepAlives: true}
var client = &http.Client{Transport: tr}
resp, err := client.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
log.Println("Response error.")
log.Println(err)
return nil, err
}
if resp.StatusCode == http.StatusOK {
/* Convert GBK to UTF-8 */
reader := simplifiedchinese.GB18030.NewDecoder().Reader(resp.Body)
content, err = ioutil.ReadAll(reader)
if err != nil {
log.Println("Content error.")
log.Println(err)
return
}
}
return
}
func Md5encode(v string) string {
m := md5.Sum([]byte(v))
return hex.EncodeToString(m[:])
}
/* Use a as Baseline*/
func CompareString(a, b []string) []string {
for _, av := range a {
for i, bv := range b {
if av == bv {
RemoveString(b, i)
b[len(b)-1] = ""
b = b[:len(b)-1]
break
}
}
}
return b
}
func RemoveString(s []string, i int) []string {
return append(s[:i], s[i+1:]...)
}
type Sql struct {
Db *sql.DB
Err error
dbName string
dsnNormal string
dsnRoot string
}
func (my *Sql) getDSN() {
user := viper.GetString("mysql.user")
pass := viper.GetString("mysql.pass")
host := viper.GetString("mysql.host")
port := viper.GetString("mysql.port")
my.dbName = viper.GetString("mysql.schema")
my.dsnRoot = fmt.Sprintf("%s:%s@tcp(%s:%s)/",
user, pass, host, port)
my.dsnNormal = fmt.Sprintf("%s%s",
my.dsnRoot, my.dbName)
}
func (my *Sql) Connection() *sql.DB {
/* Create connection pool */
my.getDSN()
var db *sql.DB
db, my.Err = sql.Open("mysql", my.dsnRoot)
if my.Err != nil {
log.Println(my.Err)
return nil
}
/* Create database */
_, my.Err = db.Exec("CREATE DATABASE IF NOT EXISTS " + my.dbName)
if my.Err != nil {
log.Println(my.Err)
return nil
}
db.Close()
/* Re-Connect to MySQL */
my.Db, my.Err = sql.Open("mysql", my.dsnNormal)
if my.Err != nil {
log.Println(my.Err)
return nil
}
// defer my.Db.Close()
return my.Db
}
func (my *Sql) CreateTable(query string) {
ctx, cancelfunc := context.WithTimeout(context.Background(), 1*time.Second)
defer cancelfunc()
/* Create Table */
_, err := my.Db.ExecContext(ctx, query)
if err != nil {
log.Println(err)
}
}
func (my *Sql) DropTable(tableName string) {
query := "DROP TABLE IF EXISTS " + tableName
my.CreateTable(query)
}
func (my *Sql) QueryData(query string) (err error) {
_, err = my.Db.Query(query)
if err != nil {
log.Println(err)
}
return
}
func (my *Sql) InsertData(query string, val []interface{}) (err error) {
/* Insert data */
ins, err := my.Db.Prepare(query)
if err != nil {
log.Println(err)
return
}
_, err = ins.Exec(val...)
if err != nil {
log.Println(err)
}
defer ins.Close()
return
}
func (my *Sql) QueryWestDomain(query string) (result []string, err error) {
rows, err := my.Db.Query(query)
if err != nil {
log.Println(err)
return
}
defer rows.Close()
for rows.Next() {
var domain string
if err := rows.Scan(&domain); err != nil {
log.Println(err)
return nil, err
}
result = append(result, domain)
}
return
}
func NewDB() *Sql {
return new(Sql)
}