-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiling.go
207 lines (181 loc) · 5.37 KB
/
filing.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
204
205
206
207
/*
* Copyright icp-filing Author(https://houseme.github.io/icp-filing/). All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* You can obtain one at https://github.com/houseme/icp-filing.
*/
// Package filling is the icp filling number
package filling
import (
"context"
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"math/rand"
"net/url"
"strconv"
"time"
"github.com/houseme/icp-filing/tld"
"github.com/houseme/icp-filing/utility/logger"
"github.com/houseme/icp-filing/utility/request"
)
const (
authorizePath = "auth"
queryPath = "icpAbbreviateInfo/queryByCondition"
authorizeContentType = "application/x-www-form-urlencoded;charset=UTF-8"
queryContentType = "application/json;charset=UTF-8"
httpOrigin = "https://beian.miit.gov.cn"
httpReferer = "https://beian.miit.gov.cn/"
defaultToken = "0"
domainLevel = 0
randomIP = "101.%d.%d.%d"
maxValue = 255
)
// Filling is the icp filling number object
type Filling struct {
token string
ip string
request request.Request
logger logger.ILogger
}
type options struct {
Request request.Request
Logger logger.ILogger
}
// Option is the option for logger.
type Option func(o *options)
// WithRequest is the option for request.
func WithRequest(req request.Request) Option {
return func(o *options) {
o.Request = req
}
}
// WithLogger is the option for logger.
func WithLogger(logger logger.ILogger) Option {
return func(o *options) {
o.Logger = logger
}
}
// New return a new filling number object
func New(ctx context.Context, opts ...Option) *Filling {
var op = options{}
for _, opt := range opts {
opt(&op)
}
f := &Filling{
token: defaultToken,
ip: fmt.Sprintf(randomIP, rand.Intn(maxValue), rand.Intn(maxValue), rand.Intn(maxValue)),
logger: op.Logger,
request: op.Request,
}
return f
}
// doRequest execute request
func (i *Filling) doRequest(ctx context.Context, in *ParamInput) (resp []byte, err error) {
headMap := map[string]string{
"Content-Type": in.ContentType,
"Origin": httpOrigin,
"Referer": httpReferer,
"Token": i.token,
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36",
"CLIENT_IP": i.ip,
"X-FORWARDED-FOR": i.ip,
"Sign": i.token,
}
i.logger.Debugf(ctx, "do request in params: %s", in.String())
if in.Path != authorizePath {
resp, err = i.request.PostJSON(ctx, "https://hlwicpfwc.miit.gov.cn/icpproject_query/api/"+in.Path, in.QueryRequest, headMap)
} else {
data := url.Values{}
data.Set("authKey", in.AuthorizeRequest.AuthKey)
data.Set("timeStamp", in.AuthorizeRequest.Timestamp)
encodedData := data.Encode()
fmt.Println("encodedData:", encodedData)
resp, err = i.request.Post(ctx, "https://hlwicpfwc.miit.gov.cn/icpproject_query/api/"+in.Path, []byte(encodedData), headMap)
}
return
}
// authorize .
func (i *Filling) authorize(ctx context.Context) error {
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
resp, err := i.doRequest(ctx,
&ParamInput{
AuthorizeRequest: &AuthorizeRequest{
AuthKey: i.md5("testtest" + timestamp),
Timestamp: timestamp,
},
QueryRequest: nil,
Path: authorizePath,
ContentType: authorizeContentType,
})
if err != nil {
return err
}
var response *AuthorizeResponse
if err = json.Unmarshal(resp, &response); err != nil {
return err
}
if response == nil {
return errors.New("response is nil")
}
if !response.Success {
return errors.New("code: " + strconv.Itoa(response.Code) + " errMsg: " + response.Msg)
}
i.token = response.Params.Business
return nil
}
// QueryFilling query domain filling number
func (i *Filling) QueryFilling(ctx context.Context, req *QueryRequest) (*QueryResponse, error) {
resp, err := i.doRequest(ctx, &ParamInput{
QueryRequest: req,
AuthorizeRequest: nil,
ContentType: queryContentType,
Path: queryPath,
})
if err != nil {
return nil, err
}
var queryResp *QueryResponse
if err = json.Unmarshal(resp, &queryResp); err != nil {
return nil, err
}
return queryResp, nil
}
// md5 .
func (i *Filling) md5(str string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(str))) // 将 []byte 转成 16 进制
}
// String return filling JSON string
func (i *Filling) String() string {
return `{"ip":"` + i.ip + `","token":"` + i.token + `"}`
}
// DomainFilling query domain filling number
func (i *Filling) DomainFilling(ctx context.Context, req *QueryRequest) (*QueryResponse, error) {
if req == nil {
return nil, errors.New("request is nil")
}
if req.UnitName == "" && req.Link != "" {
resp, err := tld.GetTLD(ctx, req.Link, domainLevel)
if err != nil {
return nil, err
}
i.logger.Debugf(ctx, "GetTld resp: %s", resp.String())
req.UnitName = resp.Domain
}
if err := i.authorize(ctx); err != nil {
return nil, err
}
return i.QueryFilling(ctx, req)
}